1 #include <stdio.h>
2 #include <unistd.h>
3 #include <signal.h>
4 #include <string.h>
5 #include <stdlib.h>
6 
7 #include "aftersteplib.h"
8 
9 /****************************************************************************
10  *
11  * Find the specified icon file somewhere along the given path.
12  *
13  * There is a possible race condition here:  We check the file and later
14  * do something with it.  By then, the file might not be accessible.
15  * Oh well.
16  *
17  ****************************************************************************/
findIconFile(char * icon,char * pathlist,int type)18 char *findIconFile(char *icon, char *pathlist, int type)
19 {
20   char *path;
21   char *dir_end;
22   int l1,l2;
23 
24   if(icon != NULL)
25     l1 = strlen(icon);
26   else
27     l1 = 0;
28 
29   if(pathlist != NULL)
30     l2 = strlen(pathlist);
31   else
32     l2 = 0;
33 
34   path = safemalloc(l1 + l2 + 10);
35   *path = '\0';
36   if (*icon == '/')
37     {
38       /* No search if icon begins with a slash */
39       strcpy(path, icon);
40       return path;
41     }
42 
43   if ((pathlist == NULL) || (*pathlist == '\0'))
44     {
45       /* No search if pathlist is empty */
46       strcpy(path, icon);
47       return path;
48     }
49 
50   /* Search each element of the pathlist for the icon file */
51   while ((pathlist)&&(*pathlist))
52     {
53       dir_end = strchr(pathlist, ':');
54       if (dir_end != NULL)
55 	{
56 	  strncpy(path, pathlist, dir_end - pathlist);
57 	  path[dir_end - pathlist] = 0;
58 	}
59       else
60 	strcpy(path, pathlist);
61 
62       strcat(path, "/");
63       strcat(path, icon);
64       if (access(path, type) == 0)
65 	return path;
66       strcat(path, ".gz");
67       if (access(path, type) == 0)
68 	return path;
69 
70       /* Point to next element of the path */
71       if(dir_end == NULL)
72 	pathlist = NULL;
73       else
74 	pathlist = dir_end + 1;
75     }
76   /* Hmm, couldn't find the file.  Return NULL */
77   free(path);
78   return NULL;
79 }
80 
81 
82