signal() returns the previous value of the signal handler. On failure, it returns SIG_ERR, and errno is set to indicate the error. system()
and read()
also return something meaningful.
Neither of the calls to signal()
are checked in your application.
The calls to clock_gettime()
, system()
read()
, mkdir()
, and fgets()
et cetera also go unchecked.
Include stdbool.h
for bool
, true
, and false
. This is not required in C2X, as they are keywords. You could also make a struct
containing all these flags and pass it around as an argument instead of making all the flags global. I'd also suggest moving this input parsing to a separate function.
Furthermore, consider useusing EXIT_FAILURE
and EXIT_SUCCESS
from stdlib.h
instead of non-standard exit codes.
Consider using fp
or stream
. Additionally, the arguments that are not modified in the function should be const
-qualified. If you're using C99 or higher, you should also be using the ptr[static 1]
notation for pointers that are expected to be non-null.
Do not use dynamic allocation when a fixed-size array would suffice
In here:
int bufsize = 32;
char *buf = malloc(bufsize * sizeof(char));
if (buf == NULL) {
perror("Could not allocate memory to restore previous time.\n");
}
memset(buf, 0, bufsize);
The size of the buffer is already known, and there's no reason to call malloc()
for it. Simply do:
#define BUFSIZE 32
char buf[BUFSIZE];
To zero-initialize it, which I don't believe has any value here:
char buf[BUFSIZE] = {0};
// Or in C2X, like C++:
char buf[BUFSIZE] = {};
And malloc()
+memset()
should really be calloc()
.
Also note that sizeof(char)
is defined by the standard to be 1. So it can be safely elided.
Additionally, use an extra variable to avoid the two calls to strlen()
.