home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / batch_msc94_v2.lha / batch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-09  |  2.9 KB  |  138 lines

  1. /* Reunit les articles listes dans UUSPOOL:BATCH/<SYSNAME>
  2.  * dans le fichier liste en parametre -- MSC94
  3.  * Support: schaefer@alphanet.ch
  4.  * CDate: 09/01/94
  5.  * MDate: 09/01/94
  6.  * PV: 1
  7.  */
  8.  
  9. #undef DEBUGGING
  10.  
  11. #define BATCHFROM "UUNEWS:%s"
  12. #define BATCHINST "UUSPOOL:BATCH/%s"
  13. #define MAXPATHLEN 300  /* Some more thought here, please */
  14. #define MAXLINELENGTH 256
  15. #define TRUE 1
  16. #define FALSE 0
  17.  
  18. #include <stdio.h>
  19. /* #include <fnctl.h> */
  20. #include <sys/stat.h>
  21.  
  22. int create_batch_file(char *sysname, char *tofile);
  23. int cat_batch(FILE *tof, char *current_line);
  24. void convert_path(char *str);
  25. unsigned long get_file_length(char *file);
  26.  
  27. int main(argc, argv)
  28. int argc;
  29. char *argv[];
  30. {
  31.    if (argc < 3) {
  32.       printf("%s sysname tofile\n", argv[0]);
  33.       printf("%s: expected 2 args\n", argv[0]);
  34.       exit(1);
  35.    }
  36.  
  37. #ifdef DEBUGGING
  38.    printf("debug: syname='%s'\n", argv[1]);
  39.    printf("debug: tofile='%s'\n", argv[2]);
  40. #endif
  41.  
  42.    if (create_batch_file(argv[1], argv[2]))
  43.       exit(0);
  44.    else
  45.       exit(1);
  46. }
  47.  
  48. int create_batch_file(sysname, tofile)
  49. char *sysname;
  50. char *tofile;
  51. {
  52.    char instfile[MAXPATHLEN];
  53.    FILE *instf;
  54.    FILE *tof;
  55.    int err = FALSE;
  56.  
  57.    sprintf(instfile, BATCHINST, sysname);
  58.  
  59.    if (instf = fopen(instfile, "r")) {
  60.       if (tof = fopen(tofile, "w")) {
  61.          int ok = TRUE;
  62.          char current_line[MAXPATHLEN];
  63.  
  64.          while (ok && fgets(current_line, sizeof(current_line), instf)) {
  65. #ifdef DEBUGGING
  66.             printf("todo: %s\n", current_line);
  67. #endif
  68.             convert_path(current_line); /* will also remove the leading \n */
  69.             if (!(cat_batch(tof, current_line))) {
  70.                ok = FALSE;
  71.                err = TRUE;
  72.                printf("cat_batch failed on %s\n", current_line);
  73.             }
  74.          }
  75.          fclose(tof);
  76.       }
  77.       else {
  78.          err = TRUE;
  79.          printf("open %s failed\n", tofile);
  80.       }
  81.       fclose(instf);
  82.    }
  83.    else {
  84.       err = TRUE;
  85.       printf("open %s failed\n", instfile);
  86.    }
  87.    return !err;
  88. }
  89.  
  90. int cat_batch(tof, current_line)
  91. FILE *tof;
  92. char *current_line;
  93. {
  94.    char buf[MAXLINELENGTH];
  95.    char filename[MAXPATHLEN];
  96.    FILE *fi;
  97.  
  98.    sprintf(filename, BATCHFROM, current_line);
  99.    if (fi = fopen(filename, "r")) {
  100.  
  101. #ifdef DEBUGGING
  102.       printf("file is %ld bytes long\n", get_file_length(filename));
  103. #endif
  104.       fprintf(tof, "#! rnews %.6d\n", get_file_length(filename));
  105.  
  106.       while (fgets(buf, sizeof(buf), fi))  /* here we need the \n */
  107.          fputs(buf, tof);
  108.       fclose(fi);
  109.       return TRUE;
  110.    }
  111.    else {
  112.       printf("open %s failed\n", current_line);
  113.       return FALSE;
  114.    }
  115. }
  116.  
  117. void convert_path(str)
  118. char *str;
  119. {
  120.    while (*str != '\n') {
  121.       if (*str == '.')
  122.          *str = '/';
  123.       str++;
  124.    }
  125.    *str = '\0';
  126. }
  127.  
  128. unsigned long get_file_length(file)
  129. char *file;
  130. {
  131.    struct stat stat_buf;
  132.  
  133.    stat(file, &stat_buf); /* highly non portable ? */
  134.    /* we should do something with returncode ... */
  135.  
  136.    return stat_buf.st_size;
  137. }
  138.