diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2004-08-03 06:16:31 +0000 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2004-08-03 06:16:31 +0000 |
commit | b871878b299a03ea405b12848ce5773d2dd30ab0 (patch) | |
tree | a17ff36f0f9f8cf6870c26dd47642bbba226bef3 | |
parent | 3d3030da664c179fae27ab5859ab6ea3bb4e4de7 (diff) | |
download | coreutils-b871878b299a03ea405b12848ce5773d2dd30ab0.tar.xz |
(presume_input_pipe, print_headers, have_read_stdin,
write_header, elide_tail_bytes_pipe, elide_tail_bytes_file,
elide_tail_lines_pipe, elide_tail_lines_seekable,
elide_tail_lines_file, head_bytes, head_lines, head, head_file,
string_to_integer, main):
Use bool for booleans.
(main): Rewrite to avoid cast.
-rw-r--r-- | src/head.c | 170 |
1 files changed, 85 insertions, 85 deletions
diff --git a/src/head.c b/src/head.c index ee86ac01e..0aa59bab9 100644 --- a/src/head.c +++ b/src/head.c @@ -50,13 +50,13 @@ #define DEFAULT_NUMBER 10 /* Useful only when eliding tail bytes or lines. - If nonzero, skip the is-regular-file test used to determine whether + If true, skip the is-regular-file test used to determine whether to use the lseek optimization. Instead, use the more general (and more expensive) code unconditionally. Intended solely for testing. */ -static int presume_input_pipe; +static bool presume_input_pipe; -/* If nonzero, print filename headers. */ -static int print_headers; +/* If true, print filename headers. */ +static bool print_headers; /* When to print the filename banners. */ enum header_mode @@ -71,7 +71,7 @@ static char const header_mode_option[][4] = { "", " -v", " -q" }; char *program_name; /* Have we ever read standard input? */ -static int have_read_stdin; +static bool have_read_stdin; enum Copy_fd_status { @@ -168,10 +168,10 @@ diagnose_copy_fd_failure (enum Copy_fd_status err, char const *filename) static void write_header (const char *filename) { - static int first_file = 1; + static bool first_file = true; printf ("%s==> %s <==\n", (first_file ? "" : "\n"), filename); - first_file = 0; + first_file = false; } /* Copy no more than N_BYTES from file descriptor SRC_FD to O_STREAM. @@ -204,9 +204,9 @@ copy_fd (int src_fd, FILE *o_stream, uintmax_t n_bytes) } /* Print all but the last N_ELIDE lines from the input available via - the non-seekable file descriptor FD. Return zero upon success. - Give a diagnostic and return nonzero upon error. */ -static int + the non-seekable file descriptor FD. Return true upon success. + Give a diagnostic and return false upon error. */ +static bool elide_tail_bytes_pipe (const char *filename, int fd, uintmax_t n_elide_0) { size_t n_elide = n_elide_0; @@ -248,16 +248,16 @@ elide_tail_bytes_pipe (const char *filename, int fd, uintmax_t n_elide_0) if (n_elide <= HEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD) { - int fail = 0; + bool ok = true; bool first = true; bool eof = false; size_t n_to_read = READ_BUFSIZE + n_elide; - unsigned int i; + bool i; char *b[2]; b[0] = xnmalloc (2, n_to_read); b[1] = b[0] + n_to_read; - for (i = 0; ! eof ; i = !i) + for (i = false; ! eof ; i = !i) { size_t n_read = full_read (fd, b[i], n_to_read); size_t delta = 0; @@ -266,7 +266,7 @@ elide_tail_bytes_pipe (const char *filename, int fd, uintmax_t n_elide_0) if (errno != 0) { error (0, errno, _("error reading %s"), quote (filename)); - fail = 1; + ok = false; break; } @@ -302,20 +302,20 @@ elide_tail_bytes_pipe (const char *filename, int fd, uintmax_t n_elide_0) && fwrite (b[i], 1, n_read - n_elide, stdout) < n_read - n_elide) { error (0, errno, _("write error")); - fail = 1; + ok = false; break; } } free (b[0]); - return fail; + return ok; } else { /* Read blocks of size READ_BUFSIZE, until we've read at least n_elide bytes. Then, for each new buffer we read, also write an old one. */ - int fail = 0; + bool ok = true; bool eof = false; size_t n_read; bool buffered_enough; @@ -338,7 +338,7 @@ elide_tail_bytes_pipe (const char *filename, int fd, uintmax_t n_elide_0) if (errno != 0) { error (0, errno, _("error reading %s"), quote (filename)); - fail = 1; + ok = false; goto free_mem; } eof = true; @@ -352,7 +352,7 @@ elide_tail_bytes_pipe (const char *filename, int fd, uintmax_t n_elide_0) if (fwrite (b[i_next], 1, n_read, stdout) < n_read) { error (0, errno, _("write error")); - fail = 1; + ok = false; goto free_mem; } } @@ -400,18 +400,18 @@ elide_tail_bytes_pipe (const char *filename, int fd, uintmax_t n_elide_0) free (b[i]); free (b); - return fail; + return ok; } } /* Print all but the last N_ELIDE lines from the input available - via file descriptor FD. Return zero upon success. - Give a diagnostic and return nonzero upon error. */ + via file descriptor FD. Return true upon success. + Give a diagnostic and return false upon error. */ /* NOTE: if the input file shrinks by more than N_ELIDE bytes between the length determination and the actual reading, then head fails. */ -static int +static bool elide_tail_bytes_file (const char *filename, int fd, uintmax_t n_elide) { struct stat stats; @@ -435,7 +435,7 @@ elide_tail_bytes_file (const char *filename, int fd, uintmax_t n_elide) || (end_pos = lseek (fd, (off_t) 0, SEEK_END)) == -1) { error (0, errno, _("cannot lseek %s"), quote (filename)); - return 1; + return false; } /* Be careful here. The current position may actually be @@ -443,7 +443,7 @@ elide_tail_bytes_file (const char *filename, int fd, uintmax_t n_elide) bytes_remaining = (diff = end_pos - current_pos) < 0 ? 0 : diff; if (bytes_remaining <= n_elide) - return 0; + return true; /* Seek back to `current' position, then copy the required number of bytes from fd. */ @@ -451,24 +451,24 @@ elide_tail_bytes_file (const char *filename, int fd, uintmax_t n_elide) { error (0, errno, _("%s: cannot lseek back to original position"), quote (filename)); - return 1; + return false; } err = copy_fd (fd, stdout, bytes_remaining - n_elide); if (err == COPY_FD_OK) - return 0; + return true; diagnose_copy_fd_failure (err, filename); - return 1; + return false; } } /* Print all but the last N_ELIDE lines from the input stream open for reading via file descriptor FD. Buffer the specified number of lines as a linked list of LBUFFERs, - adding them as needed. Return 0 if successful, 1 upon error. */ + adding them as needed. Return true if successful. */ -static int +static bool elide_tail_lines_pipe (const char *filename, int fd, uintmax_t n_elide) { struct linebuffer @@ -481,7 +481,7 @@ elide_tail_lines_pipe (const char *filename, int fd, uintmax_t n_elide) typedef struct linebuffer LBUFFER; LBUFFER *first, *last, *tmp; size_t total_lines = 0; /* Total number of newlines in all buffers. */ - int errors = 0; + bool ok = true; size_t n_read; /* Size in bytes of most recent read */ first = last = xmalloc (sizeof (LBUFFER)); @@ -547,7 +547,7 @@ elide_tail_lines_pipe (const char *filename, int fd, uintmax_t n_elide) if (n_read == SAFE_READ_ERROR) { error (0, errno, _("error reading %s"), quote (filename)); - errors = 1; + ok = false; goto free_lbuffers; } @@ -587,7 +587,7 @@ free_lbuffers: free (first); first = tmp; } - return errors; + return ok; } /* Output all but the last N_LINES lines of the input stream defined by @@ -595,13 +595,13 @@ free_lbuffers: START_POS is the starting position of the read pointer for the file associated with FD (may be nonzero). END_POS is the file offset of EOF (one larger than offset of last byte). - Return zero upon success. - Give a diagnostic and return nonzero upon error. + Return true upon success. + Give a diagnostic and return false upon error. NOTE: this code is very similar to that of tail.c's file_lines function. Unfortunately, factoring out some common core looks like it'd result in a less efficient implementation or a messy interface. */ -static int +static bool elide_tail_lines_seekable (const char *pretty_filename, int fd, uintmax_t n_lines, off_t start_pos, off_t end_pos) @@ -623,13 +623,13 @@ elide_tail_lines_seekable (const char *pretty_filename, int fd, char offset_buf[INT_BUFSIZE_BOUND (off_t)]; error (0, errno, _("%s: cannot seek to offset %s"), pretty_filename, offtostr (pos, offset_buf)); - return 1; + return false; } bytes_read = safe_read (fd, buffer, bytes_read); if (bytes_read == SAFE_READ_ERROR) { error (0, errno, _("error reading %s"), quote (pretty_filename)); - return 1; + return false; } /* Count the incomplete line on files that don't end with a newline. */ @@ -662,14 +662,14 @@ elide_tail_lines_seekable (const char *pretty_filename, int fd, error (0, errno, "%s: unable to restore file pointer to initial offset", quote (pretty_filename)); - return 1; + return false; } err = copy_fd (fd, stdout, pos - start_pos); if (err != COPY_FD_OK) { diagnose_copy_fd_failure (err, pretty_filename); - return 1; + return false; } } @@ -678,7 +678,7 @@ elide_tail_lines_seekable (const char *pretty_filename, int fd, Don't bother testing for failure for such a small amount. Any failure will be detected upon close. */ fwrite (buffer, 1, n + 1, stdout); - return 0; + return true; } } @@ -686,7 +686,7 @@ elide_tail_lines_seekable (const char *pretty_filename, int fd, if (pos == start_pos) { /* Not enough lines in the file. */ - return 0; + return true; } pos -= BUFSIZ; if (lseek (fd, pos, SEEK_SET) < 0) @@ -694,28 +694,28 @@ elide_tail_lines_seekable (const char *pretty_filename, int fd, char offset_buf[INT_BUFSIZE_BOUND (off_t)]; error (0, errno, _("%s: cannot seek to offset %s"), pretty_filename, offtostr (pos, offset_buf)); - return 1; + return false; } bytes_read = safe_read (fd, buffer, BUFSIZ); if (bytes_read == SAFE_READ_ERROR) { error (0, errno, _("error reading %s"), quote (pretty_filename)); - return 1; + return false; } /* FIXME: is this dead code? Consider the test, pos == start_pos, above. */ if (bytes_read == 0) - return 0; + return true; } } /* Print all but the last N_ELIDE lines from the input available - via file descriptor FD. Return zero upon success. + via file descriptor FD. Return true upon success. Give a diagnostic and return nonzero upon error. */ -static int +static bool elide_tail_lines_file (const char *filename, int fd, uintmax_t n_elide) { /* We need binary input, since `head' relies on `lseek' and byte counts, @@ -727,7 +727,7 @@ elide_tail_lines_file (const char *filename, int fd, uintmax_t n_elide) /* Find the offset, OFF, of the Nth newline from the end, but not counting the last byte of the file. If found, write from current position to OFF, inclusive. - Otherwise, just return 0. */ + Otherwise, just return true. */ off_t start_pos = lseek (fd, (off_t) 0, SEEK_CUR); off_t end_pos = lseek (fd, (off_t) 0, SEEK_END); @@ -735,7 +735,7 @@ elide_tail_lines_file (const char *filename, int fd, uintmax_t n_elide) { /* If the file is empty, we're done. */ if (end_pos == 0) - return 0; + return true; return elide_tail_lines_seekable (filename, fd, n_elide, start_pos, end_pos); @@ -748,7 +748,7 @@ elide_tail_lines_file (const char *filename, int fd, uintmax_t n_elide) return elide_tail_lines_pipe (filename, fd, n_elide); } -static int +static bool head_bytes (const char *filename, int fd, uintmax_t bytes_to_write) { char buffer[BUFSIZ]; @@ -766,7 +766,7 @@ head_bytes (const char *filename, int fd, uintmax_t bytes_to_write) if (bytes_read == SAFE_READ_ERROR) { error (0, errno, _("error reading %s"), quote (filename)); - return 1; + return false; } if (bytes_read == 0) break; @@ -774,10 +774,10 @@ head_bytes (const char *filename, int fd, uintmax_t bytes_to_write) error (EXIT_FAILURE, errno, _("write error")); bytes_to_write -= bytes_read; } - return 0; + return true; } -static int +static bool head_lines (const char *filename, int fd, uintmax_t lines_to_write) { char buffer[BUFSIZ]; @@ -794,7 +794,7 @@ head_lines (const char *filename, int fd, uintmax_t lines_to_write) if (bytes_read == SAFE_READ_ERROR) { error (0, errno, _("error reading %s"), quote (filename)); - return 1; + return false; } if (bytes_read == 0) break; @@ -818,12 +818,12 @@ head_lines (const char *filename, int fd, uintmax_t lines_to_write) if (fwrite (buffer, 1, bytes_to_write, stdout) < bytes_to_write) error (EXIT_FAILURE, errno, _("write error")); } - return 0; + return true; } -static int -head (const char *filename, int fd, uintmax_t n_units, int count_lines, - int elide_from_end) +static bool +head (const char *filename, int fd, uintmax_t n_units, bool count_lines, + bool elide_from_end) { if (print_headers) write_header (filename); @@ -845,16 +845,16 @@ head (const char *filename, int fd, uintmax_t n_units, int count_lines, return head_bytes (filename, fd, n_units); } -static int -head_file (const char *filename, uintmax_t n_units, int count_lines, - int elide_from_end) +static bool +head_file (const char *filename, uintmax_t n_units, bool count_lines, + bool elide_from_end) { int fd; - int fail; + bool ok; if (STREQ (filename, "-")) { - have_read_stdin = 1; + have_read_stdin = true; fd = STDIN_FILENO; filename = _("standard input"); } @@ -864,17 +864,17 @@ head_file (const char *filename, uintmax_t n_units, int count_lines, if (fd < 0) { error (0, errno, _("cannot open %s for reading"), quote (filename)); - return 1; + return false; } } - fail = head (filename, fd, n_units, count_lines, elide_from_end); + ok = head (filename, fd, n_units, count_lines, elide_from_end); if (fd != STDIN_FILENO && close (fd) == -1) { error (0, errno, _("closing %s"), quote (filename)); - fail = 1; + return false; } - return fail; + return ok; } /* Convert a string of decimal digits, N_STRING, with a single, optional suffix @@ -884,7 +884,7 @@ head_file (const char *filename, uintmax_t n_units, int count_lines, of lines. It is used solely to give a more specific diagnostic. */ static uintmax_t -string_to_integer (int count_lines, const char *n_string) +string_to_integer (bool count_lines, const char *n_string) { strtol_error s_err; uintmax_t n; @@ -913,20 +913,20 @@ int main (int argc, char **argv) { enum header_mode header_mode = multiple_files; - int exit_status = 0; + bool ok = true; int c; size_t i; /* Number of items to print. */ uintmax_t n_units = DEFAULT_NUMBER; - /* If nonzero, interpret the numeric argument as the number of lines. + /* If true, interpret the numeric argument as the number of lines. Otherwise, interpret it as the number of bytes. */ - int count_lines = 1; + bool count_lines = true; /* Elide the specified number of lines or bytes, counting from the end of the file. */ - int elide_from_end = 0; + bool elide_from_end = false; /* Initializer for file_list if no file-arguments were specified on the command line. */ @@ -941,9 +941,9 @@ main (int argc, char **argv) atexit (close_stdout); - have_read_stdin = 0; + have_read_stdin = false; - print_headers = 0; + print_headers = false; if (1 < argc && argv[1][0] == '-' && ISDIGIT (argv[1][1])) { @@ -966,19 +966,19 @@ main (int argc, char **argv) switch (*a) { case 'c': - count_lines = 0; + count_lines = false; multiplier_char = 0; break; case 'b': case 'k': case 'm': - count_lines = 0; + count_lines = false; multiplier_char = *a; break; case 'l': - count_lines = 1; + count_lines = true; break; case 'q': @@ -997,9 +997,10 @@ main (int argc, char **argv) if (200112 <= posix2_version ()) { + int n_string_prefix_len = end_n_string - n_string; error (0, 0, _("`-%s' option is obsolete; use `-%c %.*s%.*s%s'"), n_string, count_lines ? 'n' : 'c', - (int) (end_n_string - n_string), n_string, + n_string_prefix_len, n_string, multiplier_char != 0, &multiplier_char, header_mode_option[header_mode]); usage (EXIT_FAILURE); @@ -1030,11 +1031,11 @@ main (int argc, char **argv) break; case PRESUME_INPUT_PIPE_OPTION: - presume_input_pipe = 1; + presume_input_pipe = true; break; case 'c': - count_lines = 0; + count_lines = false; elide_from_end = (*optarg == '-'); if (elide_from_end) ++optarg; @@ -1042,7 +1043,7 @@ main (int argc, char **argv) break; case 'n': - count_lines = 1; + count_lines = true; elide_from_end = (*optarg == '-'); if (elide_from_end) ++optarg; @@ -1068,7 +1069,7 @@ main (int argc, char **argv) if (header_mode == always || (header_mode == multiple_files && optind < argc - 1)) - print_headers = 1; + print_headers = true; if ( ! count_lines && elide_from_end && OFF_T_MAX < n_units) { @@ -1082,11 +1083,10 @@ main (int argc, char **argv) : default_file_list); for (i = 0; file_list[i]; ++i) - exit_status |= head_file (file_list[i], n_units, count_lines, - elide_from_end); + ok &= head_file (file_list[i], n_units, count_lines, elide_from_end); if (have_read_stdin && close (STDIN_FILENO) < 0) error (EXIT_FAILURE, errno, "-"); - exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE); + exit (ok ? EXIT_SUCCESS : EXIT_FAILURE); } |