home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / extras / mkmk / dofile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-24  |  1.6 KB  |  75 lines

  1. /* Copyright (c) 1993 SAS Institute, Inc, Cary, NC USA */
  2. /* All Rights Reserved */
  3.  
  4. #include "mkmk.h"
  5.  
  6. char buf[1024];
  7.  
  8. // Add a dependancy to a file
  9. void AddDep(struct FileDesc *fd, struct FileDesc *newfd)
  10. {
  11.    int i;
  12.  
  13.    if(fd == newfd) return;  // Trivial circular dependancy
  14.  
  15.    for(i=0; i<newfd->ndirect; i++)
  16.    {
  17.       if(newfd->Direct[i] == fd)
  18.       {
  19.          myfprintf(out, "Circular dependancy: \"%s\" and \"%s\"\n",
  20.             fd->name, newfd->name);
  21.          return;  // Circular dependancy
  22.       }
  23.    }
  24.  
  25.    if(fd->ndirect >= fd->adirect)
  26.    {
  27.       fd->adirect += 20;
  28.       fd->Direct = realloc(fd->Direct, fd->adirect*sizeof(struct FileDesc *));
  29.       if(!fd->Direct) panic("No Memory!\n");
  30.    }
  31.    fd->Direct[fd->ndirect++] = newfd;
  32. }
  33.  
  34. // Allocate memory and copy a string to it
  35. char *scopy(char *s)
  36. {
  37.    char *t;
  38.    t = malloc(strlen(s)+1);
  39.    if(!t) panic("No Memory!\n");
  40.    strcpy(t, s);
  41.    return t;
  42. }
  43.  
  44. // Get dependancies for a single file
  45. void DoFile(struct FileList *fl, struct FileDesc *fd)
  46. {
  47.    BPTR fp;
  48.    char *s, *t;
  49.    struct FileDesc *newfd;
  50.  
  51.    myfprintf(out, "Processing \"%s\"\n", fd->name);
  52.  
  53.    if(!(fp = Open(fd->name, MODE_OLDFILE)))
  54.    {
  55.       myfprintf(out, "Can't open file \"%s\"!\n", fd->name);
  56.       return;
  57.    }
  58.  
  59.    while(FGets(fp, buf, sizeof(buf)))
  60.    {
  61.       if(!memcmp(buf, "#include ", sizeof("#include")))
  62.       {
  63.          for(t=buf+sizeof("#include"); *t && *t == ' '; t++);
  64.          if(*t != '"') continue;
  65.          s = ++t;
  66.          for(; *t && *t != '"'; t++);
  67.          *t = 0;
  68.          newfd = AddFile(fl, scopy(s));
  69.          AddDep(fd, newfd);
  70.       }
  71.    }
  72.  
  73.    Close(fp);   
  74. }
  75.