-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin_functions.c
More file actions
157 lines (143 loc) · 2.78 KB
/
Copy pathbuiltin_functions.c
File metadata and controls
157 lines (143 loc) · 2.78 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "shell.h"
#include "symtab.h"
/**
* dump - Dump the symbol table
* @cmd: The command data
*
* Return: 0
*/
int dump(UCommand * cmd __attribute__((unused)))
{
dump_local_symtab();
return (0);
}
/**
* exit_shell - Exit the shell
* @cmd: The command data
*
* Return: 0 on success, 1 on failure
*/
int exit_shell(UCommand *cmd)
{
int check;
if (cmd->ac > 1)
{
check = _erratoi(cmd->av[1]);
if (check == -1)
{
char buffer[20];
cmd->status = 2;
print(cmd->shell_name, STDERR_FILENO);
print(": ", STDERR_FILENO);
print(itoa(cmd->run_count, buffer, 10), STDERR_FILENO);
print(": exit: Illegal number: ", STDERR_FILENO);
print(cmd->av[1], STDERR_FILENO);
print("\n", STDERR_FILENO);
return (1);
}
cmd->exit_status = _erratoi(cmd->av[1]);
cmd->exit_state = 1;
}
else if (cmd->ac == 1)
{
cmd->exit_status = 0;
cmd->exit_state = 1;
}
return (0);
}
/**
* _cd - Change the current working directory
* @cmd: The command data
*
* Return: 0 on success, 1 on failure
*/
int _cd(UCommand *cmd)
{
ST_entry *entry;
int ret;
char *home = NULL, *oldpwd = NULL, *pwd = NULL, buf[1024];
entry = get_symtab_entry("PWD");
if (entry == NULL)
return (1);
pwd = entry->val;
entry = get_symtab_entry("HOME");
if (entry == NULL)
return (1);
home = entry->val;
if (!cmd->av[1])
ret = chdir(home);
else if (_strcmp(cmd->av[1], "-") == 0)
{
entry = get_symtab_entry("OLDPWD");
if (!entry)
{
print(cmd->av[2], STDOUT_FILENO), print("\n", STDOUT_FILENO);
return (1);
}
oldpwd = entry->val;
print(oldpwd, STDOUT_FILENO), print("\n", STDOUT_FILENO);
ret = chdir(oldpwd);
}
else
ret = chdir(cmd->av[1]);
if (ret == -1)
{
print_custom_error(cmd, "can't cd to "), print("\n", STDERR_FILENO);
return (1);
}
else
{
symtab_entry_setval(get_symtab_entry("OLDPWD"), pwd);
symtab_entry_setval(get_symtab_entry("PWD"), getcwd(buf, 1024));
}
return (ret);
}
/**
* remove_comments - Remove comment
* @cmd: string to check
*
* Return: The changed command
*/
char **remove_comments(char **cmd)
{
int i = 0;
for (i = 0; cmd[i] != NULL; i++)
{
if ((cmd[i][0] == '#') || (strcmp(cmd[i], "#") == 0))
{
int j;
for (j = i; cmd[j] != NULL; j++)
{
free(cmd[j]);
cmd[j] = NULL;
}
}
}
return (cmd);
}
/**
* _erratoi - converts a string to an integer
* @s: the string to be converted
* Return: 0 if no numbers in string, converted number otherwise
* -1 on error
*/
int _erratoi(char *s)
{
int i = 0;
unsigned long int result = 0;
if (*s == '+')
s++; /* TODO: why does this make main return 255? */
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] >= '0' && s[i] <= '9')
{
result *= 10;
result += (s[i] - '0');
if (result > __INT_MAX__)
return (-1);
}
else
return (-1);
}
return (result);
}