3 * @brief ファイル入出力管理 / Purpose: code dealing with files (and death)
7 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
8 * This software may be copied and distributed for educational, research,
9 * and not for profit purposes provided that this copyright and statement
10 * are included in all such copies. Other copyrights may also apply.
11 * 2014 Deskull rearranged comment for Doxygen.\n
17 #include "uid-checker.h"
19 #include "core.h" // リファクタリングして後で消す
21 #include "character-dump.h"
23 #include "view-mainwindow.h"
25 #include "io/tokenizer.h"
26 #include "io/process-pref-file.h" // 暫定。依存性の向きがこれで良いか要確認.
28 #define PREF_TYPE_NORMAL 0
29 #define PREF_TYPE_AUTOPICK 1
30 #define PREF_TYPE_HISTPREF 2
32 concptr ANGBAND_DIR; //!< Path name: The main "lib" directory This variable is not actually used anywhere in the code
33 concptr ANGBAND_DIR_APEX; //!< High score files (binary) These files may be portable between platforms
34 concptr ANGBAND_DIR_BONE; //!< Bone files for player ghosts (ascii) These files are portable between platforms
35 concptr ANGBAND_DIR_DATA; //!< Binary image files for the "*_info" arrays (binary) These files are not portable between platforms
36 concptr ANGBAND_DIR_EDIT; //!< Textual template files for the "*_info" arrays (ascii) These files are portable between platforms
37 concptr ANGBAND_DIR_SCRIPT; //!< Script files These files are portable between platforms.
38 concptr ANGBAND_DIR_FILE; //!< Various extra files (ascii) These files may be portable between platforms
39 concptr ANGBAND_DIR_HELP; //!< Help files (normal) for the online help (ascii) These files are portable between platforms
40 concptr ANGBAND_DIR_INFO; //!< Help files (spoilers) for the online help (ascii) These files are portable between platforms
41 concptr ANGBAND_DIR_PREF; //!< Default user "preference" files (ascii) These files are rarely portable between platforms
42 concptr ANGBAND_DIR_SAVE; //!< Savefiles for current characters (binary)
43 concptr ANGBAND_DIR_USER; //!< User "preference" files (ascii) These files are rarely portable between platforms
44 concptr ANGBAND_DIR_XTRA; //!< Various extra files (binary) These files are rarely portable between platforms
47 * Buffer to hold the current savefile name
48 * 'savefile' holds full path name. 'savefile_base' holds only base name.
51 char savefile_base[40];
54 * todo サブルーチンとは言い難い。autopick.c から直接呼ばれている
55 * @brief process_pref_fileのサブルーチンとして条件分岐処理の解釈と結果を返す
56 * Helper function for "process_pref_file()"
57 * @param creature_ptr プレーヤーへの参照ポインタ
58 * @param sp テキスト文字列の参照ポインタ
59 * @param fp 再帰中のポインタ参照
64 * v: output buffer array
70 concptr process_pref_file_expr(player_type *creature_ptr, char **sp, char *fp)
74 while (iswspace(*s)) s++;
94 t = process_pref_file_expr(creature_ptr, &s, &f);
99 else if (streq(t, "IOR"))
102 while (*s && (f != b2))
104 t = process_pref_file_expr(creature_ptr, &s, &f);
105 if (*t && !streq(t, "0")) v = "1";
108 else if (streq(t, "AND"))
111 while (*s && (f != b2))
113 t = process_pref_file_expr(creature_ptr, &s, &f);
114 if (*t && streq(t, "0")) v = "0";
117 else if (streq(t, "NOT"))
120 while (*s && (f != b2))
122 t = process_pref_file_expr(creature_ptr, &s, &f);
123 if (*t && streq(t, "1")) v = "0";
126 else if (streq(t, "EQU"))
131 t = process_pref_file_expr(creature_ptr, &s, &f);
133 while (*s && (f != b2))
135 p = process_pref_file_expr(creature_ptr, &s, &f);
136 if (streq(t, p)) v = "1";
139 else if (streq(t, "LEQ"))
144 t = process_pref_file_expr(creature_ptr, &s, &f);
146 while (*s && (f != b2))
149 t = process_pref_file_expr(creature_ptr, &s, &f);
150 if (*t && atoi(p) > atoi(t)) v = "0";
153 else if (streq(t, "GEQ"))
158 t = process_pref_file_expr(creature_ptr, &s, &f);
160 while (*s && (f != b2))
163 t = process_pref_file_expr(creature_ptr, &s, &f);
164 if (*t && atoi(p) < atoi(t)) v = "0";
169 while (*s && (f != b2))
171 t = process_pref_file_expr(creature_ptr, &s, &f);
175 if (f != b2) v = "?x?x?";
177 if ((f = *s) != '\0') *s++ = '\0';
184 /* Accept all printables except spaces and brackets */
186 while (iskanji(*s) || (isprint(*s) && !my_strchr(" []", *s)))
188 if (iskanji(*s)) s++;
192 while (isprint(*s) && !my_strchr(" []", *s)) ++s;
195 if ((f = *s) != '\0') *s++ = '\0';
205 if (streq(b + 1, "SYS"))
209 else if (streq(b + 1, "KEYBOARD"))
211 v = ANGBAND_KEYBOARD;
213 else if (streq(b + 1, "GRAF"))
217 else if (streq(b + 1, "MONOCHROME"))
224 else if (streq(b + 1, "RACE"))
232 else if (streq(b + 1, "CLASS"))
240 else if (streq(b + 1, "PLAYER"))
242 static char tmp_player_name[32];
244 for (pn = creature_ptr->name, tpn = tmp_player_name; *pn; pn++, tpn++)
254 *tpn = my_strchr(" []", *pn) ? '_' : *pn;
260 else if (streq(b + 1, "REALM1"))
263 v = E_realm_names[creature_ptr->realm1];
265 v = realm_names[creature_ptr->realm1];
268 else if (streq(b + 1, "REALM2"))
271 v = E_realm_names[creature_ptr->realm2];
273 v = realm_names[creature_ptr->realm2];
276 else if (streq(b + 1, "LEVEL"))
278 sprintf(tmp, "%02d", creature_ptr->lev);
281 else if (streq(b + 1, "AUTOREGISTER"))
283 if (creature_ptr->autopick_autoregister)
288 else if (streq(b + 1, "MONEY"))
290 sprintf(tmp, "%09ld", (long int)creature_ptr->au);
301 * @brief process_pref_fileのサブルーチン /
302 * Open the "user pref file" and parse it.
303 * @param creature_ptr プレーヤーへの参照ポインタ
304 * @param name 読み込むファイル名
305 * @param preftype prefファイルのタイプ
310 * v: output buffer array
316 static errr process_pref_file_aux(player_type *creature_ptr, concptr name, int preftype)
319 fp = my_fopen(name, "r");
327 while (my_fgets(fp, buf, sizeof(buf)) == 0)
330 if (!buf[0]) continue;
333 if (!iskanji(buf[0]))
335 if (iswspace(buf[0])) continue;
337 if (buf[0] == '#') continue;
340 /* Process "?:<expr>" */
341 if ((buf[0] == '?') && (buf[1] == ':'))
346 concptr v = process_pref_file_expr(creature_ptr, &s, &f);
347 bypass = streq(v, "0");
351 if (bypass) continue;
353 /* Process "%:<file>" */
356 static int depth_count = 0;
357 if (depth_count > 20) continue;
362 case PREF_TYPE_AUTOPICK:
363 (void)process_autopick_file(creature_ptr, buf + 2);
365 case PREF_TYPE_HISTPREF:
366 (void)process_histpref_file(creature_ptr, buf + 2);
369 (void)process_pref_file(creature_ptr, buf + 2);
377 err = process_pref_file_command(creature_ptr, buf);
380 if (preftype != PREF_TYPE_AUTOPICK)
382 err = process_autopick_file_command(buf);
388 /* Print error message */
389 /* ToDo: Add better error messages */
390 msg_format(_("ファイル'%s'の%d行でエラー番号%dのエラー。", "Error %d in line %d of file '%s'."),
391 _(name, err), line, _(err, name));
392 msg_format(_("('%s'を解析中)", "Parsing '%s'"), old);
402 * @brief pref設定ファイルを読み込み設定を反映させる /
403 * Process the "user pref file" with the given name
404 * @param creature_ptr プレーヤーへの参照ポインタ
405 * @param name 読み込むファイル名
409 * See the functions above for a list of legal "commands".
410 * We also accept the special "?" and "%" directives, which
411 * allow conditional evaluation and filename inclusion.
414 errr process_pref_file(player_type *creature_ptr, concptr name)
417 path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, name);
419 errr err1 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL);
420 if (err1 > 0) return err1;
422 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
423 errr err2 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL);
424 if (err2 < 0 && !err1)
432 * @brief プレイヤーステータスをファイルダンプ出力する
433 * Hack -- Dump a character description file
434 * @param creature_ptr プレーヤーへの参照ポインタ
435 * @param name 出力ファイル名
438 * Allow the "full" flag to dump additional info,
439 * and trigger its usage from various places in the code.
441 errr file_character(player_type *creature_ptr, concptr name, display_player_pf display_player, map_name_pf map_name)
444 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
446 FILE_TYPE(FILE_TYPE_TEXT);
448 int fd = fd_open(buf, O_RDONLY);
453 (void)sprintf(out_val, _("現存するファイル %s に上書きしますか? ", "Replace existing file %s? "), buf);
454 if (get_check_strict(out_val, CHECK_NO_HISTORY)) fd = -1;
458 if (fd < 0) fff = my_fopen(buf, "w");
462 prt(_("キャラクタ情報のファイルへの書き出しに失敗しました!", "Character dump failed!"), 0, 0);
467 make_character_dump(creature_ptr, fff, update_playtime, display_player, map_name);
469 msg_print(_("キャラクタ情報のファイルへの書き出しに成功しました。", "Character dump successful."));
476 * @brief ファイル内容の一行をコンソールに出力する
477 * Display single line of on-line help file
484 * You can insert some special color tag to change text color.
486 * WHITETEXT [[[[y|SOME TEXT WHICH IS DISPLAYED IN YELLOW| WHITETEXT
487 * A colored segment is between "[[[[y|" and the last "|".
488 * You can use any single character in place of the "|".
491 static void show_file_aux_line(concptr str, int cy, concptr shower)
503 static const char tag_str[] = "[[[[";
504 byte color = TERM_WHITE;
506 for (int i = 0; str[i];)
508 int len = strlen(&str[i]);
509 int showercol = len + 1;
510 int bracketcol = len + 1;
515 ptr = my_strstr(&lcstr[i], shower);
516 if (ptr) showercol = ptr - &lcstr[i];
519 ptr = in_tag ? my_strchr(&str[i], in_tag) : my_strstr(&str[i], tag_str);
520 if (ptr) bracketcol = ptr - &str[i];
521 if (bracketcol < endcol) endcol = bracketcol;
522 if (showercol < endcol) endcol = showercol;
524 Term_addstr(endcol, color, &str[i]);
528 if (endcol == showercol)
530 int showerlen = strlen(shower);
531 Term_addstr(showerlen, TERM_YELLOW, &str[i]);
537 if (endcol != bracketcol) continue;
547 i += sizeof(tag_str) - 1;
548 color = color_char_to_attr(str[i]);
549 if (color == 255 || str[i + 1] == '\0')
552 Term_addstr(-1, TERM_WHITE, tag_str);
553 cx += sizeof(tag_str) - 1;
562 Term_erase(cx, cy, 255);
567 * @brief ファイル内容をコンソールに出力する
568 * Recursive file perusal.
569 * @param creature_ptr プレーヤーへの参照ポインタ
570 * @param show_version TRUEならばコンソール上にゲームのバージョンを表示する
571 * @param name ファイル名の文字列
572 * @param what 内容キャプションの文字列
578 * Process various special text in the input file, including
579 * the "menu" structures used by the "help file" system.
580 * Return FALSE on 'q' to exit from a deep, otherwise TRUE.
583 bool show_file(player_type *creature_ptr, bool show_version, concptr name, concptr what, int line, BIT_FLAGS mode)
586 Term_get_size(&wid, &hgt);
589 strcpy(finder_str, "");
592 strcpy(shower_str, "");
598 for (int i = 0; i < 68; i++)
604 strcpy(filename, name);
605 int n = strlen(filename);
608 for (int i = 0; i < n; i++)
610 if (filename[i] == '#')
613 tag = filename + i + 1;
623 strcpy(caption, what);
625 fff = my_fopen(path, "r");
630 sprintf(caption, _("ヘルプ・ファイル'%s'", "Help file '%s'"), name);
631 path_build(path, sizeof(path), ANGBAND_DIR_HELP, name);
632 fff = my_fopen(path, "r");
637 sprintf(caption, _("スポイラー・ファイル'%s'", "Info file '%s'"), name);
638 path_build(path, sizeof(path), ANGBAND_DIR_INFO, name);
639 fff = my_fopen(path, "r");
644 path_build(path, sizeof(path), ANGBAND_DIR, name);
646 for (int i = 0; path[i]; i++)
648 path[i] = PATH_SEP[0];
650 sprintf(caption, _("スポイラー・ファイル'%s'", "Info file '%s'"), name);
651 fff = my_fopen(path, "r");
656 msg_format(_("'%s'をオープンできません。", "Cannot open '%s'."), name);
668 bool reverse = (line < 0);
672 if (my_fgets(fff, buf, sizeof(buf))) break;
673 if (!prefix(str, "***** "))
679 if ((str[6] == '[') && isalpha(str[7]))
681 int k = str[7] - 'A';
683 if ((str[8] == ']') && (str[9] == ' '))
685 strncpy(hook[k], str + 10, 31);
692 if (str[6] != '<') continue;
694 size_t len = strlen(str);
695 if (str[len - 1] == '>')
698 if (tag && streq(str + 7, tag)) line = next;
705 line = ((size - 1) / rows)*rows;
712 if (line >= size - rows)
714 if (line < 0) line = 0;
719 fff = my_fopen(path, "r");
720 if (!fff) return FALSE;
727 if (my_fgets(fff, buf, sizeof(buf))) break;
728 if (prefix(buf, "***** ")) continue;
733 concptr shower = NULL;
734 for (int i = 0; i < rows; i++)
738 if (my_fgets(fff, buf, sizeof(buf))) break;
739 if (prefix(buf, "***** ")) continue;
746 if (!my_strstr(lc_buf, find)) continue;
750 show_file_aux_line(str, i + 2, shower);
754 while (row_count < rows)
756 Term_erase(0, row_count + 2, 255);
770 prt(format(_("[変愚蛮怒 %d.%d.%d, %s, %d/%d]", "[Hengband %d.%d.%d, %s, Line %d/%d]"),
771 FAKE_VER_MAJOR - 10, FAKE_VER_MINOR, FAKE_VER_PATCH,
772 caption, line, size), 0, 0);
776 prt(format(_("[%s, %d/%d]", "[%s, Line %d/%d]"),
777 caption, line, size), 0, 0);
782 prt(_("[キー:(?)ヘルプ (ESC)終了]", "[Press ESC to exit.]"), hgt - 1, 0);
788 prt("[キー:(RET/スペース)↑ (-)↓ (?)ヘルプ (ESC)終了]", hgt - 1, 0);
790 prt("[キー:(RET/スペース)↓ (-)↑ (?)ヘルプ (ESC)終了]", hgt - 1, 0);
792 prt("[Press Return, Space, -, =, /, |, or ESC to exit.]", hgt - 1, 0);
796 skey = inkey_special(TRUE);
800 if (strcmp(name, _("jhelpinfo.txt", "helpinfo.txt")) != 0)
801 show_file(creature_ptr, TRUE, _("jhelpinfo.txt", "helpinfo.txt"), NULL, 0, mode);
804 prt(_("強調: ", "Show: "), hgt - 1, 0);
807 strcpy(back_str, shower_str);
808 if (askfor(shower_str, 80))
812 str_tolower(shower_str);
817 else strcpy(shower_str, back_str);
822 prt(_("検索: ", "Find: "), hgt - 1, 0);
823 strcpy(back_str, finder_str);
824 if (askfor(finder_str, 80))
831 str_tolower(finder_str);
836 else strcpy(finder_str, back_str);
842 prt(_("行: ", "Goto Line: "), hgt - 1, 0);
845 if (askfor(tmp, 80)) line = atoi(tmp);
854 line = ((size - 1) / rows) * rows;
860 prt(_("ファイル・ネーム: ", "Goto File: "), hgt - 1, 0);
861 strcpy(tmp, _("jhelp.hlp", "help.hlp"));
865 if (!show_file(creature_ptr, TRUE, tmp, NULL, 0, mode)) skey = 'q';
872 line = line + (reverse ? rows : -rows);
873 if (line < 0) line = 0;
878 if (line < 0) line = 0;
883 line = line + (reverse ? -1 : 1);
884 if (line < 0) line = 0;
890 if (line < 0) line = 0;
899 line = line + (reverse ? -rows : rows);
900 if (line < 0) line = 0;
911 if (!(skey & SKEY_MASK) && isalpha(skey))
914 if ((key > -1) && hook[key][0])
916 /* Recurse on that file */
917 if (!show_file(creature_ptr, TRUE, hook[key], NULL, 0, mode))
930 if (!get_string(_("ファイル名: ", "File name: "), xtmp, 80)) continue;
932 path_build(buff, sizeof(buff), ANGBAND_DIR_USER, xtmp);
934 /* Hack -- Re-Open the file */
935 fff = my_fopen(path, "r");
937 ffp = my_fopen(buff, "w");
941 msg_print(_("ファイルを開けません。", "Failed to open file."));
946 sprintf(xtmp, "%s: %s", creature_ptr->name, what ? what : caption);
947 my_fputs(ffp, xtmp, 80);
948 my_fputs(ffp, "\n", 80);
950 while (!my_fgets(fff, buff, sizeof(buff)))
951 my_fputs(ffp, buff, 80);
954 fff = my_fopen(path, "r");
957 if ((skey == ESCAPE) || (skey == '<')) break;
959 if (skey == KTRL('q')) skey = 'q';
961 if (skey == 'q') break;
965 return (skey != 'q');
970 * @brief ファイルからランダムに行を一つ取得する /
971 * Get a random line from a file
972 * @param file_name ファイル名
973 * @param entry 特定条件時のN:タグヘッダID
974 * @param output 出力先の文字列参照ポインタ
978 * Based on the monster speech patch by Matt Graham,
981 errr get_rnd_line(concptr file_name, int entry, char *output)
984 path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, file_name);
986 fp = my_fopen(buf, "r");
993 if (my_fgets(fp, buf, sizeof(buf)) != 0)
1000 if ((buf[0] != 'N') || (buf[1] != ':')) continue;
1006 else if (buf[2] == 'M')
1008 if (r_info[entry].flags1 & RF1_MALE) break;
1010 else if (buf[2] == 'F')
1012 if (r_info[entry].flags1 & RF1_FEMALE) break;
1014 else if (sscanf(&(buf[2]), "%d", &test) != EOF)
1016 if (test == entry) break;
1019 msg_format("Error in line %d of %s!", line_num, file_name);
1025 for (counter = 0; ; counter++)
1029 test = my_fgets(fp, buf, sizeof(buf));
1032 /* Ignore lines starting with 'N:' */
1033 if ((buf[0] == 'N') && (buf[1] == ':')) continue;
1035 if (buf[0] != '#') break;
1042 if (one_in_(counter + 1)) strcpy(output, buf);
1046 return counter ? 0 : -1;
1052 * @brief ファイルからランダムに行を一つ取得する(日本語文字列のみ) /
1053 * @param file_name ファイル名
1054 * @param entry 特定条件時のN:タグヘッダID
1055 * @param output 出力先の文字列参照ポインタ
1060 errr get_rnd_line_jonly(concptr file_name, int entry, char *output, int count)
1063 for (int i = 0; i < count; i++)
1065 result = get_rnd_line(file_name, entry, output);
1068 for (int j = 0; output[j]; j++) kanji |= iskanji(output[j]);
1078 * @brief 自動拾いファイルを読み込む /
1079 * @param creature_ptr プレーヤーへの参照ポインタ
1083 errr process_autopick_file(player_type *creature_ptr, concptr name)
1086 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
1087 errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_AUTOPICK);
1093 * @brief プレイヤーの生い立ちファイルを読み込む /
1094 * Process file for player's history editor.
1095 * @param creature_ptr プレーヤーへの参照ポインタ
1100 errr process_histpref_file(player_type *creature_ptr, concptr name)
1102 bool old_character_xtra = current_world_ptr->character_xtra;
1104 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
1106 /* Hack -- prevent modification birth options in this file */
1107 current_world_ptr->character_xtra = TRUE;
1108 errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_HISTPREF);
1109 current_world_ptr->character_xtra = old_character_xtra;
1115 * @brief ファイル位置をシーク /
1116 * @param creature_ptr プレーヤーへの参照ポインタ
1117 * @param fd ファイルディスクリプタ
1118 * @param where ファイルバイト位置
1119 * @param flag FALSEならば現ファイルを超えた位置へシーク時エラー、TRUEなら足りない間を0で埋め尽くす
1123 static errr counts_seek(player_type *creature_ptr, int fd, u32b where, bool flag)
1125 char temp1[128], temp2[128];
1126 #ifdef SAVEFILE_USE_UID
1127 (void)sprintf(temp1, "%d.%s.%d%d%d", creature_ptr->player_uid, savefile_base, creature_ptr->pclass, creature_ptr->pseikaku, creature_ptr->age);
1129 (void)sprintf(temp1, "%s.%d%d%d", savefile_base, creature_ptr->pclass, creature_ptr->pseikaku, creature_ptr->age);
1131 for (int i = 0; temp1[i]; i++)
1132 temp1[i] ^= (i + 1) * 63;
1135 u32b zero_header[3] = { 0L, 0L, 0L };
1138 if (fd_seek(fd, seekpoint + 3 * sizeof(u32b)))
1140 if (fd_read(fd, (char*)(temp2), sizeof(temp2)))
1145 fd_seek(fd, seekpoint);
1146 fd_write(fd, (char*)zero_header, 3 * sizeof(u32b));
1147 fd_write(fd, (char*)(temp1), sizeof(temp1));
1151 if (strcmp(temp1, temp2) == 0)
1154 seekpoint += 128 + 3 * sizeof(u32b);
1157 return fd_seek(fd, seekpoint + where * sizeof(u32b));
1162 * @brief ファイル位置を読み込む
1163 * @param creature_ptr プレーヤーへの参照ポインタ
1164 * @param where ファイルバイト位置
1168 u32b counts_read(player_type *creature_ptr, int where)
1171 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, _("z_info_j.raw", "z_info.raw"));
1172 int fd = fd_open(buf, O_RDONLY);
1175 if (counts_seek(creature_ptr, fd, where, FALSE) ||
1176 fd_read(fd, (char*)(&count), sizeof(u32b)))
1186 * @brief ファイル位置に書き込む /
1187 * @param creature_ptr プレーヤーへの参照ポインタ
1188 * @param where ファイルバイト位置
1189 * @param count 書き込む値
1193 errr counts_write(player_type *creature_ptr, int where, u32b count)
1196 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, _("z_info_j.raw", "z_info.raw"));
1199 int fd = fd_open(buf, O_RDWR);
1203 FILE_TYPE(FILE_TYPE_DATA);
1205 fd = fd_make(buf, 0644);
1210 errr err = fd_lock(fd, F_WRLCK);
1214 counts_seek(creature_ptr, fd, where, TRUE);
1215 fd_write(fd, (char*)(&count), sizeof(u32b));
1217 err = fd_lock(fd, F_UNLCK);
1228 * @brief 墓のアスキーアートテンプレを読み込む
1229 * @param buf テンプレへのバッファ
1230 * @param buf_size バッファの長さ
1233 void read_dead_file(char *buf, size_t buf_size)
1235 path_build(buf, buf_size, ANGBAND_DIR_FILE, _("dead_j.txt", "dead.txt"));
1238 fp = my_fopen(buf, "r");
1242 while (my_fgets(fp, buf, buf_size) == 0)
1244 put_str(buf, i++, 0);