Created
September 14, 2022 18:25
-
-
Save moreati/291d17593c837e83950bde8a4f8daa03 to your computer and use it in GitHub Desktop.
Example calling scandirat() in glibc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _DEFAULT_SOURCE | |
#define _GNU_SOURCE | |
#include <dirent.h> | |
#include <err.h> | |
#include <fcntl.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
/* Compile with | |
gcc -Wall -Wextra -O2 scandirat.c -o scandirat | |
*/ | |
int main(void) | |
{ | |
struct dirent **namelist; | |
int dirfd, count; | |
dirfd = open("/", O_CLOEXEC|O_DIRECTORY|O_RDONLY); | |
if (dirfd < 0) { | |
err(EXIT_FAILURE, NULL); | |
} | |
count = scandirat(dirfd, ".", &namelist, NULL, alphasort); | |
if (count < 0) { | |
perror("scandirat"); | |
err(EXIT_FAILURE, NULL); | |
} | |
for (int i=0; i<count; i++) { | |
printf("%s\n", namelist[i]->d_name); | |
free(namelist[i]); | |
} | |
free(namelist); | |
close(dirfd); | |
exit(EXIT_SUCCESS); | |
} |
Author
moreati
commented
Sep 14, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment