diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-03-16 23:02:57 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-03-16 23:03:43 +1300 |
commit | 6522895fdf6d741a64f75e3ee7fe24b6f354eaa7 (patch) | |
tree | d84689fa25547505a22c81703dbf5309d12849f0 | |
parent | ls(1) accepts one argument (diff) | |
download | tunics-6522895fdf6d741a64f75e3ee7fe24b6f354eaa7.tar.gz tunics-6522895fdf6d741a64f75e3ee7fe24b6f354eaa7.zip |
Some basic ls(1) error checking
-rw-r--r-- | ls.c | 10 |
1 files changed, 9 insertions, 1 deletions
@@ -1,4 +1,5 @@ #include <dirent.h> +#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> @@ -15,11 +16,18 @@ int main(int argc, char **argv) dirname = "."; } - dir = opendir(dirname); + if ((dir = opendir(dirname)) == NULL) { + perror("opendir"); + exit(EXIT_FAILURE); + } while ((dirent = readdir(dir)) != NULL) { fprintf(stdout, "%s\n", dirent->d_name); } + if (errno) { + perror("readdir"); + exit(EXIT_FAILURE); + } exit(EXIT_SUCCESS); } |