]>
git.saurik.com Git - apple/network_cmds.git/blob - makedbm.tproj/makedbm.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
24 /* $OpenBSD: makedbm.c,v 1.9 1997/08/18 03:11:34 millert Exp $ */
27 * Copyright (c) 1994-97 Mats O Jansson <moj@stacken.kth.se>
28 * All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
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.
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
58 static char rcsid
[] = "$OpenBSD: makedbm.c,v 1.9 1997/08/18 03:11:34 millert Exp $";
65 #include <sys/param.h>
68 #include <sys/errno.h>
73 extern char *__progname
; /* from crt0.o */
79 static int read_line(fp
, buf
, size
)
89 while (fgets(buf
, size
, fp
)) {
90 int len
= strlen(buf
);
92 if (len
> 1 && buf
[len
-2] == '\\' &&
97 *buf
= '\n'; buf
[1] = '\0';
99 /* Skip leading white space on next line */
100 while ((ch
= getc(fp
)) != EOF
&&
101 isascii(ch
) && isspace(ch
))
103 (void) ungetc(ch
, fp
);
108 } while (size
> 0 && !feof(fp
));
114 add_record(db
, str1
, str2
, check
)
123 key
.dsize
= strlen(str1
);
126 val
= ypdb_fetch(db
,key
);
128 if (val
.dptr
!= NULL
)
129 return; /* already there */
134 val
.dsize
= strlen(str2
);
135 status
= ypdb_store(db
, key
, val
, YPDB_INSERT
);
138 printf("%s: problem storing %s %s\n",__progname
,str1
,str2
);
148 static char datestr
[11];
151 if (strcmp(filename
,"-") == 0) {
152 sprintf(datestr
, "%010d", time(0));
154 status
= stat(filename
, &finfo
);
156 fprintf(stderr
, "%s: can't stat %s\n", __progname
, filename
);
159 sprintf(datestr
, "%010d", finfo
.st_mtime
);
166 list_database(database
,Uflag
)
173 db
= ypdb_open(database
, O_RDONLY
, 0444);
179 if (db_hash_list_database(database
)) return;
184 fprintf(stderr
, "%s: can't open database %s\n", __progname
, database
);
188 key
= ypdb_firstkey(db
);
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
);
204 create_database(infile
,database
,
205 yp_input_file
,yp_output_file
,
206 yp_master_name
,yp_domain_name
,
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
;
214 char data_line
[4096]; /* XXX: DB bsize = 4096 in ypdb.c */
215 char myname
[MAXHOSTNAMELEN
];
221 static char mapname
[] = "ypdbXXXXXXXXXX";
222 char db_mapname
[MAXPATHLEN
],db_outfile
[MAXPATHLEN
],
223 db_tempname
[MAXPATHLEN
];
224 char empty_str
[] = "";
226 if (strcmp(infile
,"-") == 0) {
229 data_file
= fopen(infile
, "r");
231 (void)fprintf(stderr
,"%s: ",__progname
);
237 if (strlen(database
) + strlen(YPDB_SUFFIX
) > MAXPATHLEN
) {
238 fprintf(stderr
,"%s: %s: file name too long\n",
239 __progname
, database
);
242 snprintf(db_outfile
, sizeof(db_outfile
), "%s%s", database
, YPDB_SUFFIX
);
244 slash
= strrchr(database
, '/');
246 slash
[1] = 0; /* truncate to dir */
248 *database
= 0; /* elminate */
250 /* note: database is now directory where map goes ! */
252 if (strlen(database
) + strlen(mapname
)
253 + strlen(YPDB_SUFFIX
) > MAXPATHLEN
) {
254 fprintf(stderr
,"%s: %s: directory name too long\n",
255 __progname
, database
);
259 snprintf(db_tempname
, sizeof(db_tempname
), "%s%s", database
,
262 snprintf(db_mapname
, sizeof(db_mapname
), "%s%s", db_tempname
,
265 new_db
= ypdb_open(db_tempname
, O_RDWR
|O_CREAT
, 0444);
267 while (read_line(data_file
,data_line
,sizeof(data_line
))) {
270 len
= strlen(data_line
);
272 /* Check if we have the whole line */
274 if (data_line
[len
-1] != '\n') {
275 fprintf(stderr
, "line %d in \"%s\" is too long",
278 data_line
[len
-1] = '\0';
281 p
= (char *) &data_line
;
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 */
289 while (isspace(*p
)) { /* replace space with <NUL> */
294 v
= p
; /* save start of value */
295 while(*p
!= '\0') { p
++; }; /* find end of string */
297 add_record(new_db
, k
, v
, TRUE
); /* save record */
301 if (strcmp(infile
,"-") != 0) {
302 (void) fclose(data_file
);
305 add_record(new_db
, YP_LAST_KEY
, file_date(infile
), FALSE
);
308 add_record(new_db
, YP_INPUT_KEY
, yp_input_file
, FALSE
);
311 if (yp_output_file
) {
312 add_record(new_db
, YP_OUTPUT_KEY
, yp_output_file
, FALSE
);
315 if (yp_master_name
) {
316 add_record(new_db
, YP_MASTER_KEY
, yp_master_name
, FALSE
);
318 gethostname(myname
, sizeof(myname
) - 1);
319 add_record(new_db
, YP_MASTER_KEY
, myname
, FALSE
);
322 if (yp_domain_name
) {
323 add_record(new_db
, YP_DOMAIN_KEY
, yp_domain_name
, FALSE
);
327 add_record(new_db
, YP_INTERDOMAIN_KEY
, empty_str
, FALSE
);
331 add_record(new_db
, YP_SECURE_KEY
, empty_str
, FALSE
);
335 if (rename(db_mapname
,db_outfile
) < 0) {
337 fprintf(stderr
,"rename %s -> %s failed!\n", db_mapname
,
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
;
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
;
363 while ((ch
= getopt(argc
, argv
, "Ublsui:o:m:d:")) != -1)
382 yp_input_file
= argv
[optind
];
386 yp_output_file
= argv
[optind
];
390 yp_master_name
= argv
[optind
];
394 yp_domain_name
= argv
[optind
];
405 if ((uflag
!= 0) && (aflag
!= 0)) {
410 if (argc
== (optind
+ 1)) {
411 infile
= argv
[optind
];
416 if (argc
== (optind
+ 2)) {
417 infile
= argv
[optind
];
418 outfile
= argv
[optind
+1];
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");
434 list_database(infile
,Uflag
);
436 create_database(infile
,outfile
,
437 yp_input_file
,yp_output_file
,
438 yp_master_name
,yp_domain_name
,
439 bflag
, lflag
, sflag
);