4d80c883df915496802d5c3e3c3904e298ae864f
[oweals/cde.git] / cde / programs / dtlogin / 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 /* $XConsortium: qualify.c /main/3 1995/10/27 16:14:33 rswiston $ */
24 /*                                                                      *
25  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
26  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
27  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
28  * (c) Copyright 1993, 1994 Novell, Inc.                                *
29  */
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 /*********************************************************************
35  * qualifyWithFirst
36  *
37  * takes:   an unqualified filename like foo.txt, and
38  *          a colon-separated list of pathnames, such as 
39  *                /etc/opt/dt:/opt/dt/config
40  *
41  * returns: a fully qualified filename.  Space for the filename
42  *          has been allocated off the heap.  It is the responsibility
43  *          of the calling function to dispose of the space.
44  **********************************************************************/
45
46 char * qualifyWithFirst
47   (
48   char * filename,
49   char * searchPath
50   )
51 {
52 char * paths = strdup(searchPath);
53 char * savepaths = paths;
54 char * path;
55 char * chance;
56 FILE * f;
57
58   /* assert that the arguments cannot be NULL */
59
60   if (filename == NULL || searchPath == NULL)
61     return NULL;
62
63   while (1) {
64
65     /* if there is a :, zero it */
66
67     if ((path = strchr(paths, ':')) != NULL)
68       *path = 0;
69
70     /* allocate space and create the qualified filename */
71
72     chance = (char *)malloc(strlen(paths) + strlen(filename) + 2);
73     sprintf(chance,"%s/%s",paths,filename);
74
75     /* see if it is there by opening it for reading */
76
77     if (f = fopen(chance,"r")) {
78
79       /* it's there so close it, .... */
80
81       fclose(f);
82
83       /* ... restore the colon, .... */
84
85       if (path)
86         *path = ':';
87
88       /* return the fully qualified filename */
89
90       free(savepaths);
91       return chance;
92     }
93
94     free(chance);
95
96     /* reached the end of the list of paths */
97
98     if (path == NULL)
99       break;
100
101     /* try the next path */
102
103     paths = path + 1;
104   }
105   free(savepaths);
106   return NULL;
107 }