2019-05-12 11:20:31 -04:00
|
|
|
#include <stdio.h>
|
2019-12-11 20:57:23 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2019-05-12 11:20:31 -04:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <string.h>
|
2019-06-02 16:22:07 -04:00
|
|
|
#include <fnmatch.h>
|
2019-07-11 21:21:54 -04:00
|
|
|
#include <libgen.h>
|
2019-06-02 16:35:41 -04:00
|
|
|
#include <sys/stat.h>
|
2019-05-12 11:20:31 -04:00
|
|
|
|
2019-12-31 13:57:52 -05:00
|
|
|
#include "cfs.h"
|
2019-05-12 11:20:31 -04:00
|
|
|
|
2019-12-11 20:57:23 -05:00
|
|
|
void usage()
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: cfspack [-p pattern] [/path/to/dir...]\n");
|
|
|
|
}
|
|
|
|
|
2019-05-12 11:20:31 -04:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2019-12-11 20:57:23 -05:00
|
|
|
int patterncount = 0;
|
|
|
|
char **patterns = malloc(sizeof(char**));
|
|
|
|
patterns[0] = NULL;
|
|
|
|
while (1) {
|
|
|
|
int c = getopt(argc, argv, "p:");
|
|
|
|
if (c < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (c) {
|
|
|
|
case 'p':
|
|
|
|
patterns[patterncount] = optarg;
|
|
|
|
patterncount++;
|
|
|
|
patterns = realloc(patterns, sizeof(char**)*(patterncount+1));
|
|
|
|
patterns[patterncount] = NULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
2019-05-12 11:20:31 -04:00
|
|
|
}
|
2019-12-11 20:57:23 -05:00
|
|
|
int res = 0;
|
|
|
|
for (int i=optind; i<argc; i++) {
|
|
|
|
if (is_regular_file(argv[i])) {
|
|
|
|
// special case: just one file
|
|
|
|
res = spitblock(argv[i], basename(argv[i]));
|
|
|
|
} else {
|
|
|
|
res = spitdir(argv[i], "", patterns);
|
|
|
|
}
|
2019-06-02 16:22:07 -04:00
|
|
|
}
|
2019-12-11 20:57:23 -05:00
|
|
|
if (res == 0) {
|
|
|
|
spitempty();
|
2019-06-02 16:35:41 -04:00
|
|
|
}
|
2019-12-11 20:57:23 -05:00
|
|
|
free(patterns);
|
|
|
|
return res;
|
2019-05-12 11:20:31 -04:00
|
|
|
}
|
|
|
|
|