-
Notifications
You must be signed in to change notification settings - Fork 1
/
mhdb-add.c
55 lines (45 loc) · 871 Bytes
/
mhdb-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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <sys/param.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mhdb.h"
static mhdb_t d;
static char *p;
static void mhdb_add(const char *mhpath);
int
main(int argc, char *argv[]) {
if (strcmp(argv[1], "-") != 0) {
mhdb_add(argv[1]);
} else {
char line[MAXPATHLEN+1];
while (fgets(line, sizeof line, stdin)) {
char *t = strrchr(line, '\n');
if (t != NULL)
*t = '\0';
mhdb_add(line);
}
}
if (d != NULL)
mhdb_close(d);
if (p != NULL)
free(p);
exit(0);
}
static void
mhdb_add(const char *mhpath) {
char *t = mhdb_path(mhpath);
int uid;
if (d != NULL && p != NULL && strcmp(t, p) != 0) {
mhdb_close(d);
d = NULL;
free(p);
p = NULL;
}
if (d == NULL) {
assert(p == NULL);
d = mhdb_open(t, mhdb_acc_exclusive);
p = t;
}
uid = mhdb_assign_uid(d, mhpath, 0);
}