home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / t / tcu_32.zip / TCU_32.ZIP / MLIB.C < prev    next >
C/C++ Source or Header  |  1991-03-26  |  4KB  |  125 lines

  1. /*===========================================================================
  2.  *                                                                          *
  3.  *   Library Merge Procedure                                                *
  4.  *                                                                          *
  5.  *   Written by     : Karl Keyte            Gross-Gerauer Strasse 10        *
  6.  *   Date           : 91.03.16              6108 Weiterstadt                *
  7.  *                                          Germany                         *
  8.  *                                          Tel: +(49) 6150 2499            *
  9.  *                                                                          *
  10.  *==========================================================================*/
  11.  
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <dir.h>
  17. #include <io.h>
  18. #include <fcntl.h>
  19.  
  20.  
  21.  
  22. int merge_libs (char *mainlib, char *sublib)
  23. {
  24.    int               linenum,
  25.                      modules,
  26.                      nul,
  27.                      old_stderr,
  28.                      old_stdout;
  29.    char              cmd[81],
  30.                      line[81];
  31.    FILE             *inp,
  32.                     *outp1,
  33.                     *outp2;
  34.  
  35.    if (!strcmp (mainlib, sublib))        /* Don't merge same libraries! */
  36.       return (1);
  37.  
  38.    if (searchpath ("tlib.exe") == NULL)  /* Die if there's no librarian */
  39.       return (2);
  40.  
  41.    nul = open ("nul", O_WRONLY);         /* Open the nul device */
  42.  
  43.    old_stdout = dup (1);                 /* Redirect STDOUT and STDERR to */
  44.    old_stderr = dup (2);                 /* the nul device so that we don't */
  45.    dup2 (nul, 1);                        /* get any messy output */
  46.    dup2 (1, 2);
  47.  
  48.    strcpy (cmd, "tlib ");
  49.    strcat (cmd, sublib);
  50.    strcat (cmd, ",$lib$lst.$$$");        /* Get a file listing all the */
  51.                                          /* modules in the sub library */
  52.    system (cmd);
  53.  
  54.    inp = fopen ("$lib$lst.$$$", "r");
  55.    outp1 = fopen ("$mod$ext.$$$", "w");
  56.    outp2 = fopen ("$mod$rep.$$$", "w");
  57.  
  58.    linenum = 0;
  59.    modules = 0;
  60.  
  61.    while (fgets (line, 80, inp)) {       /* Scan through the list of modules */
  62.       if (++linenum < 3)                 /* picking out the individual name */
  63.          continue;                       /* and write them to two files */
  64.       if (line[0] == '\t' || line[0] == ' ')  /* which will be used as */
  65.          continue;                       /* response files to the librarian */
  66.       if (strchr (line, '\n'))
  67.          *strchr (line, '\n') = '\0';
  68.       if (strchr (line, '\t'))
  69.          *strchr (line, '\t') = '\0';
  70.       if (strchr (line, ' '))
  71.          *strchr (line, ' ') = '\0';
  72.       if (line[0] == '\0')
  73.          continue;
  74.       modules++;
  75.       if (modules > 1) {
  76.          fprintf (outp1, " &\n*%s", line);
  77.          fprintf (outp2, " &\n-+%s", line);
  78.       } else {
  79.          fprintf (outp1, "*%s", line);
  80.          fprintf (outp2, "-+%s", line);
  81.       }
  82.    }
  83.  
  84.    fclose (inp);
  85.    fclose (outp1);
  86.    fclose (outp2);
  87.  
  88.    unlink ("$lib$lst.$$$");            /* Remove the list of modules file */
  89.  
  90.    strcpy (cmd, "tlib ");
  91.    strcat (cmd, sublib);
  92.    strcat (cmd, " @$mod$ext.$$$");     /* Extract all the modules from the */
  93.                                        /* sub library */
  94.    system (cmd);
  95.  
  96.    unlink ("$mod$ext.$$$");            /* Remove the extraction response file */
  97.  
  98.    strcpy (cmd, "tlib ");
  99.    strcat (cmd, mainlib);
  100.    strcat (cmd, " @$mod$rep.$$$");     /* Insert the modules into the main */
  101.                                        /* library */
  102.    system (cmd);
  103.  
  104.    inp = fopen ("$mod$rep.$$$", "r");
  105.  
  106.    while (fgets (line, 80, inp)) {     /* Scan through the response file to */
  107.       if (strchr (line, '\n'))         /* get the module names so that they */
  108.          *strchr (line, '\n') = '\0';  /* can be deleted again. */
  109.       if (strchr (line, ' '))
  110.          *strchr (line, ' ') = '\0';
  111.       strcpy (cmd, line+2);
  112.       strcat (cmd, ".obj");
  113.       unlink (cmd);
  114.    }
  115.  
  116.    fclose (inp);
  117.  
  118.    unlink ("$mod$rep.$$$");       /* Remove the insertion response file */
  119.  
  120.    dup2 (old_stdout, 1);          /* Reset STDOUT and STDERR */
  121.    dup2 (old_stderr, 2);
  122.  
  123.    return (0);                    /* End */
  124. }
  125.