summaryrefslogtreecommitdiff
path: root/xc/programs/rgb/rgb.c
blob: a8d2500cbcbcb1537e4d3a282a60ba843ecd704d (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* reads from standard input lines of the form:
	red green blue name
   where red/green/blue are decimal values, and inserts them in a database.
 */

#include <dbm.h>
#undef NULL
#include <stdio.h>
#include <sys/file.h>
#include "../X/rgb.h"

main(argc, argv)
    int argc;
    char **argv;
{
    char *dbname;
    char line[512];
    int red, green, blue;
    RGB rgb;
    datum key, content;
    char name[512];
    int items;
    int lineno;

    if (argc == 2)
	dbname = argv[1];
    else
	dbname = RGB_DB;
    strcpy (name, dbname);
    strcat (name, ".dir");
    close (open (name, O_WRONLY|O_CREAT, 0666));
    strcpy (name, dbname);
    strcat (name, ".pag");
    close (open (name, O_WRONLY|O_CREAT, 0666));
    if (dbminit (dbname))
	exit (1);
    key.dptr = name;
    content.dptr = (char *) &rgb;
    content.dsize = sizeof (rgb);
    lineno = 0;
    while (fgets (line, sizeof (line), stdin)) {
	lineno++;
	items = sscanf (line, "%d %d %d %[^\n]\n", &red, &green, &blue, name);
	if (items != 4) {
	    fprintf (stderr, "syntax error on line %d\n", lineno);
	    fflush (stderr);
	    continue;
	}
	if (red < 0 || red > 0xff ||
	    green < 0 || green > 0xff ||
	    blue < 0 || blue > 0xff) {
	    fprintf (stderr, "value for %s out of range\n", name);
	    fflush (stderr);
	    continue;
	}
	key.dsize = strlen (name);
	rgb.red = red << 8;
	rgb.green = green << 8;
	rgb.blue = blue << 8;
	if (store (key, content)) {
	    fprintf (stderr, "store of %s failed\n", name);
	    fflush (stderr);
	}
    }
}