-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltins.c
156 lines (145 loc) · 2.55 KB
/
builtins.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
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
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "main.h"
/**
* ex_it - shell with a status
* @cmd: parsed command
* @input: user input
* @argv: argument vector
* @count: command count
* Return: Void
*/
void ex_it(char **cmd, char *input, char **argv, int count)
{
int status, i = 0;
if (cmd[1] == NULL)
{
free(input);
free(cmd);
exit(0);
}
while (cmd[1][i])
{
if (_isalpha(cmd[1][i++]) != 0)
{
custom_error(argv, count, cmd);
break;
}
else
{
status = _atoi(cmd[1]);
free(input);
free(cmd);
exit(status);
}
}
}
/**
* change_dir - change directory
* @cmd: command
* @er: exit status of the last command executed.
* Return: 0 on success, 1 on failure.
*/
int change_dir(char **cmd, __attribute__((unused))int er)
{
int value = -1;
char cwd[PATH_MAX];
if (cmd[1] == NULL || _strcmp(cmd[1], ".") == 0)
value = chdir(getenv("HOME"));
else if (_strcmp(cmd[1], "-") == 0)
{
value = chdir(getenv("OLDPWD"));
}
else
value = chdir(cmd[1]);
if (value == -1)
{
perror("hsh");
return (-1);
}
else
{
getcwd(cwd, sizeof(cwd));
setenv("OLDPWD", getenv("PWD"), 1);
setenv("PWD", cwd, 1);
}
return (0);
}
/**
* print_env - display environment variables
* @cmd:Parsed Command
* @x:status of Last command executed
* Return:Always 0
*/
int print_env(__attribute__((unused))char **cmd, __attribute__((unused))int x)
{
size_t i;
int len;
for (i = 0; environ[i] != NULL; i++)
{
len = _strlen(environ[i]);
write(1, environ[i], len);
write(STDOUT_FILENO, "\n", 1);
}
return (0);
}
/**
* print_help - Displaying Help For Builtin
* @cmd:Parsed Command
* @er: status of the last command executed
* Return: 0 on success, on -1 failure.
*/
int print_help(char **cmd, __attribute__((unused))int er)
{
int fd, fw, rd = 1;
char c;
fd = open(cmd[1], O_RDONLY);
if (fd < 0)
{
perror("Error");
return (0);
}
while (rd > 0)
{
rd = read(fd, &c, 1);
fw = write(STDOUT_FILENO, &c, rd);
if (fw < 0)
{
return (-1);
}
}
_putchar('\n');
return (0);
}
/**
* echo_call - execute echo command
* @st: status of the last command executed
* @cmd: Parsed Command
* Return: 0 or 1
*/
int echo_call(char **cmd, int st)
{
char *path;
unsigned int pid = getppid();
if (_strncmp(cmd[1], "$?", 2) == 0)
{
print_int(st);
PRINT("\n");
}
else if (_strncmp(cmd[1], "$$", 2) == 0)
{
print_pos_number(pid);
PRINT("\n");
}
else if (_strncmp(cmd[1], "$PATH", 5) == 0)
{
path = _getenv("PATH");
PRINT(path);
PRINT("\n");
free(path);
}
else
return (print_echo(cmd));
return (1);
}