home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / c / cmail26q.zip / PAGIFILE.C < prev    next >
Text File  |  1992-03-31  |  3KB  |  142 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.   short noquotes=0;
  15.   int    i,handle;
  16.   char    file[100],drive[3],path[80],name[9],ext[5],inchar;
  17.   FILE    *instream,*outstream;
  18.  
  19.   if (argc<2)
  20.     {
  21.     cprintf("\n\rUsage: PAGIFILE <input file> [/d].\n\r");
  22.     exit(1);
  23.     }
  24.  
  25.   if ((instream=fopen(argv[1],"rb"))==NULL)
  26.     {
  27.     cprintf("\n\rCan't open file %s\n\r",argv[1]);
  28.     exit(2);
  29.     }
  30.  
  31.   fnsplit(argv[1],drive,path,name,ext);
  32.  
  33.   for(i=strlen(name);i<7;i++)
  34.     name[i]='0';
  35.   name[7]='0';
  36.   name[8]=0;
  37.  
  38.   fnmerge(file,drive,path,name,ext);
  39.  
  40.   outstream=nextfile(file);
  41.   for(;(inchar=getc(instream))!=EOF;)
  42.     {
  43.     if (noquotes && inchar=='"')
  44.       {
  45.       noquotes=0;
  46.       inchar=getc(instream);
  47.       }
  48.  
  49.     if (inchar==0x0C)
  50.       {
  51.       fclose(outstream);
  52.       outstream=nextfile(file);
  53.       do
  54.     {
  55.     inchar=getc(instream);
  56.     }
  57.       while (inchar<0x20 || inchar>0x7E);
  58.       }
  59.  
  60.     if (inchar=='I' || inchar=='i')
  61.       {
  62.       fputc(inchar,outstream);
  63.       inchar=getc(instream);
  64.       if (inchar=='N' || inchar=='n')
  65.     {
  66.     fputc(inchar,outstream);
  67.     inchar=getc(instream);
  68.     if (inchar=='%')
  69.       {
  70.       fputc(inchar,outstream);
  71.       inchar=getc(instream);
  72.       if (inchar=='"')
  73.         {
  74.         fseek(outstream,-3,SEEK_CUR);
  75.         inchar=getc(instream);
  76.         noquotes=1;
  77.         }
  78.       }
  79.     }
  80.       }
  81.  
  82.     if (inchar!=EOF && fputc(inchar,outstream)==EOF)
  83.       {
  84.       cprintf("Can't write to output file");
  85.       exit(2);
  86.       }
  87.     }
  88.  
  89.   fclose(outstream);
  90.  
  91.   if (argc>2 && argv[2][0]=='/' && argv[2][1]=='d')
  92.     remove(argv[1]);
  93.   }
  94.  
  95.  
  96. FILE    *nextfile(char *file)
  97.   {
  98.   FILE    *outstream;
  99.   char    drive[3],path[80],name[9],ext[5];
  100.   int    i,handle;
  101.  
  102.   for (;;)
  103.     {
  104.     if ((handle=open(file,O_CREAT|O_BINARY|O_EXCL|O_RDWR,S_IREAD|S_IWRITE))==-1)
  105.       if (errno==EEXIST)
  106.     {
  107.     fnsplit(file,drive,path,name,ext);
  108.     for (i=7;i>=0;i--)
  109.       {
  110.       if (!isdigit(name[i])) name[i]='0';
  111.       if (name[i]<'9')
  112.         {
  113.         name[i]++;
  114.         break;
  115.         }
  116.       else
  117.         name[i]='0';
  118.       }
  119.     fnmerge(file,drive,path,name,ext);
  120.     if (i<0)
  121.       {
  122.       cprintf("\n\rOut of file names at %s\n\r",file);
  123.       exit(2);
  124.       }
  125.     }
  126.       else
  127.     {
  128.     cprintf("\n\rCan't open output file %s.\n\r",file);
  129.     exit(2);
  130.     }
  131.     else
  132.       {
  133.       if ((outstream=fdopen(handle,"wb"))==NULL)
  134.     {
  135.     cprintf("\n\rCan't open output stream %s.\n\r");
  136.     exit(2);
  137.     }
  138.       return outstream;
  139.       }
  140.     }
  141.   }
  142.