-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.c
More file actions
53 lines (43 loc) · 1.03 KB
/
ls.c
File metadata and controls
53 lines (43 loc) · 1.03 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
52
53
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#define BUFFER_SIZE 4096
static char buffer[BUFFER_SIZE];
static char *buf_ptr = buffer;
static char *buf_end = buffer;
static inline void flush_buffer(void) {
if (buf_ptr > buffer) {
write(1, buffer, buf_ptr - buffer);
buf_ptr = buffer;
}
}
static inline void write_char(char c) {
if (buf_ptr >= buf_end) {
buf_end = buffer + BUFFER_SIZE;
flush_buffer();
}
*buf_ptr++ = c;
}
static inline void write_str(const char *s) {
while (*s) {
write_char(*s++);
}
}
int main(int argc, char **argv) {
DIR *dir;
struct dirent *entry;
const char *path = (argc > 1) ? argv[1] : ".";
buf_end = buffer + BUFFER_SIZE;
dir = opendir(path);
if (!dir) return 1;
while ((entry = readdir(dir))) {
if (entry->d_name[0] != '.') {
write_str(entry->d_name);
write_char(' ');
}
}
closedir(dir);
flush_buffer();
return 0;
}