home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume4 / echo / echo.c < prev   
C/C++ Source or Header  |  1989-02-03  |  2KB  |  99 lines

  1. /* xc
  2. % cc -O echo.c -o echo
  3. % strip echo
  4.  */
  5. /*
  6.  * echo - echo arguments
  7.  * Usage: echo [-n] [string...]
  8.  *
  9.  * If the first argument is "-n", the trailing newline is omitted.
  10.  * The "-n" is not echoed.
  11.  *
  12.  * Recognizes System V escape sequences:
  13.  * \0ooo    octal ASCII character (1-3 digits)
  14.  * \b        backspace
  15.  * \c        omit trailing newline; don't print anything more (same as -n)
  16.  * \f        form feed
  17.  * \n        newline (line feed)
  18.  * \r        carriage return
  19.  * \t        horizontal tab
  20.  * \v        vertical tab
  21.  * \\        backslash
  22.  *
  23.  * David MacKenzie
  24.  * Latest revision: 08/07/88
  25.  */
  26.  
  27. #include <stdio.h>
  28.  
  29. main(argc, argv)
  30.     int             argc;
  31.     char          **argv;
  32. {
  33.     void            echo();
  34.     register int    optind;
  35.  
  36.     for (optind = 1; optind < argc; ++optind)
  37.     if (optind != 1 || strcmp(argv[optind], "-n")) {
  38.         echo(argv[optind]);
  39.         if (optind < argc - 1)
  40.         putchar(' ');
  41.     }
  42.     if (argc == 1 || strcmp(argv[1], "-n"))
  43.     putchar('\n');
  44.  
  45.     exit(0);
  46. }
  47.  
  48. void
  49. echo(s)
  50.     register char  *s;
  51. {
  52.     register int    i,        /* Digit counter for octal numbers. */
  53.                     n;        /* Value of octal numbers. */
  54.  
  55.     for (; *s; ++s) {
  56.     if (*s != '\\')
  57.         putchar(*s);
  58.     else
  59.         switch (*++s) {
  60.         case 0:
  61.         putchar('\\');
  62.         return;
  63.         case '\\':
  64.         putchar('\\');
  65.         break;
  66.         case '0':
  67.         for (i = n = 0, ++s; i < 3 && *s >= '0' && *s <= '7'; ++i, ++s)
  68.             n += *s - '0';
  69.         --s;
  70.         putchar(n);
  71.         break;
  72.         case 'b':
  73.         putchar(8);
  74.         break;
  75.         case 'c':
  76.         exit(0);
  77.         case 'f':
  78.         putchar(12);
  79.         break;
  80.         case 'n':
  81.         putchar('\n');
  82.         break;
  83.         case 'r':
  84.         putchar('\r');
  85.         break;
  86.         case 't':
  87.         putchar('\t');
  88.         break;
  89.         case 'v':
  90.         putchar(11);
  91.         break;
  92.         default:
  93.         putchar('\\');
  94.         putchar(*s);
  95.         break;
  96.         }
  97.     }
  98. }
  99.