home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / OS2MNX1.ZIP / REV.C < prev    next >
C/C++ Source or Header  |  1989-12-26  |  3KB  |  100 lines

  1. /* rev - reverse an ASCII line      Authors: Paul Polderman & Michiel Huisjes */
  2. /* $Header: D:/RCS/RCS/rev.c 1.1 89/12/10 06:47:29 RCA Exp $
  3.  * $Log:    rev.c $
  4.  * Revision 1.1  89/12/10  06:47:29  RCA
  5.  * Initial revision
  6.  * 
  7.  */
  8.  
  9. #include <sys/types.h>
  10. #include <fcntl.h>
  11. /* #include <blocksize.h> */
  12. #define BLOCK_SIZE 80  /* RCA */
  13.  
  14. #ifndef EOF
  15. #define    EOF    ((char) -1)
  16. #endif
  17. #define std_err printf  /* RCA */
  18.  
  19. int fd;                /* File descriptor from file being read */
  20.  
  21. main(argc, argv)
  22. int argc;
  23. char *argv[];
  24. {
  25.   register unsigned short i;
  26.  
  27. /*  if (argc == 1) {              \\ If no arguments given, use stdin as input \\
  28.  *        fd = 0;
  29.  *       rev();
  30.  *       exit(0);
  31.  * }
  32.  */
  33.      if (argv[1][0] == '-')           /* RCA Use stdin */
  34.     {
  35.        fd = 0;
  36.        rev();
  37.        exit(0);
  38.  
  39.      }
  40.      if (argc == 1)
  41.      {
  42.         printf("█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█\n");
  43.         printf("█ REV (reverse ASCII input)                 $Author: RCA $ █\n");
  44.         printf("█          $Date: 89/12/10 06:47:29 $     $Revision: 1.1 $ █\n");
  45.         printf("█ Usage:   REV [inputfiles] [-]                            █\n");
  46.         printf("█ Purpose: Reverse the spelling of input file.  Uses:      █\n");
  47.         printf("█          Projection TV? Hebrew?                          █\n");
  48.         printf("█ Options: [inputfiles] Reverse the contents of file(s).   █\n");
  49.         printf("█          [-] Use standard input (^Z ends input).         █\n");
  50.         printf("█ OS:      DOS or OS/2                                     █\n");
  51.         printf("█ Credits: Paul Polderman and Michiel Huisjes              █\n");
  52.         printf("█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█\n");
  53.  
  54.      }
  55.      for (i = 1; i < argc; i++) {  /* Reverse each line in arguments */
  56.     if ((fd = open(argv[i], O_RDONLY)) < 0) {
  57.         std_err("Cannot open ");
  58.         std_err(argv[i]);
  59.         std_err("\n");
  60.         continue;
  61.     }
  62.     rev();
  63.     close(fd);
  64.   }
  65.   exit(0);
  66. }
  67.  
  68.  
  69.  
  70.  
  71. rev()
  72. {
  73.   char output[BLOCK_SIZE];    /* Contains a reversed line */
  74.   register unsigned short i;    /* Index in output array */
  75.  
  76.   do {
  77.     i = BLOCK_SIZE - 1;
  78.     while ((output[i] = nextchar()) != '\n' && output[i] != EOF) i--;
  79.     write(1, &output[i + 1], BLOCK_SIZE - 1 - i); /* write reversed line */
  80.     if (output[i] == '\n')    /* and write a '\n' */
  81.         write(1, "\n", 1);
  82.   } while (output[i] != EOF);
  83. }
  84.  
  85.  
  86.  
  87.  
  88. char buf[BLOCK_SIZE];
  89. nextchar()
  90. {                /* Does a sort of buffered I/O */
  91.   static int n = 0;        /* Read count */
  92.   static int i;            /* Index in input buffer to next character */
  93.  
  94.   if (--n <= 0) {        /* We've had this block. Read in next block */
  95.     n = read(fd, buf, BLOCK_SIZE);
  96.     i = 0;            /* Reset index in array */
  97.   }
  98.   return((n <= 0) ? EOF : buf[i++]);    /* Return -1 on EOF */
  99. }
  100.