sbase/util/enmasse.c

51 lines
1.1 KiB
C
Raw Normal View History

2011-05-24 12:00:30 +00:00
/* See LICENSE file for copyright and license details. */
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "../util.h"
2011-06-22 22:04:56 +00:00
static void fnck(const char *, const char *, int (*)(const char *, const char *));
2011-05-24 12:00:30 +00:00
void
enmasse(int argc, char **argv, int (*fn)(const char *, const char *))
{
char *buf, *dir;
int i;
long size;
struct stat st;
if(argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) {
2011-06-22 22:04:56 +00:00
fnck(argv[0], argv[1], fn);
2011-05-24 12:00:30 +00:00
return;
}
else if(argc == 1)
dir = ".";
else
dir = argv[--argc];
2011-06-21 04:05:37 +00:00
if((size = pathconf(dir, _PC_PATH_MAX)) == -1)
2011-05-24 12:00:30 +00:00
size = BUFSIZ;
if(!(buf = malloc(size)))
eprintf("malloc:");
for(i = 0; i < argc; i++) {
snprintf(buf, size, "%s/%s", dir, basename(argv[i]));
2011-06-22 22:04:56 +00:00
fnck(argv[i], buf, fn);
2011-05-24 12:00:30 +00:00
}
free(buf);
}
2011-06-22 22:04:56 +00:00
void
fnck(const char *a, const char *b, int (*fn)(const char *, const char *))
{
struct stat sta, stb;
if(stat(a, &sta) == 0 && stat(b, &stb) == 0
&& sta.st_dev == stb.st_dev && sta.st_ino == stb.st_ino)
eprintf("%s: same file as: %s\n", b, a);
if(fn(a, b) == -1)
eprintf("%s:", b);
}