enmasse.c (926B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <libgen.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <sys/stat.h> 7 #include <unistd.h> 8 9 #include "../util.h" 10 11 void 12 enmasse(int argc, char *argv[], int (*fn)(const char *, const char *)) 13 { 14 char *buf, *dir; 15 int i, len; 16 long size; 17 struct stat st; 18 size_t dlen; 19 20 if (argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) { 21 fnck(argv[0], argv[1], fn); 22 return; 23 } else { 24 dir = (argc == 1) ? "." : argv[--argc]; 25 } 26 27 apathmax(&buf, &size); 28 for (i = 0; i < argc; i++) { 29 dlen = strlen(dir); 30 if (dlen > 0 && dir[dlen - 1] == '/') 31 len = snprintf(buf, size, "%s%s", dir, basename(argv[i])); 32 else 33 len = snprintf(buf, size, "%s/%s", dir, basename(argv[i])); 34 if (len < 0 || len >= size) { 35 eprintf("%s/%s: filename too long\n", dir, 36 basename(argv[i])); 37 } 38 fnck(argv[i], buf, fn); 39 } 40 free(buf); 41 }