-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathmyls.c
78 lines (70 loc) · 2.23 KB
/
myls.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
#include <stdio.h> // fprintf, perror
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> // getopt
#include <stdlib.h> // exit, EXIT_FAILURE, EXIT_SUCCESS
#include <dirent.h> // opendir, readdir, closedir
#include <string.h> // strlen, strncpy, strncmp, strncat
#include <stdbool.h>
#include <time.h> // strftime, localtime
#define STRINGSIZE 1024
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
void print_file(struct stat sb) {
printf("%4lo ", (unsigned long) sb.st_mode);
printf("%3ld ", (long) sb.st_nlink);
printf("%3ld %3ld ", (long) sb.st_uid, (long) sb.st_gid);
printf("%4lld ", (long long) sb.st_size);
char timeString[STRINGSIZE] = "";
strftime(timeString, STRINGSIZE, "%b %d %H:%M", localtime(&sb.st_mtime));
printf("%s ", timeString);
}
int main(int argc, char *argv[]) {
struct stat sb;
int opt;
char * pathname = ".";
bool list = false;
DIR *dp;
opterr = 0; // disable getopt() error message
while ((opt = getopt(argc, argv, "l:")) != -1) {
switch (opt) {
case 'l':
pathname = optarg;
list = true;
break;
case '?':
if (optopt == 'l')
list = true;
break;
default:
break;
}
}
if (!list && argc > 1)
pathname = argv[1];
if (stat(pathname, &sb) == -1)
handle_error("stat");
if (S_ISDIR(sb.st_mode)) {
if ((dp = opendir(pathname)) == NULL)
handle_error("opendir");
struct dirent *d;
while ((d = readdir(dp)) != NULL) {
if (list) {
char filePath[STRINGSIZE] = "";
strncpy(filePath, pathname, strlen(pathname));
strncat(filePath, "/", 1);
strncat(filePath, d->d_name, strlen(d->d_name));
if (stat(filePath, &sb) == -1)
handle_error("stat");
print_file(sb);
}
printf("%s\n", d->d_name);
}
closedir(dp);
} else {
if (list)
print_file(sb);
printf("%s\n", pathname);
}
exit(EXIT_SUCCESS);
}