aboutsummaryrefslogtreecommitdiff
path: root/tests/emit_keys.c
blob: 0b98689ebea7d57833457254674c5757defa3493 (plain)
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
#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;
}