unix_file.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. Copyright (C) 2004 Michael Liebscher
  3. Copyright (C) 1997-2001 Id Software, Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. /*
  17. * unix_file.c: Handles non-portable file services.
  18. *
  19. * Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
  20. * Date: 2004
  21. *
  22. * Acknowledgement:
  23. * Portion of this code was derived from Quake II, and was originally
  24. * written by Id Software, Inc.
  25. *
  26. */
  27. #include "../wolfiphone.h"
  28. PRIVATE char findbase[ MAX_OSPATH ];
  29. PRIVATE char findpath[ MAX_OSPATH ];
  30. PRIVATE char findpattern[ MAX_OSPATH ];
  31. PRIVATE DIR *fdir;
  32. /*
  33. -----------------------------------------------------------------------------
  34. Function: FS_CreateDirectory() -Creates a new directory.
  35. Parameters: dirname -[in] Pointer to a NUL-terminated string that specifies
  36. the path of the directory to be created.
  37. Returns: On success nonzero, otherwise zero.
  38. Notes:
  39. -----------------------------------------------------------------------------
  40. */
  41. PUBLIC W8 FS_CreateDirectory( const char *dirname )
  42. {
  43. int ret_val = mkdir( dirname, S_IRUSR | S_IWUSR | S_IXUSR );
  44. if( ret_val == -1 && errno == EEXIST )
  45. {
  46. return 1;
  47. }
  48. return (W8)(! ret_val);
  49. }
  50. /*
  51. -----------------------------------------------------------------------------
  52. Function: FS_ChangeCurrentDirectory() -Changes the current directory
  53. Parameters: path -[in] Pointer to a NUL-terminated string that specifies
  54. the path to the new directory.
  55. Returns: On success nonzero, otherwise zero.
  56. Notes:
  57. -----------------------------------------------------------------------------
  58. */
  59. PUBLIC W8 FS_ChangeCurrentDirectory( const char *path )
  60. {
  61. return ! chdir( path );
  62. }
  63. /*
  64. -----------------------------------------------------------------------------
  65. Function: CompareAttributes() -Compare directory and file attributes.
  66. Parameters: path -[in] Specifies the path to compare file attributes.
  67. musthave -[in] File or directory must have these attributes.
  68. canthave- [in] File or directory can not have these attributes.
  69. Returns: On success true, otherwise false.
  70. Notes:
  71. -----------------------------------------------------------------------------
  72. */
  73. PRIVATE _boolean CompareAttributes( const char *path, W32 musthave, W32 canthave )
  74. {
  75. struct stat st;
  76. if( stat( path, &st ) == -1 )
  77. {
  78. return false;
  79. }
  80. if( ( st.st_mode & S_IFDIR ) && ( canthave & FA_DIR ) )
  81. {
  82. return false;
  83. }
  84. if( ( musthave & FA_DIR ) && !( st.st_mode & S_IFDIR ) )
  85. {
  86. return false;
  87. }
  88. return true;
  89. }
  90. /*
  91. -----------------------------------------------------------------------------
  92. Function: FS_FindFirstFile() -Searches a directory for a file.
  93. Parameters: path -[in] Pointer to a NUL-terminated string that specifies
  94. a valid directory or path and file name.
  95. musthave -[in] File or directory must have these attributes.
  96. canthave- [in] File or directory can not have these attributes.
  97. Returns: On success string of file name or directory, otherwise NULL.
  98. Notes:
  99. -----------------------------------------------------------------------------
  100. */
  101. PUBLIC char *FS_FindFirst( const char *path, W32 musthave, W32 canthave )
  102. {
  103. struct dirent *d;
  104. char *p;
  105. p;
  106. if( fdir )
  107. {
  108. Com_Printf( "FS_FindFirst without close\n" );
  109. return NULL;
  110. }
  111. FS_FilePath( (char *)path, findbase );
  112. my_strlcpy( (char *)findpattern, FS_SkipPath( (char *)path ), sizeof( findpattern ) );
  113. if( ! *findbase )
  114. {
  115. if( (fdir = opendir( "." )) == NULL )
  116. {
  117. return NULL;
  118. }
  119. }
  120. else
  121. {
  122. if( (fdir = opendir( findbase )) == NULL )
  123. {
  124. return NULL;
  125. }
  126. }
  127. while( (d = readdir( fdir )) != NULL )
  128. {
  129. if( ! *findpattern || glob_match( findpattern, d->d_name ) )
  130. {
  131. if( ! *findbase )
  132. {
  133. my_strlcpy( findpath, d->d_name, sizeof( findpath ) );
  134. }
  135. else
  136. {
  137. my_snprintf( findpath, sizeof( findpath ), "%s/%s", findbase, d->d_name );
  138. }
  139. if( CompareAttributes( findpath, musthave, canthave ) )
  140. {
  141. return findpath;
  142. }
  143. }
  144. }
  145. return NULL;
  146. }
  147. /*
  148. -----------------------------------------------------------------------------
  149. Function: FS_FindNext -Continues a file search from a previous call to
  150. the FS_FindFirst function.
  151. Parameters: musthave -[in] File or directory must have these attributes.
  152. canthave- [in] File or directory can not have these attributes.
  153. Returns: On success string of file name or directory, otherwise NULL.
  154. Notes:
  155. -----------------------------------------------------------------------------
  156. */
  157. PUBLIC char *FS_FindNext( W32 musthave, W32 canthave )
  158. {
  159. struct dirent *d;
  160. if( fdir == NULL )
  161. {
  162. return NULL;
  163. }
  164. while( (d = readdir( fdir ) ) != NULL)
  165. {
  166. if( ! *findpattern || glob_match( findpattern, d->d_name ) )
  167. {
  168. if( ! *findbase )
  169. {
  170. my_strlcpy( findpath, d->d_name, sizeof( findpath ) );
  171. }
  172. else
  173. {
  174. my_snprintf( findpath, sizeof( findpath ), "%s/%s", findbase, d->d_name );
  175. }
  176. if( CompareAttributes( findpath, musthave, canthave ) )
  177. {
  178. return findpath;
  179. }
  180. }
  181. }
  182. return NULL;
  183. }
  184. /*
  185. -----------------------------------------------------------------------------
  186. Function: FS_FindClose() -Closes the search handle.
  187. Parameters: Nothing.
  188. Returns: Nothing.
  189. Notes:
  190. -----------------------------------------------------------------------------
  191. */
  192. PUBLIC void FS_FindClose( void )
  193. {
  194. if( fdir )
  195. {
  196. closedir( fdir );
  197. }
  198. fdir = NULL;
  199. }
  200. /*
  201. -----------------------------------------------------------------------------
  202. Function: FS_DeleteFile() -Deletes an existing file.
  203. Parameters: filename -[in] Pointer to a NUL-terminated string that
  204. specifies the file to be deleted.
  205. Returns: If successful the return value is nonzero, otherwise zero.
  206. Notes:
  207. -----------------------------------------------------------------------------
  208. */
  209. PUBLIC _boolean FS_DeleteFile( const char *filename )
  210. {
  211. return( ! unlink( filename ) );
  212. }
  213. /*
  214. -----------------------------------------------------------------------------
  215. Function: FS_RemoveDirectory() -Deletes an existing empty directory.
  216. Parameters: pathname -[in] Pointer to a NUL-terminated string that
  217. specifies the directory to be deleted.
  218. Returns: If successful the return value is nonzero, otherwise zero.
  219. Notes:
  220. -----------------------------------------------------------------------------
  221. */
  222. PUBLIC _boolean FS_RemoveDirectory( const char *pathname )
  223. {
  224. return( ! rmdir( pathname ) );
  225. }