aboutsummaryrefslogblamecommitdiff
path: root/ls.c
blob: 251d5fbce6fa1772e2867298e42fd453e21ca2bb (plain) (tree)
1
2
3
4
5
6
7
8
9
                   
                  



                      
                               
 
                  
             






                          
 



                                           

                                             
                                                
     



                           



                       
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.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");
        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);
}