mirror of
https://git.zap.org.au/git/trader.git
synced 2025-02-02 15:08:13 -05:00
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!
This commit is contained in:
parent
445cca0d5d
commit
3f3f6157f0
@ -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;
|
||||
|
12
src/utils.c
12
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);
|
||||
}
|
||||
|
||||
|
||||
|
18
src/utils.h
18
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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user