2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /************************************<+>*************************************
24 ****************************************************************************
28 ** RCS: $XConsortium: Qualify.c /main/3 1995/10/26 15:09:47 rswiston $
32 ** Description: Fully qualify a file with the first path found
33 ** in a list of colon-separated paths
35 ** (c) Copyright 1993 by Hewlett-Packard Company
37 ****************************************************************************
38 ************************************<+>*************************************/
42 /*********************************************************************
45 * takes: an unqualified filename like foo.txt, and
46 * a colon-separated list of pathnames, such as
47 * /etc/dt:/usr/dt/config
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.
57 * filename = _DtQualifyWithFirst("configFile",
58 * "/foo/first/location:/foo/second/choice");
62 **********************************************************************/
64 char * _DtQualifyWithFirst
70 char * paths = searchPath;
75 /* assert that the arguments cannot be NULL and cannot be empty */
77 if (filename == NULL || searchPath == NULL ||
78 filename[0] == 0 || searchPath[0] == 0)
83 /* if there is a :, zero it */
85 if ((path = strchr(paths, ':')) != NULL)
88 /* allocate space and create the qualified filename */
90 chance = (char *)malloc(strlen(paths) + strlen(filename) + 2);
91 if (filename[0] == '/')
92 sprintf(chance,"%s%s",paths,filename);
94 sprintf(chance,"%s/%s",paths,filename);
96 /* see if it is there by opening it for reading */
98 if (f = fopen(chance,"r")) {
99 fclose(f); /* it's there so close it, .... */
100 if (path) /* ... restore the colon, .... */
102 return chance; /* return the fully qualified filename */
106 if (path == NULL) /* reached the end of the list of paths */
108 *path = ':'; /* restore the colon */
109 paths = path + 1; /* try the next path */