Skip to main content
added 837 characters in body
Source Link
Harith
  • 9.8k
  • 1
  • 14
  • 50

Include stdbool.h for bool, true, and false. This is not required in C2X, as they are keywords.

#define C2X_PLACEHOLDER 202000L

#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= C2X_PLACEHOLDER
    /* Coast is clear. */
#else
    #include <stdbool.h>            /* bool, true, and false. */
#endif

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 using EXIT_FAILURE and EXIT_SUCCESS from stdlib.h instead of non-standard exit codes.

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 using EXIT_FAILURE and EXIT_SUCCESS from stdlib.h instead of non-standard exit codes.

Include stdbool.h for bool, true, and false. This is not required in C2X, as they are keywords.

#define C2X_PLACEHOLDER 202000L

#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= C2X_PLACEHOLDER
    /* Coast is clear. */
#else
    #include <stdbool.h>            /* bool, true, and false. */
#endif

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 using EXIT_FAILURE and EXIT_SUCCESS from stdlib.h instead of non-standard exit codes.

added 837 characters in body
Source Link
Harith
  • 9.8k
  • 1
  • 14
  • 50

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().

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() 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 use 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.

signal() returns the previous value of the signal handler. On failure, it returns SIG_ERR, and errno is set to indicate the error.

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 using 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().

added 358 characters in body
Source Link
Harith
  • 9.8k
  • 1
  • 14
  • 50

The identifier fd is conventionally used for a file descriptor, and not a FILE *

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.

The identifier fd is conventionally used for a file descriptor, and not a FILE *

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.

added 247 characters in body
Source Link
Harith
  • 9.8k
  • 1
  • 14
  • 50
Loading
Source Link
Harith
  • 9.8k
  • 1
  • 14
  • 50
Loading