home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / NETWORK / CMAIL25V.ZIP / PAGIFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-20  |  1.9 KB  |  108 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.       }
  47.     else
  48.       if(fputc(inchar,outstream)==EOF)
  49.     {
  50.     cprintf("Can't write to output file");
  51.     exit(2);
  52.     }
  53.     }
  54.  
  55.   fclose(outstream);
  56.  
  57.   if (argc>2 && argv[2][0]=='/' && argv[2][1]=='d')
  58.     remove(argv[1]);
  59.   }
  60.  
  61.  
  62. FILE    *nextfile(char *file)
  63.   {
  64.   FILE    *outstream;
  65.   char    drive[3],path[80],name[9],ext[5];
  66.   int    i,handle;
  67.  
  68.   for (;;)
  69.     {
  70.     if ((handle=open(file,O_CREAT|O_BINARY|O_EXCL|O_RDWR,S_IREAD|S_IWRITE))==-1)
  71.       if (errno==EEXIST)
  72.     {
  73.     fnsplit(file,drive,path,name,ext);
  74.     for (i=7;i>=0;i--)
  75.       {
  76.       if (!isdigit(name[i])) name[i]='0';
  77.       if (name[i]<'9')
  78.         {
  79.         name[i]++;
  80.         break;
  81.         }
  82.       else
  83.         name[i]='0';
  84.       }
  85.     fnmerge(file,drive,path,name,ext);
  86.     if (i<0)
  87.       {
  88.       cprintf("\n\rOut of file names at %s\n\r",file);
  89.       exit(2);
  90.       }
  91.     }
  92.       else
  93.     {
  94.     cprintf("\n\rCan't open output file %s.\n\r",file);
  95.     exit(2);
  96.     }
  97.     else
  98.       {
  99.       if ((outstream=fdopen(handle,"wb"))==NULL)
  100.     {
  101.     cprintf("\n\rCan't open output stream %s.\n\r");
  102.     exit(2);
  103.     }
  104.       return outstream;
  105.       }
  106.     }
  107.   }
  108.