-
Notifications
You must be signed in to change notification settings - Fork 1
/
bui1tin_11.c
92 lines (86 loc) · 2.06 KB
/
bui1tin_11.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
#include "shell.h"
/**
* _myexit - a function that handles the 'exit' command in the shell.
* @info: A pointer to a structure containing information about the program.
* Return: 1 if there is an exit argument with an illegal number, -2 otherwise.
*/
int _myexit(info_t *info)
{
int check_ex;
if (info->argv[1]) /* If there is an exit argument */
{
check_ex = _erratoi(info->argv[1]);
if (check_ex == -1)
{
info->status = 2;
print_error(info, "Illegal number: ");
_eputs(info->argv[1]);
_eputchar('\n');
return (1);
}
info->err_num = _erratoi(info->argv[1]);
return (-2);
}
info->err_num = -1;
return (-2);
}
/**
* _mycd - a function that handles the 'cd' command in the shell.
* @info: A pointer to a structure containing information about the program.
* Return: 0 on successful directory change, 1 if an error occurs.
*/
int _mycd(info_t *info)
{
char *str, *direc, buf[1024];
int chdirec_ret;
str = getcwd(buf, 1024);
if (!str)
_puts("TODO: >>getcwd failure emsg here<<\n");
if (!info->argv[1])
{
direc = _getenv(info, "HOME=");
if (!direc)
chdirec_ret = chdir((direc = _getenv(info, "PWD=")) ? direc : "/");
else
chdirec_ret = chdir(direc);
}
else if (_strcmp(info->argv[1], "-") == 0)
{
if (!_getenv(info, "OLDPWD="))
{
_puts(str);
_putchar('\n');
return (1);
}
_puts(_getenv(info, "OLDPWD=")), _putchar('\n');
chdirec_ret = chdir((direc = _getenv(info, "OLDPWD=")) ? direc : "/");
}
else
chdirec_ret = chdir(info->argv[1]);
if (chdirec_ret == -1)
{
print_error(info, "can't cd to ");
_eputs(info->argv[1]);
_eputchar('\n');
}
else
{
_setenv(info, "OLDPWD", _getenv(info, "PWD="));
_setenv(info, "PWD", getcwd(buf, 1024));
}
return (0);
}
/**
* _myhelp - a function that displays a help message for the shell.
* @info: A pointer to a structure containing information about the program.
* Return: 0 always.
*/
int _myhelp(info_t *info)
{
char **arg_arr;
arg_arr = info->argv;
_puts("help call works. Function not yet implemented \n");
if (0)
_puts(*arg_arr); /* temp att_unused workaround */
return (0);
}