-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilt_in_cd.c
More file actions
109 lines (101 loc) · 2.41 KB
/
built_in_cd.c
File metadata and controls
109 lines (101 loc) · 2.41 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
#include "main.h"
/**
* cd - change the current working directory.
*
* @CMD: Command type single list.
* @lastSignal: last signal from executed functions.
* @cmdNum: the executed command number.
* @programName: the running executable name.
* @buffer: the original buffer from getline function.
*
* Return: (0) always success, (EXIT_CODE) otherwise.
*/
int cd(Commands *CMD, int *lastSignal, int cmdNum,
char *programName, char *buffer)
{
char *dir = NULL, *cwd = NULL;
char **env = NULL;
int len, chdirStatus, retStatus;
(void) lastSignal, (void) buffer;
retStatus = EXIT_SUCCESS;
for (len = 0; CMD->cmd[len];)
len++;
cwd = getcwd(cwd, 200);
if (len == 1)
{
env = _getenv("HOME");
if (env == NULL || *env == NULL)
{
free(cwd);
if (env != NULL)
free(env);
return (retStatus);
}
dir = *env;
}
else if (_strcmp(CMD->cmd[1], "-") == 0)
{
if (cd_prevDir(&env, cwd, &dir))
return (retStatus);
}
else
dir = CMD->cmd[1];
chdirStatus = chdir(dir);
if (chdirStatus == 0)
setenv("OLDPWD", cwd, 1);
else
retStatus = print_cdErr(programName, cmdNum, dir);
if (env != NULL)
free_2D(env, 1);
free(cwd);
return (retStatus);
}
/**
* cd_prevDir - change the current working directory to
* the previous directory.
*
* @env: var to store the OLDPWD environment variable.
* @cwd: the current working directory.
* @dir: the directory to change to.
*
* Return: (0) always success, (1) otherwise.
*/
int cd_prevDir(char ***env, char *cwd, char **dir)
{
*env = _getenv("OLDPWD");
if (*env == NULL || **env == NULL)
{
write(STDOUT_FILENO, cwd, _strlen(cwd));
write(STDOUT_FILENO, "\n", 1);
if (!(*env))
free(*env);
free(cwd);
return (1);
}
*dir = **env;
write(STDOUT_FILENO, *dir, _strlen(*dir));
write(STDOUT_FILENO, "\n", 1);
return (0);
}
/**
* print_cdErr - prints cd errors.
*
* @programName: the executable program name.
* @cmdNum: the executed command number.
* @dir: the directory to change to.
*
* Return: (2) indicates cd error.
*/
int print_cdErr(char *programName, int cmdNum, char *dir)
{
char *cmdNumStr = NULL;
cmdNumStr = _btoi(cmdNum);
write(STDERR_FILENO, programName, _strlen(programName));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, cmdNumStr, _strlen(cmdNumStr));
write(STDERR_FILENO, ": cd: can't cd to ", _strlen(": cd: can't cd to "));
write(STDERR_FILENO, dir, _strlen(dir));
write(STDERR_FILENO, "\n", 1);
free(cmdNumStr);
return (2);
}