diff --git a/SCinter.c b/SCinter.c index 76f5c938..ee443509 100644 --- a/SCinter.c +++ b/SCinter.c @@ -415,7 +415,10 @@ int WriteSicsStatus(SicsInterp * self, char *file, int iMot) pIDrivable pDriv = NULL; void *pTest = NULL; char tmpfile[PATH_MAX]; + char buf[PATH_MAX]; int l; + static int minStatusFileLength = 0; + static int firstbad = 1; assert(self); assert(file); @@ -466,6 +469,27 @@ int WriteSicsStatus(SicsInterp * self, char *file, int iMot) if (iMot) { fprintf(fd, "Success \n"); } + l = ftell(fd); + if (l <= minStatusFileLength) { + /* do not rename file, if length is short (this seems to happen when the computer crashes, why?)*/ + snprintf(buf, sizeof buf, "do not write status file with length only %d", l); + Log(FATAL,"sys", buf); + fclose(fd); + strcpy(buf, tmpfile); + l = strlen(buf); + if (firstbad) { + if (l < sizeof buf - 12) { + firstbad = 0; + snprintf(buf + l, 12, "%ld", time(NULL)); + rename(tmpfile, buf); + } + } else { + buf[l-1] = '_'; + rename(tmpfile, buf); + } + return 1; + } + minStatusFileLength = l / 2; fclose(fd); /* rename temporary to final file (this is an atomic action) */ if (0 > rename(tmpfile, file)) {