-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcd.c
35 lines (30 loc) · 826 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int cd(char **args)
{
/* static local variable, to preserve the old pwd */
static char *oldpwd;
char *tmp = getcwd(NULL, 0);
/* if called with no arguments, then chdir to $HOME */
if (!args[1]) {
if (chdir(getenv("HOME")) != 0) {
perror("chdir($HOME)");
return -1;
}
} else if (args[1][0] == '-' && args[1][1] == '\0') {
/* if called with '-', then chdir to $OLDPWD */
if (chdir(oldpwd) != 0) {
perror("chdir($OLDPWD)");
return -1;
}
} else {
if (chdir(args[1]) != 0) {
fprintf(stderr, "cd: %s: No such file or directory\n", args[1]);
return -1;
}
}
free(oldpwd);
oldpwd = tmp;
return 0;
}