home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume8 / mdcopy / part01 / unixcopy.c < prev   
Encoding:
C/C++ Source or Header  |  1989-11-02  |  3.6 KB  |  160 lines

  1. /*
  2.  * unixcopy    split a file into multiple parts
  3.  *
  4.  * This is a sample program used to illustrate the multi-disk i/o
  5.  * (mdio) package.  By setting a limit on the filesize, a large file
  6.  * can be split into multiple small files.  I know, I know.  This can
  7.  * already be done with split, but this is only a demonstration.  It
  8.  * can also reassemble the parts into a whole.  Again, this is only a
  9.  * demonstration.
  10.  *
  11.  * When writing a file, after a file is full the file is closed and
  12.  * the `output' prompt routine is called.  This is completely silent.
  13.  * As the next file is opened, the `filenm' function computes a new
  14.  * filename.  The initial file is named as specified.  Subsequent files
  15.  * have an extension created from the sequence number (.002, .003, etc).
  16.  *
  17.  * When reading a file, at EOF, the `at eof' function is called.  This
  18.  * looks for a file with same basename, and an extension of the next
  19.  * sequence number.  If found, it returns FALSE since we are not really
  20.  * at EOF.  If this file is NOT found then we are really at EOF, so TRUE
  21.  * is returned.
  22.  *
  23.  * Note:  using an extension can be ambiguous, but this is only a
  24.  *        sample program.
  25.  *
  26.  * March 1989    created by greg yachuk.  placed in the public domain.
  27.  */
  28. #include <stdio.h>
  29. #include <malloc.h>
  30. #include <string.h>
  31. #include <fcntl.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include "mdiskio.h"
  35.  
  36. /* these two guys are global for debugging purposes */
  37. MDFILE *ip, *op;
  38.  
  39. main(argc, argv)
  40. int    argc;
  41. char  **argv;
  42. {
  43.     char    buf[BUFSIZ];
  44.     int    len;
  45.     int    ateof();
  46.     char   *filenm();
  47.     int    prompt();
  48.  
  49.     if (argc != 3)
  50.     {
  51.         puts("usage: unixcopy srcfile dstfile");
  52.         exit(1);
  53.     }
  54.  
  55.     /* filename, READ, no filename function, sequence starts at 1 */
  56.     if ((ip = mdopen(argv[1], "r", NULL, 1)) == NULL)
  57.     {
  58.         perror(argv[1]);
  59.         exit(2);
  60.     }
  61.  
  62.     /* filename, WRITE, no filename function, sequence starts at 1 */
  63.     if ((op = mdopen(argv[2], "w", NULL, 1)) == NULL)
  64.     {
  65.         perror(argv[2]);
  66.         exit(3);
  67.     }
  68.  
  69.     /* set buffer sizes to 16K each */
  70.  
  71.     mdsetbuf(ip, 16*1024);
  72.     mdsetbuf(op, 16*1024);
  73.  
  74.     /* override the default "prompt" routines */
  75.  
  76.     mdfnateof(ip, ateof);
  77.     mdfnprompt(ip, prompt);
  78.     mdfnprompt(op, prompt);
  79.     mdfnfilenm(ip, filenm);
  80.     mdfnfilenm(op, filenm);
  81.  
  82.     /* and copy away... */
  83. #ifdef    PROFILE
  84.     PRFstart((long far *) main);
  85. #endif
  86.     while (!mdeof(ip))
  87.     {
  88.         len = mdread(buf, sizeof (char), sizeof buf, ip);
  89.         if (mdwrite(buf, sizeof (char), len, op) != len)
  90.             break;
  91.     }
  92. #ifdef    PROFILE
  93.     PRFstop();
  94. #endif
  95.     /* alternative copy... */
  96.  
  97.     while (!mdeof(ip) && !mdeof(op))
  98.         mdputc(mdgetc(ip), op);
  99.  
  100.     /* gotta make sure we close, cause this flushes the buffers */
  101.  
  102.     mdclose(ip);
  103.     mdclose(op);
  104.  
  105.     exit(0);
  106. }
  107.  
  108. /*
  109.  * filefnm -    construct the name of the next file to be opened in
  110.  *        sequence.  It consists of the base filename followed
  111.  *        by a three character extension.
  112.  */
  113. char   *
  114. filenm(mdp)
  115. MDFILE *mdp;
  116. {
  117.     static char filename[256];
  118.     char   *base;
  119.  
  120.     /* copy the basename */
  121.     strcpy(filename, mdp->basename);
  122.  
  123.     /* isolate the base, without preceding path */
  124.     if ((base = strrchr(filename, '/')) == NULL)
  125.         base = filename;
  126.  
  127.     /* remove any extension */
  128.     if ((base = strrchr(base, '.')) == NULL)
  129.         base = filename + strlen(filename);
  130.  
  131.     sprintf(base, ".%.3d", ++mdp->seq);
  132.     return (filename);
  133. }
  134.  
  135. /*
  136.  * ateof -    this routine is called when EOF is reached while reading
  137.  *        the input file.  We check for the existence of the next
  138.  *        file in sequence.
  139.  */
  140. ateof(mdp, mode)
  141. MDFILE *mdp;
  142. int    mode;
  143. {
  144.     char   *fname;
  145.     struct stat buf;
  146.  
  147.     fname = filenm(mdp);
  148.     --mdp->seq;
  149.     return (stat(fname, &buf));
  150. }
  151.  
  152. /*
  153.  * prompt -    completely silent.
  154.  */
  155. prompt(mdp)
  156. MDFILE *mdp;
  157. {
  158.     return (1);
  159. }
  160.