/
add.c
40 lines (38 loc) · 907 Bytes
/
add.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <errno.h>
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "commands.h"
#include "blob.h"
#include "path.h"
int ftw_add(const char *fpath, const struct stat *sb, int typeflag) {
if (typeflag == FTW_F) {
write_blob((char *) fpath);
}
return 0;
}
int cmd_add(int argc, const char *argv[]) {
die_if_not_troll_repo();
if (argc == 1) {
printf("Nothing specified, nothing added.\n");
return 0;
}
int i;
for (i = 1; i < argc; i++) {
struct stat buf;
if (stat(argv[i], &buf) != 0) {
printf("Ain't gon' work: %s\n", strerror(errno));
return 128;
}
if (S_ISDIR(buf.st_mode)) {
ftw(argv[i], ftw_add, 256);
}
else {
write_blob((char *) argv[i]);
}
}
return 0;
}