home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff384.lzh / NorthC / Example2.LZH / make / file.c < prev    next >
C/C++ Source or Header  |  1990-08-30  |  7KB  |  333 lines

  1. /*
  2.   (c) 1990 S.Hawtin.
  3.   Permission is granted to copy this file provided that:
  4.    1) It is not used for commercial gain
  5.    2) This notice is included in all copies
  6.    3) Altered copies are marked as such.
  7.  
  8.   No liability is accepted for the contents of the file.
  9.  
  10.   file.c    within        WBmake
  11.  
  12. */
  13.  
  14. /* Handle the file and variable data types */
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <libraries/dos.h>
  19. #include "make.h"
  20.  
  21. #define MAX_HASH 128
  22.  
  23. extern ConsCell *defaults;
  24.  
  25. static ConsCell *files[MAX_HASH];
  26.  
  27. static Variable *vars[MAX_HASH];
  28.  
  29. /* The file info block must be word alligned */
  30. static long padding;
  31. static struct FileInfoBlock infodata;
  32.  
  33. static int
  34. hash(str)
  35.     char *str;
  36.    {/* Simple hashing */
  37.     int i;
  38.  
  39.     for(i=0;*str;str++)
  40.         i += *str;
  41.     return(i % MAX_HASH);
  42.     }
  43.  
  44. long
  45. type_of(str)
  46.     char *str;
  47.    {/* Clasify the file type */
  48.     char *temp;
  49.     int  i;
  50.     long ret;
  51.  
  52.     temp = strrchr(str,'.');
  53.     ret = 0;
  54.     if(temp!=NULL)
  55.        {/* Only first three bytes of extension are used */
  56.         temp++;
  57.         for(i=0;i<4 && temp[i]!='\0';i++)
  58.             ret = (ret<<8)+temp[i];
  59.         return(ret);
  60.         }
  61.     return(EXE_FILE);
  62.     }
  63.  
  64. ConsCell *
  65. cons(the_car,the_cdr)
  66.     FileInfo *the_car;
  67.     ConsCell *the_cdr;
  68.    {/* Create a list element */
  69.     ConsCell *this;
  70.  
  71.     this = (ConsCell *)calloc(sizeof(ConsCell),1);
  72.     if(this==NULL)
  73.        {printf("Out of memory\n");
  74.         exit(10);
  75.         }
  76.     this->car = the_car;
  77.     this->cdr = the_cdr;
  78.     return(this);
  79.     }
  80.  
  81. long
  82. dstodate(ds)
  83.     struct DateStamp *ds;
  84.    {/* Convert a datestanp to a date */
  85.     long ret;
  86.  
  87.     ret = ds->ds_Days;
  88.     ret = ret*24*60 + ds->ds_Minute;
  89.     ret = ret*60 + (ds->ds_Tick/TICKS_PER_SECOND);
  90.     return(ret);
  91.     }
  92.  
  93. static char *
  94. get_addr(addr)
  95.     unsigned long addr;
  96.    {/* Chop the bottom two bits from the address to ensure long
  97.        word allignment */
  98.     return(0xfffffffc & addr);
  99.     }
  100.  
  101. static FileInfo *
  102. make_file(str)
  103.     char *str;
  104.    {/* Create a file info structure */
  105.     extern APTR Lock();
  106.     APTR lock;
  107.     FileInfo *this;
  108.     char *tname;
  109.     struct FileInfoBlock *finfo;
  110.  
  111.     finfo = (struct FileInfoBlock *)get_addr(&infodata);
  112.     tname = (char *)calloc(strlen(str)+1,1);
  113.     this = (FileInfo *)calloc(sizeof(FileInfo),1);
  114.     if(this==NULL || tname==NULL)
  115.        {printf("Out of memory\n");
  116.         exit(10);
  117.         }
  118.     strcpy(tname,str);
  119.     this->name = tname;
  120.     this->type = type_of(str);
  121.     /* Read the file date at this point */
  122.     lock = Lock(str,ACCESS_READ);
  123.     if(lock!=0)
  124.        {/* Got the lock now find the file info */
  125.         if(Examine(lock,finfo))
  126.            {
  127.             this->date = dstodate(&(finfo->fib_Date));
  128.             }
  129.         UnLock(lock);
  130.         }
  131.     return(this);
  132.     }
  133.  
  134. static Variable *
  135. create_var(str)
  136.     char *str;
  137.    {/* Create a variable slot */
  138.     Variable *temp;
  139.     char     *tname;
  140.  
  141.     temp=(Variable *)calloc(sizeof(Variable),1);
  142.     tname=(char *)malloc(strlen(str)+1);
  143.     if(temp==NULL || tname==NULL)
  144.        {printf("Out of memory\n");
  145.         exit(10);
  146.         }
  147.     strcpy(tname,str);
  148.     temp->var = tname;
  149.     return(temp);
  150.     }
  151.  
  152. Variable *
  153. find_var(str,create)
  154.     char *str;
  155.     int  create;
  156.    {/* Find a variable definition */
  157.     Variable *ret_val;
  158.     Variable *here;
  159.     int hash_val;
  160.  
  161.     hash_val = hash(str);
  162.     if(vars[hash_val]==NULL)
  163.        {/* Create new variable? */
  164.         if(create)
  165.            {
  166.             ret_val = create_var(str);
  167.             vars[hash_val] = ret_val;
  168.             return(ret_val);
  169.             }
  170.           else
  171.             return(NULL);
  172.         }
  173.       else
  174.        {/* We must wander the list looking for our man */
  175.         int found;
  176.  
  177.         found = 0;
  178.         here = vars[hash_val];
  179.  
  180.         while(here->next && (found = strcmp(here->var,str))!=0)
  181.             here = here->next;
  182.  
  183.         if(found)
  184.             return(here);
  185.           else if (create)
  186.            {
  187.             ret_val = create_var(str);
  188.             here->next = ret_val;
  189.             return(ret_val);
  190.             }
  191.           else
  192.             return(NULL);
  193.         }
  194.     }
  195.  
  196. FileInfo *
  197. find_file(str,create)
  198.     char *str;
  199.     int  create;
  200.    {/* Find a file structure in the hash table */
  201.     FileInfo *ret_val;
  202.     ConsCell *here;
  203.     int  hash_val;
  204.  
  205.     hash_val = hash(str);
  206.     if(files[hash_val]==NULL)
  207.        {/* Empty slot, should we create an entry */
  208.         if(create)
  209.            {
  210.             ret_val = make_file(str);
  211.             here = cons(ret_val,NULL);
  212.             files[hash_val] = here;
  213.             return(ret_val);
  214.             }
  215.           else
  216.             return(NULL);
  217.         }
  218.       else
  219.        {/* We must wander the list looking for our man */
  220.         int found;
  221.  
  222.         found = 0;
  223.         here = files[hash_val];
  224.  
  225.         while((found = strcmp(here->car->name,str))!=0 && here->cdr)
  226.             here = here->cdr;
  227.  
  228.         if(found==0)
  229.             return(here->car);
  230.           else if (create)
  231.            {
  232.             ret_val = make_file(str);
  233.             here->cdr = cons(ret_val,NULL);
  234.             return(ret_val);
  235.             }
  236.           else
  237.             return(NULL);
  238.         }
  239.     }
  240.  
  241. print_file(file)
  242.     FileInfo *file;
  243.    {/* Print out the details of a file */
  244.     ConsCell *this;
  245.  
  246.     printf("File \"%s\"\n",file->name);
  247.     this = file->depends;
  248.     if(this)
  249.        {int i = 0;
  250.         printf("  Depends\n  ");
  251.         while(this)
  252.            {printf("%s ",this->car->name);
  253.             i++;
  254.             if(i>3)
  255.                {printf("\n    ");
  256.                 i = 0;
  257.                 }
  258.             this = this->cdr;
  259.             }
  260.         printf("\n");
  261.         }
  262.       else
  263.         printf("  No depends\n");
  264.     printf("  Create = ");
  265.     print_str(file->create);
  266.     }
  267.  
  268. print_files()
  269.    {/* Print out all the files in the hash table */
  270.     int i;
  271.     ConsCell *this;
  272.  
  273.     for(i=0;i<MAX_HASH;i++)
  274.        {this = files[i];
  275.         while(this)
  276.            {print_file(this->car);
  277.             printf("\n");
  278.             this = this->cdr;
  279.             }
  280.         }
  281.     }
  282.  
  283. print_str(str)
  284.     String *str;
  285.    {/* Print out the string */
  286.     if(str==NULL)
  287.         printf("<NULL>\n");
  288.       else
  289.        {do {
  290.             printf("\"%s\"\n",str->contents);
  291.             str = str->next;
  292.             if(str)
  293.                 printf("      ");
  294.             } while (str);
  295.         }
  296.     }
  297.  
  298. print_var(variable)
  299.     Variable *variable;
  300.    {/* Print out a single variable */
  301.     String *value;
  302.  
  303.     printf("|%s| = ",variable->var);
  304.     value = variable->val;
  305.     print_str(variable->val);
  306.     }
  307.        
  308. print_vars()
  309.    {/* Print out all the variables and values */
  310.     int i;
  311.     Variable *this;
  312.  
  313.     for(i=0;i<MAX_HASH;i++)
  314.        {this = vars[i];
  315.         while(this)
  316.            {print_var(this);
  317.             this = this->next;
  318.             }
  319.         }
  320.     }
  321.  
  322. print_def()
  323.    {/* Print the items on the defaults list */
  324.     ConsCell *this;
  325.  
  326.     this = defaults;
  327.  
  328.     while(this)
  329.        {print_file(this->car);
  330.         this = this->cdr;
  331.         }
  332.     }
  333.