From: Paul Eggert Date: Tue, 29 Jun 1999 02:26:25 +0000 (+0000) Subject: Handle EINTR correctly; use STDIN_FILENO instead of 0, etc. X-Git-Url: https://git.dogcows.com/gitweb?a=commitdiff_plain;h=1521a94b11b0b7a2fe2a8318207d7d6b4bdbf76b;p=chaz%2Ftar Handle EINTR correctly; use STDIN_FILENO instead of 0, etc. --- diff --git a/src/buffer.c b/src/buffer.c index 6f78545..5eb1cc8 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -39,9 +39,6 @@ time_t time (); #define DEBUG_FORK 0 /* if nonzero, childs are born stopped */ -#define STDIN 0 /* standard input file descriptor */ -#define STDOUT 1 /* standard output file descriptor */ - #define PREAD 0 /* read file descriptor from pipe() */ #define PWRITE 1 /* write file descriptor from pipe() */ @@ -368,7 +365,7 @@ child_open_for_compress (void) program_name = _("tar (child)"); - xdup2 (parent_pipe[PREAD], STDIN, _("(child) Pipe to stdin")); + xdup2 (parent_pipe[PREAD], STDIN_FILENO, _("(child) Pipe to stdin")); xclose (parent_pipe[PWRITE]); /* Check if we need a grandchild tar. This happens only if either: @@ -396,7 +393,7 @@ child_open_for_compress (void) FATAL_ERROR ((0, saved_errno, _("Cannot open archive %s"), archive_name_array[0])); } - xdup2 (archive, STDOUT, _("Archive to stdout")); + xdup2 (archive, STDOUT_FILENO, _("Archive to stdout")); execlp (use_compress_program_option, use_compress_program_option, (char *) 0); FATAL_ERROR ((0, errno, _("Cannot exec %s"), @@ -416,7 +413,8 @@ child_open_for_compress (void) { /* The child tar is still here! Launch the compressor. */ - xdup2 (child_pipe[PWRITE], STDOUT, _("((child)) Pipe to stdout")); + xdup2 (child_pipe[PWRITE], STDOUT_FILENO, + _("((child)) Pipe to stdout")); xclose (child_pipe[PREAD]); execlp (use_compress_program_option, use_compress_program_option, (char *) 0); @@ -430,11 +428,11 @@ child_open_for_compress (void) /* Prepare for reblocking the data from the compressor into the archive. */ - xdup2 (child_pipe[PREAD], STDIN, _("(grandchild) Pipe to stdin")); + xdup2 (child_pipe[PREAD], STDIN_FILENO, _("(grandchild) Pipe to stdin")); xclose (child_pipe[PWRITE]); if (strcmp (archive_name_array[0], "-") == 0) - archive = STDOUT; + archive = STDOUT_FILENO; else archive = rmtcreat (archive_name_array[0], 0666, rsh_command_option); if (archive < 0) @@ -459,7 +457,7 @@ child_open_for_compress (void) if (size < BLOCKSIZE) size = BLOCKSIZE; - status = read (STDIN, cursor, size); + status = full_read (STDIN_FILENO, cursor, size); if (status <= 0) break; } @@ -530,7 +528,7 @@ child_open_for_uncompress (void) program_name = _("tar (child)"); - xdup2 (parent_pipe[PWRITE], STDOUT, _("(child) Pipe to stdout")); + xdup2 (parent_pipe[PWRITE], STDOUT_FILENO, _("(child) Pipe to stdout")); xclose (parent_pipe[PREAD]); /* Check if we need a grandchild tar. This happens only if either: @@ -549,7 +547,7 @@ child_open_for_uncompress (void) if (archive < 0) FATAL_ERROR ((0, errno, _("Cannot open archive %s"), archive_name_array[0])); - xdup2 (archive, STDIN, _("Archive to stdin")); + xdup2 (archive, STDIN_FILENO, _("Archive to stdin")); execlp (use_compress_program_option, use_compress_program_option, "-d", (char *) 0); FATAL_ERROR ((0, errno, _("Cannot exec %s"), @@ -569,7 +567,7 @@ child_open_for_uncompress (void) { /* The child tar is still here! Launch the uncompressor. */ - xdup2 (child_pipe[PREAD], STDIN, _("((child)) Pipe to stdin")); + xdup2 (child_pipe[PREAD], STDIN_FILENO, _("((child)) Pipe to stdin")); xclose (child_pipe[PWRITE]); execlp (use_compress_program_option, use_compress_program_option, "-d", (char *) 0); @@ -583,11 +581,11 @@ child_open_for_uncompress (void) /* Prepare for unblocking the data from the archive into the uncompressor. */ - xdup2 (child_pipe[PWRITE], STDOUT, _("(grandchild) Pipe to stdout")); + xdup2 (child_pipe[PWRITE], STDOUT_FILENO, _("(grandchild) Pipe to stdout")); xclose (child_pipe[PREAD]); if (strcmp (archive_name_array[0], "-") == 0) - archive = STDIN; + archive = STDIN_FILENO; else archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY, 0666, rsh_command_option); @@ -620,7 +618,7 @@ child_open_for_uncompress (void) while (maximum) { count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE; - status = write (STDOUT, cursor, count); + status = full_write (STDOUT_FILENO, cursor, count); if (status < 0) FATAL_ERROR ((0, errno, _("\ Cannot write to compression program"))); @@ -761,16 +759,16 @@ open_archive (enum access_mode access) switch (access) { case ACCESS_READ: - archive = STDIN; + archive = STDIN_FILENO; break; case ACCESS_WRITE: - archive = STDOUT; + archive = STDOUT_FILENO; stdlis = stderr; break; case ACCESS_UPDATE: - archive = STDIN; + archive = STDIN_FILENO; stdlis = stderr; write_archive_to_stdout = 1; break; @@ -1531,7 +1529,7 @@ new_volume (enum access_mode access) if (!read_file && !info_script_option) /* FIXME: if fopen is used, it will never be closed. */ - read_file = archive == STDIN ? fopen (TTY_NAME, "r") : stdin; + read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin; if (now_verifying) return 0; diff --git a/src/create.c b/src/create.c index 3a7e56a..160b9cd 100644 --- a/src/create.c +++ b/src/create.c @@ -518,7 +518,7 @@ deal_with_sparse (char *name, union block *header) init_sparsearray (); clear_buffer (buffer); - while (count = read (file, buffer, sizeof buffer), count != 0) + while (count = full_read (file, buffer, sizeof buffer), count != 0) { /* Realloc the scratch area as necessary. FIXME: should reallocate only at beginning of a new instance of non-zero data. */ @@ -637,8 +637,8 @@ finish_sparse_file (int file, off_t *sizeleft, off_t fullsize, char *name) #if 0 if (amount_read) { - count = read (file, start->buffer + amount_read, - BLOCKSIZE - amount_read); + count = full_read (file, start->buffer + amount_read, + BLOCKSIZE - amount_read); bufsize -= BLOCKSIZE - amount_read; amount_read = 0; set_next_block_after (start); @@ -648,7 +648,7 @@ finish_sparse_file (int file, off_t *sizeleft, off_t fullsize, char *name) #endif /* Store the data. */ - count = read (file, start->buffer, BLOCKSIZE); + count = full_read (file, start->buffer, BLOCKSIZE); if (count < 0) { char buf[UINTMAX_STRSIZE_BOUND]; @@ -669,7 +669,7 @@ Read error at byte %s, reading %lu bytes, in file %s"), char buffer[BLOCKSIZE]; clear_buffer (buffer); - count = read (file, buffer, bufsize); + count = full_read (file, buffer, bufsize); memcpy (start->buffer, buffer, BLOCKSIZE); } @@ -1173,7 +1173,7 @@ Removing leading `/' from absolute links"))); if (f < 0) count = bufsize; else - count = read (f, start->buffer, bufsize); + count = full_read (f, start->buffer, bufsize); if (count < 0) { char buf[UINTMAX_STRSIZE_BOUND]; diff --git a/src/extract.c b/src/extract.c index e498cf4..0728a99 100644 --- a/src/extract.c +++ b/src/extract.c @@ -343,7 +343,7 @@ extract_sparse_file (int fd, off_t *sizeleft, off_t totalsize, char *name) written = sparsearray[sparse_ind++].numbytes; while (written > BLOCKSIZE) { - count = write (fd, data_block->buffer, BLOCKSIZE); + count = full_write (fd, data_block->buffer, BLOCKSIZE); if (count < 0) ERROR ((0, errno, _("%s: Could not write to file"), name)); written -= count; @@ -352,7 +352,7 @@ extract_sparse_file (int fd, off_t *sizeleft, off_t totalsize, char *name) data_block = find_next_block (); } - count = write (fd, data_block->buffer, written); + count = full_write (fd, data_block->buffer, written); if (count < 0) ERROR ((0, errno, _("%s: Could not write to file"), name)); @@ -659,7 +659,7 @@ Removing leading `/' from absolute path names in the archive"))); if (written > size) written = size; errno = 0; /* FIXME: errno should be read-only */ - sstatus = write (fd, data_block->buffer, written); + sstatus = full_write (fd, data_block->buffer, written); set_next_block_after ((union block *) (data_block->buffer + written - 1)); @@ -706,7 +706,7 @@ Removing leading `/' from absolute path names in the archive"))); written = SIZE_FROM_OCT (exhdr->sparse_header.sp[counter].numbytes); lseek (fd, offset, 0); - sstatus = write (fd, data_block->buffer, written); + sstatus = full_write (fd, data_block->buffer, written); if (sstatus == written) continue; } diff --git a/src/rmt.c b/src/rmt.c index ac0317d..745120d 100644 --- a/src/rmt.c +++ b/src/rmt.c @@ -1,5 +1,5 @@ /* Remote connection server. - Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc. + Copyright 1994, 1995, 1996, 1997, 1999 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the @@ -81,12 +81,12 @@ char *strerror (); static char * private_strerror (int errnum) { - extern const char *const sys_errlist[]; + extern char *sys_errlist[]; extern int sys_nerr; if (errnum > 0 && errnum <= sys_nerr) - return sys_errlist[errnum]; - return N_("Unknown system error"); + return _(sys_errlist[errnum]); + return _("Unknown system error"); } # define strerror private_strerror #endif @@ -101,7 +101,7 @@ report_error_message (const char *string) DEBUG1 ("rmtd: E 0 (%s)\n", string); sprintf (reply_buffer, "E0\n%s\n", string); - write (1, reply_buffer, strlen (reply_buffer)); + full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer)); } /*---. @@ -114,7 +114,7 @@ report_numbered_error (int num) DEBUG2 ("rmtd: E %d (%s)\n", num, strerror (num)); sprintf (reply_buffer, "E%d\n%s\n", num, strerror (num)); - write (1, reply_buffer, strlen (reply_buffer)); + full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer)); } /*---. @@ -128,7 +128,7 @@ get_string (char *string) for (counter = 0; counter < STRING_SIZE; counter++) { - if (read (0, string + counter, 1) != 1) + if (full_read (STDIN_FILENO, string + counter, 1) != 1) exit (EXIT_SUCCESS); if (string[counter] == '\n') @@ -164,7 +164,9 @@ prepare_record_buffer (size_t size) #ifdef SO_RCVBUF while (size > 1024 && - setsockopt (0, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof (size)) < 0) + (setsockopt (STDIN_FILENO, SOL_SOCKET, SO_RCVBUF, + (char *) &size, sizeof size) + < 0)) size -= 1024; #else /* FIXME: I do not see any purpose to the following line... Sigh! */ @@ -208,7 +210,7 @@ main (int argc, char *const *argv) top: errno = 0; /* FIXME: errno should be read-only */ status = 0; - if (read (0, &command, 1) != 1) + if (full_read (STDIN_FILENO, &command, 1) != 1) exit (EXIT_SUCCESS); switch (command) @@ -301,7 +303,7 @@ top: DEBUG1 ("rmtd: A %s\n", p); sprintf (reply_buffer, "A%s\n", p); - write (1, reply_buffer, strlen (reply_buffer)); + full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer)); goto top; } @@ -318,7 +320,8 @@ top: prepare_record_buffer (size); for (counter = 0; counter < size; counter += status) { - status = read (0, &record_buffer[counter], size - counter); + status = full_read (STDIN_FILENO, &record_buffer[counter], + size - counter); if (status <= 0) { DEBUG (_("rmtd: Premature eof\n")); @@ -327,7 +330,7 @@ top: exit (EXIT_FAILURE); /* exit status used to be 2 */ } } - status = write (tape, record_buffer, size); + status = full_write (tape, record_buffer, size); if (status < 0) goto ioerror; goto respond; @@ -343,12 +346,12 @@ top: size = atol (count_string); prepare_record_buffer (size); - status = read (tape, record_buffer, size); + status = full_read (tape, record_buffer, size); if (status < 0) goto ioerror; sprintf (reply_buffer, "A%ld\n", status); - write (1, reply_buffer, strlen (reply_buffer)); - write (1, record_buffer, (size_t) status); + full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer)); + full_write (STDOUT_FILENO, record_buffer, (size_t) status); goto top; } @@ -423,8 +426,8 @@ top: goto ioerror; status = sizeof (operation); sprintf (reply_buffer, "A%ld\n", status); - write (1, reply_buffer, strlen (reply_buffer)); - write (1, (char *) &operation, sizeof (operation)); + full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer)); + full_write (STDOUT_FILENO, (char *) &operation, sizeof (operation)); } #endif goto top; @@ -441,7 +444,7 @@ respond: DEBUG1 ("rmtd: A %ld\n", status); sprintf (reply_buffer, "A%ld\n", status); - write (1, reply_buffer, strlen (reply_buffer)); + full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer)); goto top; ioerror: diff --git a/src/rmt.h b/src/rmt.h index 510559f..7b3c298 100644 --- a/src/rmt.h +++ b/src/rmt.h @@ -65,11 +65,11 @@ int rmt_ioctl__ PARAMS ((int, int, char *)); #define rmtread(Fd, Buffer, Length) \ (_isrmt (Fd) ? rmt_read__ (Fd - __REM_BIAS, Buffer, Length) \ - : read (Fd, Buffer, Length)) + : full_read (Fd, Buffer, Length)) #define rmtwrite(Fd, Buffer, Length) \ (_isrmt (Fd) ? rmt_write__ (Fd - __REM_BIAS, Buffer, Length) \ - : write (Fd, Buffer, Length)) + : full_write (Fd, Buffer, Length)) #define rmtlseek(Fd, Offset, Where) \ (_isrmt (Fd) ? rmt_lseek__ (Fd - __REM_BIAS, Offset, Where) \ diff --git a/src/rtapelib.c b/src/rtapelib.c index d02386b..c9cd90e 100644 --- a/src/rtapelib.c +++ b/src/rtapelib.c @@ -119,7 +119,7 @@ do_command (int handle, const char *buffer) pipe_handler = signal (SIGPIPE, SIG_IGN); length = strlen (buffer); - if (write (WRITE_SIDE (handle), buffer, length) == length) + if (full_write (WRITE_SIDE (handle), buffer, length) == length) { signal (SIGPIPE, pipe_handler); return 0; @@ -144,7 +144,7 @@ get_status_string (int handle, char *command_buffer) counter < COMMAND_BUFFER_SIZE; counter++, cursor++) { - if (read (READ_SIDE (handle), cursor, 1) != 1) + if (full_read (READ_SIDE (handle), cursor, 1) != 1) { _rmt_shutdown (handle, EIO); return 0; @@ -180,7 +180,7 @@ get_status_string (int handle, char *command_buffer) { char character; - while (read (READ_SIDE (handle), &character, 1) == 1) + while (full_read (READ_SIDE (handle), &character, 1) == 1) if (character == '\n') break; } @@ -276,8 +276,8 @@ get_status_off (int handle) static int _rmt_rexec (char *host, char *user) { - int saved_stdin = dup (fileno (stdin)); - int saved_stdout = dup (fileno (stdout)); + int saved_stdin = dup (STDIN_FILENO); + int saved_stdout = dup (STDOUT_FILENO); struct servent *rexecserv; int result; @@ -436,12 +436,12 @@ rmt_open__ (const char *path, int open_mode, int bias, const char *remote_shell) { /* Child. */ - close (0); + close (STDIN_FILENO); dup (to_remote[remote_pipe_number][PREAD]); close (to_remote[remote_pipe_number][PREAD]); close (to_remote[remote_pipe_number][PWRITE]); - close (1); + close (STDOUT_FILENO); dup (from_remote[remote_pipe_number][PWRITE]); close (from_remote[remote_pipe_number][PREAD]); close (from_remote[remote_pipe_number][PWRITE]); @@ -527,7 +527,7 @@ rmt_read__ (int handle, char *buffer, size_t length) for (counter = 0; counter < status; counter += rlen, buffer += rlen) { - rlen = read (READ_SIDE (handle), buffer, status - counter); + rlen = full_read (READ_SIDE (handle), buffer, status - counter); if (rlen <= 0) { _rmt_shutdown (handle, EIO); @@ -554,7 +554,7 @@ rmt_write__ (int handle, char *buffer, size_t length) return -1; pipe_handler = signal (SIGPIPE, SIG_IGN); - if (write (WRITE_SIDE (handle), buffer, length) == length) + if (full_write (WRITE_SIDE (handle), buffer, length) == length) { signal (SIGPIPE, pipe_handler); return get_status (handle); @@ -653,7 +653,8 @@ rmt_ioctl__ (int handle, int operation, char *argument) for (; status > 0; status -= counter, argument += counter) { - counter = read (READ_SIDE (handle), argument, (size_t) status); + counter = full_read (READ_SIDE (handle), + argument, (size_t) status); if (counter <= 0) { _rmt_shutdown (handle, EIO); diff --git a/src/update.c b/src/update.c index b839872..023fb97 100644 --- a/src/update.c +++ b/src/update.c @@ -73,7 +73,7 @@ append_file (char *path) (size_t) (BLOCKSIZE - status)); } - status = read (handle, start->buffer, buffer_size); + status = full_read (handle, start->buffer, buffer_size); if (status < 0) { char buf[UINTMAX_STRSIZE_BOUND];