Skip to content

Commit

Permalink
util.c fix checking ret value of mkdir
Browse files Browse the repository at this point in the history
  • Loading branch information
KrahJohlito committed Sep 4, 2021
1 parent a41cc2b commit 24bfe5b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -916,13 +916,13 @@ int sysExecElf(const char *path)

int sysCheckMC(void)
{
DIR *mc0_root_dir = opendir("mc0:");
DIR *mc0_root_dir = opendir("mc0:/");
if (mc0_root_dir != NULL) {
closedir(mc0_root_dir);
return 0;
}

DIR *mc1_root_dir = opendir("mc1:");
DIR *mc1_root_dir = opendir("mc1:/");
if (mc1_root_dir != NULL) {
closedir(mc1_root_dir);
return 1;
Expand Down
20 changes: 11 additions & 9 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ void checkMCFolder(void)
mkdir(path, 0777);

snprintf(path, sizeof(path), "mc%d:OPL/opl.icn", mcID & 1);
fd = open(path, O_RDONLY, 0666);
fd = open(path, O_RDONLY);
if (fd < 0) {
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC);
fd = openFile(path, O_WRONLY | O_CREAT | O_TRUNC);
if (fd >= 0) {
write(fd, &icon_icn, size_icon_icn);
close(fd);
Expand All @@ -117,9 +117,9 @@ void checkMCFolder(void)
}

snprintf(path, sizeof(path), "mc%d:OPL/icon.sys", mcID & 1);
fd = open(path, O_RDONLY, 0666);
fd = open(path, O_RDONLY);
if (fd < 0) {
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC);
fd = openFile(path, O_WRONLY | O_CREAT | O_TRUNC);
if (fd >= 0) {
write(fd, &icon_sys, size_icon_sys);
close(fd);
Expand Down Expand Up @@ -151,11 +151,13 @@ static int checkFile(char *path, int mode)
if (pos) {
memcpy(dirPath, path, (pos - path));
dirPath[(pos - path)] = '\0';
int res = mkdir(dirPath, 0777);
// Non-standard POSIX check: the error value is supposed to be assigned to errno, not the return value
if (res >= 0 || res == -EEXIST) {
return 0;
}
DIR *dir = opendir(dirPath);
if (dir == NULL) {
int res = mkdir(dirPath, 0777);
if (res != 0)
return 0;
} else
closedir(dir);
}
}
}
Expand Down

0 comments on commit 24bfe5b

Please sign in to comment.