-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltins1.c
109 lines (102 loc) · 1.64 KB
/
builtins1.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
#include "shell.h"
/**
* sh_env - prints the current enviroment
* @args: unused attribute
*
* Return: 0 if successful.
*/
int sh_env(__attribute__((unused))char **args)
{
char **env = environ;
while (*env != NULL)
{
printf("%s\n", *env);
env++;
}
return (0);
}
/**
* sh_cd - change working directory to the specified directory
* @args: string of arguments to specify directory to change to
*
* Return: 0 if successful, or 1 if not
*/
int sh_cd(char **args)
{
static char *oldpwd;
char *dir = args[1];
char *pwd;
if (dir == NULL)
{
dir = getenv("HOME");
}
if (dir == NULL)
{
printf("sh: cd HOME not set\n");
return (1);
}
else if (strcmp(dir, "-") == 0)
{
if (oldpwd == NULL)
{
printf("sh: cd OLDPWD not set\n");
return (1);
}
dir = oldpwd;
}
pwd = getcwd(NULL, 0);
if (pwd == NULL)
{
perror("getcwd");
return (1);
}
if (chdir(dir) != 0)
{
perror("chdir");
free(pwd);
return (1);
}
if (oldpwd)
{
free(oldpwd);
}
oldpwd = pwd;
return (0);
}
/**
* sh_help - prints help message to user
* @args: unused attribute
*
* Return: Always 0.
*/
int sh_help(__attribute__((unused))char **args)
{
printf("Type command names and arguments then hit enter.\n");
printf("Use the man command for information on other commands.\n");
return (0);
}
/**
* sh_exit - exits program
* @args: exit status(if any)
*
* Return: exit with status(if any) else
*/
int sh_exit(char **args)
{
int status, i = 0;
if (args[1] == NULL)
{
while (args[i])
free(args[i++]);
free(args);
exit(0);
}
else
{
status = _atoi(args[1]);
while (args[++i])
free(args[i - 1]);
free(args);
exit(status);
}
}