home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / AP / JED / JED097-1.TAR / jed / src / fmerge.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  2.5 KB  |  157 lines

  1. /* Merges small files back to original using descriptor file. */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <limits.h>
  6. #ifndef sequent
  7. #include <stdlib.h>
  8. #endif
  9.  
  10. #ifdef VMS
  11. #include <file.h>
  12. #endif
  13.  
  14. #ifdef unix
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/file.h>
  18. #ifdef SYSV
  19. #include <sys/fcntl.h>
  20. #endif
  21. #endif
  22.  
  23. #ifdef msdos
  24. #include <fcntl.h>
  25. #include <io.h>
  26. #include <sys/stat.h>
  27. #endif
  28.  
  29. #ifndef msdos
  30. #define MAX_LEN 65536
  31. #else
  32. #define MAX_LEN 4096
  33. #endif
  34.  
  35. #include "vfile.h"
  36. int Quiet = 0;
  37.  
  38. void usage(char *prog)
  39. {
  40.    char *str = "filename";
  41.    
  42.    fprintf(stderr, "%s: Usage: %s [-quiet]\n", prog, str);
  43.    exit(1);
  44. }
  45.  
  46. void error(char *fmt, char *str)
  47. {
  48.    fprintf(stderr, fmt, str);
  49.    putc('\n', stderr);
  50.    exit(1);
  51. }
  52.  
  53.  
  54.  
  55. int Max_Lines = 2000;
  56.  
  57.  
  58. FILE *open_descriptor_file(char *file)
  59. {
  60.    FILE *fp; 
  61.    
  62. #ifdef msdos
  63.    _fmode = O_TEXT;
  64. #endif
  65.    if (NULL == (fp = fopen(file, "r")))
  66.      {
  67.     error("Unable to open descriptor file %s", file);
  68.      }
  69. #ifdef msdos
  70.    _fmode = O_BINARY;
  71. #endif
  72.    return (fp);
  73. }
  74.  
  75. /* file is name of dsc file */
  76. void merge(char *file)
  77. {
  78.    VFILE *vp;
  79.    unsigned int total, len;
  80.    int fp;
  81.    char *buf, *p;
  82.    char buffer[256], fsplit[256];
  83.    FILE *fd;
  84.    
  85.    fd = open_descriptor_file(file);
  86.    fgets(fsplit, 255, fd);
  87.    
  88.    /* remove newline char */
  89.    p = fsplit;
  90.    while (*p > ' ') p++;  
  91.    *p = 0;
  92.    
  93.    /* next line is format--- ignore it. */
  94.    fgets(buffer, 255, fd);
  95.    
  96. #ifdef VMS
  97.    fp = open(fsplit, O_WRONLY | O_CREAT | O_TRUNC, 0, "rat=cr", "rfm=var");
  98. #else
  99. #ifdef unix
  100.    fp = open(fsplit, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  101. #else
  102.    _fmode = O_BINARY;
  103.    fp = open(fsplit, O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  104. #endif
  105. #endif
  106.    if (fp < 0)
  107.      {
  108.     error("Error creating %s", fsplit);
  109.      }
  110.    
  111.    while (1)
  112.      {
  113.     if (buf == NULL)
  114.       {
  115.          if (vp != NULL) vclose(vp);
  116.          
  117.          if (fgets(fsplit, 255, fd) == NULL) break;
  118.          p = fsplit;
  119.          while (*p > ' ') p++;
  120.          *p = 0;
  121.          
  122.          if (NULL == (vp = vopen(fsplit, MAX_LEN)))
  123.            {
  124.           error("Error opening %s", fsplit);
  125.            }
  126.          if (!Quiet) fprintf(stdout, "Processing %s.\n", fsplit);
  127.       }
  128.     else write(fp, buf, len);
  129.     
  130.     buf = vgets(vp, &len);
  131.      }
  132.    
  133.    if (vp != NULL)
  134.      {
  135.     vclose(vp);
  136.      }
  137.    fclose(fd);
  138.    close(fp);
  139. }
  140.  
  141.  
  142. int main(int argc, char **argv)
  143. {
  144.    char *file;
  145.    switch (argc)
  146.      {
  147.       case 3: Quiet = 1;
  148.       case 2: file = argv[1];
  149.     break;
  150.       default: usage(argv[0]);
  151.      }
  152.    merge(file);
  153.    return(0);
  154. }
  155.  
  156.     
  157.