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)
23 fd_set *no_fds = NULL;
28 core(_("不当な usleep() 呼び出し", "Illegal usleep() call"));
30 timer.tv_sec = (usecs / 1000000L);
31 timer.tv_usec = (usecs % 1000000L);
32 if (select(nfds, no_fds, no_fds, no_fds, &timer) < 0) {
42 * Hack -- External functions
45 extern struct passwd *getpwuid(uid_t uid);
46 extern struct passwd *getpwnam(concptr name);
50 * Find a default user name from the system.
52 void user_name(char *buf, int id)
55 if ((pw = getpwuid(id))) {
56 (void)strcpy(buf, pw->pw_name);
63 buf[0] = toupper(buf[0]);
68 strcpy(buf, "PLAYER");
75 * Extract a "parsed" path from an initial filename
76 * Normally, we simply copy the filename into the buffer
77 * But leading tilde symbols must be handled in a special way
78 * Replace "~user/" by the home directory of the user named "user"
79 * Replace "~/" by the home directory of the current user
81 errr path_parse(char *buf, int max, concptr file)
88 (void)strnfmt(buf, max, "%s", file);
93 concptr s = angband_strstr(u, PATH_SEP);
95 if (s && (s >= u + sizeof(user)))
100 for (i = 0; u < s; ++i)
113 pw = getpwuid(getuid());
119 strnfmt(buf, max, "%s%s", pw->pw_dir, s);
121 strnfmt(buf, max, "%s", pw->pw_dir);
127 * Extract a "parsed" path from an initial filename
129 * This requires no special processing on simple machines,
130 * except for verifying the size of the filename.
132 errr path_parse(char *buf, int max, concptr file)
134 (void)strnfmt(buf, max, "%s", file);
142 * Hack -- acquire a "temporary" file name if possible
144 * This filename is always in "system-specific" form.
146 static errr path_temp(char *buf, int max)
148 concptr s = tmpnam(NULL);
152 #if !defined(WIN32) || (defined(_MSC_VER) && (_MSC_VER >= 1900))
153 (void)strnfmt(buf, max, "%s", s);
155 (void)strnfmt(buf, max, ".%s", s);
163 * @brief ファイル入出力のためのパス生成する。/ Create a new path by appending a file (or directory) to a path.
164 * @param buf ファイルのフルを返すバッファ
168 * @return エラーコード(ただし常に0を返す)
170 * This requires no special processing on simple machines, except
171 * for verifying the size of the filename, but note the ability to
172 * bypass the given "path" with certain special file-names.
174 * Note that the "file" may actually be a "sub-path", including
177 * Note that this function yields a path which must be "parsed"
178 * using the "parse" function above.
180 errr path_build(char *buf, int max, concptr path, concptr file)
182 if (file[0] == '~') {
183 (void)strnfmt(buf, max, "%s", file);
184 } else if (prefix(file, PATH_SEP) && !streq(PATH_SEP, "")) {
185 (void)strnfmt(buf, max, "%s", file);
186 } else if (!path[0]) {
187 (void)strnfmt(buf, max, "%s", file);
189 (void)strnfmt(buf, max, "%s%s%s", path, PATH_SEP, file);
196 * Hack -- replacement for "fopen()"
198 FILE *angband_fopen(concptr file, concptr mode)
201 if (path_parse(buf, 1024, file))
204 return (fopen(buf, mode));
208 * Hack -- replacement for "fclose()"
210 errr angband_fclose(FILE *fff)
214 if (fclose(fff) == EOF)
220 FILE *angband_fopen_temp(char *buf, int max)
222 strncpy(buf, "/tmp/anXXXXXX", max);
223 int fd = mkstemp(buf);
227 return (fdopen(fd, "w"));
229 #else /* HAVE_MKSTEMP */
230 FILE *angband_fopen_temp(char *buf, int max)
232 if (path_temp(buf, max))
234 return (angband_fopen(buf, "w"));
236 #endif /* HAVE_MKSTEMP */
239 * Hack -- replacement for "fgets()"
241 * Read a string, without a newline, to a file
243 * Process tabs, strip internal non-printables
245 errr angband_fgets(FILE *fff, char *buf, huge n)
251 if (fgets(tmp, 1024, fff)) {
253 guess_convert_to_system_encoding(tmp, sizeof(tmp));
255 for (s = tmp; *s; s++) {
259 } else if (*s == '\t') {
268 else if (iskanji(*s)) {
273 } else if (iskana(*s)) {
280 else if (isprint((unsigned char)*s)) {
296 * Hack -- replacement for "fputs()"
297 * Dump a string, plus a newline, to a file
298 * Process internal weirdness?
300 errr angband_fputs(FILE *fff, concptr buf, huge n)
303 (void)fprintf(fff, "%s\n", buf);
308 * Several systems have no "O_BINARY" flag
312 #endif /* O_BINARY */
315 * Hack -- attempt to delete a file
317 errr fd_kill(concptr file)
320 if (path_parse(buf, 1024, file))
328 * Hack -- attempt to move a file
330 errr fd_move(concptr file, concptr what)
334 if (path_parse(buf, 1024, file))
336 if (path_parse(aux, 1024, what))
339 (void)rename(buf, aux);
344 * Hack -- attempt to copy a file
346 errr fd_copy(concptr file, concptr what)
353 if (path_parse(buf, 1024, file))
355 if (path_parse(aux, 1024, what))
358 src_fd = fd_open(buf, O_RDONLY);
362 dst_fd = fd_open(aux, O_WRONLY | O_TRUNC | O_CREAT);
366 while ((read_num = read(src_fd, buf, 1024)) > 0) {
368 while (write_num < read_num) {
369 int ret = write(dst_fd, buf + write_num, read_num - write_num);
387 * Hack -- attempt to open a file descriptor (create file)
388 * This function should fail if the file already exists
389 * Note that we assume that the file should be "binary"
391 int fd_make(concptr file, BIT_FLAGS mode)
394 if (path_parse(buf, 1024, file))
397 return (open(buf, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode));
401 * Hack -- attempt to open a file descriptor (existing file)
403 * Note that we assume that the file should be "binary"
405 int fd_open(concptr file, int flags)
408 if (path_parse(buf, 1024, file))
411 return (open(buf, flags | O_BINARY, 0));
415 * Hack -- attempt to lock a file descriptor
417 * Legal lock types -- F_UNLCK, F_RDLCK, F_WRLCK
419 errr fd_lock(int fd, int what)
421 what = what ? what : 0;
425 #if defined(SET_UID) && defined(LOCK_UN) && defined(LOCK_EX)
426 if (what == F_UNLCK) {
427 (void)flock(fd, LOCK_UN);
429 if (flock(fd, LOCK_EX) != 0)
438 * Hack -- attempt to seek on a file descriptor
440 errr fd_seek(int fd, huge n)
445 huge p = lseek(fd, n, SEEK_SET);
453 * Hack -- attempt to truncate a file descriptor
455 errr fd_chop(int fd, huge n)
458 return fd >= 0 ? 0 : -1;
462 * Hack -- attempt to read data from a file descriptor
464 errr fd_read(int fd, char *buf, huge n)
470 if (read(fd, buf, 16384) != 16384)
478 if (read(fd, buf, n) != (int)n)
485 * Hack -- Attempt to write data to a file descriptor
487 errr fd_write(int fd, concptr buf, huge n)
494 if (write(fd, buf, 16384) != 16384)
502 if (write(fd, buf, n) != (int)n)
509 * Hack -- attempt to close a file descriptor
511 errr fd_close(int fd)