rgb.c revision afb14e9b
1/*
2
3Copyright 1985, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27*/
28
29
30/* reads from standard input lines of the form:
31	red green blue name
32   where red/green/blue are decimal values, and inserts them in a database.
33 */
34
35#ifdef HAVE_CONFIG_H
36# include "config.h"
37#endif
38
39#include DBM_HEADER
40#ifndef NDBM
41#define dbm_open(name,flags,mode) (!dbminit(name))
42#define dbm_store(db,key,content,flags) (store(key,content))
43#define dbm_close(db) dbmclose()
44#endif
45
46#undef NULL
47#include <stdio.h>
48#include <stdlib.h>
49#include <X11/Xos.h>
50#include "rgb.h"
51#include <ctype.h>
52
53#include <errno.h>
54
55static char *ProgramName;
56
57int
58main(int argc, char **argv)
59{
60    const char *dbname;
61    char line[512];
62    int red, green, blue;
63    RGB rgb;
64    datum key, content;
65    char name[512];
66    int items;
67    int lineno;
68    int i, n;
69    int fd;
70#ifdef NDBM
71    DBM *rgb_dbm;
72#else
73    int rgb_dbm;
74#endif
75
76    ProgramName = argv[0];
77
78    if (argc == 2)
79	dbname = argv[1];
80    else
81	dbname = RGB_DB;
82
83    if (strlen(dbname) > (sizeof(name) - 5)) {
84        fprintf (stderr,
85		 "%s:  dbm file name too long: \"%s\" (%lu max allowed)\n",
86		 ProgramName, dbname, sizeof(name) - 5);
87	exit (1);
88    }
89
90    snprintf (name, sizeof(name), "%s.dir", dbname);
91    fd = open (name, O_WRONLY|O_CREAT, 0666);
92    if (fd < 0) {
93	fprintf (stderr,
94		 "%s:  unable to create dbm file \"%s\" (error %d, %s)\n",
95		 ProgramName, name, errno, strerror(errno));
96	exit (1);
97    }
98    (void) close (fd);
99
100    snprintf (name, sizeof(name), "%s.pag", dbname);
101    fd = open (name, O_WRONLY|O_CREAT, 0666);
102    if (fd < 0) {
103	fprintf (stderr,
104		 "%s:  unable to create dbm file \"%s\" (error %d, %s)\n",
105		 ProgramName, name, errno, strerror(errno));
106	exit (1);
107    }
108    (void) close (fd);
109
110    rgb_dbm = dbm_open (dbname, O_RDWR|O_CREAT, 0666);
111    if (!rgb_dbm) {
112	fprintf (stderr,
113		 "%s:  unable to open dbm database \"%s\" (error %d, %s)\n",
114		 ProgramName, dbname, errno, strerror(errno));
115	exit (1);
116    }
117
118    key.dptr = name;
119    content.dptr = (char *) &rgb;
120    content.dsize = sizeof (rgb);
121    lineno = 0;
122    while (fgets (line, sizeof (line), stdin)) {
123	lineno++;
124	if (line[0] == '!')
125	    continue;
126	items = sscanf (line, "%d %d %d %[^\n]\n", &red, &green, &blue, name);
127	if (items != 4) {
128	    fprintf (stderr, "syntax error on line %d\n", lineno);
129	    fflush (stderr);
130	    continue;
131	}
132	if (red < 0 || red > 0xff ||
133	    green < 0 || green > 0xff ||
134	    blue < 0 || blue > 0xff) {
135	    fprintf (stderr, "value for %s out of range\n", name);
136	    fflush (stderr);
137	    continue;
138	}
139	n = strlen (name);
140	for (i = 0; i < n; i++) {
141	    if (isupper (name[i]))
142		name[i] = tolower (name[i]);
143	}
144	key.dsize = n;
145	rgb.red = (red * 65535) / 255;
146	rgb.green = (green * 65535) / 255;
147	rgb.blue = (blue * 65535) / 255;
148	if (dbm_store (rgb_dbm, key, content, DBM_REPLACE)) {
149	    fprintf (stderr, "%s:  store of entry \"%s\" failed\n",
150		     ProgramName, name);
151	    fflush (stderr);
152	}
153    }
154
155    dbm_close(rgb_dbm);
156
157    exit(0);
158}
159