home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / slackwar / a / util / util-lin.2 / util-lin / util-linux-2.2 / text-utils / rev.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-22  |  4.1 KB  |  160 lines

  1. /*-
  2.  * Copyright (c) 1987, 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  *
  33.  * Modified for Linux by Charles Hannum (mycroft@gnu.ai.mit.edu)
  34.  *                   and Brian Koehmstedt (bpk@gnu.ai.mit.edu)
  35.  *
  36.  * Wed Sep 14 22:26:00 1994: Patch from bjdouma <bjdouma@xs4all.nl> to handle
  37.  *                           last line that has no newline correctly.
  38.  */
  39.  
  40. #ifndef lint
  41. char copyright[] =
  42. "@(#) Copyright (c) 1987, 1992 The Regents of the University of California.\n\
  43.  All rights reserved.\n";
  44. #endif /* not lint */
  45.  
  46. #ifndef lint
  47. /*static char sccsid[] = "from: @(#)rev.c    5.2 (Berkeley) 3/21/92";*/
  48. static char rcsid[] = "rev.c,v 1.1.1.1 1995/02/22 19:09:19 faith Exp";
  49. #endif /* not lint */
  50.  
  51. #include <sys/types.h>
  52. #include <errno.h>
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #ifdef linux
  57. #include <unistd.h>
  58. #endif /* linux */
  59.  
  60. void usage __P((void));
  61. void warn __P((const char *, ...));
  62.  
  63. int
  64. main(argc, argv)
  65.     int argc;
  66.     char *argv[];
  67. {
  68.     register char *filename, *t;
  69. #ifdef linux
  70.     char p[512];
  71. #else /* linux */
  72.     char *p;
  73. #endif /* linux */
  74.     FILE *fp;
  75.     size_t len;
  76.     int ch, rval;
  77.  
  78.     while ((ch = getopt(argc, argv, "")) != EOF)
  79.         switch(ch) {
  80.         case '?':
  81.         default:
  82.             usage();
  83.         }
  84.  
  85.     argc -= optind;
  86.     argv += optind;
  87.  
  88.     fp = stdin;
  89.     filename = "stdin";
  90.     rval = 0;
  91.     do {
  92.         if (*argv) {
  93.             if ((fp = fopen(*argv, "r")) == NULL) {
  94.                 warn("%s: %s", *argv, strerror(errno));
  95.                 rval = 1;
  96.                 ++argv;
  97.                 continue;
  98.             }
  99.             filename = *argv++;
  100.         }
  101. #ifndef linux
  102.         while (p = fgetline(fp, &len)) {
  103.             t = p + len - 1;
  104.             for (t = p + len - 1; t >= p; --t)
  105.                 putchar(*t);
  106.         }
  107. #else /* linux */
  108.         while (fgets(p, 511, fp)) {
  109.             len = strlen(p);
  110.                         t = p + len - 1 - (*(p+len-1)=='\r'
  111.                        || *(p+len-1)=='\n');
  112.                         for ( ; t >= p; --t)
  113.                 if(strcmp(t, '\0') != 0)
  114.                   putchar(*t);
  115. #endif /* linux */
  116.             putchar('\n');
  117.         }
  118.         if (ferror(fp)) {
  119.             warn("%s: %s", filename, strerror(errno));
  120.             rval = 1;
  121.         }
  122.         (void)fclose(fp);
  123.     } while(*argv);
  124.     exit(rval);
  125. }
  126.  
  127. #if __STDC__
  128. #include <stdarg.h>
  129. #else
  130. #include <varargs.h>
  131. #endif
  132.  
  133. void
  134. #if __STDC__
  135. warn(const char *fmt, ...)
  136. #else
  137. warn(fmt, va_alist)
  138.     char *fmt;
  139.         va_dcl
  140. #endif
  141. {
  142.     va_list ap;
  143. #if __STDC__
  144.     va_start(ap, fmt);
  145. #else
  146.     va_start(ap);
  147. #endif
  148.     (void)fprintf(stderr, "rev: ");
  149.     (void)vfprintf(stderr, fmt, ap);
  150.     va_end(ap);
  151.     (void)fprintf(stderr, "\n");
  152. }
  153.  
  154. void
  155. usage()
  156. {
  157.     (void)fprintf(stderr, "usage: rev [file ...]\n");
  158.     exit(1);
  159. }
  160.