"Fossies" - the Fresh Open Source Software Archive

Member "authforce-0.9.9/src/files.c" (13 May 2007, 4559 Bytes) of package /linux/www/old/authforce-0.9.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* $Id: files.c,v 1.4 2001/04/28 20:32:47 kapheine Exp $ */
    2 
    3 #include <config.h>
    4 #include <stdio.h>
    5 #include <stdlib.h>
    6 #include <string.h>
    7 #include <unistd.h>
    8 #include <sys/stat.h>
    9 #include <readline/readline.h>
   10 #ifdef MEMWATCH
   11 #include "memwatch.h"
   12 #endif /* MEMWATCH */
   13 #include "extern.h"
   14 
   15 //#define MAX_ENTRIES 10000
   16 #define BUFFER_LEN 82
   17 
   18 int num_lines=0;
   19 
   20 char **textlist(char *textfile) {
   21     FILE *fp;
   22     char **wordlist;
   23     char buffer[BUFFER_LEN];
   24     int count;
   25     char *chop;
   26     struct stat status;
   27 
   28     num_lines = 0;
   29 
   30     stat(textfile, &status);
   31     
   32     if (S_ISDIR(status.st_mode)) {
   33         fprintf(stderr, "textfile: %s is a directory, quitting\n", textfile);
   34         exit(EXIT_FAILURE);
   35     }
   36 
   37     fp = fopen(textfile, "r");
   38 
   39     if (!fp) {
   40         fprintf(stderr, "textlist: error opening %s\n", textfile);
   41         exit(EXIT_FAILURE);
   42     }
   43 
   44     while (!feof(fp)) {
   45         fgets(buffer, sizeof(buffer), fp);
   46         if (buffer[0] != '#' && buffer[0] != ';' && buffer[0] != '\n')
   47             num_lines++;
   48     }
   49 
   50     if (num_lines != 0)
   51         num_lines--;
   52 
   53     debug(3, "textlist: reading %s [%i]\n", textfile, num_lines);
   54 
   55     rewind(fp);
   56 
   57     /* add +1 to num_lines to make room for NULL */
   58 
   59     wordlist = malloc_w((num_lines+1)*sizeof(char*));
   60 
   61     count=0;
   62     while (fgets(buffer, sizeof(buffer), fp)) {
   63         if (buffer[0] == '#' || buffer[0] == ';' || buffer[0] == '\n')
   64             continue;   
   65         wordlist[count] = (char*)strdup_w(buffer);
   66         chop = (char*)strstr(wordlist[count], "\n"); /* credits to cgichk */
   67         if (chop) *chop = 0;                         /* and toby deshane  */
   68         count++;
   69     }
   70 
   71     /* MEMWATCH: this isnt freed, why? */
   72     wordlist[count] = malloc_w(sizeof(NULL));
   73     wordlist[count] = (char)NULL;
   74 
   75     fclose(fp);
   76 
   77     return(wordlist);
   78 }
   79 
   80 /* make copy of list returned by textlist above */
   81 char **copy_list(char **list) {
   82     int i=0;
   83     char **n_list; /* new list */
   84 
   85     /* find num of elements */
   86     while (list[i] != (char)NULL) 
   87         i++;
   88     /* plus one for NULL */
   89     i++;
   90 
   91     n_list = malloc_w(i*sizeof(char*));
   92 
   93     i=0;
   94     while (list[i] != (char)NULL) {
   95         n_list[i] = (char*)strdup_w(list[i]);
   96         i++;
   97     }
   98 
   99     n_list[i] = (char)NULL;
  100 
  101     return (n_list);
  102 }
  103 
  104 void free_list(char **list) {
  105     int i=0;
  106 
  107     while (list[i] != (char)NULL) {
  108         free(list[i]);
  109         i++;
  110     }
  111     free(list);
  112 }
  113 
  114 void read_session(char *sessionfile) {
  115     FILE *fp;
  116     char buffer[30];
  117     int result;
  118 
  119     fp = fopen(sessionfile, "r");
  120 
  121     if (!fp) {
  122         fprintf(stderr, "read_session: error opening %s\n", sessionfile);
  123         exit(EXIT_FAILURE);
  124     }
  125 
  126     fgets(buffer, sizeof(buffer), fp);
  127     result = sscanf(buffer, "%i, %i, %i", &session_usernumber, &session_function, &session_count);
  128 
  129     if (result != 3) {
  130         fprintf(stderr, "read_session: invalid format (#, #, #)\n");
  131         exit(EXIT_FAILURE);
  132     }
  133 
  134     debug(3, "read_session: reading %s [%i, %i, %i]\n", sessionfile, session_usernumber, session_function, session_count);
  135         
  136     fclose(fp);
  137 }
  138 
  139 void write_session(char *sessionfile) {
  140     FILE *fp;
  141 
  142     fp = fopen(sessionfile, "w");
  143 
  144     if (!fp) {
  145         fprintf(stderr, "write_session: error opening %s\n", sessionfile);
  146         exit(EXIT_FAILURE);
  147     }
  148 
  149     fprintf(fp, "%i, %i, %i\n", session_usernumber, session_function, session_count);
  150 
  151     debug(3, "write_session: writing %s [%i, %i, %i]\n", sessionfile, session_usernumber, session_function, session_count);
  152 
  153     fclose(fp);
  154 }
  155 
  156 /* search : delimited pathlist for filename, if found returns full path else
  157  * returns NULL
  158  */
  159 
  160 char *search_path(char *filename, char *pathlist) {
  161     int i = 0;
  162     char *curpath;
  163     char *fullpath;
  164 
  165     /* check for absolute filenames */
  166     if (*filename == '~') {
  167         debug(3, "search_path: attempting to access %s\n", tilde_expand(filename));
  168         if (access(tilde_expand(filename), R_OK) == 0)
  169             return(tilde_expand(filename));
  170     }
  171 
  172     fullpath = (char*)malloc_w(sizeof(char)*80);
  173     *(fullpath+79)='\0';
  174 
  175     if (*filename == '.' && *(filename+1) == '.') {
  176         debug(3, "search_path: attempting to access %s\n", filename);
  177         if (access(filename, R_OK) == 0)
  178             return(strncpy(fullpath,filename,79));
  179     }
  180     if (*filename == '/') {
  181         debug(3, "search_path: attempting to access %s\n", filename);
  182         if (access(filename, R_OK) == 0)
  183             return(strncpy(fullpath,filename,79));
  184     }
  185 
  186     if (rindex(filename, '/')) {
  187         filename = rindex(filename, '/');
  188         filename++;
  189     }
  190 
  191     /* then look for it in the path */
  192     while ((curpath = extract_colon_unit(pathlist, &i)) != NULL) {
  193         snprintf(fullpath, sizeof(char)*80, "%s/%s", curpath, filename);
  194         debug(3, "search_path: attempting to access %s\n", fullpath);
  195         if (access(fullpath, R_OK) == 0) {
  196             free(curpath);
  197             return(fullpath);
  198         }
  199         free(curpath);
  200     }
  201 
  202     free(fullpath);
  203     free(curpath);
  204 
  205     fprintf(stderr, "search_path: couldn't find or read %s\n", filename);
  206     exit(EXIT_FAILURE);
  207 }