"Fossies" - the Fresh Open Source Software Archive

Member "unrtf-0.21.9/src/path.c" (22 Dec 2014, 2615 Bytes) of package /linux/privat/unrtf-0.21.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. For more information about "path.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.21.8_vs_0.21.9.

    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <unistd.h>
    4 #include <stdlib.h>
    5 
    6 #include "path.h"
    7 #include "malloc.h"
    8 
    9 char *search_path;
   10 int   path_checked;
   11 
   12 static int n_path_dirs;
   13 static struct path_dir topdir;
   14 
   15 char *search_in_path(const char *name, char *suffix)
   16 {
   17     int maxplen = 0;
   18     int l;
   19     char *fullname = 0;
   20     char *path = 0;
   21     struct path_dir *path_dir_p;
   22 
   23     if (n_path_dirs == 0) {
   24     fprintf(stderr,"no directories to search for %s\n", name);
   25     return NULL;
   26     }
   27     l = strlen(name) + (suffix?strlen(suffix):0) + 2;
   28     if ((fullname = my_malloc(l)) == NULL) {
   29     fprintf(stderr, "Out of memory\n");
   30     return NULL;
   31     }
   32     strcpy(fullname, name);
   33     if (suffix && *suffix && suffix[0] != '.')
   34     strcat(fullname, ".");
   35     if (suffix && *suffix)
   36     strcat(fullname, suffix);
   37     maxplen = 0;
   38 
   39     for(path_dir_p = &topdir; path_dir_p->dir_name; 
   40     path_dir_p = path_dir_p->next) {
   41     l = strlen(path_dir_p->dir_name);
   42     if (maxplen < l)
   43         maxplen = l;
   44     }
   45     maxplen += strlen(fullname) + 2; /* final 0 + possible '/' */
   46     if ((path = my_malloc(maxplen)) == NULL) {
   47     fprintf(stderr, "Out of memory\n");
   48     return NULL;
   49     }
   50 
   51     for (path_dir_p = &topdir; path_dir_p->dir_name; 
   52      path_dir_p = path_dir_p->next) {
   53 
   54     strcpy(path, path_dir_p->dir_name);
   55 
   56     if(path[strlen(path)-1] != '/')
   57         strcat(path, "/");
   58     strcat(path, fullname);
   59     /* fprintf(stderr, "Testing for [%s]\n", path); */
   60     if(access(path, F_OK|R_OK))
   61         continue;
   62     my_free(fullname);
   63     return path;
   64     }
   65     my_free(fullname);
   66     my_free(path);
   67     return NULL;
   68 }
   69 
   70 int check_dirs()
   71 {
   72     char *p;
   73     char *colon;
   74     char *dir_name;
   75     struct path_dir *path_dir_p = &topdir;
   76 
   77     /*fprintf(stderr, "check_dirs: search_path: [%s]\n", search_path);*/
   78 
   79     for (p = search_path; *p != '\0';) {
   80     dir_name = p;
   81     if((colon = strchr(p, ':')) != NULL) {
   82         p = colon;
   83         *p++ = '\0';
   84     } else
   85         p += strlen(p);
   86 
   87     if(access(dir_name, R_OK|X_OK))
   88         continue;
   89     path_dir_p->dir_name = dir_name;
   90     if((path_dir_p->next = (struct path_dir *)malloc(sizeof(struct path_dir))) == NULL)
   91         {
   92         fprintf(stderr,"Out of memory\n");
   93         exit(1);
   94         }
   95     path_dir_p = path_dir_p->next;
   96     path_dir_p->dir_name = 0;
   97     n_path_dirs++;
   98     }
   99     path_checked = 1;
  100     return(n_path_dirs);
  101 }
  102 
  103 void show_dirs()
  104 {
  105     struct path_dir *path_dir_p;
  106     if(n_path_dirs == 0) {
  107     fprintf(stderr,"no directories to show\n");
  108     exit(1);
  109     }
  110     fprintf(stderr,"show_dirs: %d directories\n", n_path_dirs);
  111     for(path_dir_p = &topdir; path_dir_p->dir_name; 
  112     path_dir_p = path_dir_p->next)
  113     fprintf(stderr,"directory = %s\n", path_dir_p->dir_name);
  114 }