-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.c
More file actions
107 lines (96 loc) · 2.16 KB
/
misc.c
File metadata and controls
107 lines (96 loc) · 2.16 KB
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
107
#include "shs.h"
/**
* _itoa - Stores an integer in a pointer to
* already allocated memory
*
* @wait_status: Integet to store in memory address
* @s: Pointer to memory address to store integer into
*/
void _itoa(int wait_status, char *s)
{
int i, status;
status = wait_status;
for (i = 0; status / 10 != 0; i++)
status = status / 10;
s[i + 1] = '\0';
for (; i >= 0; i--)
{
s[i] = (wait_status % 10) + '0';
wait_status = wait_status / 10;
}
}
/**
* trimspaces - Removes spaces at the start of a string
*
* @pinput: pointer to address with string to remove spaces from
*/
void trimspaces(char **pinput)
{
int i = 0;
while ((*pinput)[0] == ' ' || (*pinput)[0] == '\t')
{
(*pinput)++;
i++;
}
}
/**
* trimcomments - Removes all character after a #.
*
* @pinput: double pointer to prompt input.
*/
void trimcomments(char **pinput)
{
int i;
for (i = 0; *(*pinput + i) != '\0'; i++)
if (*(*pinput + i) == ' ' && *(*pinput + i + 1) == '#')
{
for (i += 1; *(*pinput + i) != '\0'; i++)
*(*pinput + i) = '\0';
break;
}
}
/**
* trimexit - Removes spaces at the start of a string
*
* @pinput: pointer to save string in.
* @env: pointer to string with PATH values
* @exit_c: pointer to string with exit code
* Return: void.
*/
void trimexit(char **pinput, char **env, char *exit_c)
{
char *command_exit = "exit", *temp = NULL;
int i = 0, j = 0, exit_flag = 0, size_of_exit_code = 0, exit_code = 0;
temp = malloc(5 * sizeof(char));
if (temp == NULL)
return;
for (i = 0; *(*pinput + i) != '\0' && i < 4; i++)
*(temp + i) = *(*pinput + i);
*(temp + 4) = '\0';
if (_strcmp(command_exit, temp) == 0)
exit_flag = 1;
else
return;
free(temp);
for (i = 0; *(*pinput + i) != ' '; i++)
;
for (j = i + 1; *(*pinput + j) != ' ' && *(*pinput + j) != '\0'; j++)
size_of_exit_code++;
free(temp);
temp = NULL;
temp = malloc((size_of_exit_code + 1) * sizeof(char));
if (temp == NULL)
return;
for (i += 1, j = 0; j < size_of_exit_code; i++, j++)
*(temp + j) = *(*pinput + i);
*(temp + j) = '\0';
exit_code = _atoi(temp);
free(temp);
if (exit_flag && exit_code)
{
free(exit_c);
free(*env);
free(*pinput);
exit(exit_code);
}
}