From 3f3f6157f0250535267f096f30b7eea5c1c22ee4 Mon Sep 17 00:00:00 2001 From: John Zaitseff Date: Tue, 2 Aug 2022 21:06:18 +1000 Subject: [PATCH] Use -1 instead of EOF for an invalid file descriptor The POSIX standards specify that the open() and openat() functions technically return -1, not EOF, on failure -- even though most (perhaps all) systems define EOF to be -1! --- src/fileio.c | 2 +- src/utils.c | 12 ++++++------ src/utils.h | 18 +++++++++--------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/fileio.c b/src/fileio.c index 7ff8550..9df1c18 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -466,7 +466,7 @@ bool save_game (int num) // Create the data directory, if needed data_dir = data_directory(); data_dir_fd = data_directory_fileno(); - if (data_dir != NULL && data_dir_fd == EOF) { + if (data_dir != NULL && data_dir_fd == -1) { if (xmkdir(data_dir, S_IRWXU | S_IRWXG | S_IRWXO) != 0) { // Data directory could not be created saved_errno = errno; diff --git a/src/utils.c b/src/utils.c index 9bb9c54..311e0a2 100644 --- a/src/utils.c +++ b/src/utils.c @@ -231,7 +231,7 @@ static const unsigned char xor_table[] = { static char *home_directory_str = NULL; // Full pathname to home static char *data_directory_str = NULL; // Writable data dir pathname -static int data_directory_fd = EOF; // Data dir file descriptor +static int data_directory_fd = -1; // Data dir file descriptor static bool is_posix_locale = false; // Override strfmon()? @@ -418,7 +418,7 @@ const char *data_directory (void) strcat(p, program_name); data_directory_fd = open(p, O_DIRECTORY | O_SEARCH); - if (data_directory_fd != EOF) { + if (data_directory_fd != -1) { // Directory "$HOME/.trader" exists: use that data_directory_str = p; } else { @@ -460,7 +460,7 @@ const char *data_directory (void) } } - if (data_directory_str != NULL && data_directory_fd == EOF) { + if (data_directory_str != NULL && data_directory_fd == -1) { data_directory_fd = open(data_directory_str, O_DIRECTORY | O_SEARCH); } @@ -473,7 +473,7 @@ const char *data_directory (void) int data_directory_fileno (void) { - if (data_directory_fd == EOF) { + if (data_directory_fd == -1) { (void) data_directory(); } @@ -553,7 +553,7 @@ FILE *game_fopen (int gamenum, const char *mode) } ddfd = data_directory_fileno(); - if (ddfd == EOF) { + if (ddfd == -1) { ddfd = AT_FDCWD; } @@ -572,7 +572,7 @@ FILE *game_fopen (int gamenum, const char *mode) fd = openat(ddfd, gfn, oflag, omode); free(gfn); - return (fd == EOF) ? NULL : fdopen(fd, mode); + return (fd == -1) ? NULL : fdopen(fd, mode); } diff --git a/src/utils.h b/src/utils.h index d44825e..9f61930 100644 --- a/src/utils.h +++ b/src/utils.h @@ -108,12 +108,12 @@ extern const char *data_directory (void); /* Function: data_directory_fileno - Return file descriptor of data directory Parameters: (none) - Returns: int - File descriptor, or EOF on failure + Returns: int - File descriptor, or -1 on failure This function returns the file descriptor of the potentially-writable - data directory returned by data_directory(), or EOF (-1) if that - directory does not exist. Note that this function does NOT create the - data directory itself. + data directory returned by data_directory(), or -1 if that directory + does not exist. Note that this function does NOT create the data + directory itself. */ extern int data_directory_fileno (void); @@ -142,11 +142,11 @@ extern char *game_filename (int gamenum); Returns: FILE * - Pointer to open file structure This function opens a game file for reading or writing, depending on - the contents of mode, returning a pointer to the open file structure. - Note that the data directory must be created before calling this - function, by checking whether data_directory_fileno() returns EOF: if - so, the directory must be created. NULL is returned (and errno set) if - the file cannot be opened successfully. + the mode string, returning a pointer to the open file structure. Note + that the data directory must be created before calling this function, + by checking whether data_directory_fileno() returns -1: if so, the + directory must be created. NULL is returned (and errno set) if the + file cannot be opened successfully. */ extern FILE *game_fopen (int gamenum, const char *mode);