-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.c
63 lines (56 loc) · 1.22 KB
/
info.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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "info.h"
/**
* init_info - initialize shell info
* @argc: the arg count
* @argv: the arg values
* Return: pointer to the info
*/
info_t *init_info(int argc, char **argv)
{
static info_t info;
char *error = NULL;
info.argc = argc;
info.argv = argv;
info.fileno = STDIN_FILENO;
if (argc > 1)
{
info.file = argv[1];
info.fileno = open(info.file, O_RDONLY);
if (info.fileno == -1)
{
error = strjoin(NULL, " ", "Can't open", info.file);
perrorl_default(*argv, info.lineno, error, NULL);
free(error);
info.status = 127;
exit(free_info(&info));
}
}
info.interactive = isatty(info.fileno);
info.pid = getpid();
info.cwd = getcwd(NULL, 0);
info.env = env_to_dict(environ);
return (&info);
}
/**
* free_info - free and nullify dynamically allocated info
* @info: pointer to the info
* Return: current exit status
*/
int free_info(info_t *info)
{
free(info->line);
info->line = _getline(-1);
free_tokens(&info->tokens);
free(info->cwd);
info->cwd = NULL;
free(info->exe);
info->exe = NULL;
free_dict(&info->env);
free_list(&info->path);
free_dict(&info->aliases);
free_cmdlist(&info->commands);
return (info->status);
}