Add GNU LGPL headers to all .c .C and .h files
[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
33 /*********************************************************************
34  * qualifyWithFirst
35  *
36  * takes:   an unqualified filename like foo.txt, and
37  *          a colon-separated list of pathnames, such as 
38  *                /etc/opt/dt:/opt/dt/config
39  *
40  * returns: a fully qualified filename.  Space for the filename
41  *          has been allocated off the heap.  It is the responsibility
42  *          of the calling function to dispose of the space.
43  **********************************************************************/
44
45 char * qualifyWithFirst
46   (
47   char * filename,
48   char * searchPath
49   )
50 {
51 char * paths = strdup(searchPath);
52 char * savepaths = paths;
53 char * path;
54 char * chance;
55 FILE * f;
56
57   /* assert that the arguments cannot be NULL */
58
59   if (filename == NULL || searchPath == NULL)
60     return NULL;
61
62   while (1) {
63
64     /* if there is a :, zero it */
65
66     if ((path = strchr(paths, ':')) != NULL)
67       *path = 0;
68
69     /* allocate space and create the qualified filename */
70
71     chance = (char *)malloc(strlen(paths) + strlen(filename) + 2);
72     sprintf(chance,"%s/%s",paths,filename);
73
74     /* see if it is there by opening it for reading */
75
76     if (f = fopen(chance,"r")) {
77
78       /* it's there so close it, .... */
79
80       fclose(f);
81
82       /* ... restore the colon, .... */
83
84       if (path)
85         *path = ':';
86
87       /* return the fully qualified filename */
88
89       free(savepaths);
90       return chance;
91     }
92
93     free(chance);
94
95     /* reached the end of the list of paths */
96
97     if (path == NULL)
98       break;
99
100     /* try the next path */
101
102     paths = path + 1;
103   }
104   free(savepaths);
105   return NULL;
106 }