sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

enmasse.c (919B)


      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 *, int))
     13 {
     14 	struct stat st;
     15 	char buf[PATH_MAX], *dir;
     16 	int i, len;
     17 	size_t dlen;
     18 
     19 	if (argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) {
     20 		fnck(argv[0], argv[1], fn, 0);
     21 		return;
     22 	} else {
     23 		dir = (argc == 1) ? "." : argv[--argc];
     24 	}
     25 
     26 	for (i = 0; i < argc; i++) {
     27 		dlen = strlen(dir);
     28 		if (dlen > 0 && dir[dlen - 1] == '/')
     29 			len = snprintf(buf, sizeof(buf), "%s%s", dir, basename(argv[i]));
     30 		else
     31 			len = snprintf(buf, sizeof(buf), "%s/%s", dir, basename(argv[i]));
     32 		if (len < 0 || len >= sizeof(buf)) {
     33 			eprintf("%s/%s: filename too long\n", dir,
     34 			        basename(argv[i]));
     35 		}
     36 		fnck(argv[i], buf, fn, 0);
     37 	}
     38 }