(turn): Change to stdio.
(turn): Always close the file after attempting to write. Fixes bug where the file is not close when there is writing error.
This commit is contained in:
parent
0aaf0084aa
commit
df7dc203c4
1 changed files with 7 additions and 6 deletions
|
@ -46,7 +46,7 @@
|
||||||
int
|
int
|
||||||
turn(void)
|
turn(void)
|
||||||
{
|
{
|
||||||
int fd;
|
FILE *fptr;
|
||||||
struct telstr tgm;
|
struct telstr tgm;
|
||||||
char *p;
|
char *p;
|
||||||
char buf[MAXTELSIZE];
|
char buf[MAXTELSIZE];
|
||||||
|
@ -95,8 +95,8 @@ turn(void)
|
||||||
pr("Writing empty no-login message.\n");
|
pr("Writing empty no-login message.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
fd = open(msgfilepath, O_RDWR | O_CREAT | O_TRUNC, 0660);
|
fptr = fopen(msgfilepath, "w");
|
||||||
if (fd == -1) {
|
if (fptr == NULL) {
|
||||||
pr("Something went wrong opening the message file.\n");
|
pr("Something went wrong opening the message file.\n");
|
||||||
logerror("Could not open message file (%s).\n", msgfilepath);
|
logerror("Could not open message file (%s).\n", msgfilepath);
|
||||||
return RET_SYS;
|
return RET_SYS;
|
||||||
|
@ -105,14 +105,15 @@ turn(void)
|
||||||
if (msgfilepath == downfil)
|
if (msgfilepath == downfil)
|
||||||
pr("Logins disabled.\n");
|
pr("Logins disabled.\n");
|
||||||
|
|
||||||
if ((write(fd, &tgm, sizeof(tgm)) < (ssize_t)sizeof(tgm)) ||
|
if ((fwrite((void *)&tgm, sizeof(tgm), 1, fptr) != 1) ||
|
||||||
(write(fd, buf, tgm.tel_length) < tgm.tel_length) ||
|
(fwrite((void *)buf, tgm.tel_length, 1, fptr) != 1)) {
|
||||||
(close(fd) == -1)) {
|
fclose(fptr);
|
||||||
pr("Something went wrong writing the message file.\n");
|
pr("Something went wrong writing the message file.\n");
|
||||||
logerror("Could not properly write message file (%s).\n",
|
logerror("Could not properly write message file (%s).\n",
|
||||||
msgfilepath);
|
msgfilepath);
|
||||||
return RET_SYS;
|
return RET_SYS;
|
||||||
}
|
}
|
||||||
|
fclose(fptr);
|
||||||
pr("\n");
|
pr("\n");
|
||||||
|
|
||||||
return RET_OK;
|
return RET_OK;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue