-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd.c
More file actions
34 lines (29 loc) · 637 Bytes
/
cd.c
File metadata and controls
34 lines (29 loc) · 637 Bytes
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
#include<stdlib.h>
#include<stdio.h>
#include <unistd.h>
void cd(char* arg){
// comment for debugging
// printf("custom cd:");
// check if the path exists
if(chdir(arg) == -1){
// print error message
printf("cd: %s: No such file or directory\n", arg);
} else {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
// print the current working directory
printf("Current working directory: %s\n", cwd);
} else {
// print error message
perror("getcwd() error");
}
}
}
int main(int argc, char** argv){
if(argc<2){
printf("Pass the path as an argument\n");
return 0;
}
cd(argv[1]);
return 0;
}