home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / NETWORK / CMAIL26L.ZIP / PAGIFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-22  |  2.0 KB  |  112 lines

  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <dir.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <ctype.h>
  8. #include <sys\stat.h>
  9.  
  10. FILE    *nextfile(char *file);
  11.  
  12. main(int argc, char *argv[])
  13.   {
  14.   int    i,handle;
  15.   char    file[100],drive[3],path[80],name[9],ext[5],inchar;
  16.   FILE    *instream,*outstream;
  17.  
  18.   if (argc<2)
  19.     {
  20.     cprintf("\n\rUsage: PAGIFILE <input file> [/d].\n\r");
  21.     exit(1);
  22.     }
  23.  
  24.   if ((instream=fopen(argv[1],"rb"))==NULL)
  25.     {
  26.     cprintf("\n\rCan't open file %s\n\r",argv[1]);
  27.     exit(2);
  28.     }
  29.  
  30.   fnsplit(argv[1],drive,path,name,ext);
  31.  
  32.   for(i=strlen(name);i<7;i++)
  33.     name[i]='0';
  34.   name[7]='0';
  35.   name[8]=0;
  36.  
  37.   fnmerge(file,drive,path,name,ext);
  38.  
  39.   outstream=nextfile(file);
  40.   for(;(inchar=getc(instream))!=EOF;)
  41.     {
  42.     if (inchar==0x0C)
  43.       {
  44.       fclose(outstream);
  45.       outstream=nextfile(file);
  46.       do
  47.     {
  48.     inchar=getc(instream);
  49.     }
  50.       while (inchar<0x20 || inchar>0x7E);
  51.       }
  52.     if (inchar!=EOF && fputc(inchar,outstream)==EOF)
  53.       {
  54.       cprintf("Can't write to output file");
  55.       exit(2);
  56.       }
  57.     }
  58.  
  59.   fclose(outstream);
  60.  
  61.   if (argc>2 && argv[2][0]=='/' && argv[2][1]=='d')
  62.     remove(argv[1]);
  63.   }
  64.  
  65.  
  66. FILE    *nextfile(char *file)
  67.   {
  68.   FILE    *outstream;
  69.   char    drive[3],path[80],name[9],ext[5];
  70.   int    i,handle;
  71.  
  72.   for (;;)
  73.     {
  74.     if ((handle=open(file,O_CREAT|O_BINARY|O_EXCL|O_RDWR,S_IREAD|S_IWRITE))==-1)
  75.       if (errno==EEXIST)
  76.     {
  77.     fnsplit(file,drive,path,name,ext);
  78.     for (i=7;i>=0;i--)
  79.       {
  80.       if (!isdigit(name[i])) name[i]='0';
  81.       if (name[i]<'9')
  82.         {
  83.         name[i]++;
  84.         break;
  85.         }
  86.       else
  87.         name[i]='0';
  88.       }
  89.     fnmerge(file,drive,path,name,ext);
  90.     if (i<0)
  91.       {
  92.       cprintf("\n\rOut of file names at %s\n\r",file);
  93.       exit(2);
  94.       }
  95.     }
  96.       else
  97.     {
  98.     cprintf("\n\rCan't open output file %s.\n\r",file);
  99.     exit(2);
  100.     }
  101.     else
  102.       {
  103.       if ((outstream=fdopen(handle,"wb"))==NULL)
  104.     {
  105.     cprintf("\n\rCan't open output stream %s.\n\r");
  106.     exit(2);
  107.     }
  108.       return outstream;
  109.       }
  110.     }
  111.   }
  112.