cat_nonblock: Use canonical name scheme and fix unintended assignment

This commit is contained in:
Jim Huang 2021-08-05 14:28:12 +08:00
parent 38586974ee
commit 466e8a00fd
2 changed files with 8 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/* /*
* cat_noblock.c - open a file and display its contents, but exit rather than * cat_nonblock.c - open a file and display its contents, but exit rather than
* wait for input. * wait for input.
*/ */
#include <errno.h> /* for errno */ #include <errno.h> /* for errno */
@ -16,7 +16,6 @@ int main(int argc, char *argv[])
size_t bytes; /* The number of bytes read */ size_t bytes; /* The number of bytes read */
char buffer[MAX_BYTES]; /* The buffer for the bytes */ char buffer[MAX_BYTES]; /* The buffer for the bytes */
/* Usage */ /* Usage */
if (argc != 2) { if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]); printf("Usage: %s <filename>\n", argv[0]);
@ -29,17 +28,12 @@ int main(int argc, char *argv[])
/* If open failed */ /* If open failed */
if (fd == -1) { if (fd == -1) {
if (errno = EAGAIN) puts(errno == EAGAIN ? "Open would block" : "Open failed");
puts("Open would block");
else
puts("Open failed");
exit(-1); exit(-1);
} }
/* Read the file and output its contents */ /* Read the file and output its contents */
do { do {
int i;
/* Read characters from the file */ /* Read characters from the file */
bytes = read(fd, buffer, MAX_BYTES); bytes = read(fd, buffer, MAX_BYTES);
@ -54,11 +48,12 @@ int main(int argc, char *argv[])
/* Print the characters */ /* Print the characters */
if (bytes > 0) { if (bytes > 0) {
for (i = 0; i < bytes; i++) for (int i = 0; i < bytes; i++)
putchar(buffer[i]); putchar(buffer[i]);
} }
/* While there are no errors and the file isn't over */ /* While there are no errors and the file isn't over */
} while (bytes > 0); } while (bytes > 0);
return 0; return 0;
} }

View File

@ -1238,7 +1238,7 @@ There is one more point to remember. Some times processes don't want to sleep, t
\begin{verbatim} \begin{verbatim}
$ sudo insmod sleep.ko $ sudo insmod sleep.ko
$ cat_noblock /proc/sleep $ cat_nonblock /proc/sleep
Last input: Last input:
$ tail -f /proc/sleep & $ tail -f /proc/sleep &
Last input: Last input:
@ -1250,18 +1250,18 @@ Last input:
Last input: Last input:
tail: /proc/sleep: file truncated tail: /proc/sleep: file truncated
[1] 6540 [1] 6540
$ cat_noblock /proc/sleep $ cat_nonblock /proc/sleep
Open would block Open would block
$ kill %1 $ kill %1
[1]+ Terminated tail -f /proc/sleep [1]+ Terminated tail -f /proc/sleep
$ cat_noblock /proc/sleep $ cat_nonblock /proc/sleep
Last input: Last input:
$ $
\end{verbatim} \end{verbatim}
\samplec{examples/sleep.c} \samplec{examples/sleep.c}
\samplec{examples/other/cat_noblock.c} \samplec{examples/other/cat_nonblock.c}
\subsection{Completions} \subsection{Completions}
\label{sec:orgd9f9de4} \label{sec:orgd9f9de4}