Skip to content

Commit 885da47

Browse files
Add missing close() and use EXIT_FAILURE macro (#284)
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.
1 parent 60d3915 commit 885da47

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

examples/other/cat_nonblock.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ int main(int argc, char *argv[])
2020
if (argc != 2) {
2121
printf("Usage: %s <filename>\n", argv[0]);
2222
puts("Reads the content of a file, but doesn't wait for input");
23-
exit(-1);
23+
exit(EXIT_FAILURE);
2424
}
2525

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

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

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

58+
close(fd);
5859
return 0;
5960
}

0 commit comments

Comments
 (0)