-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextras.c
89 lines (81 loc) · 1.5 KB
/
extras.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include "main.h"
/**
* comment_out - it shortens the array of tokens by relacing # with NULL
* @arg: the array of tokens
*
* Return: 0 always
*/
int comment_out(char **arg)
{
int i;
for (i = 0; arg[i] != NULL; i++)
{
if ((strcmp(arg[i], "#") == 0) || arg[i][0] == '#')
{
arg[i] = NULL;
break;
}
}
return (0);
}
/**
* echo_dollar - handles the xases of $$, $? as well as envp
* @arg: the array of tokens
*
* Return: 0 always
*/
int echo_dollar(char **arg)
{
int i, j, exit = 0;
pid_t ppid = 0;
char *val = NULL, *ptr = NULL;
if (arg[1][1] == '$')
{
ppid = getppid();
printf("%d\n", ppid);
}
else if (arg[1][1] == '?')
{
exit = error_stat(-16);
printf("%d\n", exit);
}
else
{
val = malloc(sizeof(char) * strlen(arg[1]));
if (!val)
return (-1);
for (j = 0, i = 1; arg[1][i] != '\0'; i++, j++)
val[j] = arg[1][i];
val[j] = '\0';
ptr = getenv(val);
if (ptr)
printf("%s\n", ptr);
else
printf("\n");
free(val);
}
return (0);
}
/**
* exit_with_code - exiting and freeing resources
* @err_code: error code to be exited with
* @count: the number of commands until now
* @arg: the arguments array of tokens
* @string: a tring to be freed
*
* Return: Nothing.
*/
void exit_with_code(int err_code, int count, char **arg, char *string)
{
if (arg[1] != NULL)
err_code = exiting(arg[1], count);
free_grid(environ);
_alias(NULL, 0);
free(string);
free(arg);
exit(err_code);
}