home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FORK.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  6KB  |  236 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** fork.c (fork.h) - fork output to multiple file(s).
  5. **
  6. ** By Dustin Puryear <dpuryear@delphi.com>
  7. ** Placed in the Public Domain.
  8. */
  9.  
  10. #include "fork.h"
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.       LL  files;     /* Linked-list of file pointers */
  15.       int i;
  16.  
  17.       if ( argc < 2 )
  18.       {
  19.             help();
  20.             exit(EXIT_FAILURE);
  21.       }
  22.  
  23.       files.head = files.tail = NULL;
  24.  
  25.       /*
  26.       ** First of all, find out if the user request help or if
  27.       ** he disabled the console.
  28.       */
  29.  
  30.       {
  31.             int x;   /* Use as console toggle */
  32.  
  33.             for ( x = 0, i = 1; i < argc; i++ )
  34.                   if ( argv[i][0] == '/' )
  35.                         switch ( argv[i][1] )
  36.                         {
  37.                               case '?' :
  38.                                     help();
  39.                                     exit(EXIT_FAILURE);
  40.                               case 'C' :
  41.                               case 'c' :
  42.                                     x = 1;
  43.                                     break;
  44.                         }
  45.  
  46.             /*
  47.             ** x not toggled, therefore add stdout (console) to list.
  48.             */
  49.  
  50.             if ( !x )
  51.                   llopen(&files, stdout);
  52.             else
  53.             {
  54.                   puts("Output to files only ...");
  55.                   fflush(stdout);
  56.             }
  57.  
  58.       }
  59.  
  60.       /*
  61.       ** Go through command line again. Now look to open files. If
  62.       ** you cannot open a file, ignore it!
  63.       */
  64.  
  65.       {
  66.             FILE  *fptr;               /* current file opened         */
  67.  
  68.             for ( i = 1; i < argc; i++ )
  69.                   if ( argv[i][0] != '/' )
  70.                   {
  71.                         switch ( argv[i][0] )
  72.                         {
  73.                               case '*' :    /* Overwrite mode         */
  74.                                     fptr = fopen(argv[i] + 1, "wb");
  75.                                     break;
  76.                               default  :    /* Append mode            */
  77.                                     fptr = fopen(argv[i], "ab");
  78.                                     break;
  79.                         }
  80.                         if ( fptr != NULL )
  81.                               llopen(&files, fptr);
  82.                   }
  83.  
  84.       }
  85.  
  86.       /*
  87.       ** Get characters from stdin and place into a buffer. When buffer
  88.       ** becomes full, write it out to all files in list.
  89.       */
  90.  
  91.       {
  92.             char  buffer[MAXBUFF]; /* buffer of chars      */
  93.             int   i;               /* buffer index         */
  94.             int   ch;              /* character from stdin */
  95.  
  96.             i = 0;
  97.             while ( (ch = getchar()) != EOF )
  98.             {
  99.                   buffer[i++] = (char) ch;
  100.                   if ( i == MAXBUFF )
  101.                   {
  102.                         output(&files, buffer, i - 1);
  103.                         i = 0;
  104.                   }
  105.             }
  106.  
  107.             /*
  108.             ** If characters read, but have not been written out,
  109.             ** write them now.
  110.             */
  111.  
  112.             if ( i != 0 && i < MAXBUFF )
  113.                   output(&files, buffer, i - 1);
  114.       }
  115.  
  116.       /*
  117.       ** Close all files and remove list.
  118.       */
  119.  
  120.       llfree(&files);
  121.  
  122.       return (EXIT_SUCCESS);
  123. }
  124.  
  125. /*
  126. ** llopen()
  127. **
  128. ** Place a new node at the end of the ll, containing the file pointer.
  129. */
  130.  
  131. void llopen(LL *files, FILE *fptr)
  132. {
  133.       FILENODE *pnode;
  134.  
  135.       pnode = (FILENODE *) malloc(sizeof(FILENODE));
  136.       if ( pnode == NULL )
  137.       {
  138.             perror("FORK ");
  139.             llfree(files);
  140.             exit(EXIT_FAILURE);
  141.       }
  142.       pnode->ptr = fptr;
  143.       pnode->next = NULL;
  144.  
  145.       /*
  146.       ** If a ll does not exist, start a new one. Otherwise, continue
  147.       ** with the old.
  148.       */
  149.  
  150.       if ( files->head == NULL )
  151.             files->head = files->tail = pnode;
  152.       else
  153.       {
  154.             files->tail->next = pnode;
  155.             files->tail = pnode;
  156.       }
  157. }
  158.  
  159. /*
  160. ** llfree()
  161. **
  162. ** Free the memory consumed by the linked-list and closes any open files.
  163. */
  164.  
  165. void llfree(LL *files)
  166. {
  167.       FILENODE *del;
  168.  
  169.       while ( files->head != NULL )
  170.       {
  171.             fclose(files->head->ptr);
  172.             del = files->head;
  173.             files->head = files->head->next;
  174.             free(del);
  175.       }
  176. }
  177.  
  178. /*
  179. ** output()
  180. **
  181. ** Output buffer to the file within the structure.
  182. */
  183.  
  184. void output(LL *files, char *buffer, int size)
  185. {
  186.       int      i;
  187.       FILENODE *current;
  188.  
  189.       /*
  190.       ** If console is still active, output to it first.
  191.       */
  192.  
  193.       current = files->head;
  194.       if ( current->ptr == stdout )
  195.       {
  196.             for ( i = 0; i <= size; i++ )
  197.                   putchar(buffer[i]);
  198.             fflush(stdout);
  199.             current = current->next;
  200.       }
  201.  
  202.       while ( current != NULL )
  203.       {
  204.             fwrite(buffer, sizeof(char) * size, 1, current->ptr);
  205.             current = current->next;
  206.       }
  207. }
  208.  
  209. /*
  210. ** help()
  211. **
  212. ** Show a help screen to the user.
  213. */
  214.  
  215. void help(void)
  216. {
  217.       putchar('\n');
  218.       printf("Fork - (pd) %s v%s Dustin Puryear\n", __DATE__, VER);
  219.       puts("This program is in Public Domain.");
  220.       putchar('\n');
  221.       puts("Fork piped output to the console and/or file(s).");
  222.       putchar('\n');
  223.       puts("[command |] fork [/?][/C] [filename.ext *filename.ext ...]");
  224.       putchar('\n');
  225.       puts("command       - Command that you wish to fork output.");
  226.       puts("/?            - Call this screen.");
  227.       puts("/C            - Do not output to console.");
  228.       puts("filename.ext  - Fork output to this file (append).");
  229.       puts("*filename.ext - Fork output to this file (overwrite).");
  230.       putchar('\n');
  231.       puts("NOTE: FORK is case insensitive.");
  232.       putchar('\n');
  233.  
  234.       fflush(stdout);
  235. }
  236.