From 4391e8e523b85946ece9242a0ae56ebca3b14c7a Mon Sep 17 00:00:00 2001 From: Keith Bostic Date: Sun, 13 Dec 1987 21:59:04 -0800 Subject: [PATCH] handle lines larger than 256 chars SCCS-vsn: usr.bin/rev/rev.c 4.3 --- usr/src/usr.bin/rev/rev.c | 76 ++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/usr/src/usr.bin/rev/rev.c b/usr/src/usr.bin/rev/rev.c index c0deb03219..ed8e52ff5a 100644 --- a/usr/src/usr.bin/rev/rev.c +++ b/usr/src/usr.bin/rev/rev.c @@ -1,46 +1,48 @@ -static char *sccsid = "@(#)rev.c 4.2 (Berkeley) %G%"; -#include +/* + * Copyright (c) 1987 Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that this notice is preserved and that due credit is given + * to the University of California at Berkeley. The name of the University + * may not be used to endorse or promote products derived from this + * software without specific prior written permission. This software + * is provided ``as is'' without express or implied warranty. + */ + +#ifndef lint +char copyright[] = +"@(#) Copyright (c) 1987 Regents of the University of California.\n\ + All rights reserved.\n"; +#endif /* not lint */ -/* reverse lines of a file */ +#ifndef lint +static char sccsid[] = "@(#)rev.c 4.3 (Berkeley) %G%"; +#endif /* not lint */ -#define N 256 -char line[N]; -FILE *input; +#include -main(argc,argv) -char **argv; +main(argc, argv) + int argc; + char **argv; { - register i,c; - input = stdin; + register char *t, *bp; + char buf[BUFSIZ]; + + bp = buf; do { - if(argc>1) { - if((input=fopen(argv[1],"r"))==NULL) { - fprintf(stderr,"rev: cannot open %s\n", - argv[1]); - exit(1); - } + if (argc > 1 && !freopen(*++argv, "r", stdin)) { + fprintf(stderr, "rev: cannot open %s.\n", *argv); + exit(1); } - for(;;){ - for(i=0;i=0) - putc(line[i],stdout); - putc('\n',stdout); + while (fgets(bp, sizeof(buf), stdin)) { + for (t = bp; *t; ++t); + if (*--t == '\n') + --t; + for (; t >= bp; --t) + putchar(*t); + putchar('\n'); } -eof: - fclose(input); - argc--; - argv++; - } while(argc>1); + } while(--argc > 1); exit(0); } -- 2.20.1