Skip to content

Commit

Permalink
Add missing close() and use EXIT_FAILURE macro (#284)
Browse files Browse the repository at this point in the history
In cat_nonblock.c, function call of close() is missing, which could
lead to resource leak. Besides, using EXIT_FAILURE macro defined in
stdlib.h provides better readability.
  • Loading branch information
Integral-Tech authored Nov 12, 2024
1 parent 60d3915 commit 885da47
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions examples/other/cat_nonblock.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main(int argc, char *argv[])
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
puts("Reads the content of a file, but doesn't wait for input");
exit(-1);
exit(EXIT_FAILURE);
}

/* Open the file for reading in non blocking mode */
Expand All @@ -29,7 +29,7 @@ int main(int argc, char *argv[])
/* If open failed */
if (fd == -1) {
puts(errno == EAGAIN ? "Open would block" : "Open failed");
exit(-1);
exit(EXIT_FAILURE);
}

/* Read the file and output its contents */
Expand All @@ -43,7 +43,7 @@ int main(int argc, char *argv[])
puts("Normally I'd block, but you told me not to");
else
puts("Another read error");
exit(-1);
exit(EXIT_FAILURE);
}

/* Print the characters */
Expand All @@ -55,5 +55,6 @@ int main(int argc, char *argv[])
/* While there are no errors and the file isn't over */
} while (bytes > 0);

close(fd);
return 0;
}

0 comments on commit 885da47

Please sign in to comment.