home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1987 / 12 / naro / readmap.c < prev    next >
Text File  |  1987-12-21  |  10KB  |  106 lines

  1. #include <stdio.h>                                                                           
  2. #include <stdlib.h>                                                                          
  3. #include <string.h>                                                                          
  4. #include <malloc.h>                                                                          
  5.                                                                                              
  6. #include "loc.h"                                                                             
  7. #include "externs.h"                                                                         
  8.                                                                                              
  9. #define  BUFSIZE  256                                                                        
  10.                                                                                              
  11.                                                                                              
  12. SEG_DESCRIPTOR *build_seg_list()                                                             
  13. {                                                                                            
  14.    int count ;                                                                               
  15.    unsigned long  start_seg, end_seg, length;                                                
  16.    char  seg_name[32], class[32] ;                                                           
  17.    char  *buf ;                                                                              
  18.                                                                                              
  19.    SEG_DESCRIPTOR *p, *previous, *list_start, *class_start ;                                 
  20.                                                                                              
  21.    /*                                                                                        
  22.       This function is responsible for the processing of the link map.                       
  23.       The link map is read and the segment information such as segment                       
  24.       name, segment length and class name are recorded.                                      
  25.    */                                                                                        
  26.                                                                                              
  27.    /* Seek to the beginning of the file */                                                   
  28.    if (fseek(map_file, 0L, SEEK_SET) != 0)   {                                               
  29.       perror(__FILE__) ;                                                                     
  30.       exit(1) ;                                                                              
  31.    }                                                                                         
  32.                                                                                              
  33.    /* Allocate some memory for the line buffer */                                            
  34.    if ((buf = malloc(BUFSIZE)) == NULL)   {                                                  
  35.       perror(__FILE__) ;                                                                     
  36.       exit(1) ;                                                                              
  37.    }                                                                                         
  38.                                                                                              
  39.    /* Search thru the file looking for the start of the segment informati/                   
  40.    while (1)   {                                                                             
  41.       if (fgets(buf, BUFSIZE, map_file) == NULL)   {                                         
  42.          fprintf(stderr, "Unable to find the segment list in %s\n", map_f;                   
  43.          exit(1) ;                                                                           
  44.       }                                                                                      
  45.                                                                                              
  46.       if (strstr(strupr(buf), "START") != NULL)                                              
  47.          break ;                                                                             
  48.    }                                                                                         
  49.                                                                                              
  50.    /* Scan to the start of the first segment record */                                       
  51.    while (fgets(buf, BUFSIZE, map_file) != NULL)   {                                         
  52.       count = sscanf(buf, " %lxH %lxH %lxH %s %s", &start_seg, &end_seg, ;                   
  53.       if (count == 5)                                                                        
  54.          break ;                                                                             
  55.    }                                                                                         
  56.                                                                                              
  57.    /* Check if EOF was detected and an error message should be printed */                    
  58.    if (feof(map_file))   {                                                                   
  59.       fprintf(stderr, "Unable to find the segment list in %s\n", map_fnam;                   
  60.       exit(1) ;                                                                              
  61.    }                                                                                         
  62.                                                                                              
  63.    /* Begin processing the list of segments */                                               
  64.    p = previous = NULL ;                                                                     
  65.    while (count == 5)   {                                                                    
  66.       /* Allocate some memory to hold the data structure */                                  
  67.       if ((p = (SEG_DESCRIPTOR *) malloc(sizeof (*p))) == NULL)   {                          
  68.          perror(__FILE__) ;                                                                  
  69.          exit(1) ;                                                                           
  70.       }                                                                                      
  71.                                                                                              
  72.       if (previous == NULL)                                                                  
  73.          list_start = p ;                                                                    
  74.       else                                                                                   
  75.          previous->next = p ;                                                                
  76.                                                                                              
  77.       strcpy(p->name, strupr(seg_name)) ;                                                    
  78.       strcpy(p->class, strupr(class)) ;                                                      
  79.       p->vseg = (unsigned int) start_seg / 16 ;                                              
  80.       p->offset = (unsigned int) start_seg % 16 ;                                            
  81.       p->len = (unsigned int) length ;                                                       
  82.       p->position = start_seg ;                                                              
  83.       p->inited = FALSE ;                                                                    
  84.       p->romable = FALSE ;                                                                   
  85.       p->symbols = 0 ;                                                                       
  86.       p->symbol_list = NULL ;                                                                
  87.       p->next = NULL ;                                                                       
  88.                                                                                              
  89.       /* Check if the class name has changed and reset the offset */                         
  90.       if (strcmp(p->class, class_start->class) != 0)   {                                     
  91.          p->pseg = 0 ;                                                                       
  92.          class_start = p ;                                                                   
  93.       }                                                                                      
  94.       else                                                                                   
  95.          p->pseg = p->vseg - class_start->vseg ;                                             
  96.                                                                                              
  97.       previous = p;                                                                          
  98.                                                                                              
  99.       /* Read the next line of segment information */                                        
  100.       fgets(buf, BUFSIZE, map_file) ;                                                        
  101.       count = sscanf(buf, " %lxH %lxH %lxH %s %s", &start_seg, &end_seg, ;                   
  102.    }                                                                                         
  103.                                                                                              
  104.    free(buf) ;                                                                               
  105.    return (list_start) ;                                                                     
  106. }