home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 329_01 / cat.c < prev    next >
C/C++ Source or Header  |  1988-12-16  |  2KB  |  108 lines

  1. /*-
  2.  * cat - concatenate files
  3.  * Usage: cat [-] [file...]
  4.  *
  5.  * Contents of argument files are written to standard output.
  6.  * If there are no arguments, or if a file named "-" is encountered,
  7.  * standard input is written to standard output.
  8.  * Exit status is number of files that couldn't be opened or had read errors.
  9.  *
  10.  * This program is in the public domain.
  11.  * David MacKenzie
  12.  * 6522 Elgin Lane
  13.  * Bethesda, MD 20817
  14.  *
  15.  * Latest revision: 05/08/88
  16. **
  17. **    Roberto Artigas Jr
  18. **    P.O. Box 281415
  19. **    Memphis, TN 38168-1415
  20. **    home: 901-762-6092
  21. **    work: 901-373-4738
  22. **
  23. **    1988.12.09 - Get to run under OS/2.
  24. **        Used C/2 version 1.10 under OS/2 E 1.1
  25. **        cl -c -AL cat.c
  26. **        link cat,/noi,cat,llibce+os2, ;
  27. **+
  28. */
  29. #define    CMP    0
  30. #define    DOS    0
  31. #define    OS2    1
  32. #include    <stdio.h>
  33.  
  34. #if    DOS || OS2
  35. #include    <stdlib.h>
  36. #include    <string.h>
  37. #include    <io.h>
  38. #define    agetc    getc
  39. #define    aputc    putc
  40. #endif
  41.  
  42. /*
  43.  * Function prototypes 
  44.  */
  45. int cat();
  46.  
  47. int 
  48. main(argc, argv)
  49. int argc;
  50. char **argv;
  51. {
  52.     int errs = 0;               /* # of files with read errors */
  53.     int optind;                   /* loop index */
  54.  
  55.     if (argc == 1)
  56.     errs += cat("-");
  57.     else
  58.     for (optind = 1; optind < argc; ++optind)
  59.         errs += cat(argv[optind]);
  60.  
  61.     return (0);
  62.     exit(errs);
  63. }
  64. /*
  65.  * Send the contents of file to standard output with no translation. If file
  66.  * is "-", use standard input, and if input is from con:, do newline and ^Z
  67.  * translation. Return 0 if ok, 1 if error. 
  68.  */
  69.  
  70. int 
  71. cat(file)
  72. char *file;
  73. {
  74.     FILE *fp;                   /* input file pointer */
  75.     int c;                   /* one byte of input */
  76.  
  77.     if (!strcmp(file, "-"))
  78.     {
  79.     /* Agetc and aputc translate cr-lf to lf and ^Z to EOF. */
  80.     if (isatty(0))
  81.         while ((c = agetc(stdin)) != EOF)
  82.         aputc(c, stdout);
  83.     else
  84.         while ((c = getc(stdin)) != EOF)
  85.         putc(c, stdout);
  86.     } else
  87.     {
  88. #if    DOS || OS2
  89.     if ((fp = fopen(file, "rb")) == NULL)
  90.     {
  91. #else
  92.     if ((fp = fopen(file, "r")) == NULL)
  93.     {
  94. #endif
  95.         perror(file);
  96.         return 1;
  97.     }
  98.     while ((c = getc(fp)) != EOF)
  99.         putc(c, stdout);
  100.     if (fclose(fp) == EOF)
  101.     {
  102.         fprintf(stderr, "cat: %s: Read error\n", file);
  103.         return 1;
  104.     }
  105.     }
  106.     return 0;
  107. }
  108.