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.
*/
#include <errno.h> /* for errno */
@ -16,7 +16,6 @@ int main(int argc, char *argv[])
size_t bytes; /* The number of bytes read */
char buffer[MAX_BYTES]; /* The buffer for the bytes */
/* Usage */
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
@ -29,17 +28,12 @@ int main(int argc, char *argv[])
/* If open failed */
if (fd == -1) {
if (errno = EAGAIN)
puts("Open would block");
else
puts("Open failed");
puts(errno == EAGAIN ? "Open would block" : "Open failed");
exit(-1);
}
/* Read the file and output its contents */
do {
int i;
/* Read characters from the file */
bytes = read(fd, buffer, MAX_BYTES);
@ -54,11 +48,12 @@ int main(int argc, char *argv[])
/* Print the characters */
if (bytes > 0) {
for (i = 0; i < bytes; i++)
for (int i = 0; i < bytes; i++)
putchar(buffer[i]);
}
/* While there are no errors and the file isn't over */
} while (bytes > 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}
$ sudo insmod sleep.ko
$ cat_noblock /proc/sleep
$ cat_nonblock /proc/sleep
Last input:
$ tail -f /proc/sleep &
Last input:
@ -1250,18 +1250,18 @@ Last input:
Last input:
tail: /proc/sleep: file truncated
[1] 6540
$ cat_noblock /proc/sleep
$ cat_nonblock /proc/sleep
Open would block
$ kill %1
[1]+ Terminated tail -f /proc/sleep
$ cat_noblock /proc/sleep
$ cat_nonblock /proc/sleep
Last input:
$
\end{verbatim}
\samplec{examples/sleep.c}
\samplec{examples/other/cat_noblock.c}
\samplec{examples/other/cat_nonblock.c}
\subsection{Completions}
\label{sec:orgd9f9de4}