-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.c
62 lines (45 loc) · 814 Bytes
/
ls.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
/**
carrot unix commands :)
unix "ls" implemention in c
**/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <limits.h>
#include <unistd.h>
void list(char dirc[PATH_MAX])
{
DIR *d;
struct dirent *dir;
d = opendir(dirc);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
printf("%s\n", dir->d_name);
}
closedir(d);
}
}
void current_directory(void)
{
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
list(cwd);
} else {
perror("getcwd() error");
}
}
int main(int argc, char **argv)
{
if(argc == 1){
current_directory();
return 0;
}
else if( argc > 2 ) {
printf("Too many arguments supplied.\n");
return 0;
}
list(argv[1]);
}