1 #include "util/angband-files.h"
2 #include "util/string-processor.h"
4 #include "locale/japanese.h"
12 * For those systems that don't have "usleep()" but need it.
14 * Fake "usleep()" function grabbed from the inl netrek server -cba
16 int usleep(huge usecs)
22 fd_set *no_fds = NULL;
24 core(_("不当な usleep() 呼び出し", "Illegal usleep() call"));
26 timer.tv_sec = (usecs / 1000000L);
27 timer.tv_usec = (usecs % 1000000L);
28 if (select(nfds, no_fds, no_fds, no_fds, &timer) < 0) {
38 * Hack -- External functions
41 extern struct passwd *getpwuid(uid_t uid);
42 extern struct passwd *getpwnam(concptr name);
46 * Find a default user name from the system.
48 void user_name(char *buf, int id)
51 if ((pw = getpwuid(id))) {
52 (void)strcpy(buf, pw->pw_name);
59 buf[0] = toupper(buf[0]);
64 strcpy(buf, "PLAYER");
71 * Extract a "parsed" path from an initial filename
72 * Normally, we simply copy the filename into the buffer
73 * But leading tilde symbols must be handled in a special way
74 * Replace "~user/" by the home directory of the user named "user"
75 * Replace "~/" by the home directory of the current user
77 errr path_parse(char *buf, int max, concptr file)
84 (void)strnfmt(buf, max, "%s", file);
89 concptr s = angband_strstr(u, PATH_SEP);
91 if (s && (s >= u + sizeof(user)))
96 for (i = 0; u < s; ++i)
109 pw = getpwuid(getuid());
115 strnfmt(buf, max, "%s%s", pw->pw_dir, s);
117 strnfmt(buf, max, "%s", pw->pw_dir);
123 * Extract a "parsed" path from an initial filename
125 * This requires no special processing on simple machines,
126 * except for verifying the size of the filename.
128 errr path_parse(char *buf, int max, concptr file)
130 (void)strnfmt(buf, max, "%s", file);
138 * Hack -- acquire a "temporary" file name if possible
140 * This filename is always in "system-specific" form.
142 static errr path_temp(char *buf, int max)
144 concptr s = tmpnam(NULL);
148 #if !defined(WIN32) || (defined(_MSC_VER) && (_MSC_VER >= 1900))
149 (void)strnfmt(buf, max, "%s", s);
151 (void)strnfmt(buf, max, ".%s", s);
159 * @brief ファイル入出力のためのパス生成する。/ Create a new path by appending a file (or directory) to a path.
160 * @param buf ファイルのフルを返すバッファ
164 * @return エラーコード(ただし常に0を返す)
166 * This requires no special processing on simple machines, except
167 * for verifying the size of the filename, but note the ability to
168 * bypass the given "path" with certain special file-names.
170 * Note that the "file" may actually be a "sub-path", including
173 * Note that this function yields a path which must be "parsed"
174 * using the "parse" function above.
176 errr path_build(char *buf, int max, concptr path, concptr file)
178 if (file[0] == '~') {
179 (void)strnfmt(buf, max, "%s", file);
180 } else if (prefix(file, PATH_SEP) && !streq(PATH_SEP, "")) {
181 (void)strnfmt(buf, max, "%s", file);
182 } else if (!path[0]) {
183 (void)strnfmt(buf, max, "%s", file);
185 (void)strnfmt(buf, max, "%s%s%s", path, PATH_SEP, file);
192 * Hack -- replacement for "fopen()"
194 FILE *angband_fopen(concptr file, concptr mode)
197 if (path_parse(buf, 1024, file))
200 return (fopen(buf, mode));
204 * Hack -- replacement for "fclose()"
206 errr angband_fclose(FILE *fff)
210 if (fclose(fff) == EOF)
216 FILE *angband_fopen_temp(char *buf, int max)
218 strncpy(buf, "/tmp/anXXXXXX", max);
219 int fd = mkstemp(buf);
223 return (fdopen(fd, "w"));
225 #else /* HAVE_MKSTEMP */
226 FILE *angband_fopen_temp(char *buf, int max)
228 if (path_temp(buf, max))
230 return (angband_fopen(buf, "w"));
232 #endif /* HAVE_MKSTEMP */
235 * Hack -- replacement for "fgets()"
237 * Read a string, without a newline, to a file
239 * Process tabs, strip internal non-printables
241 errr angband_fgets(FILE *fff, char *buf, huge n)
247 if (fgets(tmp, 1024, fff)) {
249 guess_convert_to_system_encoding(tmp, sizeof(tmp));
251 for (s = tmp; *s; s++) {
255 } else if (*s == '\t') {
264 else if (iskanji(*s)) {
269 } else if (iskana(*s)) {
276 else if (isprint((unsigned char)*s)) {
292 * Hack -- replacement for "fputs()"
293 * Dump a string, plus a newline, to a file
294 * Process internal weirdness?
296 errr angband_fputs(FILE *fff, concptr buf, huge n)
299 (void)fprintf(fff, "%s\n", buf);
304 * Several systems have no "O_BINARY" flag
308 #endif /* O_BINARY */
311 * Hack -- attempt to delete a file
313 errr fd_kill(concptr file)
316 if (path_parse(buf, 1024, file))
324 * Hack -- attempt to move a file
326 errr fd_move(concptr file, concptr what)
330 if (path_parse(buf, 1024, file))
332 if (path_parse(aux, 1024, what))
335 (void)rename(buf, aux);
340 * Hack -- attempt to copy a file
342 errr fd_copy(concptr file, concptr what)
349 if (path_parse(buf, 1024, file))
351 if (path_parse(aux, 1024, what))
354 src_fd = fd_open(buf, O_RDONLY);
358 dst_fd = fd_open(aux, O_WRONLY | O_TRUNC | O_CREAT);
362 while ((read_num = read(src_fd, buf, 1024)) > 0) {
364 while (write_num < read_num) {
365 int ret = write(dst_fd, buf + write_num, read_num - write_num);
383 * Hack -- attempt to open a file descriptor (create file)
384 * This function should fail if the file already exists
385 * Note that we assume that the file should be "binary"
387 int fd_make(concptr file, BIT_FLAGS mode)
390 if (path_parse(buf, 1024, file))
393 return (open(buf, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode));
397 * Hack -- attempt to open a file descriptor (existing file)
399 * Note that we assume that the file should be "binary"
401 int fd_open(concptr file, int flags)
404 if (path_parse(buf, 1024, file))
407 return (open(buf, flags | O_BINARY, 0));
411 * Hack -- attempt to lock a file descriptor
413 * Legal lock types -- F_UNLCK, F_RDLCK, F_WRLCK
415 errr fd_lock(int fd, int what)
417 what = what ? what : 0;
421 #if defined(SET_UID) && defined(LOCK_UN) && defined(LOCK_EX)
422 if (what == F_UNLCK) {
423 (void)flock(fd, LOCK_UN);
425 if (flock(fd, LOCK_EX) != 0)
434 * Hack -- attempt to seek on a file descriptor
436 errr fd_seek(int fd, huge n)
441 huge p = lseek(fd, n, SEEK_SET);
449 * Hack -- attempt to truncate a file descriptor
451 errr fd_chop(int fd, huge n)
454 return fd >= 0 ? 0 : -1;
458 * Hack -- attempt to read data from a file descriptor
460 errr fd_read(int fd, char *buf, huge n)
466 if (read(fd, buf, 16384) != 16384)
474 if (read(fd, buf, n) != (int)n)
481 * Hack -- Attempt to write data to a file descriptor
483 errr fd_write(int fd, concptr buf, huge n)
490 if (write(fd, buf, 16384) != 16384)
498 if (write(fd, buf, n) != (int)n)
505 * Hack -- attempt to close a file descriptor
507 errr fd_close(int fd)