forked from troydhanson/uthash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemit_keys.c
48 lines (39 loc) · 1010 Bytes
/
emit_keys.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
#include <stdlib.h> /* malloc */
#include <errno.h> /* perror */
#include <stdio.h> /* printf */
#include <unistd.h> /* write */
/* this define must precede uthash.h */
#define HASH_EMIT_KEYS 1
#include "uthash.h"
#define BUFLEN 30
typedef struct name_rec {
char boy_name[BUFLEN];
UT_hash_handle hh;
} name_rec;
int main(int argc,char *argv[])
{
name_rec *name, *names=NULL;
char linebuf[BUFLEN];
FILE *file;
int i=0;
if (argc != 2) {
fprintf(stderr,"usage: %s file\n", argv[0]);
exit(-1);
}
if ( (file = fopen( argv[1], "r" )) == NULL ) {
perror("can't open: ");
exit(-1);
}
while (fgets(linebuf,BUFLEN,file) != NULL) {
name = (name_rec*)malloc(sizeof(name_rec));
if (name == NULL) {
exit(-1);
}
strcpy(name->boy_name, linebuf);
HASH_ADD_STR(names,boy_name,name);
i++;
}
fprintf(stderr,"%d keys emitted.\n", i);
fclose(file);
return 0;
}