-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccd.c
74 lines (65 loc) · 1.4 KB
/
ccd.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <stdlib.h>
#define PATH 1024
/*
Simulates the behavior of the 'cd' command in Linux
cd ..
cd Dir
cd ~
*/
int cd(char *arg)
{
char current_dir[PATH];
// Case 1: Change to the home directory (cd ~)
if (strcmp(arg, "~") == 0 || strcmp(arg, "~/") == 0)
{
struct passwd *pw = getpwuid(getuid());
const char *home_dir = pw->pw_dir;
if (chdir(home_dir) != 0)
{
perror("chdir to home directory failed");
return 1;
}
}
// Case 2: Change to the parent directory (cd ..)
else if (strcmp(arg, "..") == 0)
{
if (chdir("..") != 0)
{
perror("chdir to parent directory failed");
return 1;
}
}
// Case 3: Change to a specified directory (cd Dir)
else
{
if (chdir(arg) != 0)
{
perror("chdir to specified directory failed");
return 1;
}
}
if (getcwd(current_dir, sizeof(current_dir)) != NULL)
{
printf("Current directory: %s\n", current_dir);
}
else
{
perror("getcwd error");
return 1;
}
return 0;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: %s <directory>\n", argv[0]);
return 1;
}
return cd(argv[1]);
return 0;
}