Skip to content

Instantly share code, notes, and snippets.

@boryspoplawski
Created August 23, 2019 09:23
Show Gist options
  • Save boryspoplawski/72d43b53551ef64ef5ddddf17d65ff4c to your computer and use it in GitHub Desktop.
Save boryspoplawski/72d43b53551ef64ef5ddddf17d65ff4c to your computer and use it in GitHub Desktop.
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void dirread(const char *dname) {
DIR *dp;
struct dirent *de;
dp = opendir(dname);
while (1) {
errno = 0;
de = readdir(dp);
if (!de) {
if (errno) {
printf("ERROR: readdir: %s\n", strerror(errno));
_exit(1);
} else {
break;
}
}
printf("GOT: %s\n", de->d_name);
}
closedir(dp);
}
int main(int argc, char *argv[]) {
int p = fork();
if (p < 0) {
puts("FORK");
return 1;
} else if (p == 0) {
dirread("/proc");
} else {
wait(&p);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment