]> git.saurik.com Git - apple/network_cmds.git/blob - makedbm.tproj/makedbm.c
network_cmds-245.tar.gz
[apple/network_cmds.git] / makedbm.tproj / makedbm.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /* $OpenBSD: makedbm.c,v 1.9 1997/08/18 03:11:34 millert Exp $ */
25
26 /*
27 * Copyright (c) 1994-97 Mats O Jansson <moj@stacken.kth.se>
28 * All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by Mats O Jansson
41 * 4. The name of the author may not be used to endorse or promote products
42 * derived from this software without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
45 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
46 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
48 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57 #ifndef LINT
58 static char rcsid[] = "$OpenBSD: makedbm.c,v 1.9 1997/08/18 03:11:34 millert Exp $";
59 #endif
60
61 #include <stdio.h>
62 #include <fcntl.h>
63 #include <ctype.h>
64 #include <sys/stat.h>
65 #include <sys/param.h>
66 #include <unistd.h>
67 #include <string.h>
68 #include <sys/errno.h>
69 #include "ypdb.h"
70 #include "ypdef.h"
71 #include "db.h"
72
73 extern char *__progname; /* from crt0.o */
74
75 /*
76 * Read one line
77 */
78
79 static int read_line(fp, buf, size)
80 FILE *fp;
81 char *buf;
82 int size;
83 {
84 int done;
85
86 done = 0;
87
88 do {
89 while (fgets(buf, size, fp)) {
90 int len = strlen(buf);
91 done += len;
92 if (len > 1 && buf[len-2] == '\\' &&
93 buf[len-1] == '\n') {
94 int ch;
95 buf += len - 2;
96 size -= len - 2;
97 *buf = '\n'; buf[1] = '\0';
98
99 /* Skip leading white space on next line */
100 while ((ch = getc(fp)) != EOF &&
101 isascii(ch) && isspace(ch))
102 ;
103 (void) ungetc(ch, fp);
104 } else {
105 return done;
106 }
107 }
108 } while (size > 0 && !feof(fp));
109
110 return done;
111 }
112
113 void
114 add_record(db, str1, str2, check)
115 DBM *db;
116 char *str1, *str2;
117 int check;
118 {
119 datum key,val;
120 int status;
121
122 key.dptr = str1;
123 key.dsize = strlen(str1);
124
125 if (check) {
126 val = ypdb_fetch(db,key);
127
128 if (val.dptr != NULL)
129 return; /* already there */
130
131 }
132
133 val.dptr = str2;
134 val.dsize = strlen(str2);
135 status = ypdb_store(db, key, val, YPDB_INSERT);
136
137 if (status != 0) {
138 printf("%s: problem storing %s %s\n",__progname,str1,str2);
139 exit(1);
140 }
141 }
142
143 static char *
144 file_date(filename)
145 char *filename;
146 {
147 struct stat finfo;
148 static char datestr[11];
149 int status;
150
151 if (strcmp(filename,"-") == 0) {
152 sprintf(datestr, "%010d", time(0));
153 } else {
154 status = stat(filename, &finfo);
155 if (status < 0) {
156 fprintf(stderr, "%s: can't stat %s\n", __progname, filename);
157 exit(1);
158 }
159 sprintf(datestr, "%010d", finfo.st_mtime);
160 }
161
162 return datestr;
163 }
164
165 void
166 list_database(database,Uflag)
167 char *database;
168 int Uflag;
169 {
170 DBM *db;
171 datum key,val;
172
173 db = ypdb_open(database, O_RDONLY, 0444);
174
175 if (db == NULL) {
176
177 if (Uflag != 0) {
178
179 if (db_hash_list_database(database)) return;
180
181 }
182
183
184 fprintf(stderr, "%s: can't open database %s\n", __progname, database);
185 exit(1);
186 }
187
188 key = ypdb_firstkey(db);
189
190 while (key.dptr != NULL) {
191 val = ypdb_fetch(db,key);
192 printf("%*.*s %*.*s\n",
193 key.dsize, key.dsize, key.dptr,
194 val.dsize, val.dsize, val.dptr);
195 key = ypdb_nextkey(db);
196 }
197
198 ypdb_close(db);
199
200 }
201
202 void
203
204 create_database(infile,database,
205 yp_input_file,yp_output_file,
206 yp_master_name,yp_domain_name,
207 bflag, lflag, sflag)
208 char *infile, *database;
209 char *yp_input_file, *yp_output_file;
210 char *yp_master_name, *yp_domain_name;
211 int bflag, lflag, sflag;
212 {
213 FILE *data_file;
214 char data_line[4096]; /* XXX: DB bsize = 4096 in ypdb.c */
215 char myname[MAXHOSTNAMELEN];
216 int line_no = 0;
217 int len;
218 char *p,*k,*v;
219 char *slash;
220 DBM *new_db;
221 static char mapname[] = "ypdbXXXXXXXXXX";
222 char db_mapname[MAXPATHLEN],db_outfile[MAXPATHLEN],
223 db_tempname[MAXPATHLEN];
224 char empty_str[] = "";
225
226 if (strcmp(infile,"-") == 0) {
227 data_file = stdin;
228 } else {
229 data_file = fopen(infile, "r");
230 if (errno != 0) {
231 (void)fprintf(stderr,"%s: ",__progname);
232 perror(infile);
233 exit(1);
234 }
235 }
236
237 if (strlen(database) + strlen(YPDB_SUFFIX) > MAXPATHLEN) {
238 fprintf(stderr,"%s: %s: file name too long\n",
239 __progname, database);
240 exit(1);
241 }
242 snprintf(db_outfile, sizeof(db_outfile), "%s%s", database, YPDB_SUFFIX);
243
244 slash = strrchr(database, '/');
245 if (slash != NULL)
246 slash[1] = 0; /* truncate to dir */
247 else
248 *database = 0; /* elminate */
249
250 /* note: database is now directory where map goes ! */
251
252 if (strlen(database) + strlen(mapname)
253 + strlen(YPDB_SUFFIX) > MAXPATHLEN) {
254 fprintf(stderr,"%s: %s: directory name too long\n",
255 __progname, database);
256 exit(1);
257 }
258
259 snprintf(db_tempname, sizeof(db_tempname), "%s%s", database,
260 mapname);
261 mktemp(db_tempname);
262 snprintf(db_mapname, sizeof(db_mapname), "%s%s", db_tempname,
263 YPDB_SUFFIX);
264
265 new_db = ypdb_open(db_tempname, O_RDWR|O_CREAT, 0444);
266
267 while (read_line(data_file,data_line,sizeof(data_line))) {
268
269 line_no++;
270 len = strlen(data_line);
271
272 /* Check if we have the whole line */
273
274 if (data_line[len-1] != '\n') {
275 fprintf(stderr, "line %d in \"%s\" is too long",
276 line_no, infile);
277 } else {
278 data_line[len-1] = '\0';
279 }
280
281 p = (char *) &data_line;
282
283 k = p; /* save start of key */
284 while (!isspace(*p)) { /* find first "space" */
285 if (lflag && isupper(*p)) /* if force lower case */
286 *p = tolower(*p); /* fix it */
287 p++;
288 };
289 while (isspace(*p)) { /* replace space with <NUL> */
290 *p = '\0';
291 p++;
292 };
293
294 v = p; /* save start of value */
295 while(*p != '\0') { p++; }; /* find end of string */
296
297 add_record(new_db, k, v, TRUE); /* save record */
298
299 }
300
301 if (strcmp(infile,"-") != 0) {
302 (void) fclose(data_file);
303 }
304
305 add_record(new_db, YP_LAST_KEY, file_date(infile), FALSE);
306
307 if (yp_input_file) {
308 add_record(new_db, YP_INPUT_KEY, yp_input_file, FALSE);
309 }
310
311 if (yp_output_file) {
312 add_record(new_db, YP_OUTPUT_KEY, yp_output_file, FALSE);
313 }
314
315 if (yp_master_name) {
316 add_record(new_db, YP_MASTER_KEY, yp_master_name, FALSE);
317 } else {
318 gethostname(myname, sizeof(myname) - 1);
319 add_record(new_db, YP_MASTER_KEY, myname, FALSE);
320 }
321
322 if (yp_domain_name) {
323 add_record(new_db, YP_DOMAIN_KEY, yp_domain_name, FALSE);
324 }
325
326 if (bflag) {
327 add_record(new_db, YP_INTERDOMAIN_KEY, empty_str, FALSE);
328 }
329
330 if (sflag) {
331 add_record(new_db, YP_SECURE_KEY, empty_str, FALSE);
332 }
333
334 ypdb_close(new_db);
335 if (rename(db_mapname,db_outfile) < 0) {
336 perror("rename");
337 fprintf(stderr,"rename %s -> %s failed!\n", db_mapname,
338 db_outfile);
339 exit(1);
340 }
341
342 }
343
344 int
345 main (argc,argv)
346 int argc;
347 char *argv[];
348 {
349 int aflag, uflag, bflag, lflag, sflag, Uflag;
350 char *yp_input_file, *yp_output_file;
351 char *yp_master_name,*yp_domain_name;
352 char *infile,*outfile;
353 int usage = 0;
354 int ch;
355
356 extern int optind;
357
358 yp_input_file = yp_output_file = NULL;
359 yp_master_name = yp_domain_name = NULL;
360 aflag = uflag = bflag = lflag = sflag = Uflag = 0;
361 infile = outfile = NULL;
362
363 while ((ch = getopt(argc, argv, "Ublsui:o:m:d:")) != -1)
364 switch (ch) {
365 case 'U':
366 uflag++;
367 Uflag++;
368 break;
369 case 'b':
370 bflag++;
371 aflag++;
372 break;
373 case 'l':
374 lflag++;
375 aflag++;
376 break;
377 case 's':
378 sflag++;
379 aflag++;
380 break;
381 case 'i':
382 yp_input_file = argv[optind];
383 aflag++;
384 break;
385 case 'o':
386 yp_output_file = argv[optind];
387 aflag++;
388 break;
389 case 'm':
390 yp_master_name = argv[optind];
391 aflag++;
392 break;
393 case 'd':
394 yp_domain_name = argv[optind];
395 aflag++;
396 break;
397 case 'u':
398 uflag++;
399 break;
400 default:
401 usage++;
402 break;
403 }
404
405 if ((uflag != 0) && (aflag != 0)) {
406 usage++;
407 } else {
408
409 if (uflag != 0) {
410 if (argc == (optind + 1)) {
411 infile = argv[optind];
412 } else {
413 usage++;
414 }
415 } else {
416 if (argc == (optind + 2)) {
417 infile = argv[optind];
418 outfile = argv[optind+1];
419 } else {
420 usage++;
421 }
422 }
423 }
424
425 if (usage) {
426 fprintf(stderr,"%s%s%s",
427 "usage:\tmakedbm [-u|-U] file\n\tmakedbm [-bls]",
428 " [-i YP_INPUT_FILE] [-o YP_OUTPUT_FILE]\n\t\t",
429 "[-d YP_DOMAIN_NAME] [-m YP_MASTER_NAME] infile outfile\n");
430 exit(1);
431 }
432
433 if (uflag != 0) {
434 list_database(infile,Uflag);
435 } else {
436 create_database(infile,outfile,
437 yp_input_file,yp_output_file,
438 yp_master_name,yp_domain_name,
439 bflag, lflag, sflag);
440 }
441
442 return(0);
443
444 }