Changeset 223

Show
Ignore:
Timestamp:
04/07/05 00:52:32 (4 years ago)
Author:
athomas
Message:

Sorting actually works now. Also, fixed potential overflow with > 16 config
files in /etc/op.d

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • op/trunk/main.c

    r220 r223  
    9090} 
    9191 
     92int file_compare(const void *a, const void *b) 
     93{ 
     94        return strcmp(*(char**)a, *(char**)b); 
     95} 
     96 
    9297int ReadDir( char *dir ) 
    9398{ 
     
    102107                if (!(dir_list = malloc(sizeof(char*) * dir_capacity))) 
    103108                        fatal(1, "failed to malloc space for directory entries"); 
    104                 memset(dir_list, 0, sizeof(char*) * dir_capacity); 
    105109 
    106110                while ((f = readdir(d))) 
    107111                { 
    108                 char full_path[PATH_MAX]; 
    109  
    110112                        if (f->d_name[0] == '.' || (strlen(f->d_name) > 5 && strcmp(f->d_name + strlen(f->d_name) - 5, ".conf"))) 
    111113                                continue; 
    112 #ifdef HAVE_SNPRINTF 
    113                         snprintf(full_path, PATH_MAX, "%s/%s", OP_ACCESS_DIR, f->d_name)
    114 #else 
    115                         sprintf(full_path, "%s/%s", OP_ACCESS_DIR, f->d_name); 
    116 #endif 
    117                         if (!(dir_list[dir_entries] = strdup(full_path))) 
     114                        if (dir_entries >= dir_capacity) { 
     115                               dir_capacity += 32
     116                                if (!(dir_list = realloc(dir_list, sizeof(char*) * dir_capacity))) 
     117                                       fatal(1, "reallocation of directory entry list failed"); 
     118                        } 
     119                        if (!(dir_list[dir_entries] = strdup(f->d_name))) 
    118120                                fatal(1, "failed to malloc space for directory entry"); 
    119121                        ++dir_entries; 
    120122                } 
    121123                closedir(d); 
    122                 qsort(dir_list, dir_entries, sizeof(char*), 
    123                         (int(*)(const void *, const void *))strcmp); 
    124                 for (i = 0; i < dir_entries; ++i) 
    125                         if (ReadFile(dir_list[i])) successes++; 
     124                qsort(dir_list, dir_entries, sizeof(char*), file_compare); 
     125                for (i = 0; i < dir_entries; ++i) { 
     126                char full_path[PATH_MAX]; 
     127#ifdef HAVE_SNPRINTF 
     128                        snprintf(full_path, PATH_MAX, "%s/%s", OP_ACCESS_DIR, dir_list[i]); 
     129#else 
     130                        sprintf(full_path, "%s/%s", OP_ACCESS_DIR, dir_list[i]); 
     131#endif 
     132                        if (ReadFile(full_path)) successes++; 
     133                } 
    126134                return successes; 
    127135        }