35 |
|
|
36 |
|
static AMBTREE atrunk; /* our ambient trunk node */ |
37 |
|
|
38 |
– |
static char *ambfname = NULL; /* ambient file name */ |
38 |
|
static FILE *ambfp = NULL; /* ambient file pointer */ |
39 |
|
static int nunflshed = 0; /* number of unflushed ambient values */ |
40 |
|
|
74 |
|
/* init ambient limits */ |
75 |
|
setambres(ambres); |
76 |
|
/* open ambient file */ |
77 |
< |
if ((ambfname = afile) != NULL) { |
77 |
> |
if (afile != NULL) { |
78 |
|
if ((ambfp = fopen(afile, "r+")) != NULL) { |
79 |
|
initambfile(0); |
80 |
|
headlen = ftell(ambfp); |
324 |
|
fputformat(AMBFMT, ambfp); |
325 |
|
putc('\n', ambfp); |
326 |
|
putambmagic(ambfp); |
327 |
< |
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) { |
328 |
< |
sprintf(errmsg, "bad ambient file \"%s\"", ambfname); |
330 |
< |
error(USER, errmsg); |
331 |
< |
} |
327 |
> |
} else if (checkheader(ambfp, AMBFMT, NULL) < 0 || !hasambmagic(ambfp)) |
328 |
> |
error(USER, "bad ambient file"); |
329 |
|
} |
330 |
|
|
331 |
|
|
343 |
|
goto writerr; |
344 |
|
return; |
345 |
|
writerr: |
346 |
< |
sprintf(errmsg, "error writing ambient file \"%s\"", ambfname); |
350 |
< |
error(SYSTEM, errmsg); |
346 |
> |
error(SYSTEM, "error writing ambient file"); |
347 |
|
} |
348 |
|
|
349 |
|
|
384 |
|
} |
385 |
|
|
386 |
|
|
387 |
< |
#ifdef NIX |
387 |
> |
#ifdef F_SETLKW |
388 |
|
|
389 |
|
int |
394 |
– |
ambsync() /* flush ambient file */ |
395 |
– |
{ |
396 |
– |
if (nunflshed == 0) |
397 |
– |
return(0); |
398 |
– |
nunflshed = 0; |
399 |
– |
return(fflush(ambfp)); |
400 |
– |
} |
401 |
– |
|
402 |
– |
#else |
403 |
– |
|
404 |
– |
int |
390 |
|
ambsync() /* synchronize ambient file */ |
391 |
|
{ |
392 |
|
static FILE *ambinp = NULL; |
411 |
|
if ((flen = lseek(fileno(ambfp), 0L, 2)) < 0) |
412 |
|
error(SYSTEM, "cannot seek on ambient file"); |
413 |
|
if (n = flen - lastpos) { /* file has grown */ |
414 |
< |
if (ambinp == NULL) { |
415 |
< |
ambinp = fopen(ambfname, "r"); |
414 |
> |
if (ambinp == NULL) { /* use duplicate filedes */ |
415 |
> |
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
416 |
|
if (ambinp == NULL) |
417 |
< |
error(SYSTEM, "fopen failed in ambsync"); |
417 |
> |
error(SYSTEM, "fdopen failed in ambsync"); |
418 |
|
} |
419 |
|
if (fseek(ambinp, lastpos, 0) < 0) |
420 |
|
error(SYSTEM, "fseek failed in ambsync"); |
433 |
|
fcntl(fileno(ambfp), F_SETLKW, &fls); |
434 |
|
nunflshed = 0; |
435 |
|
return(n); |
436 |
+ |
} |
437 |
+ |
|
438 |
+ |
#else |
439 |
+ |
|
440 |
+ |
int |
441 |
+ |
ambsync() /* flush ambient file */ |
442 |
+ |
{ |
443 |
+ |
if (nunflshed == 0) |
444 |
+ |
return(0); |
445 |
+ |
nunflshed = 0; |
446 |
+ |
return(fflush(ambfp)); |
447 |
|
} |
448 |
|
|
449 |
|
#endif |