-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir.c
66 lines (49 loc) · 1.62 KB
/
dir.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
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include "dir.h"
#include <sys/stat.h>
/*
Arguments:
fd - a valid open file descriptor. This is not checked for validity
or for errors with it is used.
directory - a pointer to a null terminated string that names a
directory
Returns
-1 the named directory does not exist or you don't have permission
to read it.
-2 insufficient resources to perform request
This function takes the name of a directory and lists all the regular
files and directories in the directory.
*/
int listFiles(int fd, char * directory) {
// Get resources to see if the directory can be opened for reading
DIR * dir = NULL;
dir = opendir(directory);
if (!dir) return -1;
// Setup to read the directory. When printing the directory
// only print regular files and directories.
struct dirent *dirEntry;
int entriesPrinted = 0;
for (dirEntry = readdir(dir);
dirEntry;
dirEntry = readdir(dir)) {
if (dirEntry->d_type == DT_REG) { // Regular file
struct stat buf;
// This call really should check the return value
stat(dirEntry->d_name, &buf);
dprintf(fd, "F %-20s %lld\r\n", dirEntry->d_name, buf.st_size);
} else if (dirEntry->d_type == DT_DIR) { // Directory
dprintf(fd, "D %s\r\n", dirEntry->d_name);
} else {
dprintf(fd, "U %s\r\n", dirEntry->d_name);
}
entriesPrinted++;
}
// Release resources
closedir(dir);
return entriesPrinted;
}