-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathunix.c
More file actions
192 lines (175 loc) · 4.6 KB
/
Copy pathunix.c
File metadata and controls
192 lines (175 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright 2009-2021 Peter Kosyh <p.kosyh at gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <limits.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "externals.h"
#include "internals.h"
static char save_path[PATH_MAX];
static char cfg_path[PATH_MAX];
static char local_games_path[PATH_MAX];
static char local_themes_path[PATH_MAX];
static char local_stead_path[PATH_MAX];
char *game_locale(void)
{
char *p;
char *s;
p = getenv("LANG");
if (!p || !(s = strdup(p)))
return NULL;
if ((p = strchr(s, '_')))
*p = 0;
return s;
}
char *game_tmp_path(void)
{
static time_t t = 0;
static char tmp[PATH_MAX]="/tmp/instead-games";
if (!t) {
t = time(NULL);
sprintf(tmp, "/tmp/instead-games-%lu", (unsigned long)t);
}
if (mkdir(tmp, S_IRWXU) && errno != EEXIST)
return NULL;
return tmp;
}
char *appdir(void)
{
static char dir[PATH_MAX] = "";
struct passwd *pw;
#ifdef _LOCAL_APPDATA
if (!appdata_sw) {
strcpy(dir, game_cwd);
strcat(dir, "/appdata");
}
#endif
if (appdata_sw)
strcpy(dir, appdata_sw);
if (dir[0] && !access(dir, W_OK))
return dir;
pw = getpwuid(getuid());
if (!pw)
return NULL;
snprintf(dir, sizeof(dir) - 1 , "%s/.instead", pw->pw_dir);
return dir;
}
char *game_local_games_path(int cr)
{
char *app = appdir();
if (!app)
return NULL;
strcpy(local_games_path, app);
if (cr) {
if (mkdir(local_games_path, S_IRWXU) && errno != EEXIST)
return NULL;
}
strcat(local_games_path,"/games");
if (cr) {
if (mkdir(local_games_path, S_IRWXU) && errno != EEXIST)
return NULL;
}
return local_games_path;
}
char *game_local_themes_path(void)
{
char *app = appdir();
if (!app)
return NULL;
snprintf(local_themes_path, sizeof(local_themes_path) - 1 , "%s/themes", app);
return local_themes_path;
}
char *instead_local_stead_path(void)
{
char *app = appdir();
if (!app)
return NULL;
snprintf(local_stead_path, sizeof(local_stead_path) - 1 , "%s/stead", app);
return local_stead_path;
}
char *game_cfg_path(void)
{
char *app = appdir();
struct passwd *pw;
if (app) {
snprintf(cfg_path, sizeof(cfg_path) - 1 , "%s/", app);
if (mkdir(cfg_path, S_IRWXU) == 0 || errno == EEXIST) {
snprintf(cfg_path, sizeof(cfg_path) - 1 , "%s/insteadrc", app);
return cfg_path;
}
}
pw = getpwuid(getuid());
if (!pw)
return NULL;
snprintf(cfg_path, sizeof(cfg_path) - 1 , "%s/.insteadrc", pw->pw_dir); /* at home */
return cfg_path;
}
char *game_save_path(int cr, int nr)
{
char *app = appdir();
if (!curgame_dir)
return NULL;
if (!access("saves", R_OK)) {
if (nr)
snprintf(save_path, sizeof(save_path) - 1, "saves/save%d", nr);
else
snprintf(save_path, sizeof(save_path) - 1, "saves/autosave");
return save_path;
}
if (!app)
return NULL;
snprintf(save_path, sizeof(save_path) - 1 , "%s/", app);
if (cr && mkdir(save_path, S_IRWXU) && errno != EEXIST)
return NULL;
snprintf(save_path, sizeof(save_path) - 1 , "%s/saves", app);
if (cr && mkdir(save_path, S_IRWXU) && errno != EEXIST)
return NULL;
snprintf(save_path, sizeof(save_path) - 1, "%s/saves/%s/", app, curgame_dir);
if (cr && mkdir(save_path, S_IRWXU) && errno != EEXIST)
return NULL;
if (nr)
snprintf(save_path, sizeof(save_path) - 1, "%s/saves/%s/save%d", app, curgame_dir, nr);
else
snprintf(save_path, sizeof(save_path) - 1, "%s/saves/%s/autosave", app, curgame_dir);
return save_path;
}
int debug_init(void)
{
return 0;
}
void debug_done()
{
}
char *sdl_path(char *p)
{
unix_path(p);
return p;
}