-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls-lowmem.c
121 lines (106 loc) · 2.73 KB
/
ls-lowmem.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright 2013 Clemson University
*
* This file is part of lowmem-tools.
*
* lowmem-tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* lowmem-tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with lowmem-tools. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <dirent.h> /* Defines DT_* constants */
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include "version.h"
#define ERROR_SIZE 4096
#define GETDENTS_BUF_SIZE 32768
struct linux_dirent {
unsigned long d_ino;
unsigned long d_off;
unsigned short d_reclen;
char d_name[];
};
static char error_message[ERROR_SIZE];
static void usage(FILE *file, const char *arg0) {
fprintf(file,
"Usage: %s [options] [directory] ...\n"
"Options:\n"
" -h Print this message\n"
" -V Print version\n"
, arg0);
}
static int ls_lowmem(const char *dirname) {
int fd, nread, bpos, rc=0;
char buf[GETDENTS_BUF_SIZE];
struct linux_dirent *d;
fd = open(dirname, O_RDONLY | O_DIRECTORY);
if(fd == -1) {
perror(dirname);
return 1;
}
for(;;) {
nread = syscall(SYS_getdents, fd, buf, sizeof(buf));
if(nread == -1) {
snprintf(error_message, sizeof(error_message), "getdents failed on %s", dirname);
rc = 1;
goto out;
}
if(nread == 0)
break;
for(bpos = 0; bpos < nread; bpos += d->d_reclen) {
d = (struct linux_dirent *)(buf + bpos);
printf("%s\n", d->d_name);
}
}
out:
if(close(fd) == -1) {
perror(dirname);
rc = 1;
}
return rc;
}
int main(int argc, char *argv[]) {
int i, opt, fail=0;
const char *dirname;
while((opt = getopt(argc, argv, "hV")) != -1) {
switch(opt) {
case 'h':
usage(stdout, argv[0]);
exit(0);
case 'V':
printf("ls-lowmem (lowmem-tools) " VERSION "\n");
exit(0);
default:
usage(stderr, argv[0]);
exit(1);
}
}
if(optind == argc) {
ls_lowmem(".");
} else {
for(i = optind; i < argc; ++i) {
dirname = argv[i];
if(argc > 2) {
printf("%s:\n", dirname);
ls_lowmem(dirname);
printf("\n");
} else {
fail |= ls_lowmem(dirname);
}
}
}
exit(fail ? EXIT_FAILURE : EXIT_SUCCESS);
}