Skip to content

Instantly share code, notes, and snippets.

@dinvlad
Forked from mackstann/gist:4229933
Last active July 28, 2019 07:40
Show Gist options
  • Save dinvlad/83c078481649a35e16d9112d2743dab9 to your computer and use it in GitHub Desktop.
Save dinvlad/83c078481649a35e16d9112d2743dab9 to your computer and use it in GitHub Desktop.

Revisions

  1. dinvlad revised this gist Jul 28, 2019. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions gistfile1.c
    Original file line number Diff line number Diff line change
    @@ -29,15 +29,13 @@ void listdir(const char * dirname, size_t * ntotal)
    char * buf = malloc(BUF_SIZE);

    fd = open(dirname, O_RDONLY | O_DIRECTORY);
    if (fd == -1) {
    handle_error("open");
    return;
    }
    if (fd == -1)
    return handle_error(dirname);

    for ( ; ; ) {
    nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
    if (nread == -1)
    handle_error("getdents");
    return handle_error(dirname);

    if (nread == 0)
    break;
  2. dinvlad revised this gist Jul 28, 2019. No changes.
  3. dinvlad revised this gist Jul 28, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.c
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ struct linux_dirent {
    char d_name[];
    };

    #define BUF_SIZE (1024 * 1024 * 5)
    #define BUF_SIZE (1024 * 1024 * 128)

    void listdir(const char * dirname, size_t * ntotal)
    {
  4. dinvlad revised this gist Jul 28, 2019. 1 changed file with 10 additions and 14 deletions.
    24 changes: 10 additions & 14 deletions gistfile1.c
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,7 @@
    #include <sys/stat.h>
    #include <sys/syscall.h>

    #define handle_error(msg) \
    do { perror(msg); exit(EXIT_FAILURE); } while (0)
    #define handle_error(msg) perror(msg)

    struct linux_dirent {
    long d_ino;
    @@ -21,7 +20,7 @@ struct linux_dirent {

    #define BUF_SIZE (1024 * 1024 * 5)

    void listdir(const char * dirname)
    void listdir(const char * dirname, size_t * ntotal)
    {
    int fd, nread;
    struct linux_dirent *d;
    @@ -30,8 +29,10 @@ void listdir(const char * dirname)
    char * buf = malloc(BUF_SIZE);

    fd = open(dirname, O_RDONLY | O_DIRECTORY);
    if (fd == -1)
    if (fd == -1) {
    handle_error("open");
    return;
    }

    for ( ; ; ) {
    nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
    @@ -47,22 +48,15 @@ void listdir(const char * dirname)
    bpos += d->d_reclen;
    if(d->d_ino && strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
    {
    printf("%-10s ", (d_type == DT_REG) ? "file" :
    (d_type == DT_DIR) ? "directory" :
    (d_type == DT_FIFO) ? "FIFO" :
    (d_type == DT_SOCK) ? "socket" :
    (d_type == DT_LNK) ? "symlink" :
    (d_type == DT_BLK) ? "block dev" :
    (d_type == DT_CHR) ? "char dev" : "???");
    printf("%s/%s\n", dirname, d->d_name);
    (*ntotal)++;
    if(d_type == DT_DIR)
    {
    int dirname_len = strlen(dirname);
    char * subdir = calloc(1, PATH_MAX + 1);
    strcat(subdir, dirname);
    strcat(subdir + dirname_len, "/");
    strcat(subdir + dirname_len + 1, d->d_name);
    listdir(subdir);
    listdir(subdir, ntotal);
    free(subdir);
    }
    }
    @@ -76,6 +70,8 @@ void listdir(const char * dirname)
    int
    main(int argc, char *argv[])
    {
    listdir(argc > 1 ? argv[1] : ".");
    size_t ntotal = 0;
    listdir(argc > 1 ? argv[1] : ".", &ntotal);
    printf("%d\n", ntotal);
    exit(EXIT_SUCCESS);
    }
  5. @mackstann mackstann revised this gist Dec 7, 2012. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions gistfile1.c
    Original file line number Diff line number Diff line change
    @@ -57,10 +57,11 @@ void listdir(const char * dirname)
    printf("%s/%s\n", dirname, d->d_name);
    if(d_type == DT_DIR)
    {
    int dirname_len = strlen(dirname);
    char * subdir = calloc(1, PATH_MAX + 1);
    strcat(subdir, dirname);
    strcat(subdir + strlen(dirname), "/");
    strcat(subdir + strlen(dirname) + 1, d->d_name);
    strcat(subdir + dirname_len, "/");
    strcat(subdir + dirname_len + 1, d->d_name);
    listdir(subdir);
    free(subdir);
    }
  6. @mackstann mackstann revised this gist Dec 7, 2012. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions gistfile1.c
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // http://www.olark.com/spw/2011/08/you-can-list-a-directory-with-8-million-files-but-not-with-ls/
    #define _GNU_SOURCE
    #include <dirent.h> /* Defines DT_* constants */
    #include <fcntl.h>
    @@ -40,14 +41,11 @@ void listdir(const char * dirname)
    if (nread == 0)
    break;

    //printf("--------------- nread=%d ---------------\n", nread);
    //printf("i-node# file type d_reclen d_off d_name\n");
    for (bpos = 0; bpos < nread;) {
    d = (struct linux_dirent *) (buf + bpos);
    //printf("%8ld ", d->d_ino);
    d_type = *(buf + bpos + d->d_reclen - 1);
    bpos += d->d_reclen;
    if(strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
    if(d->d_ino && strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
    {
    printf("%-10s ", (d_type == DT_REG) ? "file" :
    (d_type == DT_DIR) ? "directory" :
    @@ -79,4 +77,4 @@ main(int argc, char *argv[])
    {
    listdir(argc > 1 ? argv[1] : ".");
    exit(EXIT_SUCCESS);
    }
    }
  7. @invalid-email-address Anonymous revised this gist Dec 7, 2012. 1 changed file with 18 additions and 15 deletions.
    33 changes: 18 additions & 15 deletions gistfile1.c
    Original file line number Diff line number Diff line change
    @@ -46,23 +46,26 @@ void listdir(const char * dirname)
    d = (struct linux_dirent *) (buf + bpos);
    //printf("%8ld ", d->d_ino);
    d_type = *(buf + bpos + d->d_reclen - 1);
    printf("%-10s ", (d_type == DT_REG) ? "file" :
    (d_type == DT_DIR) ? "directory" :
    (d_type == DT_FIFO) ? "FIFO" :
    (d_type == DT_SOCK) ? "socket" :
    (d_type == DT_LNK) ? "symlink" :
    (d_type == DT_BLK) ? "block dev" :
    (d_type == DT_CHR) ? "char dev" : "???");
    printf("%s/%s\n", dirname, d->d_name);
    bpos += d->d_reclen;
    if(d_type == DT_DIR && strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
    if(strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
    {
    char * subdir = calloc(1, PATH_MAX + 1);
    strcat(subdir, dirname);
    strcat(subdir + strlen(dirname), "/");
    strcat(subdir + strlen(dirname) + 1, d->d_name);
    listdir(subdir);
    free(subdir);
    printf("%-10s ", (d_type == DT_REG) ? "file" :
    (d_type == DT_DIR) ? "directory" :
    (d_type == DT_FIFO) ? "FIFO" :
    (d_type == DT_SOCK) ? "socket" :
    (d_type == DT_LNK) ? "symlink" :
    (d_type == DT_BLK) ? "block dev" :
    (d_type == DT_CHR) ? "char dev" : "???");
    printf("%s/%s\n", dirname, d->d_name);
    if(d_type == DT_DIR)
    {
    char * subdir = calloc(1, PATH_MAX + 1);
    strcat(subdir, dirname);
    strcat(subdir + strlen(dirname), "/");
    strcat(subdir + strlen(dirname) + 1, d->d_name);
    listdir(subdir);
    free(subdir);
    }
    }
    }
    }
  8. @invalid-email-address Anonymous created this gist Dec 7, 2012.
    79 changes: 79 additions & 0 deletions gistfile1.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    #define _GNU_SOURCE
    #include <dirent.h> /* Defines DT_* constants */
    #include <fcntl.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <sys/syscall.h>

    #define handle_error(msg) \
    do { perror(msg); exit(EXIT_FAILURE); } while (0)

    struct linux_dirent {
    long d_ino;
    off_t d_off;
    unsigned short d_reclen;
    char d_name[];
    };

    #define BUF_SIZE (1024 * 1024 * 5)

    void listdir(const char * dirname)
    {
    int fd, nread;
    struct linux_dirent *d;
    int bpos;
    char d_type;
    char * buf = malloc(BUF_SIZE);

    fd = open(dirname, O_RDONLY | O_DIRECTORY);
    if (fd == -1)
    handle_error("open");

    for ( ; ; ) {
    nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
    if (nread == -1)
    handle_error("getdents");

    if (nread == 0)
    break;

    //printf("--------------- nread=%d ---------------\n", nread);
    //printf("i-node# file type d_reclen d_off d_name\n");
    for (bpos = 0; bpos < nread;) {
    d = (struct linux_dirent *) (buf + bpos);
    //printf("%8ld ", d->d_ino);
    d_type = *(buf + bpos + d->d_reclen - 1);
    printf("%-10s ", (d_type == DT_REG) ? "file" :
    (d_type == DT_DIR) ? "directory" :
    (d_type == DT_FIFO) ? "FIFO" :
    (d_type == DT_SOCK) ? "socket" :
    (d_type == DT_LNK) ? "symlink" :
    (d_type == DT_BLK) ? "block dev" :
    (d_type == DT_CHR) ? "char dev" : "???");
    printf("%s/%s\n", dirname, d->d_name);
    bpos += d->d_reclen;
    if(d_type == DT_DIR && strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
    {
    char * subdir = calloc(1, PATH_MAX + 1);
    strcat(subdir, dirname);
    strcat(subdir + strlen(dirname), "/");
    strcat(subdir + strlen(dirname) + 1, d->d_name);
    listdir(subdir);
    free(subdir);
    }
    }
    }

    close(fd);
    free(buf);
    }

    int
    main(int argc, char *argv[])
    {
    listdir(argc > 1 ? argv[1] : ".");
    exit(EXIT_SUCCESS);
    }