aboutsummaryrefslogtreecommitdiff
path: root/ls.c
blob: 355da0243c4a2fdc210afce473bba5fc1826f79c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "ls.h"

int main(int argc, char **argv)
{
    char *dirname;
    DIR *dir;
    struct dirent *dirent;

    if (argc > 1) {
        dirname = argv[1];
    } else {
        dirname = ".";
    }

    if ((dir = opendir(dirname)) == NULL) {
        perror("opendir");
        free(dir);
        exit(EXIT_FAILURE);
    }

    while ((dirent = readdir(dir)) != NULL) {
        fprintf(stdout, "%s\n", dirent->d_name);
    }
    if (errno) {
        perror("readdir");
        free(dir);
        free(dirent);
        exit(EXIT_FAILURE);
    }

    free(dir);
    free(dirent);
    exit(EXIT_SUCCESS);
}