2011-05-24 08:00:30 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2014-11-13 13:54:28 -05:00
|
|
|
#include <unistd.h>
|
2013-03-05 15:46:48 -05:00
|
|
|
|
2011-05-24 08:00:30 -04:00
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
void
|
2015-03-02 15:43:56 -05:00
|
|
|
enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, int))
|
2011-05-24 08:00:30 -04:00
|
|
|
{
|
2015-03-06 18:48:37 -05:00
|
|
|
struct stat st;
|
2015-03-18 10:20:35 -04:00
|
|
|
char buf[PATH_MAX], *dir;
|
2014-07-10 16:30:47 -04:00
|
|
|
int i, len;
|
2015-03-18 10:20:35 -04:00
|
|
|
size_t dlen;
|
2011-05-24 08:00:30 -04:00
|
|
|
|
2014-11-13 13:54:28 -05:00
|
|
|
if (argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) {
|
2015-03-02 15:43:56 -05:00
|
|
|
fnck(argv[0], argv[1], fn, 0);
|
2011-05-24 08:00:30 -04:00
|
|
|
return;
|
2013-03-05 15:46:48 -05:00
|
|
|
} else {
|
2011-06-22 21:08:41 -04:00
|
|
|
dir = (argc == 1) ? "." : argv[--argc];
|
2013-03-05 15:46:48 -05:00
|
|
|
}
|
2011-05-24 08:00:30 -04:00
|
|
|
|
2014-11-13 13:54:28 -05:00
|
|
|
for (i = 0; i < argc; i++) {
|
2014-07-10 16:30:47 -04:00
|
|
|
dlen = strlen(dir);
|
2014-11-13 13:54:28 -05:00
|
|
|
if (dlen > 0 && dir[dlen - 1] == '/')
|
2015-03-18 10:20:35 -04:00
|
|
|
len = snprintf(buf, sizeof(buf), "%s%s", dir, basename(argv[i]));
|
2014-07-10 16:30:47 -04:00
|
|
|
else
|
2015-03-18 10:20:35 -04:00
|
|
|
len = snprintf(buf, sizeof(buf), "%s/%s", dir, basename(argv[i]));
|
|
|
|
if (len < 0 || len >= sizeof(buf)) {
|
2013-03-05 15:46:48 -05:00
|
|
|
eprintf("%s/%s: filename too long\n", dir,
|
2014-07-10 16:30:47 -04:00
|
|
|
basename(argv[i]));
|
2013-03-05 15:46:48 -05:00
|
|
|
}
|
2015-03-02 15:43:56 -05:00
|
|
|
fnck(argv[i], buf, fn, 0);
|
2011-05-24 08:00:30 -04:00
|
|
|
}
|
|
|
|
}
|