diff options
author | Jim Meyering <jim@meyering.net> | 1999-01-25 05:50:56 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1999-01-25 05:50:56 +0000 |
commit | becdfba8b99844ae94c4bf5958c3a511df708e4b (patch) | |
tree | 96bd03930b7bf341e28fb940c0230bccdaeea0a1 /src | |
parent | cb6a8b722b3e55caaadcf1c6068f9208949d3ffc (diff) | |
download | coreutils-becdfba8b99844ae94c4bf5958c3a511df708e4b.tar.xz |
New version from L. Peter Deutsch.
Diffstat (limited to 'src')
-rw-r--r-- | src/ansi2knr.c | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/src/ansi2knr.c b/src/ansi2knr.c index 442a96b2f..09244fba5 100644 --- a/src/ansi2knr.c +++ b/src/ansi2knr.c @@ -1,6 +1,6 @@ -/* Copyright (C) 1989, 1997, 1998 Aladdin Enterprises. All rights reserved. */ +/* Copyright (C) 1989, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved. */ -/*$Id: ansi2knr.c,v 1.10 1999/01/25 00:34:26 meyering Exp $*/ +/*$Id: ansi2knr.c,v 1.11 1999/01/25 05:50:56 meyering Exp $*/ /* Convert ANSI C function definitions to K&R ("traditional C") syntax */ /* @@ -51,13 +51,15 @@ program under the GPL. * The following constructs will confuse it: * - Any other construct that starts at the left margin and * follows the above syntax (such as a macro or function call). - * - Some macros that tinker with the syntax of the function header. + * - Some macros that tinker with the syntax of function headers. */ /* * The original and principal author of ansi2knr is L. Peter Deutsch * <ghost@aladdin.com>. Other authors are noted in the change history * that follows (in reverse chronological order): + lpd 1999-01-24 added a check for write errors on the output, + suggested by Jim Meyering <meyering@ascend.com> lpd 1998-11-09 added further hack to recognize identifier(void) as being a procedure lpd 1998-10-23 added hack to recognize lines consisting of @@ -191,6 +193,7 @@ main(argc, argv) FILE *out = stdout; char *filename = 0; char *program_name = argv[0]; + char *output_name = 0; #define bufsize 5000 /* arbitrary size */ char *buf; char *line; @@ -206,6 +209,7 @@ main(argc, argv) * check for this switch for backward compatibility. */ int convert_varargs = 1; + int output_error; while ( argc > 1 && argv[1][0] == '-' ) { if ( !strcmp(argv[1], "--varargs") ) { @@ -220,7 +224,8 @@ main(argc, argv) argv += 2; continue; } - fprintf(stderr, "Unrecognized switch: %s\n", argv[1]); + fprintf(stderr, "%s: Unrecognized switch: %s\n", program_name, + argv[1]); fprintf(stderr, usage); exit(1); } @@ -230,16 +235,19 @@ main(argc, argv) fprintf(stderr, usage); exit(0); case 3: - out = fopen(argv[2], "w"); + output_name = argv[2]; + out = fopen(output_name, "w"); if ( out == NULL ) { - fprintf(stderr, "Cannot open output file %s\n", argv[2]); + fprintf(stderr, "%s: Cannot open output file %s\n", + program_name, output_name); exit(1); } /* falls through */ case 2: in = fopen(argv[1], "r"); if ( in == NULL ) { - fprintf(stderr, "Cannot open input file %s\n", argv[1]); + fprintf(stderr, "%s: Cannot open input file %s\n", + program_name, argv[1]); exit(1); } if ( filename == 0 ) @@ -301,13 +309,18 @@ wl: fputs(buf, out); if ( line != buf ) fputs(buf, out); free(buf); - - if (ferror (out) || fclose (out) != 0) - { - fprintf (stderr, "%s: write error\n", program_name); - exit (1); - } - + if ( output_name ) { + output_error = ferror(out); + output_error |= fclose(out); + } else { /* out == stdout */ + fflush(out); + output_error = ferror(out); + } + if ( output_error ) { + fprintf(stderr, "%s: error writing to %s\n", program_name, + (output_name ? output_name : "stdout")); + exit(1); + } if ( in != stdin ) fclose(in); return 0; |