-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
79 lines (71 loc) · 1.45 KB
/
main.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
#include "simple_shell.h"
/**
*main - Entry point
*@argc: number of arguments
*@argv: array of strings with the arguments inside
*@env: environmental variables
*Return: 0
*/
int main(int argc, char *argv[], char **env)
{
(void)argc;
signal(SIGINT, CTRLC);
create_env(env);
invoke_shell(argv[0]);
return (0);
}
/**
* get_oldpwd - return home
*
*Return: oldpwd
*/
char *get_oldpwd(void)
{
int a = 0, cont;
char *oldpwd;
while (_strncmp(environ[a], "OLDPWD", 6) != 0)
a++;
for (cont = 0; environ[a][cont] != '='; cont++)
;
oldpwd = environ[a] + cont + 1;
return (oldpwd);
}
/**
* init_file - initialize files log & hist
*
*@key_buff: pointer to buffer
*@leido: leido
*Return: No return
*/
void init_file(char *key_buff, int leido)
{
int fd_log, fd_hist, escrito, historico;
char *my_path;
my_path = get_home();
my_path = str_concat(my_path, "/cmd_log.txt");
fd_log = open(my_path, O_RDWR | O_TRUNC | O_CREAT, 0660);
free(my_path);
my_path = get_home();
my_path = str_concat(my_path, "/cmd_hist.txt");
fd_hist = open(my_path, O_RDWR | O_APPEND | O_CREAT, 0660);
free(my_path);
escrito = write(fd_log, key_buff, leido);
if (escrito == -1)
exit(-1);
historico = write(fd_hist, key_buff, leido);
if (historico == -1)
exit(-1);
close(fd_hist);
close(fd_log);
}
/**
*CTRLC - control c handler function
*@sign: int of sign
*Return: none
*/
void CTRLC(int sign)
{
(void) sign;
write(STDOUT_FILENO, "\n", 1);
print_prompt(22);
}