-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_git.c
106 lines (97 loc) · 2.24 KB
/
find_git.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* find_git.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anboisve <anboisve@student.42quebec.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 12:13:56 by anboisve #+# #+# */
/* Updated: 2024/01/16 17:06:44 by anboisve ### ########.fr */
/* */
/* ************************************************************************** */
#include "include/minishell.h"
#include "lib/lib_ft/libft.h"
#include <limits.h>
static char *edit_git(char *in)
{
size_t i;
i = ft_strlen(in) - 1;
while (i && in[i] != '/')
i--;
if (i == 0)
return (NULL);
return (ft_strdup(in + i + 1));
}
/// @brief find number of '/' in getcwd()
/// @param void
/// @return n of '/'
static int cont_c(void)
{
int i;
int n;
char pwd[PATH_MAX + 1];
i = 0;
n = 0;
ft_bzero(pwd, PATH_MAX + 1);
getcwd(pwd, PATH_MAX);
while (pwd[i])
{
if (pwd[i] == '/')
n++;
i++;
}
return (n);
}
static char *find_file(void)
{
char *find;
char *tmp;
int i;
i = cont_c();
find = ft_strdup(".git/HEAD");
if (!find)
return (NULL);
while (access(find, F_OK) != 0)
{
if (i-- == 0)
break ;
ft_printf(NO_PRINT, "%o../%s", &tmp, find);
ft_free(find);
if (!tmp)
return (NULL);
find = tmp;
}
if (i == 0)
{
ft_free(find);
return (NULL);
}
return (find);
}
void find_git(t_mshell *shell)
{
int fd;
char *line;
char *out;
char *t;
shell->git_status = NULL;
out = NULL;
t = find_file();
fd = open(t, O_RDONLY);
if (fd > 0)
{
line = "a";
while (line)
{
line = get_next_line(fd);
out = ft_strfjoin(out, line);
ft_free(line);
}
shell->git_status = edit_git(out);
if (shell->git_status)
shell->git_status[ft_strlen(shell->git_status) - 1] = 0;
ft_free(out);
close(fd);
}
ft_free(t);
}