-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars.c
97 lines (88 loc) · 2.16 KB
/
vars.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
/*
** vars.c for 42sh in /home/nicolaspolomack/TurboSh
**
** Made by Nicolas Polomack
** Login <nicolas.polomack@epitech.eu>
**
** Started on Thu May 18 17:26:01 2017 Nicolas Polomack
** Last update Sun May 21 04:30:06 2017 Nicolas Polomack
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "my.h"
#include "shell.h"
static void insert_line(char ***tab, char *line)
{
char **ret;
int size;
size = ((*tab) == NULL) - 1;
while ((*tab) && (*tab)[++size]);
if ((ret = malloc(sizeof(char *) * (size + 2))) == NULL)
handle_error("malloc");
size = ((*tab) == NULL) - 1;
while ((*tab) && (*tab)[++size])
ret[size] = (*tab)[size];
ret[size] = line;
ret[++size] = NULL;
free(*tab);
*tab = ret;
}
void add_var(t_shell *shell, char *name, char *value)
{
char *str;
char *tmp;
int i;
if (name == NULL ||
(tmp = my_strcatdup(name, "\t")) == NULL)
handle_error("malloc");
if (value && ((str = my_strcatdup(tmp, value)) == NULL))
handle_error("malloc");
else if (!value)
str = tmp;
i = -1;
while (shell->vars && shell->vars[++i])
if (!strncmp(shell->vars[i], name, strlen(name)) &&
shell->vars[i][strlen(name)] == '\t')
{
free(shell->vars[i]);
shell->vars[i] = (str == tmp) ? strdup(str) : str;
return ;
}
insert_line(&shell->vars, (str == tmp) ? strdup(str) : str);
free(tmp);
}
char *get_var(t_shell *shell, char *name)
{
int i;
int size;
if (!name)
return (NULL);
size = strlen(name);
i = -1;
while (shell->vars[++i])
if (!strncmp(shell->vars[i], name, size) && shell->vars[i][size] == '\t')
return (shell->vars[i] + size + 1);
return (NULL);
}
void init_vars(t_shell *shell)
{
char *str;
shell->vars = NULL;
if (asprintf(&str, "%d", getpid()) == -1)
handle_error("malloc");
add_var(shell, "pid", str);
free(str);
if (asprintf(&str, "%d", getgid()) == -1)
handle_error("malloc");
add_var(shell, "gid", str);
free(str);
if (asprintf(&str, "%d", getpgrp()) == -1)
handle_error("malloc");
add_var(shell, "pgid", str);
free(str);
if (asprintf(&str, "%d", getsid(0)) == -1)
handle_error("malloc");
add_var(shell, "sid", str);
free(str);
}