home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume5 / xldimage / part01 / path.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-13  |  4.3 KB  |  201 lines

  1. /* path.c:
  2.  *
  3.  * functions that deal with the image path
  4.  *
  5.  * jim frost 10.03.89
  6.  *
  7.  * Copyright 1989 Jim Frost.  See included file "copyright.h" for complete
  8.  * copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include "xloadimage.h"
  13. #include <sys/file.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <pwd.h>
  17. #include <errno.h>
  18.  
  19. extern int errno;
  20.  
  21. static unsigned int  NumPaths= 0;
  22. static unsigned int  NumExts= 0;
  23. static char         *Paths[BUFSIZ];
  24. static char         *Exts[BUFSIZ];
  25. static char         *PathToken= "path=";
  26. static char         *ExtToken= "extension=";
  27.  
  28. #define VOIDSECTION 0
  29. #define PATHSECTION 1
  30. #define EXTSECTION  2
  31.  
  32. static void readPathsAndExts(name)
  33.      char *name;
  34. { FILE         *f;
  35.   char          tokenbuf[BUFSIZ];
  36.   char          buf[BUFSIZ];
  37.   unsigned int  secnum;
  38.   unsigned int  linenum;
  39.   unsigned int  a, b, l;
  40.   int           c;
  41.  
  42.   if (! (f= fopen(name, "r")))
  43.     return;
  44.  
  45.   secnum= VOIDSECTION;
  46.   linenum= 0;
  47.   while (fscanf(f, "%s", tokenbuf) > 0) {
  48.     linenum++;
  49.     l= strlen(tokenbuf);
  50.     for (a= 0, b= 0; a < l; a++, b++) {
  51.       if (tokenbuf[a] == '\\')
  52.     tokenbuf[b]= tokenbuf[++a];
  53.       else if (b != a)
  54.     tokenbuf[b]= tokenbuf[a];
  55.       if (tokenbuf[a] == '#') {
  56.     tokenbuf[b]= '\0';
  57.     while (((c= fgetc(f)) != '\n') && (c != EOF))
  58.       ;
  59.     break;
  60.       }
  61.     }
  62.  
  63.     if (!strncmp(tokenbuf, PathToken, strlen(PathToken))) {
  64.       secnum= PATHSECTION;
  65.       if (sscanf(tokenbuf + strlen(PathToken), "%s", buf) == 0)
  66.     continue;
  67.     }
  68.     else if (!strncmp(tokenbuf, ExtToken, strlen(ExtToken))) {
  69.       secnum= EXTSECTION;
  70.       if (sscanf(tokenbuf + strlen(ExtToken), "%s", buf) == 0)
  71.     continue;
  72.     }
  73.     else
  74.       strcpy(buf, tokenbuf);
  75.     if (buf[0] == '\0')
  76.       continue;
  77.  
  78.     switch (secnum) {
  79.     case VOIDSECTION:
  80.       printf("%s: %d: Syntax error\n", name, linenum); /* ala BASIC */
  81.       fclose(f);
  82.       return;
  83.     case PATHSECTION:
  84.       if (NumPaths < BUFSIZ - 1)
  85.     Paths[NumPaths++]= dupString(buf);
  86.       else {
  87.     printf("%s: %d: Path table overflow\n", name, linenum);
  88.     fclose(f);
  89.     return;
  90.       }
  91.       break;
  92.     case EXTSECTION:
  93.       if (NumExts < BUFSIZ - 1)
  94.     Exts[NumExts++]= dupString(buf);
  95.       else {
  96.     printf("%s: %d: Extension table overflow\n", name, linenum);
  97.     fclose(f);
  98.       }
  99.       break;
  100.     }
  101.   }
  102. }
  103.  
  104. void loadPathsAndExts()
  105. { static int     havepaths= 0;
  106.   struct passwd *pw;
  107.   char           buf[BUFSIZ];
  108.  
  109.   if (havepaths)
  110.     return;
  111.   havepaths= 1;
  112.  
  113. #ifdef SYSPATHFILE
  114.   readPathsAndExts(SYSPATHFILE);
  115. #endif
  116.   if (pw= getpwuid(getuid())) {
  117.     sprintf(buf, "%s/.xloadimagerc", pw->pw_dir);
  118.     readPathsAndExts(buf);
  119.   }
  120.   else
  121.     printf("Can't find your password file entry?!?\n");
  122. }
  123.  
  124. /* find an image with paths and extensions from defaults files.  returns
  125.  * -1 if access denied or not found, 0 if ok.
  126.  */
  127.  
  128. int findImage(name, fullname)
  129.      char *name, *fullname;
  130. { unsigned int   p, e;
  131.   struct stat    sbuf;
  132.  
  133.   strcpy(fullname, name);
  134.   if (! stat(fullname, &sbuf))
  135.     return(access(fullname, R_OK));
  136.   strcat(fullname, ".Z");
  137.   if (! stat(fullname, &sbuf))
  138.     return(access(fullname, R_OK));
  139.   for (p= 0; p < NumPaths; p++) {
  140.     sprintf(fullname, "%s/%s", Paths[p], name);
  141.     if (! stat(fullname, &sbuf))
  142.       return(access(fullname, R_OK));
  143.     strcat(fullname, ".Z");
  144.     if (! stat(fullname, &sbuf))
  145.       return(access(fullname, R_OK));
  146.     for (e= 0; e < NumExts; e++) {
  147.       sprintf(fullname, "%s/%s%s", Paths[p], name, Exts[e]);
  148.       if (! stat(fullname, &sbuf))
  149.     return(access(fullname, R_OK));
  150.       strcat(fullname, ".Z");
  151.       if (! stat(fullname, &sbuf))
  152.     return(access(fullname, R_OK));
  153.     }
  154.   }
  155.   errno= ENOENT; /* file not found */
  156.   return(-1);
  157. }
  158.  
  159. /* list images along our path
  160.  */
  161.  
  162. void listImages()
  163. { unsigned int a;
  164.   char         buf[BUFSIZ];
  165.  
  166.   if (!NumPaths) {
  167.     printf("No image path\n");
  168.     return;
  169.   }
  170.   for (a= 0; a < NumPaths; a++) {
  171.     printf("%s:\n", Paths[a]);
  172.     sprintf(buf, "ls %s", Paths[a]);
  173.     if (system(buf) < 0) {
  174.       perror("ls");
  175.       return;
  176.     }
  177.   }
  178.   return;
  179. }
  180.  
  181. void showPath()
  182. { int a;
  183.  
  184.   if (!NumPaths && !NumExts) {
  185.     printf("No image paths or extensions\n");
  186.     return;
  187.   }
  188.   if (NumPaths) {
  189.     printf("Image path:");
  190.     for (a= 0; a < NumPaths; a++)
  191.       printf(" %s", Paths[a]);
  192.     printf("\n");
  193.   }
  194.   if (NumExts) {
  195.     printf("Image extensions:");
  196.     for (a= 0; a < NumExts; a++)
  197.       printf(" %s", Exts[a]);
  198.     printf("\n");
  199.   }
  200. }
  201.