387 |
} |
} |
388 |
|
|
389 |
/* Returns the number of digits */ |
/* Returns the number of digits */ |
390 |
int simple_itoa(off64_t i, char buf[22]) |
int simple_itoa(large_int i, char buf[22]) |
391 |
{ |
{ |
392 |
/* 21 digits plus null terminator, good for 64-bit or smaller ints |
/* 21 digits plus null terminator, good for 64-bit or smaller ints |
393 |
* for bigger ints, use a bigger buffer! |
* for bigger ints, use a bigger buffer! |
461 |
return retval; |
return retval; |
462 |
} |
} |
463 |
|
|
464 |
off64_t boa_atoll(const char *s) |
large_int boa_atoll(const char *s) |
465 |
{ |
{ |
466 |
long int retval; |
long int retval; |
467 |
char reconv[22]; |
char reconv[22]; |
487 |
#define TEMP_FILE_TEMPLATE "/hydra.temp.XXXXXX" |
#define TEMP_FILE_TEMPLATE "/hydra.temp.XXXXXX" |
488 |
#define TEMP_FILE_TEMPLATE_LEN sizeof(TEMP_FILE_TEMPLATE)-1 |
#define TEMP_FILE_TEMPLATE_LEN sizeof(TEMP_FILE_TEMPLATE)-1 |
489 |
|
|
490 |
|
void close_tmp_fd( tmp_fd* fds) |
491 |
|
{ |
492 |
|
if (fds->pipe) { |
493 |
|
close( fds->fds[0]); |
494 |
|
close( fds->fds[1]); |
495 |
|
} else close( fds->fds[0]); |
496 |
|
|
497 |
|
fds->fds[0] = fds->fds[1] = -1; |
498 |
|
|
499 |
|
} |
500 |
|
|
501 |
|
const static tmp_fd EMPTY_FDS = { {-1, -1}, 0}; |
502 |
|
|
503 |
/* returns -1 on error */ |
/* returns -1 on error */ |
504 |
int create_temporary_file(short want_unlink, char *storage, int size) |
/* size holds the number of data that will be written to |
505 |
|
* the temporary file. |
506 |
|
*/ |
507 |
|
tmp_fd create_temporary_file(short want_unlink, int size) |
508 |
{ |
{ |
509 |
char boa_tempfile[MAX_PATH_LENGTH + 1]; |
char boa_tempfile[MAX_PATH_LENGTH + 1]; |
510 |
int fd, total_len; |
tmp_fd fd; |
511 |
|
int total_len; |
512 |
|
|
513 |
|
fd.pipe = 0; |
514 |
|
|
515 |
|
if (size > 0 && size < PIPE_BUF) |
516 |
|
if (pipe( fd.fds) != -1) { |
517 |
|
fd.pipe = 1; |
518 |
|
return fd; |
519 |
|
} |
520 |
|
|
521 |
total_len = tempdir_len + TEMP_FILE_TEMPLATE_LEN; |
total_len = tempdir_len + TEMP_FILE_TEMPLATE_LEN; |
522 |
if (total_len > MAX_PATH_LENGTH) { |
if (total_len > MAX_PATH_LENGTH) { |
523 |
log_error_time(); |
log_error_time(); |
524 |
fprintf(stderr, "Temporary file length (%d) is too long\n", total_len); |
fprintf(stderr, "Temporary file length (%d) is too long\n", total_len); |
525 |
return -1; |
return EMPTY_FDS; |
526 |
} |
} |
527 |
|
|
528 |
memcpy( boa_tempfile, tempdir, tempdir_len); |
memcpy( boa_tempfile, tempdir, tempdir_len); |
529 |
memcpy( &boa_tempfile[tempdir_len], TEMP_FILE_TEMPLATE, TEMP_FILE_TEMPLATE_LEN); |
memcpy( &boa_tempfile[tempdir_len], TEMP_FILE_TEMPLATE, TEMP_FILE_TEMPLATE_LEN); |
530 |
boa_tempfile[total_len] = 0; /* null terminated */ |
boa_tempfile[total_len] = 0; /* null terminated */ |
531 |
|
|
532 |
|
fd.fds[0] = fd.fds[1] = -1; |
533 |
/* open temp file |
/* open temp file |
534 |
*/ |
*/ |
535 |
fd = mkstemp(boa_tempfile); |
fd.fds[0] = mkstemp(boa_tempfile); |
536 |
if (fd == -1) { |
if (fd.fds[0] == -1) { |
537 |
log_error_time(); |
log_error_time(); |
538 |
perror("mkstemp"); |
perror("mkstemp"); |
539 |
return -1; |
return EMPTY_FDS; |
|
} |
|
|
|
|
|
if (storage != NULL) { |
|
|
if (total_len < size) { |
|
|
memcpy(storage, boa_tempfile, total_len + 1); |
|
|
} else { |
|
|
close(fd); |
|
|
fd = -1; |
|
|
log_error_time(); |
|
|
fprintf(stderr, "not enough memory for memcpy in storage\n"); |
|
|
want_unlink = 1; |
|
|
} |
|
540 |
} |
} |
541 |
|
|
542 |
if (want_unlink) { |
if (want_unlink) { |
543 |
if (unlink(boa_tempfile) == -1) { |
if (unlink(boa_tempfile) == -1) { |
544 |
close(fd); |
close(fd.fds[0]); |
545 |
fd = -1; |
fd.fds[0] = -1; |
546 |
log_error_time(); |
log_error_time(); |
547 |
fprintf(stderr, "unlink temp file\n"); |
fprintf(stderr, "unlink temp file\n"); |
548 |
} |
} |
549 |
} |
} |
550 |
|
|
551 |
|
fd.fds[1] = fd.fds[0]; |
552 |
return (fd); |
return (fd); |
553 |
} |
} |
554 |
|
|