-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd.c
101 lines (90 loc) · 2 KB
/
cd.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
#include "shellheader.h"
/**
* cd_shell - Implementation of the "cd" built-in command
* @mFV: struct containing main function variables
* Description: This function is responsible for handling the "cd"
* command in the simple shell. It parses the argument after "cd"
* and calls the cdCommand function to change the current working
* directory.
* Return: Returns 0 to indicate successful execution of the "cd"
* command.
*/
int cd_shell(struct mainFunctionVariables *mFV)
{
if (mFV->tokens[1] == NULL)
{
cdCommand(NULL);
}
else
{
cdCommand(mFV->tokens[1]);
}
return (0);
}
/**
* cdCommand - Change the current working directory based on the given
* directory.
* @directory: The target directory to change to.
*
* If the directory is NULL, interpret it as cd $HOME.
* If the directory is "-", change to the previous directory (OLPWD).
* Otherwise, change to the specified directory.
*
* Update environment variables PWD and OLDPWD accordingly.
*/
void cdCommand(char *directory)
{
char *oldPwd;
char *newDirectory;
char *currentDirectory;
if (directory == NULL)
{
newDirectory = getenv("HOME");
}
else if (strcmp(directory, "-") == 0)
{
newDirectory = getenv("OLDPWD");
}
else
{
newDirectory = directory;
}
currentDirectory = getcwd(NULL, 0);
if (chdir(newDirectory) != 0)
{
perror("cd");
}
else
{
oldPwd = getenv("PWD");
setenv("OLDPWD", oldPwd ? oldPwd : "", 1);
setenv("PWD", currentDirectory, 1);
}
free(currentDirectory);
}
/**
* handle_errors - prints errors based on case
* @error: error number associated with perror statement
* Return: void
*/
void handle_errors(int error)
{
switch (error)
{
case 1: /* fork error */
write(STDERR_FILENO, EF, _stringlen(EF));
perror("Error");
break;
case 2: /* execve error */
perror("Error");
break;
case 3: /* malloc error */
write(STDERR_FILENO, EM, _stringlen(EM));
break;
case 4: /* writes empty path error to stderr */
write(STDERR_FILENO, EP, _stringlen(EP));
break;
default:
return;
}
}