]>
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 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* $OpenBSD: makedbm.c,v 1.9 1997/08/18 03:11:34 millert Exp $ */
26 * Copyright (c) 1994-97 Mats O Jansson <moj@stacken.kth.se>
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by Mats O Jansson
40 * 4. The name of the author may not be used to endorse or promote products
41 * derived from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
47 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 static char rcsid
[] = "$OpenBSD: makedbm.c,v 1.9 1997/08/18 03:11:34 millert Exp $";
64 #include <sys/param.h>
67 #include <sys/errno.h>
72 extern char *__progname
; /* from crt0.o */
78 static int read_line(fp
, buf
, size
)
88 while (fgets(buf
, size
, fp
)) {
89 int len
= strlen(buf
);
91 if (len
> 1 && buf
[len
-2] == '\\' &&
96 *buf
= '\n'; buf
[1] = '\0';
98 /* Skip leading white space on next line */
99 while ((ch
= getc(fp
)) != EOF
&&
100 isascii(ch
) && isspace(ch
))
102 (void) ungetc(ch
, fp
);
107 } while (size
> 0 && !feof(fp
));
113 add_record(db
, str1
, str2
, check
)
122 key
.dsize
= strlen(str1
);
125 val
= ypdb_fetch(db
,key
);
127 if (val
.dptr
!= NULL
)
128 return; /* already there */
133 val
.dsize
= strlen(str2
);
134 status
= ypdb_store(db
, key
, val
, YPDB_INSERT
);
137 printf("%s: problem storing %s %s\n",__progname
,str1
,str2
);
147 static char datestr
[11];
150 if (strcmp(filename
,"-") == 0) {
151 sprintf(datestr
, "%010d", time(0));
153 status
= stat(filename
, &finfo
);
155 fprintf(stderr
, "%s: can't stat %s\n", __progname
, filename
);
158 sprintf(datestr
, "%010d", finfo
.st_mtime
);
165 list_database(database
,Uflag
)
172 db
= ypdb_open(database
, O_RDONLY
, 0444);
178 if (db_hash_list_database(database
)) return;
183 fprintf(stderr
, "%s: can't open database %s\n", __progname
, database
);
187 key
= ypdb_firstkey(db
);
189 while (key
.dptr
!= NULL
) {
190 val
= ypdb_fetch(db
,key
);
191 printf("%*.*s %*.*s\n",
192 key
.dsize
, key
.dsize
, key
.dptr
,
193 val
.dsize
, val
.dsize
, val
.dptr
);
194 key
= ypdb_nextkey(db
);
203 create_database(infile
,database
,
204 yp_input_file
,yp_output_file
,
205 yp_master_name
,yp_domain_name
,
207 char *infile
, *database
;
208 char *yp_input_file
, *yp_output_file
;
209 char *yp_master_name
, *yp_domain_name
;
210 int bflag
, lflag
, sflag
;
213 char data_line
[4096]; /* XXX: DB bsize = 4096 in ypdb.c */
214 char myname
[MAXHOSTNAMELEN
];
220 static char mapname
[] = "ypdbXXXXXXXXXX";
221 char db_mapname
[MAXPATHLEN
],db_outfile
[MAXPATHLEN
],
222 db_tempname
[MAXPATHLEN
];
223 char empty_str
[] = "";
225 if (strcmp(infile
,"-") == 0) {
228 data_file
= fopen(infile
, "r");
230 (void)fprintf(stderr
,"%s: ",__progname
);
236 if (strlen(database
) + strlen(YPDB_SUFFIX
) > MAXPATHLEN
) {
237 fprintf(stderr
,"%s: %s: file name too long\n",
238 __progname
, database
);
241 snprintf(db_outfile
, sizeof(db_outfile
), "%s%s", database
, YPDB_SUFFIX
);
243 slash
= strrchr(database
, '/');
245 slash
[1] = 0; /* truncate to dir */
247 *database
= 0; /* elminate */
249 /* note: database is now directory where map goes ! */
251 if (strlen(database
) + strlen(mapname
)
252 + strlen(YPDB_SUFFIX
) > MAXPATHLEN
) {
253 fprintf(stderr
,"%s: %s: directory name too long\n",
254 __progname
, database
);
258 snprintf(db_tempname
, sizeof(db_tempname
), "%s%s", database
,
261 snprintf(db_mapname
, sizeof(db_mapname
), "%s%s", db_tempname
,
264 new_db
= ypdb_open(db_tempname
, O_RDWR
|O_CREAT
, 0444);
266 while (read_line(data_file
,data_line
,sizeof(data_line
))) {
269 len
= strlen(data_line
);
271 /* Check if we have the whole line */
273 if (data_line
[len
-1] != '\n') {
274 fprintf(stderr
, "line %d in \"%s\" is too long",
277 data_line
[len
-1] = '\0';
280 p
= (char *) &data_line
;
282 k
= p
; /* save start of key */
283 while (!isspace(*p
)) { /* find first "space" */
284 if (lflag
&& isupper(*p
)) /* if force lower case */
285 *p
= tolower(*p
); /* fix it */
288 while (isspace(*p
)) { /* replace space with <NUL> */
293 v
= p
; /* save start of value */
294 while(*p
!= '\0') { p
++; }; /* find end of string */
296 add_record(new_db
, k
, v
, TRUE
); /* save record */
300 if (strcmp(infile
,"-") != 0) {
301 (void) fclose(data_file
);
304 add_record(new_db
, YP_LAST_KEY
, file_date(infile
), FALSE
);
307 add_record(new_db
, YP_INPUT_KEY
, yp_input_file
, FALSE
);
310 if (yp_output_file
) {
311 add_record(new_db
, YP_OUTPUT_KEY
, yp_output_file
, FALSE
);
314 if (yp_master_name
) {
315 add_record(new_db
, YP_MASTER_KEY
, yp_master_name
, FALSE
);
317 gethostname(myname
, sizeof(myname
) - 1);
318 add_record(new_db
, YP_MASTER_KEY
, myname
, FALSE
);
321 if (yp_domain_name
) {
322 add_record(new_db
, YP_DOMAIN_KEY
, yp_domain_name
, FALSE
);
326 add_record(new_db
, YP_INTERDOMAIN_KEY
, empty_str
, FALSE
);
330 add_record(new_db
, YP_SECURE_KEY
, empty_str
, FALSE
);
334 if (rename(db_mapname
,db_outfile
) < 0) {
336 fprintf(stderr
,"rename %s -> %s failed!\n", db_mapname
,
348 int aflag
, uflag
, bflag
, lflag
, sflag
, Uflag
;
349 char *yp_input_file
, *yp_output_file
;
350 char *yp_master_name
,*yp_domain_name
;
351 char *infile
,*outfile
;
357 yp_input_file
= yp_output_file
= NULL
;
358 yp_master_name
= yp_domain_name
= NULL
;
359 aflag
= uflag
= bflag
= lflag
= sflag
= Uflag
= 0;
360 infile
= outfile
= NULL
;
362 while ((ch
= getopt(argc
, argv
, "Ublsui:o:m:d:")) != -1)
381 yp_input_file
= argv
[optind
];
385 yp_output_file
= argv
[optind
];
389 yp_master_name
= argv
[optind
];
393 yp_domain_name
= argv
[optind
];
404 if ((uflag
!= 0) && (aflag
!= 0)) {
409 if (argc
== (optind
+ 1)) {
410 infile
= argv
[optind
];
415 if (argc
== (optind
+ 2)) {
416 infile
= argv
[optind
];
417 outfile
= argv
[optind
+1];
425 fprintf(stderr
,"%s%s%s",
426 "usage:\tmakedbm [-u|-U] file\n\tmakedbm [-bls]",
427 " [-i YP_INPUT_FILE] [-o YP_OUTPUT_FILE]\n\t\t",
428 "[-d YP_DOMAIN_NAME] [-m YP_MASTER_NAME] infile outfile\n");
433 list_database(infile
,Uflag
);
435 create_database(infile
,outfile
,
436 yp_input_file
,yp_output_file
,
437 yp_master_name
,yp_domain_name
,
438 bflag
, lflag
, sflag
);