From: Jim Huang Date: Thu, 5 Aug 2021 06:28:12 +0000 (+0800) Subject: cat_nonblock: Use canonical name scheme and fix unintended assignment X-Git-Tag: latest~202 X-Git-Url: https://www.ivnss.com/gitweb/?a=commitdiff_plain;h=466e8a00fd604d1cd03b71f8e7d4fb04a0c6c6a5;p=lkmpg cat_nonblock: Use canonical name scheme and fix unintended assignment --- diff --git a/examples/other/cat_noblock.c b/examples/other/cat_noblock.c deleted file mode 100644 index 42117cd..0000000 --- a/examples/other/cat_noblock.c +++ /dev/null @@ -1,64 +0,0 @@ -/* - * cat_noblock.c - open a file and display its contents, but exit rather than - * wait for input. - */ -#include /* for errno */ -#include /* for open */ -#include /* standard I/O */ -#include /* for exit */ -#include /* for read */ - -#define MAX_BYTES 1024 * 4 - -int main(int argc, char *argv[]) -{ - int fd; /* The file descriptor for the file to read */ - size_t bytes; /* The number of bytes read */ - char buffer[MAX_BYTES]; /* The buffer for the bytes */ - - - /* Usage */ - if (argc != 2) { - printf("Usage: %s \n", argv[0]); - puts("Reads the content of a file, but doesn't wait for input"); - exit(-1); - } - - /* Open the file for reading in non blocking mode */ - fd = open(argv[1], O_RDONLY | O_NONBLOCK); - - /* If open failed */ - if (fd == -1) { - if (errno = EAGAIN) - puts("Open would block"); - else - puts("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); - - /* If there's an error, report it and die */ - if (bytes == -1) { - if (errno = EAGAIN) - puts("Normally I'd block, but you told me not to"); - else - puts("Another read error"); - exit(-1); - } - - /* Print the characters */ - if (bytes > 0) { - for (i = 0; i < bytes; i++) - putchar(buffer[i]); - } - - /* While there are no errors and the file isn't over */ - } while (bytes > 0); - return 0; -} diff --git a/examples/other/cat_nonblock.c b/examples/other/cat_nonblock.c new file mode 100644 index 0000000..50f3b67 --- /dev/null +++ b/examples/other/cat_nonblock.c @@ -0,0 +1,59 @@ +/* + * cat_nonblock.c - open a file and display its contents, but exit rather than + * wait for input. + */ +#include /* for errno */ +#include /* for open */ +#include /* standard I/O */ +#include /* for exit */ +#include /* for read */ + +#define MAX_BYTES 1024 * 4 + +int main(int argc, char *argv[]) +{ + int fd; /* The file descriptor for the file to read */ + size_t bytes; /* The number of bytes read */ + char buffer[MAX_BYTES]; /* The buffer for the bytes */ + + /* Usage */ + if (argc != 2) { + printf("Usage: %s \n", argv[0]); + puts("Reads the content of a file, but doesn't wait for input"); + exit(-1); + } + + /* Open the file for reading in non blocking mode */ + fd = open(argv[1], O_RDONLY | O_NONBLOCK); + + /* If open failed */ + if (fd == -1) { + puts(errno == EAGAIN ? "Open would block" : "Open failed"); + exit(-1); + } + + /* Read the file and output its contents */ + do { + /* Read characters from the file */ + bytes = read(fd, buffer, MAX_BYTES); + + /* If there's an error, report it and die */ + if (bytes == -1) { + if (errno = EAGAIN) + puts("Normally I'd block, but you told me not to"); + else + puts("Another read error"); + exit(-1); + } + + /* Print the characters */ + if (bytes > 0) { + 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; +} diff --git a/lkmpg.tex b/lkmpg.tex index e5afe44..0bd97fa 100644 --- a/lkmpg.tex +++ b/lkmpg.tex @@ -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}