| 52 | } |
| 53 | |
| 54 | static void _read_each_message() |
| 55 | { |
| 56 | bool say_got_msg = true; |
| 57 | FILE *mf = fopen_u(SysEnv.messagefile.c_str(), "r+"); |
| 58 | if (!mf) |
| 59 | { |
| 60 | mprf(MSGCH_ERROR, "Couldn't read %s: %s", SysEnv.messagefile.c_str(), |
| 61 | strerror(errno)); |
| 62 | _kill_messaging(mf); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // Read messages, code borrowed from the SIMPLEMAIL patch. |
| 67 | char line[120]; |
| 68 | |
| 69 | if (!lock_file_handle(mf, false)) |
| 70 | { |
| 71 | mprf(MSGCH_ERROR, "Failed to lock %s: %s", SysEnv.messagefile.c_str(), |
| 72 | strerror(errno)); |
| 73 | _kill_messaging(mf); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | while (fgets(line, sizeof line, mf)) |
| 78 | { |
| 79 | unlock_file_handle(mf); |
| 80 | |
| 81 | const int len = strlen(line); |
| 82 | if (len) |
| 83 | { |
| 84 | if (line[len - 1] == '\n') |
| 85 | line[len - 1] = 0; |
| 86 | |
| 87 | if (say_got_msg) |
| 88 | { |
| 89 | mprf(MSGCH_DGL_MESSAGE, "Your messages:"); |
| 90 | say_got_msg = false; |
| 91 | } |
| 92 | |
| 93 | _show_message_line(line); |
| 94 | } |
| 95 | |
| 96 | if (!lock_file_handle(mf, false)) |
| 97 | { |
| 98 | mprf(MSGCH_ERROR, "Failed to lock %s: %s", |
| 99 | SysEnv.messagefile.c_str(), |
| 100 | strerror(errno)); |
| 101 | _kill_messaging(mf); |
| 102 | return; |
| 103 | } |
| 104 | } |
| 105 | if (!lock_file_handle(mf, true)) |
| 106 | { |
| 107 | mprf(MSGCH_ERROR, "Unable to write lock %s: %s", |
| 108 | SysEnv.messagefile.c_str(), |
| 109 | strerror(errno)); |
| 110 | } |
| 111 | if (!ftruncate(fileno(mf), 0)) |
no test coverage detected