comparison th_file.c @ 483:85039ab7e6ab

Add simplistic support for DJGPP/DOS.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 19 Feb 2019 06:12:18 +0200
parents e4ce60239d16
children 4a3d561d5406
comparison
equal deleted inserted replaced
482:1013ddd2759b 483:85039ab7e6ab
6 * Please read file 'COPYING' for information on license and distribution. 6 * Please read file 'COPYING' for information on license and distribution.
7 */ 7 */
8 #include "th_file.h" 8 #include "th_file.h"
9 #include "th_string.h" 9 #include "th_string.h"
10 #include <unistd.h> 10 #include <unistd.h>
11 #ifdef TH_PLAT_WINDOWS 11 #if defined(TH_PLAT_WINDOWS)
12 # include <shlwapi.h> 12 # include <shlwapi.h>
13 # include <shfolder.h> 13 # include <shfolder.h>
14 #elif defined(TH_PLAT_DOS)
15 # include <sys/stat.h>
16 # include <sys/types.h>
14 #else 17 #else
15 //# include <sys/wait.h> 18 //# include <sys/wait.h>
16 # include <sys/stat.h> 19 # include <sys/stat.h>
17 # include <sys/types.h> 20 # include <sys/types.h>
18 #endif 21 #endif
22 { 25 {
23 #if defined(TH_PLAT_WINDOWS) 26 #if defined(TH_PLAT_WINDOWS)
24 char tmpPath[MAX_PATH]; 27 char tmpPath[MAX_PATH];
25 if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, tmpPath) == S_OK) 28 if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, tmpPath) == S_OK)
26 return th_strdup(tmpPath); 29 return th_strdup(tmpPath);
30 #elif defined(TH_PLAT_DOS)
31 return th_strdup("C:\\");
32 #else
33 return th_strdup(getenv("HOME"));
27 #endif 34 #endif
28 return th_strdup(getenv("HOME"));
29 } 35 }
30 36
31 37
32 char * th_get_data_dir() 38 char * th_get_data_dir()
33 { 39 {
34 #if defined(TH_PLAT_WINDOWS) 40 #if defined(TH_PLAT_WINDOWS)
35 char tmpPath[MAX_PATH]; 41 char tmpPath[MAX_PATH];
36 if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, tmpPath) == S_OK) 42 if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, tmpPath) == S_OK)
37 return th_strdup(tmpPath); 43 return th_strdup(tmpPath);
44 #elif defined(TH_PLAT_DOS)
45 return th_strdup("C:\\");
46 #else
47 return th_strdup(getenv("HOME"));
38 #endif 48 #endif
39 return th_strdup(getenv("HOME"));
40 } 49 }
41 50
42 51
43 char * th_get_config_dir(const char *name) 52 char * th_get_config_dir(const char *name)
44 { 53 {
79 #endif 88 #endif
80 89
81 90
82 BOOL th_stat_path(const char *path, th_stat_data *data) 91 BOOL th_stat_path(const char *path, th_stat_data *data)
83 { 92 {
84 #ifdef TH_PLAT_WINDOWS 93 #if defined(TH_PLAT_WINDOWS)
85 WIN32_FILE_ATTRIBUTE_DATA fdata; 94 WIN32_FILE_ATTRIBUTE_DATA fdata;
86 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &fdata)) 95 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &fdata))
87 return FALSE; 96 return FALSE;
88 97
89 data->size = (((uint64_t) fdata.nFileSizeHigh) << 32ULL) | ((uint64_t) fdata.nFileSizeLow); 98 data->size = (((uint64_t) fdata.nFileSizeHigh) << 32ULL) | ((uint64_t) fdata.nFileSizeLow);
92 data->mtime = th_convert_windows_time(fdata.ftLastWriteTime); 101 data->mtime = th_convert_windows_time(fdata.ftLastWriteTime);
93 102
94 data->flags = (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? TH_IS_DIR : 0; 103 data->flags = (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? TH_IS_DIR : 0;
95 data->flags |= (fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0 : TH_IS_WRITABLE; 104 data->flags |= (fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0 : TH_IS_WRITABLE;
96 data->flags |= TH_IS_READABLE; 105 data->flags |= TH_IS_READABLE;
106
97 #else 107 #else
108 #if defined(TH_PLAT_UNIX)
98 uid_t uid = geteuid(); 109 uid_t uid = geteuid();
99 gid_t gid = getegid(); 110 gid_t gid = getegid();
111 #endif
112
100 struct stat sb; 113 struct stat sb;
101 if (stat(path, &sb) < 0) 114 if (stat(path, &sb) < 0)
102 return FALSE; 115 return FALSE;
103 116
104 data->size = sb.st_size; 117 data->size = sb.st_size;
106 data->atime = sb.st_atime; 119 data->atime = sb.st_atime;
107 data->mtime = sb.st_mtime; 120 data->mtime = sb.st_mtime;
108 121
109 data->flags = S_ISDIR(sb.st_mode) ? TH_IS_DIR : 0; 122 data->flags = S_ISDIR(sb.st_mode) ? TH_IS_DIR : 0;
110 123
124 #if defined(TH_PLAT_UNIX)
111 if ((uid == sb.st_uid && (sb.st_mode & S_IWUSR)) || 125 if ((uid == sb.st_uid && (sb.st_mode & S_IWUSR)) ||
112 (gid == sb.st_gid && (sb.st_mode & S_IWGRP)) || 126 (gid == sb.st_gid && (sb.st_mode & S_IWGRP)) ||
113 (sb.st_mode & S_IWOTH)) 127 (sb.st_mode & S_IWOTH))
114 data->flags |= TH_IS_WRITABLE; 128 data->flags |= TH_IS_WRITABLE;
115 129
116 if ((uid == sb.st_uid && (sb.st_mode & S_IRUSR)) || 130 if ((uid == sb.st_uid && (sb.st_mode & S_IRUSR)) ||
117 (gid == sb.st_gid && (sb.st_mode & S_IRGRP)) || 131 (gid == sb.st_gid && (sb.st_mode & S_IRGRP)) ||
118 (sb.st_mode & S_IROTH)) 132 (sb.st_mode & S_IROTH))
119 data->flags |= TH_IS_READABLE; 133 data->flags |= TH_IS_READABLE;
134 #elif defined(TH_PLAT_DOS)
135 data->flags |= TH_IS_READABLE | TH_IS_WRITABLE;
136 #endif
137
120 #endif 138 #endif
121 139
122 return TRUE; 140 return TRUE;
123 } 141 }
124 142
153 if (exists && (sdata.flags & TH_IS_DIR) == 0) 171 if (exists && (sdata.flags & TH_IS_DIR) == 0)
154 goto error; 172 goto error;
155 173
156 if (!exists) 174 if (!exists)
157 { 175 {
158 #ifdef TH_PLAT_WINDOWS 176 #if defined(TH_PLAT_WINDOWS)
159 if (!CreateDirectory(path, NULL)) 177 if (!CreateDirectory(path, NULL))
160 goto error; 178 goto error;
161 #else 179 #else
162 if (mkdir(path, mode) < 0) 180 if (mkdir(path, mode) < 0)
163 goto error; 181 goto error;