1 /* $NetBSD: makedbm.c,v 1.23 2009/04/19 06:06:40 lukem Exp $ */
4 * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: makedbm.c,v 1.23 2009/04/19 06:06:40 lukem Exp $");
34 #include <sys/param.h>
53 int main(int, char *[]);
55 int add_record(DBM
*, const char *, const char *, int);
56 char *file_date(char *);
57 void list_database(char *);
58 void create_database(char *, char *, char *, char *, char *, char *,
62 main(int argc
, char *argv
[])
64 int aflag
, uflag
, bflag
, lflag
, sflag
;
65 char *yp_input_file
, *yp_output_file
;
66 char *yp_master_name
, *yp_domain_name
;
67 char *infile
, *outfile
;
70 yp_input_file
= yp_output_file
= NULL
;
71 yp_master_name
= yp_domain_name
= NULL
;
72 aflag
= uflag
= bflag
= lflag
= sflag
= 0;
73 infile
= outfile
= NULL
;
75 while ((ch
= getopt(argc
, argv
, "blsui:o:m:d:")) != -1) {
90 yp_input_file
= optarg
;
95 yp_output_file
= optarg
;
100 yp_master_name
= optarg
;
105 yp_domain_name
= optarg
;
117 argc
-= optind
; argv
+= optind
;
119 if ((uflag
!= 0) && (aflag
!= 0))
137 list_database(infile
);
139 create_database(infile
, outfile
,
140 yp_input_file
, yp_output_file
, yp_master_name
,
141 yp_domain_name
, bflag
, lflag
, sflag
);
147 add_record(DBM
*db
, const char *str1
, const char *str2
, int check
)
152 key
.dptr
= __UNCONST(str1
);
153 key
.dsize
= strlen(str1
);
156 val
= ypdb_fetch(db
, key
);
158 if (val
.dptr
!= NULL
)
159 return 0; /* already there */
161 val
.dptr
= __UNCONST(str2
);
162 val
.dsize
= strlen(str2
);
163 status
= ypdb_store(db
, key
, val
, YPDB_INSERT
);
166 warnx("can't store `%s %s'", str1
, str2
);
173 file_date(char *filename
)
176 static char datestr
[11];
178 memset(datestr
, 0, sizeof(datestr
));
180 if (strcmp(filename
, "-") == 0)
181 snprintf(datestr
, sizeof(datestr
), "%010d",
184 if (stat(filename
, &finfo
) != 0)
185 err(1, "can't stat %s", filename
);
186 snprintf(datestr
, sizeof(datestr
), "%010d",
187 (int)finfo
.st_mtime
);
194 list_database(char *database
)
199 db
= ypdb_open(database
);
201 err(1, "can't open database `%s'", database
);
203 key
= ypdb_firstkey(db
);
205 while (key
.dptr
!= NULL
) {
206 val
= ypdb_fetch(db
, key
);
207 /* workround trailing \0 in aliases.db */
208 if (key
.dptr
[key
.dsize
- 1] == '\0')
210 printf("%*.*s", key
.dsize
, key
.dsize
, key
.dptr
);
212 if (val
.dptr
[val
.dsize
- 1] == '\0')
214 printf(" %*.*s", val
.dsize
, val
.dsize
, val
.dptr
);
217 key
= ypdb_nextkey(db
);
224 create_database(char *infile
, char *database
, char *yp_input_file
,
225 char *yp_output_file
, char *yp_master_name
,
226 char *yp_domain_name
, int bflag
, int lflag
, int sflag
)
229 char myname
[MAXHOSTNAMELEN
];
232 char *p
, *k
, *v
, *slash
;
234 static const char template[] = "ypdbXXXXXX";
235 char db_mapname
[MAXPATHLEN
+ 1], db_outfile
[MAXPATHLEN
+ 1];
236 char empty_str
[] = "";
238 memset(db_mapname
, 0, sizeof(db_mapname
));
239 memset(db_outfile
, 0, sizeof(db_outfile
));
241 if (strcmp(infile
, "-") == 0)
244 data_file
= fopen(infile
, "r");
245 if (data_file
== NULL
)
246 err(1, "can't open `%s'", infile
);
249 if (strlen(database
) + strlen(YPDB_SUFFIX
) > (sizeof(db_outfile
) - 1))
250 errx(1, "file name `%s' too long", database
);
252 snprintf(db_outfile
, sizeof(db_outfile
), "%s%s", database
, YPDB_SUFFIX
);
254 slash
= strrchr(database
, '/');
256 slash
[1] = '\0'; /* truncate to dir */
258 *database
= '\0'; /* eliminate */
260 /* NOTE: database is now directory where map goes ! */
262 if (strlen(database
) + strlen(template) + strlen(YPDB_SUFFIX
) >
263 (sizeof(db_mapname
) - 1))
264 errx(1, "directory name `%s' too long", database
);
266 snprintf(db_mapname
, sizeof(db_mapname
), "%s%s",
269 new_db
= ypdb_mktemp(db_mapname
);
271 err(1, "can't create temp database `%s'", db_mapname
);
274 (p
= fparseln(data_file
, &len
, &line_no
, NULL
, FPARSELN_UNESCALL
));
276 k
= p
; /* set start of key */
277 while (*k
&& isspace((unsigned char)*k
)) /* skip leading whitespace */
284 while (*v
&& !isspace((unsigned char)*v
)) { /* find leading whitespace */
285 /* convert key to lower case if forcing. */
286 if (lflag
&& isupper((unsigned char)*v
))
287 *v
= tolower((unsigned char)*v
);
290 while (*v
&& isspace((unsigned char)*v
)) /* replace space with <NUL> */
293 if (add_record(new_db
, k
, v
, TRUE
)) { /* save record */
297 errx(1, "error adding record for line %lu",
298 (unsigned long)line_no
);
302 if (strcmp(infile
, "-") != 0)
303 (void) fclose(data_file
);
305 if (add_record(new_db
, YP_LAST_KEY
, file_date(infile
), FALSE
))
309 if (add_record(new_db
, YP_INPUT_KEY
, yp_input_file
, FALSE
))
313 if (yp_output_file
) {
314 if (add_record(new_db
, YP_OUTPUT_KEY
, yp_output_file
, FALSE
))
318 if (yp_master_name
) {
319 if (add_record(new_db
, YP_MASTER_KEY
, yp_master_name
, FALSE
))
322 localhostname(myname
, sizeof(myname
) - 1);
323 if (add_record(new_db
, YP_MASTER_KEY
, myname
, FALSE
))
327 if (yp_domain_name
) {
328 if (add_record(new_db
, YP_DOMAIN_KEY
, yp_domain_name
, FALSE
))
333 if (add_record(new_db
, YP_INTERDOMAIN_KEY
, empty_str
, FALSE
))
338 if (add_record(new_db
, YP_SECURE_KEY
, empty_str
, FALSE
))
343 if (rename(db_mapname
, db_outfile
) < 0) {
345 err(1, "rename `%s' -> `%s'", db_mapname
, db_outfile
);
353 fprintf(stderr
, "usage: %s -u file\n", getprogname());
354 fprintf(stderr
, " %s [-lbs] %s\n", getprogname(),
355 "[-i YP_INPUT_FILE] [-o YP_OUTPUT_FILE]");
356 fprintf(stderr
, " %s infile outfile\n",
357 "[-d YP_DOMAIN_NAME] [-m YP_MASTER_NAME]");