Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSvc / DtUtil1 / Qualify.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /************************************<+>*************************************
24  ****************************************************************************
25  **
26  **   File:     Qualify.c
27  **
28  **   RCS:      $XConsortium: Qualify.c /main/3 1995/10/26 15:09:47 rswiston $
29  **
30  **   Project:  DT
31  **
32  **   Description: Fully qualify a file with the first path found
33  **                in a list of colon-separated paths
34  **
35  **   (c) Copyright 1993 by Hewlett-Packard Company
36  **
37  ****************************************************************************
38  ************************************<+>*************************************/
39 #include <stdio.h>
40 #include <string.h>
41
42 /*********************************************************************
43  * _DtQualifyWithFirst
44  *
45  * takes:   an unqualified filename like foo.txt, and
46  *          a colon-separated list of pathnames, such as 
47  *                /etc/dt:/usr/dt/config
48  *
49  * returns: a fully qualified filename.  Space for the filename
50  *          has been allocated off the heap using malloc.  It is 
51  *          the responsibility of the calling function to dispose 
52  *          of the space using free.
53  *
54  * example: ...
55  *          char * filename;
56  *          ...
57  *          filename = _DtQualifyWithFirst("configFile",
58  *                          "/foo/first/location:/foo/second/choice");
59  *          < use filename >
60  *          free(filename);
61  *
62  **********************************************************************/
63
64 char * _DtQualifyWithFirst
65   (
66   char * filename,
67   char * searchPath
68   )
69 {
70 char * paths = searchPath;
71 char * path;
72 char * chance;
73 FILE * f;
74
75   /* assert that the arguments cannot be NULL and cannot be empty */
76
77   if (filename == NULL || searchPath == NULL || 
78       filename[0] == 0 || searchPath[0] == 0)
79     return NULL;
80
81   while (1) {
82
83     /* if there is a :, zero it */
84
85     if ((path = strchr(paths, ':')) != NULL)
86       *path = 0;
87
88     /* allocate space and create the qualified filename */
89
90     chance = (char *)malloc(strlen(paths) + strlen(filename) + 2);
91     if (filename[0] == '/')
92       sprintf(chance,"%s%s",paths,filename);
93     else
94       sprintf(chance,"%s/%s",paths,filename);
95
96     /* see if it is there by opening it for reading */
97
98     if (f = fopen(chance,"r")) {
99       fclose(f);                    /* it's there so close it, .... */
100       if (path)                     /* ... restore the colon, .... */
101         *path = ':';
102       return chance;                /* return the fully qualified filename */
103     }
104
105     free(chance);
106     if (path == NULL)               /* reached the end of the list of paths */
107       break;
108     *path = ':';                    /* restore the colon */
109     paths = path + 1;               /* try the next path */
110   }
111   return NULL;
112 }
113
114