home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / tools / append.c next >
Encoding:
C/C++ Source or Header  |  1992-02-15  |  1.8 KB  |  87 lines

  1. /*    Copyright (c)    1990    Jin, Guojun
  2.  
  3. %    APPEND .C
  4. %    AUTHOR    Jin Guojun    Oct. 30, 1990
  5. %
  6. % important note:
  7. %    APPEND can pipe in but can not pipe out. i.e., output
  8. %    must spscity a file_name. It can not be a ">" or "|" symbol.
  9. */
  10.  
  11. #include <stdio.h>
  12. #ifndef    BufSize
  13. #define    BufSize    40960
  14. #endif
  15. #define    not_string(n, s)    strcmp(argv[n], s)
  16. #define    is_string(n, s)        !not_string(n, s)
  17.  
  18. main(argc, argv)
  19. int    argc;
  20. char*    argv[];
  21. {
  22.  
  23. FILE    *in_fp=stdin, *out_fp;
  24. char    buf[BufSize], *ifname=0;
  25. register unsigned    i=2, j;
  26.  
  27. if (argc < i || is_string(1, "-h"))
  28. usg:
  29.    {    fprintf(stderr, "Usage: %s outfile -r repeat -s string\n", *argv);
  30.     fprintf(stderr, "    %s outfile [< | -f source_file] [-t title]\n", *argv);
  31.     fprintf(stderr, "    %s -h\n", *argv);
  32.     exit(1);
  33.    }
  34.  
  35. if ((out_fp = fopen(argv[1], "ab")) == NULL)
  36.    {    perror(argv[1]);    exit(2);    }
  37.  
  38. if (*argv[i] != '-')
  39.     ifname = argv[i];
  40. if (ifname){
  41.     if ((in_fp=freopen(ifname, "rb", stdin)) == 0){
  42.         perror(ifname);
  43.         exit(3);
  44.     }
  45.     goto    apnd;    /*    case 3    */
  46. }
  47.  
  48. if (not_string(i, "-r"))    {    /* not case 1    */
  49.    if (not_string(i, "-f"))    goto    if_ttl;
  50.    {
  51.     in_fp = fopen(argv[++i], "rb");    /*    case 2    */
  52.     if (!in_fp)
  53.         {
  54.         fprintf(stderr, "can open file %s for input\n", argv[3]);
  55.         exit(-1);
  56.         }
  57.     i++;
  58. if_ttl:    if (is_string(i, "-t") && argc > ++i)    {
  59.         strcpy(buf, "\nTitle: ");
  60.         strcat(buf, argv[i]);
  61.         fwrite(buf, strlen(buf), 1, out_fp);
  62.     }
  63.     fprintf(out_fp, "\n");
  64. apnd:    do{
  65.         i = fread(buf, 1, BufSize, in_fp);
  66.         j = fwrite(buf, 1, i, out_fp);
  67.         if (j != i){
  68.             perror("wrtie");
  69.             exit(5);
  70.         }
  71.     }while (!feof(in_fp) && i == BufSize);
  72.    }
  73. } else    if (not_string(4, "-s"))    goto    usg;
  74.     else {
  75.         j = strlen(argv[5]);        /*    case 2    */
  76.         for (i = 0; i < j; i++)
  77.             *(buf + i) = argv[5][i] - '0';
  78.         *(buf + i) = 0;
  79.     /*    fprintf(stderr, "tst: %s %s\n", argv[5], buf);    */
  80.  
  81.         for (i = atoi(argv[3]); i--;)
  82.             fwrite(buf, j, 1, out_fp);
  83.     }
  84. fclose(out_fp);
  85. exit(0);
  86. }
  87.