-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtinCMd.c
49 lines (38 loc) · 846 Bytes
/
builtinCMd.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
#include "main.h"
#define MAX_PATH_LENGTH 1024
/**
* cdCommand - add descr
* @shellInfo: add descr
* Return: add descr
*/
int cdCommand(commandInfo *shellInfo)
{
char *targetDirectory;
char currentDirectory[MAX_PATH_LENGTH];
if (shellInfo->command_arguments[1] == NULL
|| _strcmp(shellInfo->command_arguments[1], "~") == 0)
{
targetDirectory = getenv("HOME");
}
else if (_strcmp(shellInfo->command_arguments[1], "-") == 0)
{
targetDirectory = getenv("OLDPWD");
}
else
{
targetDirectory = shellInfo->command_arguments[1];
}
getcwd(currentDirectory, sizeof(currentDirectory));
if (chdir(targetDirectory) != 0)
{
perror("cd");
return (EXIT_FAILURE);
}
if (setenv("PWD", getcwd(NULL, 0), 1) != 0)
{
perror("setenv");
return (EXIT_FAILURE);
}
printf("%s\n", getcwd(NULL, 0));
return (EXIT_SUCCESS);
}