+ if (len == 0) {
+ if (unlink(msgfilepath) < 0 && (errno != ENOENT)) {
+ pr("Could not remove %s file.\n", msgfilepath);
+ logerror("Could not remove %s file (%s)",
+ msgfilepath, strerror(errno));
+ return RET_FAIL;
+ }
+ } else {
+ fptr = fopen(msgfilepath, "wb");
+ if (fptr == NULL) {
+ pr("Something went wrong opening the message file.\n");
+ logerror("Could not open message file (%s).\n", msgfilepath);
+ return RET_FAIL;
+ }
+ memset(&tgm, 0, sizeof(tgm));
+ time(&tgm.tel_date);
+ tgm.tel_length = len;
+ if (fwrite(&tgm, sizeof(tgm), 1, fptr) != 1 ||
+ fwrite(msgbuf, 1, tgm.tel_length, fptr) != tgm.tel_length) {
+ fclose(fptr);
+ pr("Something went wrong writing the message file.\n");
+ logerror("Could not properly write message file (%s).\n",
+ msgfilepath);
+ return RET_FAIL;
+ }
+ if (fclose(fptr)) {
+ pr("Something went wrong closing the message.\n");
+ logerror("Could not properly close message file (%s).\n",
+ msgfilepath);
+ return RET_FAIL;
+ }