FileUtil.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 libraries 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. * File: FileUtil.c $XConsortium: FileUtil.c /main/6 1996/11/01 10:06:23 drk $
  25. * Language: C
  26. *
  27. * (c) Copyright 1988, Hewlett-Packard Company, all rights reserved.
  28. *
  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. *
  33. */
  34. #include <sys/types.h> /* stat(2) */
  35. #include <sys/stat.h> /* stat(2) */
  36. #include <sys/param.h> /* MAXPATHLEN */
  37. #include <errno.h> /* errno(2) */
  38. #ifdef __hpux
  39. #include <ndir.h> /* opendir(), directory(3C) */
  40. #else
  41. #if defined(sun) || defined(CSRG_BASED)
  42. #include <dirent.h> /* opendir(), directory(3C) */
  43. #else
  44. #include <sys/dir.h>
  45. #endif /* sun || CSRG_BASED */
  46. #endif /* __hpux */
  47. #include <X11/Xlib.h>
  48. #include <X11/Intrinsic.h> /* Xt stuff */
  49. #include <X11/StringDefs.h> /* Cardinal */
  50. #include <Dt/DtNlUtils.h>
  51. #include <Dt/ActionUtilP.h>
  52. #include <Tt/tt_c.h>
  53. /******************
  54. *
  55. * Function Name: _DtCreateDirs
  56. *
  57. * Description:
  58. *
  59. * This function is passed a directory path to create and the mode
  60. * for the directory. It will create any of the parent directories
  61. * on the path that do not already exist.
  62. *
  63. * This function may fail if any of the directories on the path already
  64. * exist and are not writable. If some component of the path already
  65. * exists and is not a directory, a failure will be returned.
  66. *
  67. * If some component of the path exists as a directory but does not have
  68. * the specified mode, this will NOT cause a failure to be returned.
  69. * This implies that if this function is called to create a writeable
  70. * directory, it is possible for the function to return successfully
  71. * but the directory may not actually be writable.
  72. *
  73. * Synopsis:
  74. *
  75. * status = _DtCreateDirs (path, mode);
  76. *
  77. * int status; Returns 0 on success and -1 on failure.
  78. * char *path; The directory path to create.
  79. * int mode; The file mode for setting any directories
  80. * that are created.
  81. *
  82. ******************/
  83. int
  84. _DtCreateDirs(
  85. char *path,
  86. int mode )
  87. {
  88. struct stat st_buf;
  89. int st_status;
  90. int ret_status;
  91. char *parent_path;
  92. char *temp_s;
  93. /* If the path already exist, make sure it is a directory. */
  94. if ((st_status = stat (path, &st_buf)) == 0) {
  95. if (S_ISDIR (st_buf.st_mode))
  96. ret_status = 0;
  97. else
  98. ret_status = -1;
  99. }
  100. /* If we can't stat it, make sure it is simply because some component
  101. of the path doesn't exist. */
  102. else if (errno != ENOENT)
  103. ret_status = -1;
  104. else {
  105. /* Some component of the path doesn't exist. Recursively make the
  106. parent of the current directory, then make the current directory. */
  107. parent_path = XtNewString (path);
  108. if ((temp_s = DtStrrchr (parent_path, '/')) != NULL) {
  109. *temp_s = '\0';
  110. (void) _DtCreateDirs (parent_path, mode);
  111. }
  112. XtFree (parent_path);
  113. /* If no error has been encountered, now make the final directory
  114. in the path. */
  115. if ((ret_status = mkdir (path, S_IFDIR)) == 0)
  116. (void) chmod (path, mode);
  117. }
  118. return (ret_status);
  119. }
  120. /******************
  121. *
  122. * Function Name: _DtIsOpenableDirContext
  123. *
  124. * Description:
  125. *
  126. * This function takes a path as an argument and determines whether
  127. * the path is a directory that can be opened. This function returns
  128. * "1" if the path is an openable directory and "0" if it is not.
  129. * In addition, if the calling function passes in another pointer,
  130. * we will return the internal representation for the path.
  131. *
  132. * The path can be in the Softbench "context" form of "host:/path/dir".
  133. *
  134. * Synopsis:
  135. *
  136. * status = _DtIsOpenableDirContext (cpath, ret_ptr)
  137. *
  138. * int status; Returns 1 for openable directories,
  139. * 0 otherwise.
  140. * char *cpath; The directory name to test.
  141. * char ** ret_ptr; Where to place internal format.
  142. *
  143. ******************/
  144. int
  145. _DtIsOpenableDirContext(
  146. char *path,
  147. char **ret_path )
  148. {
  149. char *real_path = NULL;
  150. char * tmp_real_path;
  151. DIR *dirp;
  152. int ret_status;
  153. Tt_status status;
  154. char * host;
  155. char * filename;
  156. char * netfile;
  157. if (ret_path)
  158. *ret_path = NULL;
  159. host = _DtHostString(path);
  160. filename = _DtPathname(path);
  161. if (host)
  162. {
  163. netfile = tt_host_file_netfile(host, filename);
  164. if ((status = tt_ptr_error(netfile)) == TT_OK)
  165. {
  166. tmp_real_path = tt_netfile_file(netfile);
  167. status = tt_ptr_error(real_path);
  168. tt_free(netfile);
  169. }
  170. if (status != TT_OK) {
  171. real_path = NULL;
  172. } else {
  173. real_path = XtNewString(tmp_real_path);
  174. tt_free(tmp_real_path);
  175. }
  176. XtFree(filename);
  177. XtFree(host);
  178. }
  179. else
  180. real_path = filename;
  181. if (real_path && ((dirp = opendir (real_path)) != NULL))
  182. {
  183. closedir (dirp);
  184. ret_status = 1;
  185. if (ret_path)
  186. *ret_path = real_path;
  187. }
  188. else
  189. {
  190. ret_status = 0;
  191. if (real_path)
  192. XtFree(real_path);
  193. }
  194. return (ret_status);
  195. }
  196. /******************
  197. *
  198. * Function Name: _DtIsOpenableDir
  199. *
  200. * Description:
  201. *
  202. * This function takes a path as an argument and determines whether
  203. * the path is a directory that can be opened. This function returns
  204. * "1" if the path is an openable directory and "0" if it is not.
  205. *
  206. * The path can be in the Softbench "context" form of "host:/path/dir".
  207. *
  208. * Synopsis:
  209. *
  210. * status = _DtIsOpenableDir (cpath)
  211. *
  212. * int status; Returns 1 for openable directories,
  213. * 0 otherwise.
  214. * char *cpath; The directory name to test.
  215. *
  216. ******************/
  217. int
  218. _DtIsOpenableDir(
  219. char *path )
  220. {
  221. return (_DtIsOpenableDirContext(path, NULL));
  222. }