-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbulltin.c
153 lines (142 loc) · 2.52 KB
/
bulltin.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
#include "shell.h"
/**
* exit_bul - Exit Statue Shell
* @cmd: Parsed Command
* @input: User Input
* @argv:Program Name
* @c:Excute Count
* Return: Void (Exit Statue)
*/
void exit_bul(char **cmd, char *input, char **argv, int c)
{
int statue, i = 0;
if (cmd[1] == NULL)
{
free(input);
free(cmd);
exit(EXIT_SUCCESS);
}
while (cmd[1][i])
{
if (_isalpha(cmd[1][i++]) != 0)
{
_prerror(argv, c, cmd);
break;
}
else
{
statue = _atoi(cmd[1]);
free(input);
free(cmd);
exit(statue);
}
}
}
/**
* change_dir - Change Dirctorie
* @cmd: Parsed Command
* @er: Statue Last Command Excuted
* Return: 0 Succes 1 Failed (For Old Pwd Always 0 Case No Old PWD)
*/
int change_dir(char **cmd, __attribute__((unused))int er)
{
int value = -1;
char cwd[PATH_MAX];
if (cmd[1] == NULL)
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 if (value != -1)
{
getcwd(cwd, sizeof(cwd));
setenv("OLDPWD", getenv("PWD"), 1);
setenv("PWD", cwd, 1);
}
return (0);
}
/**
* dis_env - Display Enviroment Variable
* @cmd:Parsed Command
* @er:Statue of Last command Excuted
* Return:Always 0
*/
int dis_env(__attribute__((unused)) char **cmd, __attribute__((unused)) int er)
{
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);
}
/**
* display_help - Displaying Help For Builtin
* @cmd:Parsed Command
* @er: Statue Of Last Command Excuted
* Return: 0 Succes -1 Fail
*/
int display_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_bul - Excute Echo Cases
* @st:Statue Of Last Command Excuted
* @cmd: Parsed Command
* Return: Always 0 Or Excute Normal Echo
*/
int echo_bul(char **cmd, int st)
{
char *path;
unsigned int pid = getppid();
if (_strncmp(cmd[1], "$?", 2) == 0)
{
print_number_in(st);
PRINTER("\n");
}
else if (_strncmp(cmd[1], "$$", 2) == 0)
{
print_number(pid);
PRINTER("\n");
}
else if (_strncmp(cmd[1], "$PATH", 5) == 0)
{
path = _getenv("PATH");
PRINTER(path);
PRINTER("\n");
free(path);
}
else
return (print_echo(cmd));
return (1);
}