-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
908 lines (783 loc) · 22 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
#include <dirent.h>
#include <errno.h>
#include <fnmatch.h>
#include <grp.h>
#include <libgen.h>
#include <limits.h>
#include <locale.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
/**
* a linked list containing the parsed parameters
*/
typedef struct params_s {
char *location;
int help;
int print;
int ls;
int nouser;
char type;
char *user;
unsigned int userid;
char *path;
char *name;
struct params_s *next;
} params_t;
void do_help(void);
int do_parse_params(int argc, char *argv[], params_t *params);
int do_free_params(params_t *params);
int do_location(params_t *params);
int do_file(char *path, params_t *params, struct stat attr);
int do_dir(char *path, params_t *params, struct stat attr);
int do_print(char *path);
int do_ls(char *path, struct stat attr);
int do_type(char type, struct stat attr);
int do_nouser(struct stat attr);
int do_user(unsigned int userid, struct stat attr);
int do_name(char *path, char *pattern);
int do_path(char *path, char *pattern);
char do_get_type(struct stat attr);
char *do_get_perms(struct stat attr);
char *do_get_user(struct stat attr);
char *do_get_group(struct stat attr);
char *do_get_mtime(struct stat attr);
char *do_get_symlink(char *path, struct stat attr);
/**
* a global variable containing the program name
* used for error messages
*/
char *program_name = "";
/**
* @brief entry point; calls do_parse_params and do_location
*
* @param argc number of arguments
* @param argv the arguments
*
* @returns EXIT_SUCCESS, EXIT_FAILURE
*/
int main(int argc, char *argv[]) {
params_t *params;
program_name = argv[0];
/* honor the system locale */
if (!setlocale(LC_ALL, "")) {
fprintf(stderr, "%s: setlocale() failed\n", program_name);
}
params = calloc(1, sizeof(*params));
if (!params) {
fprintf(stderr, "%s: calloc(): %s\n", program_name, strerror(errno));
return EXIT_FAILURE;
}
if (do_parse_params(argc, argv, params) != EXIT_SUCCESS) {
do_free_params(params);
return EXIT_FAILURE;
}
if (params->help) {
do_help();
do_free_params(params);
return EXIT_SUCCESS;
}
if (do_location(params) != EXIT_SUCCESS) {
do_free_params(params);
return EXIT_FAILURE;
}
do_free_params(params);
return EXIT_SUCCESS;
}
/**
* @brief prints out the program usage
*/
void do_help(void) {
if (printf("usage:\n"
"myfind [ <location> ] [ <aktion> ]\n"
"-help show this message\n"
"-user <name>|<uid> entries belonging to a user\n"
"-name <pattern> entry names matching a pattern\n"
"-type [bcdpfls] entries of a specific type\n"
"-print print entries with paths\n"
"-ls print entry details\n"
"-nouser entries not belonging to a user\n"
"-path entry paths (incl. names) matching a pattern\n") < 0) {
fprintf(stderr, "%s: printf(): %s\n", program_name, strerror(errno));
}
}
/**
* @brief parses argv and populates the params struct
*
* @param argc the number of arguments from argv
* @param argv the arguments from argv
* @param params the struct to populate
*
* @returns EXIT_SUCCESS, EXIT_FAILURE
*/
int do_parse_params(int argc, char *argv[], params_t *params) {
struct passwd *pwd;
int i; /* used outside of the loop */
/*
* 0 = ok or nothing to check
* 1 = unknown predicate
* 2 = missing argument for predicate
* 3 = unknown argument for predicate
* 4 = unknown user
* 5 = path after expression
*/
int status = 0;
int expression = 0;
/* params can start from argv[1] */
for (i = 1; i < argc; i++, params = params->next) {
/* allocate memory for the next run */
params->next = calloc(1, sizeof(*params));
if (!params->next) {
fprintf(stderr, "%s: calloc(): %s\n", program_name, strerror(errno));
return EXIT_FAILURE;
}
/* parameters consisting of a single part */
if (strcmp(argv[i], "-help") == 0) {
params->help = 1;
expression = 1;
continue;
}
if (strcmp(argv[i], "-print") == 0) {
params->print = 1;
expression = 1;
continue;
}
if (strcmp(argv[i], "-ls") == 0) {
params->ls = 1;
expression = 1;
continue;
}
if (strcmp(argv[i], "-nouser") == 0) {
params->nouser = 1;
expression = 1;
continue;
}
/* parameters expecting a non-empty second part */
if (strcmp(argv[i], "-user") == 0) {
if (argv[++i]) {
params->user = argv[i];
/* check if the user exists */
if ((pwd = getpwnam(params->user))) {
params->userid = pwd->pw_uid;
expression = 1;
continue;
}
/* otherwise, if the input is a number, use that */
if (sscanf(params->user, "%u", ¶ms->userid)) {
expression = 1;
continue;
}
status = 4;
break; /* the user is not found and not a number */
} else {
status = 2;
break; /* the second part is missing */
}
}
if (strcmp(argv[i], "-name") == 0) {
if (argv[++i]) {
params->name = argv[i];
expression = 1;
continue;
} else {
status = 2;
break; /* the second part is missing */
}
}
if (strcmp(argv[i], "-path") == 0) {
if (argv[++i]) {
params->path = argv[i];
expression = 1;
continue;
} else {
status = 2;
break; /* the second part is missing */
}
}
/* a parameter expecting a restricted second part */
if (strcmp(argv[i], "-type") == 0) {
if (argv[++i]) {
if ((strcmp(argv[i], "b") == 0) || (strcmp(argv[i], "c") == 0) ||
(strcmp(argv[i], "d") == 0) || (strcmp(argv[i], "p") == 0) ||
(strcmp(argv[i], "f") == 0) || (strcmp(argv[i], "l") == 0) ||
(strcmp(argv[i], "s") == 0)) {
params->type = argv[i][0];
expression = 1;
continue;
} else {
status = 3;
break; /* the second part is unknown */
}
} else {
status = 2;
break; /* the second part is missing */
}
}
/*
* there was no match;
* if the parameter starts with '-', return an error,
* else if there were no previous matches (expressions), save it as a location
*/
if (argv[i][0] == '-') {
status = 1;
break;
} else {
if (expression == 0) {
params->location = argv[i];
continue;
} else {
status = 5;
break;
}
}
}
/* error handling */
if (status == 1) {
fprintf(stderr, "%s: unknown predicate: `%s'\n", program_name, argv[i]);
return EXIT_FAILURE;
}
if (status == 2) {
fprintf(stderr, "%s: missing argument to `%s'\n", program_name, argv[i - 1]);
return EXIT_FAILURE;
}
if (status == 3) {
fprintf(stderr, "%s: unknown argument to %s: %s\n", program_name, argv[i - 1], argv[i]);
return EXIT_FAILURE;
}
if (status == 4) {
fprintf(stderr, "%s: `%s' is not the name of a known user\n", program_name, argv[i]);
return EXIT_FAILURE;
}
if (status == 5) {
fprintf(stderr, "%s: paths must precede expression: %s\n", program_name, argv[i]);
fprintf(stderr, "Usage: %s [ <location> ] [ <aktion> ]\n", program_name);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/**
* @brief frees the params linked list
*
* @param params the parsed parameters
*
* @returns EXIT_SUCCESS
*/
int do_free_params(params_t *params) {
while (params) {
params_t *next = params->next;
free(params);
params = next;
}
return EXIT_SUCCESS;
}
/**
* @brief calls do_file and do_directory on locations from the params struct
*
* @param params the parsed parameters
*
* @returns EXIT_SUCCESS, EXIT_FAILURE
*/
int do_location(params_t *params) {
struct stat attr;
char *location;
do {
location = params->location;
if (!location) {
location = ".";
}
/*
* try reading the attributes of the location
* to verify that it exists and to check if it is a directory
*/
if (lstat(location, &attr) == 0) {
do_file(location, params, attr);
/* if a directory, process its contents */
if (S_ISDIR(attr.st_mode)) {
do_dir(location, params, attr);
}
} else {
fprintf(stderr, "%s: lstat(%s): %s\n", program_name, location, strerror(errno));
return EXIT_FAILURE;
}
params = params->next;
} while (params && params->location);
/* GNU find returns 1 even if a single entry failed */
if (errno != 0) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/**
* @brief checks the entry using subfunctions based on params, if passed, prints it
*
* @param path the path to be processed
* @param params the parsed parameters
* @param attr the entry attributes from lstat
*
* @returns EXIT_SUCCESS, EXIT_FAILURE
*/
int do_file(char *path, params_t *params, struct stat attr) {
int printed = 0;
do {
/* filtering */
if (params->type) {
if (do_type(params->type, attr) != EXIT_SUCCESS) {
return EXIT_SUCCESS; /* the entry didn't pass the check, do not print it */
}
}
if (params->nouser) {
if (do_nouser(attr) != EXIT_SUCCESS) {
return EXIT_SUCCESS;
}
}
if (params->user) {
if (do_user(params->userid, attr) != EXIT_SUCCESS) {
return EXIT_SUCCESS;
}
}
if (params->name) {
if (do_name(path, params->name) != EXIT_SUCCESS) {
return EXIT_SUCCESS;
}
}
if (params->path) {
if (do_path(path, params->path) != EXIT_SUCCESS) {
return EXIT_SUCCESS;
}
}
/* printing */
if (params->print) {
if (do_print(path) != EXIT_SUCCESS) {
return EXIT_FAILURE; /* a fatal error occurred */
}
printed = 1;
}
if (params->ls) {
if (do_ls(path, attr) != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
printed = 1;
}
params = params->next;
} while (params);
if (printed == 0) {
if (do_print(path) != EXIT_SUCCESS) {
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
/**
* @brief calls do_file on each directory entry recursively
*
* @param path the path to be processed
* @param params the parsed parameters
* @param attr the entry attributes from lstat
*
* @returns EXIT_SUCCESS, EXIT_FAILURE
*/
int do_dir(char *path, params_t *params, struct stat attr) {
DIR *dir;
struct dirent *entry;
size_t length;
char *slash = "";
char *full_path;
dir = opendir(path);
if (!dir) {
fprintf(stderr, "%s: opendir(%s): %s\n", program_name, path, strerror(errno));
return EXIT_FAILURE;
}
while ((entry = readdir(dir))) {
/* skip '.' and '..' */
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
length = strlen(path);
/* add a trailing slash if not present */
if (path[length - 1] != '/') {
slash = "/";
}
/* allocate memory for the full entry path */
length += strlen(entry->d_name) + 2;
full_path = malloc(sizeof(char) * length);
if (!full_path) {
fprintf(stderr, "%s: malloc(): %s\n", program_name, strerror(errno));
break; /* a return would require a closedir() */
}
/* concat the path with the entry name */
if (snprintf(full_path, length, "%s%s%s", path, slash, entry->d_name) < 0) {
fprintf(stderr, "%s: snprintf(): %s\n", program_name, strerror(errno));
free(full_path);
break;
}
/* process the entry */
if (lstat(full_path, &attr) == 0) {
/*
* there are no returns for do_file and do_dir on purpose here;
* it is normal for a single entry to fail, then we try the next one
*/
do_file(full_path, params, attr);
/* if a directory, call the function recursively */
if (S_ISDIR(attr.st_mode)) {
do_dir(full_path, params, attr);
}
} else {
fprintf(stderr, "%s: lstat(%s): %s\n", program_name, full_path, strerror(errno));
free(full_path);
continue;
}
free(full_path);
}
if (closedir(dir) != 0) {
fprintf(stderr, "%s: closedir(%s): %s\n", program_name, path, strerror(errno));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/**
* @brief prints out the path
*
* @param path the path to be processed
*
* @returns EXIT_SUCCESS, EXIT_FAILURE
*/
int do_print(char *path) {
if (printf("%s\n", path) < 0) {
fprintf(stderr, "%s: printf(): %s\n", program_name, strerror(errno));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/**
* @brief prints out the path with details
*
* @param path the path to be processed
* @param attr the entry attributes from lstat
*
* @returns EXIT_SUCCESS, EXIT_FAILURE
*/
int do_ls(char *path, struct stat attr) {
unsigned long inode = attr.st_ino;
long long blocks = S_ISLNK(attr.st_mode) ? 0 : attr.st_blocks / 2;
char *perms = do_get_perms(attr);
unsigned long links = attr.st_nlink;
char *user = do_get_user(attr);
char *group = do_get_group(attr);
long long size = attr.st_size;
char *mtime = do_get_mtime(attr);
char *s = do_get_symlink(path, attr);
char *arrow = s ? " -> " : "";
char *symlink = s ? s : "";
if (printf("%6lu %4lld %10s %3lu %-8s %-8s %8lld %12s %s%s%s\n", inode, blocks, perms, links,
user, group, size, mtime, path, arrow, symlink) < 0) {
fprintf(stderr, "%s: printf(): %s\n", program_name, strerror(errno));
free(s);
return EXIT_FAILURE;
}
free(s);
return EXIT_SUCCESS;
}
/**
* @brief checks if the type matches the entry attributes
*
* @param type the type to match against
* @param attr the entry attributes from lstat
*
* @returns EXIT_SUCCESS, EXIT_FAILURE
*/
int do_type(char type, struct stat attr) {
/* comparing two chars */
if (type == do_get_type(attr)) {
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
/**
* @brief checks if the entry doesn't have a user
*
* @param attr the entry attributes from lstat
*
* @returns EXIT_SUCCESS, EXIT_FAILURE
*/
int do_nouser(struct stat attr) {
static unsigned int cache_uid = UINT_MAX;
/* skip getgrgid if we have the record in cache */
if (cache_uid == attr.st_uid) {
return EXIT_FAILURE;
}
/* reset the cache */
cache_uid = UINT_MAX;
if (!getpwuid(attr.st_uid)) {
return EXIT_SUCCESS;
}
/* cache an existing user (more common) */
cache_uid = attr.st_uid;
return EXIT_FAILURE;
}
/**
* @brief checks if the userid matches the entry attribute
*
* @param userid the uid to match against
* @param attr the entry attributes from lstat
*
* @returns EXIT_SUCCESS, EXIT_FAILURE
*/
int do_user(unsigned int userid, struct stat attr) {
if (userid == attr.st_uid) {
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
/**
* @brief checks if the filename matches the pattern
*
* @param path the entry path
* @param pattern the pattern to match against
*
* @returns EXIT_SUCCESS, EXIT_FAILURE
*/
int do_name(char *path, char *pattern) {
char *filename = basename(path);
int flags = 0;
if (fnmatch(pattern, filename, flags) == 0) {
return EXIT_SUCCESS;
}
/* basename manual: do not pass the returned pointer to free() */
return EXIT_FAILURE;
}
/**
* @brief checks if the path matches the pattern
*
* @param path the entry path
* @param pattern the pattern to match against
*
* @returns EXIT_SUCCESS, EXIT_FAILURE
*/
int do_path(char *path, char *pattern) {
int flags = 0;
if (fnmatch(pattern, path, flags) == 0) {
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
/**
* @brief converts the entry attributes to a readable type
*
* @param attr the entry attributes from lstat
*
* @returns the entry type as a char
*/
char do_get_type(struct stat attr) {
/* block special file */
if (S_ISBLK(attr.st_mode)) {
return 'b';
}
/* character special file */
if (S_ISCHR(attr.st_mode)) {
return 'c';
}
/* directory */
if (S_ISDIR(attr.st_mode)) {
return 'd';
}
/* fifo (named pipe) */
if (S_ISFIFO(attr.st_mode)) {
return 'p';
}
/* regular file */
if (S_ISREG(attr.st_mode)) {
return 'f';
}
/* symbolic link */
if (S_ISLNK(attr.st_mode)) {
return 'l';
}
/* socket */
if (S_ISSOCK(attr.st_mode)) {
return 's';
}
/* some other file type */
return '?';
}
/**
* @brief converts the entry attributes to readable permissions
*
* @param attr the entry attributes from lstat
*
* @returns the entry permissions as a string
*/
char *do_get_perms(struct stat attr) {
static char perms[11];
char type = do_get_type(attr);
/*
* cast is used to avoid the IDE warnings
* about int possibly not fitting into char
*/
perms[0] = (char)(type == 'f' ? '-' : type);
perms[1] = (char)(attr.st_mode & S_IRUSR ? 'r' : '-');
perms[2] = (char)(attr.st_mode & S_IWUSR ? 'w' : '-');
perms[3] = (char)(attr.st_mode & S_ISUID ? (attr.st_mode & S_IXUSR ? 's' : 'S')
: (attr.st_mode & S_IXUSR ? 'x' : '-'));
perms[4] = (char)(attr.st_mode & S_IRGRP ? 'r' : '-');
perms[5] = (char)(attr.st_mode & S_IWGRP ? 'w' : '-');
perms[6] = (char)(attr.st_mode & S_ISGID ? (attr.st_mode & S_IXGRP ? 's' : 'S')
: (attr.st_mode & S_IXGRP ? 'x' : '-'));
perms[7] = (char)(attr.st_mode & S_IROTH ? 'r' : '-');
perms[8] = (char)(attr.st_mode & S_IWOTH ? 'w' : '-');
perms[9] = (char)(attr.st_mode & S_ISVTX ? (attr.st_mode & S_IXOTH ? 't' : 'T')
: (attr.st_mode & S_IXOTH ? 'x' : '-'));
perms[10] = '\0';
return perms;
}
/**
* @brief converts the entry attributes to username or, if not found, uid
*
* @param attr the entry attributes from lstat
*
* @returns the username if getpwuid() worked, otherwise uid, as a string
*/
char *do_get_user(struct stat attr) {
struct passwd *pwd;
static unsigned int cache_uid = UINT_MAX;
static char *cache_pw_name = NULL;
/* skip getgrgid if we have the record in cache */
if (cache_uid == attr.st_uid) {
return cache_pw_name;
}
/* reset the cache */
cache_uid = UINT_MAX;
pwd = getpwuid(attr.st_uid);
if (!pwd) {
/*
* the user is not found or getpwuid failed,
* return the uid as a string then;
* an unsigned int needs 10 chars
*/
static char user[11];
if (snprintf(user, 11, "%u", attr.st_uid) < 0) {
fprintf(stderr, "%s: snprintf(): %s\n", program_name, strerror(errno));
return "";
}
return user;
}
cache_uid = pwd->pw_uid;
cache_pw_name = pwd->pw_name;
/* getpwuid manual: do not pass the returned pointer to free() */
return pwd->pw_name;
}
/**
* @brief converts the entry attributes to groupname or, if not found, gid
*
* @param attr the entry attributes from lstat
*
* @returns the groupname if getgrgid() worked, otherwise gid, as a string
*/
char *do_get_group(struct stat attr) {
struct group *grp;
static unsigned int cache_gid = UINT_MAX;
static char *cache_gr_name = NULL;
/* skip getgrgid if we have the record in cache */
if (cache_gid == attr.st_gid) {
return cache_gr_name;
}
/* reset the cache */
cache_gid = UINT_MAX;
grp = getgrgid(attr.st_gid);
if (!grp) {
/*
* the group is not found or getgrgid failed,
* return the gid as a string then;
* an unsigned int needs 10 chars
*/
static char group[11];
if (snprintf(group, 11, "%u", attr.st_gid) < 0) {
fprintf(stderr, "%s: snprintf(): %s\n", program_name, strerror(errno));
return "";
}
return group;
}
cache_gid = grp->gr_gid;
cache_gr_name = grp->gr_name;
/* getgrgid manual: do not pass the returned pointer to free() */
return grp->gr_name;
}
/**
* @brief converts the entry attributes to a readable modification time
*
* @param attr the entry attributes from lstat
*
* @returns the entry modification time as a string
*/
char *do_get_mtime(struct stat attr) {
static char mtime[16]; /* 12 length + 3 special + null */
char *format;
time_t now = time(NULL);
time_t six_months = 31556952 / 2; /* 365.2425 * 60 * 60 * 24 */
struct tm *local_mtime = localtime(&attr.st_mtime);
if (!local_mtime) {
fprintf(stderr, "%s: localtime(): %s\n", program_name, strerror(errno));
return "";
}
if ((now - six_months) < attr.st_mtime) {
format = "%b %e %H:%M"; /* recent */
} else {
format = "%b %e %Y"; /* older than 6 months */
}
if (strftime(mtime, sizeof(mtime), format, local_mtime) == 0) {
fprintf(stderr, "%s: strftime(): %s\n", program_name, strerror(errno));
return "";
}
mtime[15] = '\0';
return mtime;
}
/**
* @brief extracts the entry symlink
*
* @param path the entry path
* @param attr the entry attributes from lstat
*
* @returns the entry symlink as a string
*/
char *do_get_symlink(char *path, struct stat attr) {
if (S_ISLNK(attr.st_mode)) {
/*
* st_size appears to be an unreliable source of the link length
* PATH_MAX is artificial and not used by the GNU C Library
*/
ssize_t length;
size_t buffer = 128;
char *symlink = malloc(sizeof(char) * buffer);
if (!symlink) {
fprintf(stderr, "%s: malloc(): %s\n", program_name, strerror(errno));
return NULL;
}
/*
* if readlink() fills the buffer, double it and run again
* even if it equals, because we need a character for the termination;
* a check for > 0 is mandatory, because we are comparing signed and unsigned
*/
while ((length = readlink(path, symlink, buffer)) > 0 && (size_t)length >= buffer) {
buffer *= 2;
char *new_symlink = realloc(symlink, sizeof(char) * buffer);
if (!new_symlink) {
fprintf(stderr, "%s: realloc(): %s\n", program_name, strerror(errno));
free(symlink); /* realloc doesn't free the old object if it fails */
return NULL;
}
symlink = new_symlink;
}
if (length < 0) {
fprintf(stderr, "%s: readlink(%s): %s\n", program_name, path, strerror(errno));
free(symlink);
return NULL;
}
symlink[length] = '\0';
return symlink;
}
/* the entry is not a symlink */
return NULL;
}