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 libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * File: FileUtil.c $XConsortium: FileUtil.c /main/6 1996/11/01 10:06:23 drk $
27 * (c) Copyright 1988, Hewlett-Packard Company, all rights reserved.
29 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
30 * (c) Copyright 1993, 1994 International Business Machines Corp. *
31 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
32 * (c) Copyright 1993, 1994 Novell, Inc. *
35 #include <sys/types.h> /* stat(2) */
36 #include <sys/stat.h> /* stat(2) */
37 #include <sys/param.h> /* MAXPATHLEN */
38 #include <errno.h> /* errno(2) */
41 #include <ndir.h> /* opendir(), directory(3C) */
44 #include <dirent.h> /* opendir(), directory(3C) */
51 #include <X11/Intrinsic.h> /* Xt stuff */
52 #include <X11/StringDefs.h> /* Cardinal */
53 #include <Dt/DtNlUtils.h>
54 #include <Dt/ActionUtilP.h>
59 * Function Name: _DtCreateDirs
63 * This function is passed a directory path to create and the mode
64 * for the directory. It will create any of the parent directories
65 * on the path that do not already exist.
67 * This function may fail if any of the directories on the path already
68 * exist and are not writable. If some component of the path already
69 * exists and is not a directory, a failure will be returned.
71 * If some component of the path exists as a directory but does not have
72 * the specified mode, this will NOT cause a failure to be returned.
73 * This implies that if this function is called to create a writeable
74 * directory, it is possible for the function to return successfully
75 * but the directory may not actually be writable.
79 * status = _DtCreateDirs (path, mode);
81 * int status; Returns 0 on success and -1 on failure.
82 * char *path; The directory path to create.
83 * int mode; The file mode for setting any directories
99 /* If the path already exist, make sure it is a directory. */
100 if ((st_status = stat (path, &st_buf)) == 0) {
101 if (S_ISDIR (st_buf.st_mode))
107 /* If we can't stat it, make sure it is simply because some component
108 of the path doesn't exist. */
109 else if (errno != ENOENT)
113 /* Some component of the path doesn't exist. Recursively make the
114 parent of the current directory, then make the current directory. */
115 parent_path = XtNewString (path);
116 if ((temp_s = DtStrrchr (parent_path, '/')) != NULL) {
118 (void) _DtCreateDirs (parent_path, mode);
120 XtFree (parent_path);
122 /* If no error has been encountered, now make the final directory
124 if ((ret_status = mkdir (path, S_IFDIR)) == 0)
125 (void) chmod (path, mode);
133 * Function Name: _DtIsOpenableDirContext
137 * This function takes a path as an argument and determines whether
138 * the path is a directory that can be opened. This function returns
139 * "1" if the path is an openable directory and "0" if it is not.
140 * In addition, if the calling function passes in another pointer,
141 * we will return the internal representation for the path.
143 * The path can be in the Softbench "context" form of "host:/path/dir".
147 * status = _DtIsOpenableDirContext (cpath, ret_ptr)
149 * int status; Returns 1 for openable directories,
151 * char *cpath; The directory name to test.
152 * char ** ret_ptr; Where to place internal format.
157 _DtIsOpenableDirContext(
161 char *real_path = NULL;
162 char * tmp_real_path;
173 host = _DtHostString(path);
174 filename = _DtPathname(path);
177 netfile = tt_host_file_netfile(host, filename);
178 if ((status = tt_ptr_error(netfile)) == TT_OK)
180 tmp_real_path = tt_netfile_file(netfile);
181 status = tt_ptr_error(real_path);
185 if (status != TT_OK) {
188 real_path = XtNewString(tmp_real_path);
189 tt_free(tmp_real_path);
196 real_path = filename;
198 if (real_path && ((dirp = opendir (real_path)) != NULL))
203 *ret_path = real_path;
217 * Function Name: _DtIsOpenableDir
221 * This function takes a path as an argument and determines whether
222 * the path is a directory that can be opened. This function returns
223 * "1" if the path is an openable directory and "0" if it is not.
225 * The path can be in the Softbench "context" form of "host:/path/dir".
229 * status = _DtIsOpenableDir (cpath)
231 * int status; Returns 1 for openable directories,
233 * char *cpath; The directory name to test.
241 return (_DtIsOpenableDirContext(path, NULL));