diff --git a/README.md b/README.md index ca19de6..e61ebf5 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,7 @@ Code from programming books I work through—both sample code from the books and I write myself. ## Advanced Programming in the UNIX environment + +Working through both [the book](http://www.apuebook.com) and [this online course](https://stevens.netmeister.org/631/). + + diff --git a/apitue/01/ls b/apitue/01/ls new file mode 100755 index 0000000..837c256 Binary files /dev/null and b/apitue/01/ls differ diff --git a/apitue/01/ls.c b/apitue/01/ls.c new file mode 100644 index 0000000..f2a7bf6 --- /dev/null +++ b/apitue/01/ls.c @@ -0,0 +1,27 @@ +#include + +#include +#include +#include + +int main(int argc, char **argv) { + DIR *dp; + struct dirent *dirp; + + if (argc != 2) { + fprintf(stderr, "usage: %s dir_name\n", argv[0]); + exit(1); + } + + if ((dp = opendir(argv[1])) == NULL) { + fprintf(stderr, "can't open '%s'\n", argv[1]); + exit(1); + } + + while ((dirp = readdir(dp)) != NULL) { + printf("%s\n", dirp->d_name); + } + + closedir(dp); + return(EXIT_SUCCESS); +} diff --git a/apitue/01/simple_cat b/apitue/01/simple_cat new file mode 100755 index 0000000..26d7641 Binary files /dev/null and b/apitue/01/simple_cat differ diff --git a/apitue/01/simple_cat.c b/apitue/01/simple_cat.c new file mode 100644 index 0000000..f0865ea --- /dev/null +++ b/apitue/01/simple_cat.c @@ -0,0 +1,33 @@ +// Reads from STDIN only +#include +#include +#include +#include +#include + +#ifndef BUFFSIZE +#define BUFFSIZE 32768 +#endif + +int main (int argc, char **argv) { + int n; + char buf[BUFFSIZE]; + + // Cast to void to silence compiler warnings + (void)argc; + (void)argv; + + while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) { + if (write(STDOUT_FILENO, buf, n) != n) { + fprintf(stderr, "Unable to write: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + } + + if (n < 0) { + fprintf(stderr, "Unable to read: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + + return EXIT_SUCCESS; +}