-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlist.c
More file actions
51 lines (43 loc) · 1.18 KB
/
list.c
File metadata and controls
51 lines (43 loc) · 1.18 KB
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
// implementation of linux ls command
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
// a necissary function for scandir which only returns the number one
// if we wanted to list only certain things then we would edit this method
static int filter(const struct dirent *unused){
return 1;
}
void print_error(char *this, char *dir){
fprintf(stderr, "%s cannot list from %s\n%s\n", this, dir, strerror(errno));
exit(EXIT_FAILURE);
}
void print_usage(char *this){
puts("ERROR:");
fprintf(stderr, "Usage: %s [directory]\n", this);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]){
errno = 0;
struct dirent **contents;
int content_count;
if(argc < 2){
if((content_count = scandir("./", &contents, filter, alphasort))<0){
print_error(argv[0], "./");
}
}
else if(argc == 2){
if((content_count = scandir(argv[1], &contents, filter, alphasort))<0){
print_error(argv[0], argv[1]);
}
}
else{
print_usage(argv[0]);
}
int i;
for(i=0; i<content_count; i++){
puts(contents[i]->d_name);
}
return 0;
}