Mercurial > hg > th-libs
annotate th_file.c @ 783:db1a132c7754
Add missing void to mark empty function argument lists.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 20 Dec 2023 07:27:35 +0200 |
parents | 64af66cae541 |
children |
rev | line source |
---|---|
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * File, directory etc helper functions |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
726 | 4 * (C) Copyright 2016-2022 Tecnic Software productions (TNSP) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 #include "th_file.h" |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 #include "th_string.h" |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 #include <unistd.h> |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
11 #if defined(TH_PLAT_WINDOWS) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 # include <shlwapi.h> |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 # include <shfolder.h> |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
14 #elif defined(TH_PLAT_DOS) |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
15 # include <sys/stat.h> |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
16 # include <sys/types.h> |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 #else |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 //# include <sys/wait.h> |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 # include <sys/stat.h> |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 # include <sys/types.h> |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 |
739
32dab9b6058a
Add missing void to function arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
24 char * th_get_home_dir(void) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 #if defined(TH_PLAT_WINDOWS) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 char tmpPath[MAX_PATH]; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, tmpPath) == S_OK) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 return th_strdup(tmpPath); |
485
4a3d561d5406
Return empty directory paths under Windows if we can't retrieve AppData
Matti Hamalainen <ccr@tnsp.org>
parents:
483
diff
changeset
|
30 else |
4a3d561d5406
Return empty directory paths under Windows if we can't retrieve AppData
Matti Hamalainen <ccr@tnsp.org>
parents:
483
diff
changeset
|
31 return th_strdup(""); |
4a3d561d5406
Return empty directory paths under Windows if we can't retrieve AppData
Matti Hamalainen <ccr@tnsp.org>
parents:
483
diff
changeset
|
32 |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
33 #elif defined(TH_PLAT_DOS) |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
34 return th_strdup("C:\\"); |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
35 #else |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
36 return th_strdup(getenv("HOME")); |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 |
783
db1a132c7754
Add missing void to mark empty function argument lists.
Matti Hamalainen <ccr@tnsp.org>
parents:
742
diff
changeset
|
41 char * th_get_data_dir(void) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 #if defined(TH_PLAT_WINDOWS) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 char tmpPath[MAX_PATH]; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, tmpPath) == S_OK) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 return th_strdup(tmpPath); |
485
4a3d561d5406
Return empty directory paths under Windows if we can't retrieve AppData
Matti Hamalainen <ccr@tnsp.org>
parents:
483
diff
changeset
|
47 else |
4a3d561d5406
Return empty directory paths under Windows if we can't retrieve AppData
Matti Hamalainen <ccr@tnsp.org>
parents:
483
diff
changeset
|
48 return th_strdup(""); |
4a3d561d5406
Return empty directory paths under Windows if we can't retrieve AppData
Matti Hamalainen <ccr@tnsp.org>
parents:
483
diff
changeset
|
49 |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
50 #elif defined(TH_PLAT_DOS) |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
51 return th_strdup("C:\\"); |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
52 #else |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
53 return th_strdup(getenv("HOME")); |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 |
740
caeb0a44ebdf
Change th_get_config_dir() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
739
diff
changeset
|
58 char * th_get_config_dir(void) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 #if defined(TH_PLAT_WINDOWS) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 // For Windows, we just use the appdata directory |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 return th_get_data_dir(); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 #elif defined(USE_XDG) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 const char *xdgConfigDir = getenv("XDG_CONFIG_HOME"); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 // If XDG is enabled, try the environment variable first |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 if (xdgConfigDir != NULL && strcmp(xdgConfigDir, "")) |
557
5c3697bfefa3
Rename TH_DIR_SEPARATOR define to TH_DIR_SEPARATOR_CHR and add new
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
68 { |
740
caeb0a44ebdf
Change th_get_config_dir() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
739
diff
changeset
|
69 return th_strdup_printf("%s%c", |
caeb0a44ebdf
Change th_get_config_dir() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
739
diff
changeset
|
70 xdgConfigDir, TH_DIR_SEPARATOR_CHR); |
557
5c3697bfefa3
Rename TH_DIR_SEPARATOR define to TH_DIR_SEPARATOR_CHR and add new
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
71 } |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 else |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
73 { |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 // Nope, try the obvious alternative |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
75 char *data = th_get_data_dir(); |
740
caeb0a44ebdf
Change th_get_config_dir() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
739
diff
changeset
|
76 char *dir = th_strdup_printf("%s%c%s%c", |
557
5c3697bfefa3
Rename TH_DIR_SEPARATOR define to TH_DIR_SEPARATOR_CHR and add new
Matti Hamalainen <ccr@tnsp.org>
parents:
553
diff
changeset
|
77 data, TH_DIR_SEPARATOR_CHR, |
740
caeb0a44ebdf
Change th_get_config_dir() API.
Matti Hamalainen <ccr@tnsp.org>
parents:
739
diff
changeset
|
78 ".config", TH_DIR_SEPARATOR_CHR); |
207
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
79 th_free(data); |
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
80 return dir; |
75dbac8f2f59
Actually make th_get_config_dir() work.
Matti Hamalainen <ccr@tnsp.org>
parents:
201
diff
changeset
|
81 } |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 #else |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 // XDG not enabled |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 return th_get_data_dir(); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 #endif |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
89 #ifdef TH_PLAT_WINDOWS |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
90 static uint64_t th_convert_windows_time(FILETIME ftime) |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
91 { |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
92 uint64_t value = (((uint64_t) ftime.dwHighDateTime) << 32ULL) | ((uint64_t) ftime.dwLowDateTime); |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
93 |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
94 // Naive conversion (1000 ns / 100) * ns - difference_between_win_to_unix_epoch |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
95 return (value / ((1000 / 100) * 1000 * 1000)) - 11644473600ULL;; |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
96 } |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
97 #endif |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
98 |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
99 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
100 bool th_stat_path(const char *path, th_stat_data *data) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 { |
742
64af66cae541
Always clear stat structure in th_stat_path().
Matti Hamalainen <ccr@tnsp.org>
parents:
741
diff
changeset
|
102 memset(data, 0, sizeof(*data)); |
64af66cae541
Always clear stat structure in th_stat_path().
Matti Hamalainen <ccr@tnsp.org>
parents:
741
diff
changeset
|
103 |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
104 #if defined(TH_PLAT_WINDOWS) |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
105 WIN32_FILE_ATTRIBUTE_DATA fdata; |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
106 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &fdata)) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
107 return false; |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
108 |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
109 data->size = (((uint64_t) fdata.nFileSizeHigh) << 32ULL) | ((uint64_t) fdata.nFileSizeLow); |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
110 data->ctime = th_convert_windows_time(fdata.ftCreationTime); |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
111 data->atime = th_convert_windows_time(fdata.ftLastAccessTime); |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
112 data->mtime = th_convert_windows_time(fdata.ftLastWriteTime); |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
113 |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
114 data->flags = (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? TH_IS_DIR : 0; |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
115 data->flags |= (fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0 : TH_IS_WRITABLE; |
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
116 data->flags |= TH_IS_READABLE; |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
117 |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
118 #else |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
119 #if defined(TH_PLAT_UNIX) |
480
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
120 uid_t uid = geteuid(); |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
121 gid_t gid = getegid(); |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
122 #endif |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
123 |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 struct stat sb; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 if (stat(path, &sb) < 0) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
126 return false; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 |
475
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
128 data->size = sb.st_size; |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
129 data->ctime = sb.st_ctime; |
475
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
130 data->atime = sb.st_atime; |
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
131 data->mtime = sb.st_mtime; |
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
132 |
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
133 data->flags = S_ISDIR(sb.st_mode) ? TH_IS_DIR : 0; |
480
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
134 |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
135 #if defined(TH_PLAT_UNIX) |
480
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
136 if ((uid == sb.st_uid && (sb.st_mode & S_IWUSR)) || |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
137 (gid == sb.st_gid && (sb.st_mode & S_IWGRP)) || |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
138 (sb.st_mode & S_IWOTH)) |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
139 data->flags |= TH_IS_WRITABLE; |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
140 |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
141 if ((uid == sb.st_uid && (sb.st_mode & S_IRUSR)) || |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
142 (gid == sb.st_gid && (sb.st_mode & S_IRGRP)) || |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
143 (sb.st_mode & S_IROTH)) |
2aa2052cb17d
Fix th_stat_path() for *NIX.
Matti Hamalainen <ccr@tnsp.org>
parents:
478
diff
changeset
|
144 data->flags |= TH_IS_READABLE; |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
145 #elif defined(TH_PLAT_DOS) |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
146 data->flags |= TH_IS_READABLE | TH_IS_WRITABLE; |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
147 #endif |
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
148 |
478
b1e80180818a
Improve th_stat_path() some more and fix Windows issues.
Matti Hamalainen <ccr@tnsp.org>
parents:
475
diff
changeset
|
149 #endif |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
150 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
151 return true; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
152 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 |
741
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
155 int th_mkdir_path(const char *cpath, const int mode) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 char save, *path = th_strdup(cpath); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 size_t start = 0, end; |
741
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
159 int res = THERR_OK; |
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
160 |
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
161 if (path == NULL) |
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
162 return THERR_MALLOC; |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 |
688
a54298eef91a
th_mkdir_path() mode parameter is unused when compiling for Windows.
Matti Hamalainen <ccr@tnsp.org>
parents:
685
diff
changeset
|
164 #if defined(TH_PLAT_WINDOWS) |
a54298eef91a
th_mkdir_path() mode parameter is unused when compiling for Windows.
Matti Hamalainen <ccr@tnsp.org>
parents:
685
diff
changeset
|
165 (void) mode; |
a54298eef91a
th_mkdir_path() mode parameter is unused when compiling for Windows.
Matti Hamalainen <ccr@tnsp.org>
parents:
685
diff
changeset
|
166 #endif |
a54298eef91a
th_mkdir_path() mode parameter is unused when compiling for Windows.
Matti Hamalainen <ccr@tnsp.org>
parents:
685
diff
changeset
|
167 |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
168 // Start creating the directory stucture |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 do |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
170 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
171 // Split foremost path element out |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 for (save = 0, end = start; path[end] != 0; end++) |
671
45c2e474a997
Support both UNIX and DOS/Windows path separators in th_mkdir_path().
Matti Hamalainen <ccr@tnsp.org>
parents:
557
diff
changeset
|
173 if (path[end] == '/' || path[end] == '\\') |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 { |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 save = path[end]; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 path[end] = 0; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
177 break; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 // If the element is there, create it |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 if (path[start] != 0) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
182 { |
475
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
183 th_stat_data sdata; |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
184 bool exists = th_stat_path(path, &sdata); |
475
77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
Matti Hamalainen <ccr@tnsp.org>
parents:
457
diff
changeset
|
185 if (exists && (sdata.flags & TH_IS_DIR) == 0) |
741
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
186 { |
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
187 res = THERR_FWRITE; |
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
188 goto out; |
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
189 } |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
190 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
191 if (!exists) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 { |
483
85039ab7e6ab
Add simplistic support for DJGPP/DOS.
Matti Hamalainen <ccr@tnsp.org>
parents:
481
diff
changeset
|
193 #if defined(TH_PLAT_WINDOWS) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 if (!CreateDirectory(path, NULL)) |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 #else |
685 | 196 if (mkdir(path, mode != 0 ? mode : 0711) < 0) |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
197 #endif |
741
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
198 { |
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
199 res = th_get_error(); |
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
200 goto out; |
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
201 } |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
202 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
203 } |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
204 |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
205 // Restore separator character and jump to next element |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
206 path[end] = save; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
207 start = end + 1; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 } while (save != 0); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
209 |
741
0852bd106034
Change th_mkdir_path() API, instead if returning boolean, return a th-lib error code.
Matti Hamalainen <ccr@tnsp.org>
parents:
740
diff
changeset
|
210 out: |
184
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
211 th_free(path); |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
212 return res; |
b256db93cf25
Initial import of th_file module.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
213 } |