Deleted Added
full compact
mk-amd-map.c (38577) mk-amd-map.c (41145)
1/*
2 * Copyright (c) 1997-1998 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by

--- 24 unchanged lines hidden (view full) ---

33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * %W% (Berkeley) %G%
40 *
1/*
2 * Copyright (c) 1997-1998 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by

--- 24 unchanged lines hidden (view full) ---

33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * %W% (Berkeley) %G%
40 *
41 * $Id: mk-amd-map.c,v 1.3 1998/08/27 02:56:47 obrien Exp $
41 * $Id: mk-amd-map.c,v 1.4 1998/08/27 07:25:25 obrien Exp $
42 */
43
44/*
45 * Convert a file map into an ndbm map
46 */
47
48#ifdef HAVE_CONFIG_H
49# include <config.h>
50#endif /* HAVE_CONFIG_H */
51#include <am_defs.h>
52
53/* dummy variables */
54char hostname[MAXHOSTNAMELEN];
55int orig_umask, foreground, debug_flags;
56pid_t mypid;
57serv_state amd_state;
58
42 */
43
44/*
45 * Convert a file map into an ndbm map
46 */
47
48#ifdef HAVE_CONFIG_H
49# include <config.h>
50#endif /* HAVE_CONFIG_H */
51#include <am_defs.h>
52
53/* dummy variables */
54char hostname[MAXHOSTNAMELEN];
55int orig_umask, foreground, debug_flags;
56pid_t mypid;
57serv_state amd_state;
58
59/* (libdb version 2) uses .db extensions but an old dbm API */
60/* check for libgdbm to distinguish it from linux systems */
61#if defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM)
62# define HAVE_DB_SUFFIX
63#endif /* not defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM) */
59
60#ifdef HAVE_MAP_NDBM
61
62static int
63store_data(voidp db, char *k, char *v)
64{
65 datum key, val;
66

--- 46 unchanged lines hidden (view full) ---

113static int
114read_file(FILE *fp, char *map, voidp db)
115{
116 char key_val[2048];
117 int chuck = 0;
118 int line_no = 0;
119 int errs = 0;
120
64
65#ifdef HAVE_MAP_NDBM
66
67static int
68store_data(voidp db, char *k, char *v)
69{
70 datum key, val;
71

--- 46 unchanged lines hidden (view full) ---

118static int
119read_file(FILE *fp, char *map, voidp db)
120{
121 char key_val[2048];
122 int chuck = 0;
123 int line_no = 0;
124 int errs = 0;
125
121 while (read_line(key_val, sizeof(key_val), fp)) {
126 while (read_line(key_val, 2048, fp)) {
122 char *kp;
123 char *cp;
124 char *hash;
125 int len = strlen(key_val);
126
127 line_no++;
128
129 /*

--- 77 unchanged lines hidden (view full) ---

207
208 return 0;
209}
210
211
212int
213main(int argc, char *argv[])
214{
127 char *kp;
128 char *cp;
129 char *hash;
130 int len = strlen(key_val);
131
132 line_no++;
133
134 /*

--- 77 unchanged lines hidden (view full) ---

212
213 return 0;
214}
215
216
217int
218main(int argc, char *argv[])
219{
215 FILE *mapf;
216 int mapfd = -1;
217 char *map;
218 int rc = 0;
219 DBM *mapd = NULL;
220 FILE *mapf; /* the input file to read from */
221 int error;
222 char *mapsrc;
223 DBM *db = NULL;
220 static char maptmp[] = "dbmXXXXXX";
224 static char maptmp[] = "dbmXXXXXX";
225#ifdef HAVE_DB_SUFFIX
221 char maptdb[16];
226 char maptdb[16];
222 char *mapdb = (char *) NULL;
227 char *map_name_db = (char *) NULL;
228#else /* not HAVE_DB_SUFFIX */
229 char maptpag[16], maptdir[16];
230 char *map_name_pag = (char *) NULL, *map_name_dir = (char *) NULL;
231#endif /* not HAVE_DB_SUFFIX */
223 int len;
224 char *sl;
225 int printit = 0;
226 int usage = 0;
227 int ch;
228 extern int optind;
229
230 /* test options */

--- 6 unchanged lines hidden (view full) ---

237 usage++;
238 break;
239 }
240
241 if (usage || optind != (argc - 1)) {
242 fputs("Usage: mk-amd-map [-p] file-map\n", stderr);
243 exit(1);
244 }
232 int len;
233 char *sl;
234 int printit = 0;
235 int usage = 0;
236 int ch;
237 extern int optind;
238
239 /* test options */

--- 6 unchanged lines hidden (view full) ---

246 usage++;
247 break;
248 }
249
250 if (usage || optind != (argc - 1)) {
251 fputs("Usage: mk-amd-map [-p] file-map\n", stderr);
252 exit(1);
253 }
245 map = argv[optind];
254 mapsrc = argv[optind];
246
247 /* test if can get to the map directory */
255
256 /* test if can get to the map directory */
248 sl = strrchr(map, '/');
257 sl = strrchr(mapsrc, '/');
249 if (sl) {
250 *sl = '\0';
258 if (sl) {
259 *sl = '\0';
251 if (chdir(map) < 0) {
260 if (chdir(mapsrc) < 0) {
252 fputs("Can't chdir to ", stderr);
261 fputs("Can't chdir to ", stderr);
253 perror(map);
262 perror(mapsrc);
254 exit(1);
255 }
263 exit(1);
264 }
256 map = sl + 1;
265 mapsrc = sl + 1;
257 }
258
266 }
267
268 /* open source file */
269 mapf = fopen(mapsrc, "r");
270 if (!mapf) {
271 fprintf(stderr, "cannot open source file ");
272 perror(mapsrc);
273 exit(1);
274 }
275
276#ifndef DEBUG
277 signal(SIGINT, SIG_IGN);
278#endif /* DEBUG */
279
259 if (!printit) {
280 if (!printit) {
260 len = strlen(map);
261 mapdb = (char *) malloc(len + 4);
262 if (!mapdb) {
281 len = strlen(mapsrc);
282#ifdef HAVE_DB_SUFFIX
283 map_name_db = (char *) malloc(len + 4);
284 error = (map_name_db == NULL);
285#else /* not HAVE_DB_SUFFIX */
286 map_name_pag = (char *) malloc(len + 5);
287 map_name_dir = (char *) malloc(len + 5);
288 error = (map_name_pag == NULL || map_name_dir == NULL);
289#endif /* not HAVE_DB_SUFFIX */
290 if (error) {
263 perror("mk-amd-map: malloc");
264 exit(1);
265 }
291 perror("mk-amd-map: malloc");
292 exit(1);
293 }
266#ifdef HAVE_MKSTEMP
267 mapfd = mkstemp(maptmp);
268#else /* not HAVE_MKSTEMP */
269 map = mktemp(maptmp);
270 if (!maptmp) {
271 fprintf(stderr, "cannot create temporary file\n");
272 exit(1);
273 }
274 mapfd = open(map, O_RDONLY);
275#endif /* not HAVE_MKSTEMP */
276
294
277 /* open DBM files */
295 mktemp(maptmp);
296
297 /* remove existing temps (if any) */
298#ifdef HAVE_DB_SUFFIX
278 sprintf(maptdb, "%s.db", maptmp);
279 if (remove_file(maptdb) < 0) {
299 sprintf(maptdb, "%s.db", maptmp);
300 if (remove_file(maptdb) < 0) {
280 fprintf(stderr, "Can't remove existing temporary files;");
301 fprintf(stderr, "Can't remove existing temporary file; ");
281 perror(maptdb);
282 exit(1);
283 }
302 perror(maptdb);
303 exit(1);
304 }
305#else /* not HAVE_DB_SUFFIX */
306 sprintf(maptpag, "%s.pag", maptmp);
307 sprintf(maptdir, "%s.dir", maptmp);
308 if (remove_file(maptpag) < 0 || remove_file(maptdir) < 0) {
309 fprintf(stderr, "Can't remove existing temporary files; %s and ", maptpag);
310 perror(maptdir);
311 exit(1);
312 }
313#endif /* not HAVE_DB_SUFFIX */
314
315 db = dbm_open(maptmp, O_RDWR|O_CREAT, 0444);
316 if (!db) {
317 fprintf(stderr, "cannot initialize temporary database: %s", maptmp);
318 exit(1);
319 }
284 }
320 }
285 /* open and check if map file was opened OK */
286 mapf = fdopen(mapfd, "r");
287 if (mapf && !printit)
288 mapd = dbm_open(maptmp, O_RDWR|O_CREAT, 0444);
289 else
290 mapd = 0;
291
321
292#ifndef DEBUG
293 /* ignore ^C if debuggung is on (but why?) */
294 signal(SIGINT, SIG_IGN);
295#endif /* not DEBUG */
322 /* print db to stdout or to temp database */
323 error = read_file(mapf, mapsrc, db);
324 fclose(mapf);
325 if (error) {
326 if (printit)
327 fprintf(stderr, "Error reading source file %s\n", mapsrc);
328 else
329 fprintf(stderr, "Error creating database map for %s\n", mapsrc);
330 exit(1);
331 }
296
332
297 if (mapd || printit) {
298 int error = read_file(mapf, map, mapd);
299 (void) close(mapfd);
300 (void) fclose(mapf);
301 dbm_close(mapd);
302 if (printit) {
303 if (error) {
304 fprintf(stderr, "Error creating ndbm map for %s\n", map);
305 rc = 1;
306 }
307 } else {
333 if (printit)
334 exit(0); /* nothing more to do */
308
335
309 if (error) {
310 fprintf(stderr, "Error reading source file %s\n", map);
311 rc = 1;
312 } else {
313 sprintf(mapdb, "%s.db", map);
314 if (unlink(mapdb) == 0)
315 fprintf(stderr, "WARNING: existing map \"%s.db\" destroyed\n", map);
316 if (rename(maptdb, mapdb) < 0) {
317 fprintf(stderr, "Couldn't rename %s to ", maptdb);
318 perror(mapdb);
319 /* Throw away the temporary map */
320 unlink(maptdb);
321 rc = 1;
322 }
323 }
324 }
336 /* if gets here, we wrote to a database */
325
337
326 } else {
327 fprintf(stderr, "Can't open \"%s.db\" for ", map);
328 perror("writing");
329 rc = 1;
338 dbm_close(db);
339 /* all went well */
340
341#ifdef HAVE_DB_SUFFIX
342 sprintf(map_name_db, "%s.db", mapsrc);
343 if (rename(maptdb, map_name_db) < 0) {
344 fprintf(stderr, "Couldn't rename %s to ", maptdb);
345 perror(map_name_db);
346 /* Throw away the temporary map */
347 unlink(maptdb);
348 exit(1);
330 }
349 }
331 exit(rc);
350#else /* not HAVE_DB_SUFFIX */
351 sprintf(map_name_pag, "%s.pag", mapsrc);
352 sprintf(map_name_dir, "%s.dir", mapsrc);
353 if (rename(maptpag, map_name_pag) < 0) {
354 fprintf(stderr, "Couldn't rename %s to ", maptpag);
355 perror(map_name_pag);
356 /* Throw away the temporary map */
357 unlink(maptpag);
358 unlink(maptdir);
359 exit(1);
360 }
361 if (rename(maptdir, map_name_dir) < 0) {
362 fprintf(stderr, "Couldn't rename %s to ", maptdir);
363 perror(map_name_dir);
364 /* remove the (presumably bad) .pag file */
365 unlink(map_name_pag);
366 /* throw away remaining part of original map */
367 unlink(map_name_dir);
368 /* throw away the temporary map */
369 unlink(maptdir);
370 fprintf(stderr, "WARNING: existing map \"%s.{dir,pag}\" destroyed\n",
371 mapsrc);
372 exit(1);
373 }
374#endif /* not HAVE_DB_SUFFIX */
375
376 exit(0);
332}
333
334#else /* not HAVE_MAP_NDBM */
335
377}
378
379#else /* not HAVE_MAP_NDBM */
380
381int
336main()
337{
338 fputs("mk-amd-map: This system does not support hashed database files\n", stderr);
339 exit(1);
340}
341
342#endif /* not HAVE_MAP_NDBM */
382main()
383{
384 fputs("mk-amd-map: This system does not support hashed database files\n", stderr);
385 exit(1);
386}
387
388#endif /* not HAVE_MAP_NDBM */