From 001986751376dedaefca4d5869f190c3bcd7ca8a Mon Sep 17 00:00:00 2001 From: Hourier Date: Mon, 29 Jun 2020 21:13:05 +0900 Subject: [PATCH] [Refactor] #38997 Added player_type * argument to uid-checker.c and chuukei.c --- src/cmd-io/cmd-diary.c | 2 +- src/core/game-closer.c | 2 +- src/core/scores.c | 4 +- src/floor/floor-save.c | 8 +- src/io/chuukei.c | 1289 +++++++++++++++++++++--------------------- src/io/chuukei.h | 16 +- src/io/files-util.c | 8 +- src/io/input-key-processor.c | 2 +- src/io/load.c | 6 +- src/io/save.c | 18 +- src/io/uid-checker.c | 91 ++- src/io/uid-checker.h | 6 +- src/main/init.c | 62 +- src/main/init.h | 2 +- 14 files changed, 741 insertions(+), 775 deletions(-) diff --git a/src/cmd-io/cmd-diary.c b/src/cmd-io/cmd-diary.c index 616b29a84..016a7176a 100644 --- a/src/cmd-io/cmd-diary.c +++ b/src/cmd-io/cmd-diary.c @@ -149,7 +149,7 @@ void do_cmd_diary(player_type *creature_ptr) break; case 'r': case 'R': screen_load(); - prepare_movie_hooks(); + prepare_movie_hooks(creature_ptr); return; default: bell(); diff --git a/src/core/game-closer.c b/src/core/game-closer.c index 73bce1276..b8414b6a6 100644 --- a/src/core/game-closer.c +++ b/src/core/game-closer.c @@ -77,7 +77,7 @@ void close_game(player_type *player_ptr) current_world_ptr->character_icky = TRUE; char buf[1024]; path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw"); - safe_setuid_grab(); + safe_setuid_grab(player_ptr); highscore_fd = fd_open(buf, O_RDWR); safe_setuid_drop(); diff --git a/src/core/scores.c b/src/core/scores.c index b15b565b7..95b2c544c 100644 --- a/src/core/scores.c +++ b/src/core/scores.c @@ -537,7 +537,7 @@ errr top_twenty(player_type *current_player_ptr) } /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(current_player_ptr); /* Lock (for writing) the highscore file, or fail */ errr err = fd_lock(highscore_fd, F_WRLCK); @@ -551,7 +551,7 @@ errr top_twenty(player_type *current_player_ptr) int j = highscore_add(&the_score); /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(current_player_ptr); /* Unlock the highscore file, or fail */ err = fd_lock(highscore_fd, F_UNLCK); diff --git a/src/floor/floor-save.c b/src/floor/floor-save.c index 99ca885c5..5ee70270e 100644 --- a/src/floor/floor-save.c +++ b/src/floor/floor-save.c @@ -103,7 +103,7 @@ void init_saved_floors(player_type *creature_ptr, bool force) sprintf(floor_savefile, "%s.F%02d", savefile, i); /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(creature_ptr); /* Try to create the file */ fd = fd_make(floor_savefile, mode); @@ -132,7 +132,7 @@ void init_saved_floors(player_type *creature_ptr, bool force) } /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(creature_ptr); /* Simply kill the temporary file */ (void)fd_kill(floor_savefile); @@ -181,7 +181,7 @@ void clear_saved_floor_files(player_type *creature_ptr) sprintf(floor_savefile, "%s.F%02d", savefile, i); /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(creature_ptr); /* Simply kill the temporary file */ (void)fd_kill(floor_savefile); @@ -242,7 +242,7 @@ static void kill_saved_floor(player_type *creature_ptr, saved_floor_type *sf_ptr sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id); /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(creature_ptr); /* Simply kill the temporary file */ (void)fd_kill(floor_savefile); diff --git a/src/io/chuukei.c b/src/io/chuukei.c index 25f1f11bd..f65015c4f 100644 --- a/src/io/chuukei.c +++ b/src/io/chuukei.c @@ -18,9 +18,9 @@ #include "util/angband-files.h" #include "view/display-messages.h" -#include -#include #include +#include +#include #ifdef WINDOWS #include #endif @@ -29,12 +29,12 @@ #if defined(WINDOWS) #include #else -#include -#include -#include +#include #include +#include +#include #include -#include +#include #include #include @@ -43,12 +43,12 @@ #define MAX_HOSTNAME 256 #endif -#define RINGBUF_SIZE 1024*1024 +#define RINGBUF_SIZE 1024 * 1024 #define FRESH_QUEUE_SIZE 4096 #ifdef WINDOWS #define WAIT 100 #else -#define WAIT 100*1000 /* ブラウズ側のウエイト(us単位) */ +#define WAIT 100 * 1000 /* ブラウズ側のウエイト(us単位) */ #endif #define DEFAULT_DELAY 50 #define RECVBUF_SIZE 1024 @@ -60,11 +60,11 @@ char *server_name; int server_port; #endif -static long epoch_time; /* バッファ開始時刻 */ +static long epoch_time; /* バッファ開始時刻 */ static int browse_delay; /* 表示するまでの時間(100ms単位)(この間にラグを吸収する) */ #ifdef CHUUKEI static int sd; /* ソケットのファイルディスクリプタ */ -static long time_diff; /* プレイ側との時間のずれ(これを見ながらディレイを調整していく) */ +static long time_diff; /* プレイ側との時間のずれ(これを見ながらディレイを調整していく) */ static int server_port; static GAME_TEXT server_name[MAX_HOSTNAME]; #endif @@ -79,22 +79,19 @@ static int movie_mode; #endif /* 描画する時刻を覚えておくキュー構造体 */ -static struct -{ - int time[FRESH_QUEUE_SIZE]; - int next; - int tail; -}fresh_queue; - +static struct { + int time[FRESH_QUEUE_SIZE]; + int next; + int tail; +} fresh_queue; /* リングバッファ構造体 */ -static struct -{ - char *buf; - int wptr; - int rptr; - int inlen; -}ring; +static struct { + char *buf; + int wptr; + int rptr; + int inlen; +} ring; /* * Original hooks @@ -107,859 +104,833 @@ static errr (*old_text_hook)(int x, int y, int n, TERM_COLOR a, concptr s); static void disable_chuukei_server(void) { - term *t = angband_term[0]; + term *t = angband_term[0]; #ifdef CHUUKEI - chuukei_server = FALSE; + chuukei_server = FALSE; #endif /* CHUUKEI */ - t->xtra_hook = old_xtra_hook; - t->curs_hook = old_curs_hook; - t->bigcurs_hook = old_bigcurs_hook; - t->wipe_hook = old_wipe_hook; - t->text_hook = old_text_hook; + t->xtra_hook = old_xtra_hook; + t->curs_hook = old_curs_hook; + t->bigcurs_hook = old_bigcurs_hook; + t->wipe_hook = old_wipe_hook; + t->text_hook = old_text_hook; } /* ANSI Cによればstatic変数は0で初期化されるが一応初期化する */ static errr init_buffer(void) { - fresh_queue.next = fresh_queue.tail = 0; - ring.wptr = ring.rptr = ring.inlen = 0; - fresh_queue.time[0] = 0; - ring.buf = malloc(RINGBUF_SIZE); - if (ring.buf == NULL) return -1; - - return 0; + fresh_queue.next = fresh_queue.tail = 0; + ring.wptr = ring.rptr = ring.inlen = 0; + fresh_queue.time[0] = 0; + ring.buf = malloc(RINGBUF_SIZE); + if (ring.buf == NULL) + return -1; + + return 0; } /* 現在の時間を100ms単位で取得する */ static long get_current_time(void) { #ifdef WINDOWS - return timeGetTime() / 100; + return timeGetTime() / 100; #else - struct timeval tv; - gettimeofday(&tv, NULL); + struct timeval tv; + gettimeofday(&tv, NULL); - return (tv.tv_sec * 10 + tv.tv_usec / 100000); + return (tv.tv_sec * 10 + tv.tv_usec / 100000); #endif } - /* リングバッファ構造体に buf の内容を加える */ static errr insert_ringbuf(char *buf) { - int len; - len = strlen(buf) + 1; /* +1は終端文字分 */ + int len; + len = strlen(buf) + 1; /* +1は終端文字分 */ - if (movie_mode) - { - fd_write(movie_fd, buf, len); + if (movie_mode) { + fd_write(movie_fd, buf, len); #ifdef CHUUKEI - if (!chuukei_server) return 0; + if (!chuukei_server) + return 0; #else - return 0; + return 0; #endif - } + } - /* バッファをオーバー */ - if (ring.inlen + len >= RINGBUF_SIZE) - { + /* バッファをオーバー */ + if (ring.inlen + len >= RINGBUF_SIZE) { #ifdef CHUUKEI - if (chuukei_server) disable_chuukei_server(); - else chuukei_client = FALSE; + if (chuukei_server) + disable_chuukei_server(); + else + chuukei_client = FALSE; - prt("送受信バッファが溢れました。サーバとの接続を切断します。", 0, 0); - inkey(); + prt("送受信バッファが溢れました。サーバとの接続を切断します。", 0, 0); + inkey(); - close(sd); + close(sd); #endif - return -1; - } - - /* バッファの終端までに収まる */ - if (ring.wptr + len < RINGBUF_SIZE) - { - memcpy(ring.buf + ring.wptr, buf, len); - ring.wptr += len; - } - /* バッファの終端までに収まらない(ピッタリ収まる場合も含む) */ - else - { - int head = RINGBUF_SIZE - ring.wptr; /* 前半 */ - int tail = len - head; /* 後半 */ - - memcpy(ring.buf + ring.wptr, buf, head); - memcpy(ring.buf, buf + head, tail); - ring.wptr = tail; - } - - ring.inlen += len; - - /* Success */ - return 0; + return -1; + } + + /* バッファの終端までに収まる */ + if (ring.wptr + len < RINGBUF_SIZE) { + memcpy(ring.buf + ring.wptr, buf, len); + ring.wptr += len; + } + /* バッファの終端までに収まらない(ピッタリ収まる場合も含む) */ + else { + int head = RINGBUF_SIZE - ring.wptr; /* 前半 */ + int tail = len - head; /* 後半 */ + + memcpy(ring.buf + ring.wptr, buf, head); + memcpy(ring.buf, buf + head, tail); + ring.wptr = tail; + } + + ring.inlen += len; + + /* Success */ + return 0; } #ifdef CHUUKEI void flush_ringbuf(void) { - fd_set fdset; - struct timeval tv; + fd_set fdset; + struct timeval tv; - if (!chuukei_server) return; + if (!chuukei_server) + return; - if (ring.inlen == 0) return; + if (ring.inlen == 0) + return; - tv.tv_sec = 0; - tv.tv_usec = 0; + tv.tv_sec = 0; + tv.tv_usec = 0; - FD_ZERO(&fdset); - FD_SET(sd, &fdset); + FD_ZERO(&fdset); + FD_SET(sd, &fdset); - while (TRUE) - { - fd_set tmp_fdset; - int result; + while (TRUE) { + fd_set tmp_fdset; + int result; - tmp_fdset = fdset; + tmp_fdset = fdset; - /* ソケットにデータを書き込めるかどうか調べる */ - select(sd+1, (fd_set *)NULL, &tmp_fdset, (fd_set *)NULL, &tv); + /* ソケットにデータを書き込めるかどうか調べる */ + select(sd + 1, (fd_set *)NULL, &tmp_fdset, (fd_set *)NULL, &tv); - /* 書き込めなければ戻る */ - if (FD_ISSET(sd, &tmp_fdset) == 0) break; + /* 書き込めなければ戻る */ + if (FD_ISSET(sd, &tmp_fdset) == 0) + break; - result = send(sd, ring.buf + ring.rptr, ((ring.wptr > ring.rptr ) ? ring.wptr : RINGBUF_SIZE) - ring.rptr, 0); + result = send(sd, ring.buf + ring.rptr, ((ring.wptr > ring.rptr) ? ring.wptr : RINGBUF_SIZE) - ring.rptr, 0); - if (result <= 0) - { - /* サーバとの接続断? */ - if (chuukei_server) disable_chuukei_server(); + if (result <= 0) { + /* サーバとの接続断? */ + if (chuukei_server) + disable_chuukei_server(); - prt("サーバとの接続が切断されました。", 0, 0); - inkey(); - close(sd); + prt("サーバとの接続が切断されました。", 0, 0); + inkey(); + close(sd); - return; - } + return; + } - ring.rptr += result; - ring.inlen -= result; + ring.rptr += result; + ring.inlen -= result; - if (ring.rptr == RINGBUF_SIZE) ring.rptr = 0; - if (ring.inlen == 0) break; - } + if (ring.rptr == RINGBUF_SIZE) + ring.rptr = 0; + if (ring.inlen == 0) + break; + } } - static int read_chuukei_prf(concptr prf_name) { - char buf[1024]; - FILE *fp; - - path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA, prf_name); - fp = angband_fopen(buf, "r"); - - if (!fp) return -1; - - /* 初期化 */ - server_port = -1; - server_name[0] = 0; - browse_delay = DEFAULT_DELAY; - - while (0 == angband_fgets(fp, buf, sizeof(buf))) - { - /* サーバ名 */ - if (!strncmp(buf, "server:", 7)) - { - strncpy(server_name, buf + 7, MAX_HOSTNAME - 1); - server_name[MAX_HOSTNAME - 1] = '\0'; - } - - /* ポート番号 */ - if (!strncmp(buf, "port:", 5)) - { - server_port = atoi(buf + 5); - } - - /* ディレイ */ - if (!strncmp(buf, "delay:", 6)) - { - browse_delay = atoi(buf + 6); - } - } - - angband_fclose(fp); - - /* prfファイルが完全でない */ - if (server_port == -1 || server_name[0] == 0) return -1; - - return 0; + char buf[1024]; + FILE *fp; + + path_build(buf, sizeof(buf), ANGBAND_DIR_XTRA, prf_name); + fp = angband_fopen(buf, "r"); + + if (!fp) + return -1; + + /* 初期化 */ + server_port = -1; + server_name[0] = 0; + browse_delay = DEFAULT_DELAY; + + while (0 == angband_fgets(fp, buf, sizeof(buf))) { + /* サーバ名 */ + if (!strncmp(buf, "server:", 7)) { + strncpy(server_name, buf + 7, MAX_HOSTNAME - 1); + server_name[MAX_HOSTNAME - 1] = '\0'; + } + + /* ポート番号 */ + if (!strncmp(buf, "port:", 5)) { + server_port = atoi(buf + 5); + } + + /* ディレイ */ + if (!strncmp(buf, "delay:", 6)) { + browse_delay = atoi(buf + 6); + } + } + + angband_fclose(fp); + + /* prfファイルが完全でない */ + if (server_port == -1 || server_name[0] == 0) + return -1; + + return 0; } int connect_chuukei_server(char *prf_name) { #ifdef WINDOWS - WSADATA wsaData; - WORD wVersionRequested = (WORD) (( 1) | ( 1 << 8)); + WSADATA wsaData; + WORD wVersionRequested = (WORD)((1) | (1 << 8)); #endif - struct sockaddr_in ask; - struct hostent *hp; + struct sockaddr_in ask; + struct hostent *hp; - if (read_chuukei_prf(prf_name) < 0) - { - printf("Wrong prf file\n"); - return -1; - } + if (read_chuukei_prf(prf_name) < 0) { + printf("Wrong prf file\n"); + return -1; + } - if (init_buffer() < 0) - { - printf("Malloc error\n"); - return -1; - } + if (init_buffer() < 0) { + printf("Malloc error\n"); + return -1; + } #ifdef WINDOWS - if (WSAStartup(wVersionRequested, &wsaData)) - { - msg_print("Report: WSAStartup failed."); - return -1; - } + if (WSAStartup(wVersionRequested, &wsaData)) { + msg_print("Report: WSAStartup failed."); + return -1; + } #endif - printf("server = %s\nport = %d\n", server_name, server_port); - - if ((hp = gethostbyname(server_name)) != NULL) - { - memset(&ask, 0, sizeof(ask)); - memcpy(&ask.sin_addr, hp->h_addr_list[0], hp->h_length); - } - else - { - if ((ask.sin_addr.s_addr=inet_addr(server_name)) == 0) - { - printf("Bad hostname\n"); - return -1; - } - } - - ask.sin_family = AF_INET; - ask.sin_port = htons((unsigned short)server_port); + printf("server = %s\nport = %d\n", server_name, server_port); + + if ((hp = gethostbyname(server_name)) != NULL) { + memset(&ask, 0, sizeof(ask)); + memcpy(&ask.sin_addr, hp->h_addr_list[0], hp->h_length); + } else { + if ((ask.sin_addr.s_addr = inet_addr(server_name)) == 0) { + printf("Bad hostname\n"); + return -1; + } + } + + ask.sin_family = AF_INET; + ask.sin_port = htons((unsigned short)server_port); #ifndef WINDOWS - if ((sd=socket(PF_INET,SOCK_STREAM, 0)) < 0) + if ((sd = socket(PF_INET, SOCK_STREAM, 0)) < 0) #else - if ((sd=socket(PF_INET,SOCK_STREAM, 0)) == INVALID_SOCKET) + if ((sd = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) #endif - { - printf("Can't create socket\n"); - return -1; - } - - if (connect(sd, (struct sockaddr *)&ask, sizeof(ask)) < 0) - { - close(sd); - printf("Can't connect %s port %d\n", server_name, server_port); - return -1; - } - - return 0; + { + printf("Can't create socket\n"); + return -1; + } + + if (connect(sd, (struct sockaddr *)&ask, sizeof(ask)) < 0) { + close(sd); + printf("Can't connect %s port %d\n", server_name, server_port); + return -1; + } + + return 0; } #endif /* CHUUKEI */ /* strが同じ文字の繰り返しかどうか調べる */ static bool string_is_repeat(char *str, int len) { - char c = str[0]; - int i; + char c = str[0]; + int i; - if (len < 2) return FALSE; + if (len < 2) + return FALSE; #ifdef JP - if (iskanji(c)) return FALSE; + if (iskanji(c)) + return FALSE; #endif - for (i = 1; i < len; i++) - { + for (i = 1; i < len; i++) { #ifdef JP - if(c != str[i] || iskanji(str[i])) return FALSE; + if (c != str[i] || iskanji(str[i])) + return FALSE; #else - if(c != str[i]) return FALSE; + if (c != str[i]) + return FALSE; #endif - } + } - return TRUE; + return TRUE; } static errr send_text_to_chuukei_server(TERM_LEN x, TERM_LEN y, int len, TERM_COLOR col, concptr str) { - char buf[1024]; - char buf2[1024]; - - strncpy(buf2, str, len); - buf2[len] = '\0'; - - if (len == 1) - { - sprintf(buf, "s%c%c%c%c", x+1, y+1, col, buf2[0]); - } - else if(string_is_repeat(buf2, len)) - { - int i; - for (i = len; i > 0; i -= 127) - { - sprintf(buf, "n%c%c%c%c%c", x+1, y+1, MIN(i, 127), col, buf2[0]); - } - } - else - { + char buf[1024]; + char buf2[1024]; + + strncpy(buf2, str, len); + buf2[len] = '\0'; + + if (len == 1) { + sprintf(buf, "s%c%c%c%c", x + 1, y + 1, col, buf2[0]); + } else if (string_is_repeat(buf2, len)) { + int i; + for (i = len; i > 0; i -= 127) { + sprintf(buf, "n%c%c%c%c%c", x + 1, y + 1, MIN(i, 127), col, buf2[0]); + } + } else { #if defined(SJIS) && defined(JP) - sjis2euc(buf2); + sjis2euc(buf2); #endif - sprintf(buf, "t%c%c%c%c%s", x+1, y+1, len, col, buf2); - } + sprintf(buf, "t%c%c%c%c%s", x + 1, y + 1, len, col, buf2); + } - insert_ringbuf(buf); + insert_ringbuf(buf); - return (*old_text_hook)(x, y, len, col, str); + return (*old_text_hook)(x, y, len, col, str); } static errr send_wipe_to_chuukei_server(int x, int y, int len) { - char buf[1024]; + char buf[1024]; - sprintf(buf, "w%c%c%c", x+1, y+1, len); + sprintf(buf, "w%c%c%c", x + 1, y + 1, len); - insert_ringbuf(buf); + insert_ringbuf(buf); - return (*old_wipe_hook)(x, y, len); + return (*old_wipe_hook)(x, y, len); } static errr send_xtra_to_chuukei_server(int n, int v) { - char buf[1024]; - - if (n == TERM_XTRA_CLEAR || n == TERM_XTRA_FRESH || n == TERM_XTRA_SHAPE) - { - sprintf(buf, "x%c", n+1); - - insert_ringbuf(buf); - - if (n == TERM_XTRA_FRESH) - { - sprintf(buf, "d%ld", get_current_time() - epoch_time); - insert_ringbuf(buf); - } - } - - /* Verify the hook */ - if (!old_xtra_hook) return -1; - - return (*old_xtra_hook)(n, v); + char buf[1024]; + + if (n == TERM_XTRA_CLEAR || n == TERM_XTRA_FRESH || n == TERM_XTRA_SHAPE) { + sprintf(buf, "x%c", n + 1); + + insert_ringbuf(buf); + + if (n == TERM_XTRA_FRESH) { + sprintf(buf, "d%ld", get_current_time() - epoch_time); + insert_ringbuf(buf); + } + } + + /* Verify the hook */ + if (!old_xtra_hook) + return -1; + + return (*old_xtra_hook)(n, v); } static errr send_curs_to_chuukei_server(int x, int y) { - char buf[1024]; + char buf[1024]; - sprintf(buf, "c%c%c", x+1, y+1); + sprintf(buf, "c%c%c", x + 1, y + 1); - insert_ringbuf(buf); + insert_ringbuf(buf); - return (*old_curs_hook)(x, y); + return (*old_curs_hook)(x, y); } static errr send_bigcurs_to_chuukei_server(int x, int y) { - char buf[1024]; + char buf[1024]; - sprintf(buf, "C%c%c", x+1, y+1); + sprintf(buf, "C%c%c", x + 1, y + 1); - insert_ringbuf(buf); + insert_ringbuf(buf); - return (*old_bigcurs_hook)(x, y); + return (*old_bigcurs_hook)(x, y); } - /* * Prepare z-term hooks to call send_*_to_chuukei_server()'s */ void prepare_chuukei_hooks(void) { - term *t0 = angband_term[0]; - - /* Save original z-term hooks */ - old_xtra_hook = t0->xtra_hook; - old_curs_hook = t0->curs_hook; - old_bigcurs_hook = t0->bigcurs_hook; - old_wipe_hook = t0->wipe_hook; - old_text_hook = t0->text_hook; - - /* Prepare z-term hooks */ - t0->xtra_hook = send_xtra_to_chuukei_server; - t0->curs_hook = send_curs_to_chuukei_server; - t0->bigcurs_hook = send_bigcurs_to_chuukei_server; - t0->wipe_hook = send_wipe_to_chuukei_server; - t0->text_hook = send_text_to_chuukei_server; + term *t0 = angband_term[0]; + + /* Save original z-term hooks */ + old_xtra_hook = t0->xtra_hook; + old_curs_hook = t0->curs_hook; + old_bigcurs_hook = t0->bigcurs_hook; + old_wipe_hook = t0->wipe_hook; + old_text_hook = t0->text_hook; + + /* Prepare z-term hooks */ + t0->xtra_hook = send_xtra_to_chuukei_server; + t0->curs_hook = send_curs_to_chuukei_server; + t0->bigcurs_hook = send_bigcurs_to_chuukei_server; + t0->wipe_hook = send_wipe_to_chuukei_server; + t0->text_hook = send_text_to_chuukei_server; } - /* * Prepare z-term hooks to call send_*_to_chuukei_server()'s */ -void prepare_movie_hooks(void) +void prepare_movie_hooks(player_type *player_ptr) { - char buf[1024]; - char tmp[80]; + char buf[1024]; + char tmp[80]; - if (movie_mode) - { - movie_mode = 0; + if (movie_mode) { + movie_mode = 0; #ifdef CHUUKEI - if (!chuukei_server) disable_chuukei_server(); + if (!chuukei_server) + disable_chuukei_server(); #else - disable_chuukei_server(); + disable_chuukei_server(); #endif - fd_close(movie_fd); - msg_print(_("録画を終了しました。", "Stopped recording.")); - } - else - { - sprintf(tmp, "%s.amv", p_ptr->base_name); - if (get_string(_("ムービー記録ファイル: ", "Movie file name: "), tmp, 80)) - { - int fd; - - path_build(buf, sizeof(buf), ANGBAND_DIR_USER, tmp); - - fd = fd_open(buf, O_RDONLY); - - /* Existing file */ - if (fd >= 0) - { - char out_val[160]; - (void)fd_close(fd); - - /* Build query */ - (void)sprintf(out_val, _("現存するファイルに上書きしますか? (%s)", "Replace existing file %s? "), buf); - - /* Ask */ - if (!get_check(out_val)) return; - - movie_fd = fd_open(buf, O_WRONLY | O_TRUNC); - } - else - { - movie_fd = fd_make(buf, 0644); - } - - if (!movie_fd) - { - msg_print(_("ファイルを開けません!", "Can not open file.")); - return; - } - - movie_mode = 1; + fd_close(movie_fd); + msg_print(_("録画を終了しました。", "Stopped recording.")); + } else { + sprintf(tmp, "%s.amv", player_ptr->base_name); + if (get_string(_("ムービー記録ファイル: ", "Movie file name: "), tmp, 80)) { + int fd; + + path_build(buf, sizeof(buf), ANGBAND_DIR_USER, tmp); + + fd = fd_open(buf, O_RDONLY); + + /* Existing file */ + if (fd >= 0) { + char out_val[160]; + (void)fd_close(fd); + + /* Build query */ + (void)sprintf(out_val, _("現存するファイルに上書きしますか? (%s)", "Replace existing file %s? "), buf); + + /* Ask */ + if (!get_check(out_val)) + return; + + movie_fd = fd_open(buf, O_WRONLY | O_TRUNC); + } else { + movie_fd = fd_make(buf, 0644); + } + + if (!movie_fd) { + msg_print(_("ファイルを開けません!", "Can not open file.")); + return; + } + + movie_mode = 1; #ifdef CHUUKEI - if (!chuukei_server) prepare_chuukei_hooks(); + if (!chuukei_server) + prepare_chuukei_hooks(); #else - prepare_chuukei_hooks(); + prepare_chuukei_hooks(); #endif - do_cmd_redraw(p_ptr); - } - } + do_cmd_redraw(player_ptr); + } + } } #ifdef CHUUKEI static int handle_timestamp_data(int timestamp) { - long current_time = get_current_time(); - - /* 描画キューは空かどうか? */ - if (fresh_queue.tail == fresh_queue.next) - { - /* バッファリングし始めの時間を保存しておく */ - epoch_time = current_time; - epoch_time += browse_delay; - epoch_time -= timestamp; - time_diff = current_time - timestamp; - } - - /* 描画キューに保存し、保存位置を進める */ - fresh_queue.time[fresh_queue.tail] = timestamp; - fresh_queue.tail ++; - - /* キューの最後尾に到達したら先頭に戻す */ - fresh_queue.tail %= FRESH_QUEUE_SIZE; - - if (fresh_queue.tail == fresh_queue.next) - { - /* 描画キュー溢れ */ - prt("描画タイミングキューが溢れました。サーバとの接続を切断します。", 0, 0); - inkey(); - close(sd); - - return -1; - } - - /* プレイ側とのディレイを調整 */ - if (time_diff != current_time - timestamp) - { - long old_time_diff = time_diff; - time_diff = current_time - timestamp; - epoch_time -= (old_time_diff - time_diff); - } - - /* Success */ - return 0; + long current_time = get_current_time(); + + /* 描画キューは空かどうか? */ + if (fresh_queue.tail == fresh_queue.next) { + /* バッファリングし始めの時間を保存しておく */ + epoch_time = current_time; + epoch_time += browse_delay; + epoch_time -= timestamp; + time_diff = current_time - timestamp; + } + + /* 描画キューに保存し、保存位置を進める */ + fresh_queue.time[fresh_queue.tail] = timestamp; + fresh_queue.tail++; + + /* キューの最後尾に到達したら先頭に戻す */ + fresh_queue.tail %= FRESH_QUEUE_SIZE; + + if (fresh_queue.tail == fresh_queue.next) { + /* 描画キュー溢れ */ + prt("描画タイミングキューが溢れました。サーバとの接続を切断します。", 0, 0); + inkey(); + close(sd); + + return -1; + } + + /* プレイ側とのディレイを調整 */ + if (time_diff != current_time - timestamp) { + long old_time_diff = time_diff; + time_diff = current_time - timestamp; + epoch_time -= (old_time_diff - time_diff); + } + + /* Success */ + return 0; } #endif /* CHUUKEI */ static int handle_movie_timestamp_data(int timestamp) { - static int initialized = FALSE; - - /* 描画キューは空かどうか? */ - if (!initialized) - { - /* バッファリングし始めの時間を保存しておく */ - epoch_time = get_current_time(); - epoch_time += browse_delay; - epoch_time -= timestamp; - //time_diff = current_time - timestamp; - initialized = TRUE; - } - - /* 描画キューに保存し、保存位置を進める */ - fresh_queue.time[fresh_queue.tail] = timestamp; - fresh_queue.tail ++; - - /* キューの最後尾に到達したら先頭に戻す */ - fresh_queue.tail %= FRESH_QUEUE_SIZE; - - /* Success */ - return 0; + static int initialized = FALSE; + + /* 描画キューは空かどうか? */ + if (!initialized) { + /* バッファリングし始めの時間を保存しておく */ + epoch_time = get_current_time(); + epoch_time += browse_delay; + epoch_time -= timestamp; + // time_diff = current_time - timestamp; + initialized = TRUE; + } + + /* 描画キューに保存し、保存位置を進める */ + fresh_queue.time[fresh_queue.tail] = timestamp; + fresh_queue.tail++; + + /* キューの最後尾に到達したら先頭に戻す */ + fresh_queue.tail %= FRESH_QUEUE_SIZE; + + /* Success */ + return 0; } #ifdef CHUUKEI static int read_sock(void) { - static char recv_buf[RECVBUF_SIZE]; - static int remain_bytes = 0; - int recv_bytes; - int i; - - /* 前回残ったデータの後につづけて配信サーバからデータ受信 */ - recv_bytes = recv(sd, recv_buf + remain_bytes, RECVBUF_SIZE - remain_bytes, 0); - - if (recv_bytes <= 0) - return -1; - - /* 前回残ったデータ量に今回読んだデータ量を追加 */ - remain_bytes += recv_bytes; - - for (i = 0; i < remain_bytes; i ++) - { - /* データのくぎり('\0')を探す */ - if (recv_buf[i] == '\0') - { - /* 'd'で始まるデータ(タイムスタンプ)の場合は - 描画キューに保存する処理を呼ぶ */ - if ((recv_buf[0] == 'd') && - (handle_timestamp_data(atoi(recv_buf + 1)) < 0)) - return -1; - - /* 受信データを保存 */ - if (insert_ringbuf(recv_buf) < 0) - return -1; - - /* 次のデータ移行をrecv_bufの先頭に移動 */ - memmove(recv_buf, recv_buf + i + 1, remain_bytes - i - 1); - - remain_bytes -= (i+1); - i = 0; - } - } - - return 0; + static char recv_buf[RECVBUF_SIZE]; + static int remain_bytes = 0; + int recv_bytes; + int i; + + /* 前回残ったデータの後につづけて配信サーバからデータ受信 */ + recv_bytes = recv(sd, recv_buf + remain_bytes, RECVBUF_SIZE - remain_bytes, 0); + + if (recv_bytes <= 0) + return -1; + + /* 前回残ったデータ量に今回読んだデータ量を追加 */ + remain_bytes += recv_bytes; + + for (i = 0; i < remain_bytes; i++) { + /* データのくぎり('\0')を探す */ + if (recv_buf[i] == '\0') { + /* 'd'で始まるデータ(タイムスタンプ)の場合は + 描画キューに保存する処理を呼ぶ */ + if ((recv_buf[0] == 'd') && (handle_timestamp_data(atoi(recv_buf + 1)) < 0)) + return -1; + + /* 受信データを保存 */ + if (insert_ringbuf(recv_buf) < 0) + return -1; + + /* 次のデータ移行をrecv_bufの先頭に移動 */ + memmove(recv_buf, recv_buf + i + 1, remain_bytes - i - 1); + + remain_bytes -= (i + 1); + i = 0; + } + } + + return 0; } #endif static int read_movie_file(void) { - static char recv_buf[RECVBUF_SIZE]; - static int remain_bytes = 0; - int recv_bytes; - int i; - - recv_bytes = read(movie_fd, recv_buf + remain_bytes, RECVBUF_SIZE - remain_bytes); - - if (recv_bytes <= 0) - return -1; - - /* 前回残ったデータ量に今回読んだデータ量を追加 */ - remain_bytes += recv_bytes; - - for (i = 0; i < remain_bytes; i ++) - { - /* データのくぎり('\0')を探す */ - if (recv_buf[i] == '\0') - { - /* 'd'で始まるデータ(タイムスタンプ)の場合は - 描画キューに保存する処理を呼ぶ */ - if ((recv_buf[0] == 'd') && - (handle_movie_timestamp_data(atoi(recv_buf + 1)) < 0)) - return -1; - - /* 受信データを保存 */ - if (insert_ringbuf(recv_buf) < 0) - return -1; - - /* 次のデータ移行をrecv_bufの先頭に移動 */ - memmove(recv_buf, recv_buf + i + 1, remain_bytes - i - 1); - - remain_bytes -= (i+1); - i = 0; - } - } - - return 0; -} + static char recv_buf[RECVBUF_SIZE]; + static int remain_bytes = 0; + int recv_bytes; + int i; + + recv_bytes = read(movie_fd, recv_buf + remain_bytes, RECVBUF_SIZE - remain_bytes); + + if (recv_bytes <= 0) + return -1; + + /* 前回残ったデータ量に今回読んだデータ量を追加 */ + remain_bytes += recv_bytes; + for (i = 0; i < remain_bytes; i++) { + /* データのくぎり('\0')を探す */ + if (recv_buf[i] == '\0') { + /* 'd'で始まるデータ(タイムスタンプ)の場合は + 描画キューに保存する処理を呼ぶ */ + if ((recv_buf[0] == 'd') && (handle_movie_timestamp_data(atoi(recv_buf + 1)) < 0)) + return -1; + + /* 受信データを保存 */ + if (insert_ringbuf(recv_buf) < 0) + return -1; + + /* 次のデータ移行をrecv_bufの先頭に移動 */ + memmove(recv_buf, recv_buf + i + 1, remain_bytes - i - 1); + + remain_bytes -= (i + 1); + i = 0; + } + } + + return 0; +} #ifndef WINDOWS /* Win版の床の中点と壁の豆腐をピリオドとシャープにする。*/ static void win2unix(int col, char *buf) { - char kabe; - if ( col == 9 ) kabe = '%'; - else kabe = '#'; + char kabe; + if (col == 9) + kabe = '%'; + else + kabe = '#'; - while (*buf) - { + while (*buf) { #ifdef JP - if (iskanji(*buf)) - { - buf += 2; - continue; - } + if (iskanji(*buf)) { + buf += 2; + continue; + } #endif - if (*buf == 127) *buf = kabe; - else if(*buf == 31) *buf = '.'; - buf++; - } + if (*buf == 127) + *buf = kabe; + else if (*buf == 31) + *buf = '.'; + buf++; + } } #endif static bool get_nextbuf(char *buf) { - char *ptr = buf; + char *ptr = buf; - while (TRUE) - { - *ptr = ring.buf[ring.rptr ++]; - ring.inlen --; - if (ring.rptr == RINGBUF_SIZE) ring.rptr = 0; - if (*ptr++ == '\0') break; - } + while (TRUE) { + *ptr = ring.buf[ring.rptr++]; + ring.inlen--; + if (ring.rptr == RINGBUF_SIZE) + ring.rptr = 0; + if (*ptr++ == '\0') + break; + } - if (buf[0] == 'd') return FALSE; + if (buf[0] == 'd') + return FALSE; - return TRUE; + return TRUE; } /* プレイホストのマップが大きいときクライアントのマップもリサイズする */ static void update_term_size(int x, int y, int len) { - int ox, oy; - int nx, ny; - Term_get_size(&ox, &oy); - nx = ox; - ny = oy; - - /* 横方向のチェック */ - if (x + len > ox) nx = x + len; - /* 縦方向のチェック */ - if (y + 1 > oy) ny = y + 1; - - if (nx != ox || ny != oy) Term_resize(nx, ny); + int ox, oy; + int nx, ny; + Term_get_size(&ox, &oy); + nx = ox; + ny = oy; + + /* 横方向のチェック */ + if (x + len > ox) + nx = x + len; + /* 縦方向のチェック */ + if (y + 1 > oy) + ny = y + 1; + + if (nx != ox || ny != oy) + Term_resize(nx, ny); } static bool flush_ringbuf_client(void) { - char buf[1024]; - - /* 書くデータなし */ - if (fresh_queue.next == fresh_queue.tail) return FALSE; - - /* まだ書くべき時でない */ - if (fresh_queue.time[fresh_queue.next] > get_current_time() - epoch_time) return FALSE; - - /* 時間情報(区切り)が得られるまで書く */ - while (get_nextbuf(buf)) - { - char id; - int x, y, len; - TERM_COLOR col; - int i; - unsigned char tmp1, tmp2, tmp3, tmp4; - char *mesg; - - sscanf(buf, "%c%c%c%c%c", &id, &tmp1, &tmp2, &tmp3, &tmp4); - x = tmp1-1; y = tmp2-1; len = tmp3; col = tmp4; - if (id == 's') - { - col = tmp3; - mesg = &buf[4]; - } - else mesg = &buf[5]; + char buf[1024]; + + /* 書くデータなし */ + if (fresh_queue.next == fresh_queue.tail) + return FALSE; + + /* まだ書くべき時でない */ + if (fresh_queue.time[fresh_queue.next] > get_current_time() - epoch_time) + return FALSE; + + /* 時間情報(区切り)が得られるまで書く */ + while (get_nextbuf(buf)) { + char id; + int x, y, len; + TERM_COLOR col; + int i; + unsigned char tmp1, tmp2, tmp3, tmp4; + char *mesg; + + sscanf(buf, "%c%c%c%c%c", &id, &tmp1, &tmp2, &tmp3, &tmp4); + x = tmp1 - 1; + y = tmp2 - 1; + len = tmp3; + col = tmp4; + if (id == 's') { + col = tmp3; + mesg = &buf[4]; + } else + mesg = &buf[5]; #ifndef WINDOWS - win2unix(col, mesg); + win2unix(col, mesg); #endif - switch (id) - { - case 't': /* 通常 */ + switch (id) { + case 't': /* 通常 */ #if defined(SJIS) && defined(JP) - euc2sjis(mesg); + euc2sjis(mesg); #endif - update_term_size(x, y, len); - (void)((*angband_term[0]->text_hook)(x, y, len, (byte)col, mesg)); - strncpy(&Term->scr->c[y][x], mesg, len); - for (i = x; i < x+len; i++) - { - Term->scr->a[y][i] = col; - } - break; - - case 'n': /* 繰り返し */ - for (i = 1; i < len; i++) - { - mesg[i] = mesg[0]; - } - mesg[i] = '\0'; - update_term_size(x, y, len); - (void)((*angband_term[0]->text_hook)(x, y, len, (byte)col, mesg)); - strncpy(&Term->scr->c[y][x], mesg, len); - for (i = x; i < x+len; i++) - { - Term->scr->a[y][i] = col; - } - break; - - case 's': /* 一文字 */ - update_term_size(x, y, 1); - (void)((*angband_term[0]->text_hook)(x, y, 1, (byte)col, mesg)); - strncpy(&Term->scr->c[y][x], mesg, 1); - Term->scr->a[y][x] = col; - break; - - case 'w': - update_term_size(x, y, len); - (void)((*angband_term[0]->wipe_hook)(x, y, len)); - break; - - case 'x': - if (x == TERM_XTRA_CLEAR) Term_clear(); - (void)((*angband_term[0]->xtra_hook)(x, 0)); - break; - - case 'c': - update_term_size(x, y, 1); - (void)((*angband_term[0]->curs_hook)(x, y)); - break; - case 'C': - update_term_size(x, y, 1); - (void)((*angband_term[0]->bigcurs_hook)(x, y)); - break; - } - } - - fresh_queue.next++; - if (fresh_queue.next == FRESH_QUEUE_SIZE) fresh_queue.next = 0; - return TRUE; + update_term_size(x, y, len); + (void)((*angband_term[0]->text_hook)(x, y, len, (byte)col, mesg)); + strncpy(&Term->scr->c[y][x], mesg, len); + for (i = x; i < x + len; i++) { + Term->scr->a[y][i] = col; + } + break; + + case 'n': /* 繰り返し */ + for (i = 1; i < len; i++) { + mesg[i] = mesg[0]; + } + mesg[i] = '\0'; + update_term_size(x, y, len); + (void)((*angband_term[0]->text_hook)(x, y, len, (byte)col, mesg)); + strncpy(&Term->scr->c[y][x], mesg, len); + for (i = x; i < x + len; i++) { + Term->scr->a[y][i] = col; + } + break; + + case 's': /* 一文字 */ + update_term_size(x, y, 1); + (void)((*angband_term[0]->text_hook)(x, y, 1, (byte)col, mesg)); + strncpy(&Term->scr->c[y][x], mesg, 1); + Term->scr->a[y][x] = col; + break; + + case 'w': + update_term_size(x, y, len); + (void)((*angband_term[0]->wipe_hook)(x, y, len)); + break; + + case 'x': + if (x == TERM_XTRA_CLEAR) + Term_clear(); + (void)((*angband_term[0]->xtra_hook)(x, 0)); + break; + + case 'c': + update_term_size(x, y, 1); + (void)((*angband_term[0]->curs_hook)(x, y)); + break; + case 'C': + update_term_size(x, y, 1); + (void)((*angband_term[0]->bigcurs_hook)(x, y)); + break; + } + } + + fresh_queue.next++; + if (fresh_queue.next == FRESH_QUEUE_SIZE) + fresh_queue.next = 0; + return TRUE; } #ifdef CHUUKEI void browse_chuukei() { - fd_set fdset; - struct timeval tv; + fd_set fdset; + struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = WAIT; + tv.tv_sec = 0; + tv.tv_usec = WAIT; - FD_ZERO(&fdset); - FD_SET(sd, &fdset); + FD_ZERO(&fdset); + FD_SET(sd, &fdset); - Term_clear(); - Term_fresh(); - Term_xtra(TERM_XTRA_REACT, 0); + Term_clear(); + Term_fresh(); + Term_xtra(TERM_XTRA_REACT, 0); - while (TRUE) - { - fd_set tmp_fdset; - struct timeval tmp_tv; + while (TRUE) { + fd_set tmp_fdset; + struct timeval tmp_tv; - if (flush_ringbuf_client()) continue; + if (flush_ringbuf_client()) + continue; - tmp_fdset = fdset; - tmp_tv = tv; + tmp_fdset = fdset; + tmp_tv = tv; - /* ソケットにデータが来ているかどうか調べる */ - select(sd+1, &tmp_fdset, (fd_set *)NULL, (fd_set *)NULL, &tmp_tv); - if (FD_ISSET(sd, &tmp_fdset) == 0) - { - Term_xtra(TERM_XTRA_FLUSH, 0); - continue; - } + /* ソケットにデータが来ているかどうか調べる */ + select(sd + 1, &tmp_fdset, (fd_set *)NULL, (fd_set *)NULL, &tmp_tv); + if (FD_ISSET(sd, &tmp_fdset) == 0) { + Term_xtra(TERM_XTRA_FLUSH, 0); + continue; + } - if (read_sock() < 0) - { - chuukei_client = FALSE; - } + if (read_sock() < 0) { + chuukei_client = FALSE; + } - /* 接続が切れた状態で書くべきデータがなくなっていたら終了 */ - if (!chuukei_client && fresh_queue.next == fresh_queue.tail ) break; - } + /* 接続が切れた状態で書くべきデータがなくなっていたら終了 */ + if (!chuukei_client && fresh_queue.next == fresh_queue.tail) + break; + } } #endif /* CHUUKEI */ void prepare_browse_movie_aux(concptr filename) { - movie_fd = fd_open(filename, O_RDONLY); - - browsing_movie = TRUE; + movie_fd = fd_open(filename, O_RDONLY); + + browsing_movie = TRUE; - init_buffer(); + init_buffer(); } void prepare_browse_movie(concptr filename) { - char buf[1024]; - path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename); + char buf[1024]; + path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename); - prepare_browse_movie_aux(buf); + prepare_browse_movie_aux(buf); } void browse_movie(void) { - Term_clear(); - Term_fresh(); - Term_xtra(TERM_XTRA_REACT, 0); - - while (read_movie_file() == 0) - { - while (fresh_queue.next != fresh_queue.tail) - { - if (!flush_ringbuf_client()) - { - Term_xtra(TERM_XTRA_FLUSH, 0); - - /* ソケットにデータが来ているかどうか調べる */ + Term_clear(); + Term_fresh(); + Term_xtra(TERM_XTRA_REACT, 0); + + while (read_movie_file() == 0) { + while (fresh_queue.next != fresh_queue.tail) { + if (!flush_ringbuf_client()) { + Term_xtra(TERM_XTRA_FLUSH, 0); + + /* ソケットにデータが来ているかどうか調べる */ #ifdef WINDOWS - Sleep(WAIT); + Sleep(WAIT); #else - usleep(WAIT); + usleep(WAIT); #endif - } - } - } + } + } + } } diff --git a/src/io/chuukei.h b/src/io/chuukei.h index 7d0929354..f9a1b6fbb 100644 --- a/src/io/chuukei.h +++ b/src/io/chuukei.h @@ -6,13 +6,13 @@ extern bool chuukei_server; extern bool chuukei_client; -extern int connect_chuukei_server(char *server_string); -extern void browse_chuukei(void); -extern void flush_ringbuf(void); -extern void prepare_chuukei_hooks(void); +int connect_chuukei_server(char *server_string); +void browse_chuukei(void); +void flush_ringbuf(void); +void prepare_chuukei_hooks(void); #endif -extern void prepare_movie_hooks(void); -extern void prepare_browse_movie_aux(concptr filename); -extern void prepare_browse_movie(concptr filename); -extern void browse_movie(void); +void prepare_movie_hooks(player_type *player_ptr); +void prepare_browse_movie_aux(concptr filename); +void prepare_browse_movie(concptr filename); +void browse_movie(void); diff --git a/src/io/files-util.c b/src/io/files-util.c index 5920bcf45..d400d2831 100644 --- a/src/io/files-util.c +++ b/src/io/files-util.c @@ -267,16 +267,16 @@ errr counts_write(player_type *creature_ptr, int where, u32b count) char buf[1024]; path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, _("z_info_j.raw", "z_info.raw")); - safe_setuid_grab(); + safe_setuid_grab(creature_ptr); int fd = fd_open(buf, O_RDWR); safe_setuid_drop(); if (fd < 0) { - safe_setuid_grab(); + safe_setuid_grab(creature_ptr); fd = fd_make(buf, 0644); safe_setuid_drop(); } - safe_setuid_grab(); + safe_setuid_grab(creature_ptr); errr err = fd_lock(fd, F_WRLCK); safe_setuid_drop(); if (err) @@ -284,7 +284,7 @@ errr counts_write(player_type *creature_ptr, int where, u32b count) counts_seek(creature_ptr, fd, where, TRUE); fd_write(fd, (char *)(&count), sizeof(u32b)); - safe_setuid_grab(); + safe_setuid_grab(creature_ptr); err = fd_lock(fd, F_UNLCK); safe_setuid_drop(); diff --git a/src/io/input-key-processor.c b/src/io/input-key-processor.c index fe6f8e142..9e88ac090 100644 --- a/src/io/input-key-processor.c +++ b/src/io/input-key-processor.c @@ -599,7 +599,7 @@ void process_command(player_type *creature_ptr) break; } case ']': { - prepare_movie_hooks(); + prepare_movie_hooks(creature_ptr); break; } case KTRL('V'): { diff --git a/src/io/load.c b/src/io/load.c index 605538f1f..44cc73c8b 100644 --- a/src/io/load.c +++ b/src/io/load.c @@ -3320,7 +3320,7 @@ static errr rd_savefile_new_aux(player_type *creature_ptr) */ errr rd_savefile_new(player_type *player_ptr) { - safe_setuid_grab(); + safe_setuid_grab(player_ptr); fff = angband_fopen(savefile, "rb"); safe_setuid_drop(); if (!fff) @@ -3424,7 +3424,7 @@ bool load_floor(player_type *player_ptr, saved_floor_type *sf_ptr, BIT_FLAGS mod char floor_savefile[1024]; sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id); - safe_setuid_grab(); + safe_setuid_grab(player_ptr); fff = angband_fopen(floor_savefile, "rb"); safe_setuid_drop(); @@ -3438,7 +3438,7 @@ bool load_floor(player_type *player_ptr, saved_floor_type *sf_ptr, BIT_FLAGS mod is_save_successful = FALSE; angband_fclose(fff); - safe_setuid_grab(); + safe_setuid_grab(player_ptr); if (!(mode & SLF_NO_KILL)) (void)fd_kill(floor_savefile); diff --git a/src/io/save.c b/src/io/save.c index 61b010119..f637705f5 100644 --- a/src/io/save.c +++ b/src/io/save.c @@ -1436,7 +1436,7 @@ static bool wr_savefile_new(player_type *player_ptr) static bool save_player_aux(player_type *player_ptr, char *name) { /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(player_ptr); /* Create the savefile */ int file_permission = 0644; @@ -1453,7 +1453,7 @@ static bool save_player_aux(player_type *player_ptr, char *name) (void)fd_close(fd); /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(player_ptr); /* Open the savefile */ fff = angband_fopen(name, "wb"); @@ -1472,7 +1472,7 @@ static bool save_player_aux(player_type *player_ptr, char *name) } /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(player_ptr); /* Remove "broken" files */ if (!is_save_successful) (void)fd_kill(name); @@ -1502,7 +1502,7 @@ bool save_player(player_type *player_ptr) strcat(safe, ".new"); /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(player_ptr); fd_kill(safe); @@ -1521,7 +1521,7 @@ bool save_player(player_type *player_ptr) strcat(temp, ".old"); /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(player_ptr); /* Remove it */ fd_kill(temp); @@ -1795,7 +1795,7 @@ bool save_floor(player_type *player_ptr, saved_floor_type *sf_ptr, BIT_FLAGS mod sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id); /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(player_ptr); /* Remove it */ fd_kill(floor_savefile); @@ -1809,7 +1809,7 @@ bool save_floor(player_type *player_ptr, saved_floor_type *sf_ptr, BIT_FLAGS mod fff = NULL; /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(player_ptr); /* Create the savefile */ int fd = fd_make(floor_savefile, 0644); @@ -1824,7 +1824,7 @@ bool save_floor(player_type *player_ptr, saved_floor_type *sf_ptr, BIT_FLAGS mod (void)fd_close(fd); /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(player_ptr); /* Open the savefile */ fff = angband_fopen(floor_savefile, "wb"); @@ -1846,7 +1846,7 @@ bool save_floor(player_type *player_ptr, saved_floor_type *sf_ptr, BIT_FLAGS mod if (!is_save_successful) { /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(player_ptr); (void)fd_kill(floor_savefile); diff --git a/src/io/uid-checker.c b/src/io/uid-checker.c index 47ecf9769..f0b775feb 100644 --- a/src/io/uid-checker.c +++ b/src/io/uid-checker.c @@ -1,10 +1,10 @@ #include "io/uid-checker.h" #ifdef SET_UID -# ifdef SAFE_SETUID -# ifdef SAFE_SETUID_POSIX -# include "util.h" -# endif -# endif +#ifdef SAFE_SETUID +#ifdef SAFE_SETUID_POSIX +#include "util.h" +#endif +#endif #endif /*! @@ -13,59 +13,52 @@ void safe_setuid_drop(void) { #ifdef SET_UID -# ifdef SAFE_SETUID -# ifdef SAFE_SETUID_POSIX +#ifdef SAFE_SETUID +#ifdef SAFE_SETUID_POSIX - if (setuid(getuid()) != 0) - { - quit(_("setuid(): 正しく許可が取れません!", "setuid(): cannot set permissions correctly!")); - } - if (setgid(getgid()) != 0) - { - quit(_("setgid(): 正しく許可が取れません!", "setgid(): cannot set permissions correctly!")); - } -# else - if (setreuid(geteuid(), getuid()) != 0) - { - quit(_("setreuid(): 正しく許可が取れません!", "setreuid(): cannot set permissions correctly!")); - } - if (setregid(getegid(), getgid()) != 0) - { - quit(_("setregid(): 正しく許可が取れません!", "setregid(): cannot set permissions correctly!")); - } -# endif -# endif + if (setuid(getuid()) != 0) { + quit(_("setuid(): 正しく許可が取れません!", "setuid(): cannot set permissions correctly!")); + } + if (setgid(getgid()) != 0) { + quit(_("setgid(): 正しく許可が取れません!", "setgid(): cannot set permissions correctly!")); + } +#else + if (setreuid(geteuid(), getuid()) != 0) { + quit(_("setreuid(): 正しく許可が取れません!", "setreuid(): cannot set permissions correctly!")); + } + if (setregid(getegid(), getgid()) != 0) { + quit(_("setregid(): 正しく許可が取れません!", "setregid(): cannot set permissions correctly!")); + } +#endif +#endif #endif } - /*! * @brief ファイルのグラブパーミッションチェック / Hack -- grab permissions */ -void safe_setuid_grab(void) +void safe_setuid_grab(player_type *player_ptr) { #ifdef SET_UID -# ifdef SAFE_SETUID -# ifdef SAFE_SETUID_POSIX +#ifdef SAFE_SETUID +#ifdef SAFE_SETUID_POSIX - if (setuid(p_ptr->player_egid) != 0) - { - quit(_("setuid(): 正しく許可が取れません!", "setuid(): cannot set permissions correctly!")); - } - if (setgid(p_ptr->player_egid) != 0) - { - quit(_("setgid(): 正しく許可が取れません!", "setgid(): cannot set permissions correctly!")); - } -# else - if (setreuid(geteuid(), getuid()) != 0) - { - quit(_("setreuid(): 正しく許可が取れません!", "setreuid(): cannot set permissions correctly!")); - } - if (setregid(getegid(), getgid()) != 0) - { - quit(_("setregid(): 正しく許可が取れません!", "setregid(): cannot set permissions correctly!")); - } -# endif -# endif + if (setuid(player_ptr->player_egid) != 0) { + quit(_("setuid(): 正しく許可が取れません!", "setuid(): cannot set permissions correctly!")); + } + if (setgid(player_ptr->player_egid) != 0) { + quit(_("setgid(): 正しく許可が取れません!", "setgid(): cannot set permissions correctly!")); + } +#else + if (setreuid(geteuid(), getuid()) != 0) { + quit(_("setreuid(): 正しく許可が取れません!", "setreuid(): cannot set permissions correctly!")); + } + if (setregid(getegid(), getgid()) != 0) { + quit(_("setregid(): 正しく許可が取れません!", "setregid(): cannot set permissions correctly!")); + } +#endif +#endif +#else + (void)player_ptr; #endif } diff --git a/src/io/uid-checker.h b/src/io/uid-checker.h index 73010bc7c..3089cf725 100644 --- a/src/io/uid-checker.h +++ b/src/io/uid-checker.h @@ -1,4 +1,6 @@ #pragma once -extern void safe_setuid_drop(void); -extern void safe_setuid_grab(void); +#include "system/angband.h" + +void safe_setuid_drop(void); +void safe_setuid_grab(player_type *player_ptr); diff --git a/src/main/init.c b/src/main/init.c index bf4ee6cd1..09d3f44eb 100644 --- a/src/main/init.c +++ b/src/main/init.c @@ -425,7 +425,7 @@ static void update_header(angband_header *head, void **info, char **name, char * * Note that we let each entry have a unique "name" and "text" string, * even if the string happens to be empty (everyone has a unique '\0'). */ -static errr init_info(concptr filename, angband_header *head, void **info, char **name, char **text, char **tag) +static errr init_info(player_type *player_ptr, concptr filename, angband_header *head, void **info, char **name, char **text, char **tag) { /* General buffer */ char buf[1024]; @@ -521,7 +521,7 @@ static errr init_info(concptr filename, angband_header *head, void **info, char path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format(_("%s_j.raw", "%s.raw"), filename)); /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(player_ptr); /* Kill the old file */ (void)fd_kill(buf); @@ -590,7 +590,7 @@ static errr init_info(concptr filename, angband_header *head, void **info, char * Initialize the "f_info" array * @return エラーコード */ -static errr init_f_info(void) +static errr init_f_info(player_type *player_ptr) { /* Init the header */ init_header(&f_head, max_f_idx, sizeof(feature_type)); @@ -601,7 +601,7 @@ static errr init_f_info(void) /* Save a pointer to the retouch fake tags */ f_head.retouch = retouch_f_info; - return init_info("f_info", &f_head, + return init_info(player_ptr, "f_info", &f_head, (void*)&f_info, &f_name, NULL, &f_tag); } @@ -611,7 +611,7 @@ static errr init_f_info(void) * Initialize the "k_info" array * @return エラーコード */ -static errr init_k_info(void) +static errr init_k_info(player_type *player_ptr) { /* Init the header */ init_header(&k_head, max_k_idx, sizeof(object_kind)); @@ -619,7 +619,7 @@ static errr init_k_info(void) /* Save a pointer to the parsing function */ k_head.parse_info_txt = parse_k_info; - return init_info("k_info", &k_head, + return init_info(player_ptr, "k_info", &k_head, (void*)&k_info, &k_name, &k_text, NULL); } @@ -629,7 +629,7 @@ static errr init_k_info(void) * Initialize the "a_info" array * @return エラーコード */ -static errr init_a_info(void) +static errr init_a_info(player_type *player_ptr) { /* Init the header */ init_header(&a_head, max_a_idx, sizeof(artifact_type)); @@ -637,7 +637,7 @@ static errr init_a_info(void) /* Save a pointer to the parsing function */ a_head.parse_info_txt = parse_a_info; - return init_info("a_info", &a_head, + return init_info(player_ptr, "a_info", &a_head, (void*)&a_info, &a_name, &a_text, NULL); } @@ -647,7 +647,7 @@ static errr init_a_info(void) * Initialize the "e_info" array * @return エラーコード */ -static errr init_e_info(void) +static errr init_e_info(player_type *player_ptr) { /* Init the header */ init_header(&e_head, max_e_idx, sizeof(ego_item_type)); @@ -655,7 +655,7 @@ static errr init_e_info(void) /* Save a pointer to the parsing function */ e_head.parse_info_txt = parse_e_info; - return init_info("e_info", &e_head, + return init_info(player_ptr, "e_info", &e_head, (void*)&e_info, &e_name, &e_text, NULL); } @@ -665,7 +665,7 @@ static errr init_e_info(void) * Initialize the "r_info" array * @return エラーコード */ -static errr init_r_info(void) +static errr init_r_info(player_type *player_ptr) { /* Init the header */ init_header(&r_head, max_r_idx, sizeof(monster_race)); @@ -673,7 +673,7 @@ static errr init_r_info(void) /* Save a pointer to the parsing function */ r_head.parse_info_txt = parse_r_info; - return init_info("r_info", &r_head, + return init_info(player_ptr, "r_info", &r_head, (void*)&r_info, &r_name, &r_text, NULL); } @@ -683,7 +683,7 @@ static errr init_r_info(void) * Initialize the "d_info" array * @return エラーコード */ -static errr init_d_info(void) +static errr init_d_info(player_type *player_ptr) { /* Init the header */ init_header(&d_head, current_world_ptr->max_d_idx, sizeof(dungeon_type)); @@ -691,7 +691,7 @@ static errr init_d_info(void) /* Save a pointer to the parsing function */ d_head.parse_info_txt = parse_d_info; - return init_info("d_info", &d_head, + return init_info(player_ptr, "d_info", &d_head, (void*)&d_info, &d_name, &d_text, NULL); } @@ -704,7 +704,7 @@ static errr init_d_info(void) * Note that we let each entry have a unique "name" and "text" string, * even if the string happens to be empty (everyone has a unique '\0'). */ -errr init_v_info(void) +errr init_v_info(player_type *player_ptr) { /* Init the header */ init_header(&v_head, max_v_idx, sizeof(vault_type)); @@ -712,7 +712,7 @@ errr init_v_info(void) /* Save a pointer to the parsing function */ v_head.parse_info_txt = parse_v_info; - return init_info("v_info", &v_head, + return init_info(player_ptr, "v_info", &v_head, (void*)&v_info, &v_name, &v_text, NULL); } @@ -722,7 +722,7 @@ errr init_v_info(void) * Initialize the "s_info" array * @return エラーコード */ -static errr init_s_info(void) +static errr init_s_info(player_type *player_ptr) { /* Init the header */ init_header(&s_head, MAX_CLASS, sizeof(skill_table)); @@ -730,7 +730,7 @@ static errr init_s_info(void) /* Save a pointer to the parsing function */ s_head.parse_info_txt = parse_s_info; - return init_info("s_info", &s_head, + return init_info(player_ptr, "s_info", &s_head, (void*)&s_info, NULL, NULL, NULL); } @@ -740,7 +740,7 @@ static errr init_s_info(void) * Initialize the "m_info" array * @return エラーコード */ -static errr init_m_info(void) +static errr init_m_info(player_type *player_ptr) { /* Init the header */ init_header(&m_head, MAX_CLASS, sizeof(player_magic)); @@ -748,7 +748,7 @@ static errr init_m_info(void) /* Save a pointer to the parsing function */ m_head.parse_info_txt = parse_m_info; - return init_info("m_info", &m_head, + return init_info(player_ptr, "m_info", &m_head, (void*)&m_info, NULL, NULL, NULL); } @@ -1342,7 +1342,7 @@ void init_angband(player_type *player_ptr, void(*process_autopick_file_command)( if (fd < 0) { /* Grab permissions */ - safe_setuid_grab(); + safe_setuid_grab(player_ptr); /* Create a new high score file */ fd = fd_make(buf, file_permission); @@ -1375,36 +1375,36 @@ void init_angband(player_type *player_ptr, void(*process_autopick_file_command)( /* Initialize feature info */ #ifdef JP note("[データの初期化中... (地形)]"); - if (init_f_info()) quit("地形初期化不能"); + if (init_f_info(player_ptr)) quit("地形初期化不能"); if (init_feat_variables()) quit("地形初期化不能"); #else note("[Initializing arrays... (features)]"); - if (init_f_info()) quit("Cannot initialize features"); + if (init_f_info(player_ptr)) quit("Cannot initialize features"); if (init_feat_variables()) quit("Cannot initialize features"); #endif /* Initialize object info */ note(_("[データの初期化中... (アイテム)]", "[Initializing arrays... (objects)]")); - if (init_k_info()) quit(_("アイテム初期化不能", "Cannot initialize objects")); + if (init_k_info(player_ptr)) quit(_("アイテム初期化不能", "Cannot initialize objects")); /* Initialize artifact info */ note(_("[データの初期化中... (伝説のアイテム)]", "[Initializing arrays... (artifacts)]")); - if (init_a_info()) quit(_("伝説のアイテム初期化不能", "Cannot initialize artifacts")); + if (init_a_info(player_ptr)) quit(_("伝説のアイテム初期化不能", "Cannot initialize artifacts")); /* Initialize ego-item info */ note(_("[データの初期化中... (名のあるアイテム)]", "[Initializing arrays... (ego-items)]")); - if (init_e_info()) quit(_("名のあるアイテム初期化不能", "Cannot initialize ego-items")); + if (init_e_info(player_ptr)) quit(_("名のあるアイテム初期化不能", "Cannot initialize ego-items")); /* Initialize monster info */ note(_("[データの初期化中... (モンスター)]", "[Initializing arrays... (monsters)]")); - if (init_r_info()) quit(_("モンスター初期化不能", "Cannot initialize monsters")); + if (init_r_info(player_ptr)) quit(_("モンスター初期化不能", "Cannot initialize monsters")); /* Initialize dungeon info */ note(_("[データの初期化中... (ダンジョン)]", "[Initializing arrays... (dungeon)]")); - if (init_d_info()) quit(_("ダンジョン初期化不能", "Cannot initialize dungeon")); + if (init_d_info(player_ptr)) quit(_("ダンジョン初期化不能", "Cannot initialize dungeon")); { for (int i = 1; i < current_world_ptr->max_d_idx; i++) if (d_info[i].final_guardian) @@ -1413,11 +1413,11 @@ void init_angband(player_type *player_ptr, void(*process_autopick_file_command)( /* Initialize magic info */ note(_("[データの初期化中... (魔法)]", "[Initializing arrays... (magic)]")); - if (init_m_info()) quit(_("魔法初期化不能", "Cannot initialize magic")); + if (init_m_info(player_ptr)) quit(_("魔法初期化不能", "Cannot initialize magic")); /* Initialize weapon_exp info */ note(_("[データの初期化中... (熟練度)]", "[Initializing arrays... (skill)]")); - if (init_s_info()) quit(_("熟練度初期化不能", "Cannot initialize skill")); + if (init_s_info(player_ptr)) quit(_("熟練度初期化不能", "Cannot initialize skill")); /* Initialize wilderness array */ note(_("[配列を初期化しています... (荒野)]", "[Initializing arrays... (wilderness)]")); @@ -1437,7 +1437,7 @@ void init_angband(player_type *player_ptr, void(*process_autopick_file_command)( if (init_quests()) quit(_("クエストを初期化できません", "Cannot initialize quests")); /* Initialize vault info */ - if (init_v_info()) quit(_("vault 初期化不能", "Cannot initialize vaults")); + if (init_v_info(player_ptr)) quit(_("vault 初期化不能", "Cannot initialize vaults")); /* Initialize some other arrays */ note(_("[データの初期化中... (その他)]", "[Initializing arrays... (other)]")); diff --git a/src/main/init.h b/src/main/init.h index 8fce2bdc7..82c642615 100644 --- a/src/main/init.h +++ b/src/main/init.h @@ -19,7 +19,7 @@ void init_angband(player_type *player_ptr, void(*process_autopick_file_command)(char*)); concptr get_check_sum(void); void init_file_paths(char *path); -errr init_v_info(void); +errr init_v_info(player_type *player_ptr); errr init_buildings(void); #endif /* INCLUDED_INIT_H */ -- 2.11.0