From b388203a65251b73dba040148039914062685c88 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Sat, 11 Apr 1998 18:24:09 +0000 Subject: [PATCH] Include safe-read.h instead of merely declaring safe_read. --- src/csplit.c | 3 +- src/head.c | 5 ++-- src/split.c | 2 +- src/sum.c | 5 ++-- src/tac.c | 89 ++++++++++++++++++++++++------------------------------------ src/tr.c | 3 +- 6 files changed, 43 insertions(+), 64 deletions(-) diff --git a/src/csplit.c b/src/csplit.c index 691ec38..d56ab74 100644 --- a/src/csplit.c +++ b/src/csplit.c @@ -41,6 +41,7 @@ #include "error.h" #include "xstrtoul.h" #include "xalloc.h" +#include "safe-read.h" #ifdef STDC_HEADERS # include @@ -126,8 +127,6 @@ struct buffer_record struct buffer_record *next; }; -int safe_read (); - static void close_output_file PARAMS ((void)); static void create_output_file PARAMS ((void)); static void delete_all_files PARAMS ((void)); diff --git a/src/head.c b/src/head.c index d7df731..42f76e4 100644 --- a/src/head.c +++ b/src/head.c @@ -1,5 +1,5 @@ /* head -- output first part of file(s) - Copyright (C) 89, 90, 91, 95, 96, 1997 Free Software Foundation, Inc. + Copyright (C) 89, 90, 91, 95, 96, 1997, 1998 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 @@ -32,6 +32,7 @@ #include "system.h" #include "error.h" #include "xstrtoul.h" +#include "safe-read.h" /* FIXME: someday, make this really *be* `long long'. */ typedef long int U_LONG_LONG; @@ -51,8 +52,6 @@ enum header_mode multiple_files, always, never }; -int safe_read (); - /* The name this program was run with. */ char *program_name; diff --git a/src/split.c b/src/split.c index 2baa579..bce8b8d 100644 --- a/src/split.c +++ b/src/split.c @@ -30,9 +30,9 @@ #include "system.h" #include "error.h" #include "xstrtol.h" +#include "safe-read.h" int full_write (); -int safe_read (); /* The name this program was run with. */ char *program_name; diff --git a/src/sum.c b/src/sum.c index eddb80e..0e0fdeb 100644 --- a/src/sum.c +++ b/src/sum.c @@ -1,5 +1,5 @@ /* sum -- checksum and count the blocks in a file - Copyright (C) 86, 89, 91, 95, 96, 1997 Free Software Foundation, Inc. + Copyright (C) 86, 89, 91, 95, 96, 1997, 1998 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 @@ -26,8 +26,7 @@ #include #include "system.h" #include "error.h" - -int safe_read (); +#include "safe-read.h" /* The name this program was run with. */ char *program_name; diff --git a/src/tac.c b/src/tac.c index fcd8e54..f281c45 100644 --- a/src/tac.c +++ b/src/tac.c @@ -1,5 +1,5 @@ /* tac - concatenate and print files in reverse - Copyright (C) 88, 89, 90, 91, 95, 96, 1997, 1998 Free Software Foundation, Inc. + Copyright (C) 88,89,90,91,95,96,97, 1998 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 @@ -49,6 +49,7 @@ tac -r -s '.\| #endif #include "error.h" +#include "safe-read.h" #ifndef DEFAULT_TMPDIR # define DEFAULT_TMPDIR "/tmp" @@ -61,7 +62,6 @@ tac -r -s '.\| #define WRITESIZE 8192 char *mktemp (); -int safe_read (); /* The name this program was run with. */ char *program_name; @@ -113,31 +113,6 @@ static struct option const longopts[] = {NULL, 0, NULL, 0} }; -/* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted. - Return the actual number of bytes read, zero for EOF, or negative - for an error. */ - -int -safe_read (int desc, char *ptr, int len) -{ - int n_chars; - - if (len <= 0) - return len; - -#ifdef EINTR - do - { - n_chars = read (desc, ptr, len); - } - while (n_chars < 0 && errno == EINTR); -#else - n_chars = read (desc, ptr, len); -#endif - - return n_chars; -} - static void usage (int status) { @@ -492,45 +467,53 @@ memrchr (const char *buf_start, const char *buf_end_plus_one, int c) return NULL; } +/* FIXME: describe */ + static int tac_mem (const char *buf, size_t n_bytes, FILE *out) { + const char *nl; + const char *bol; + if (n_bytes == 0) return 0; - { - const char *nl = memrchr (buf, buf + n_bytes, '\n'); - const char *bol = (nl == NULL ? buf : nl + 1); + nl = memrchr (buf, buf + n_bytes, '\n'); + bol = (nl == NULL ? buf : nl + 1); - /* If the last line of the input file has no terminating newline, - treat it as a special case. */ - if (bol < buf + n_bytes) - { - /* Print out the line from bol to end of input. */ - fwrite (bol, 1, (buf + n_bytes) - bol, out); + /* If the last line of the input file has no terminating newline, + treat it as a special case. */ + if (bol < buf + n_bytes) + { + /* Print out the line from bol to end of input. */ + fwrite (bol, 1, (buf + n_bytes) - bol, out); - /* Add a newline here. Otherwise, the first and second lines - of output would appear to have been joined. */ - fputc ('\n', out); - } + /* Add a newline here. Otherwise, the first and second lines + of output would appear to have been joined. */ + fputc ('\n', out); + } - while ((nl = memrchr (buf, bol - 1, '\n')) != NULL) - { - /* Output the line (which includes a trailing newline) - from NL+1 to BOL-1. */ - fwrite (nl + 1, 1, bol - (nl + 1), out); + while ((nl = memrchr (buf, bol - 1, '\n')) != NULL) + { + /* Output the line (which includes a trailing newline) + from NL+1 to BOL-1. */ + fwrite (nl + 1, 1, bol - (nl + 1), out); - bol = nl + 1; - } + bol = nl + 1; + } + + /* If there's anything left, output the last line: BUF .. BOL-1. + When the first byte of the input is a newline, there is nothing + left to do here. */ + if (buf < bol) + fwrite (buf, 1, bol - buf, out); - /* If there's anything left, output the last line: BUF .. BOL-1. - When the first byte of the input is a newline, there is nothing - left to do here. */ - if (buf < bol) - fwrite (buf, 1, bol - buf, out); - } + /* FIXME: this is work in progress.... */ + return ferror (out); } +/* FIXME: describe */ + static int tac_stdin_to_mem (void) { diff --git a/src/tr.c b/src/tr.c index 14f8343..68ba44c 100644 --- a/src/tr.c +++ b/src/tr.c @@ -31,6 +31,7 @@ #include "system.h" #include "error.h" +#include "safe-read.h" #define N_CHARS (UCHAR_MAX + 1) @@ -205,8 +206,6 @@ struct E_string and is not escaped itself. */ #define ES_MATCH(ES, I, C) ((ES)->s[(I)] == (C) && !(ES)->escaped[(I)]) -int safe_read (); - /* The name by which this program was run. */ char *program_name; -- 2.7.4