Skip to content

Instantly share code, notes, and snippets.

@mrkn
Created October 28, 2008 08:01
Show Gist options
  • Save mrkn/20322 to your computer and use it in GitHub Desktop.
Save mrkn/20322 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/dir.h>
int
main(int argc, char** argv)
{
if (argc < 2) {
fprintf(stderr, "%s DIRNAME\n", argv[0]);
exit(EXIT_FAILURE);
}
DIR* dir = opendir(argv[1]);
if (dir == NULL) {
perror("opendir");
exit(EXIT_FAILURE);
}
if (chdir(argv[1]) < 0) {
perror("chdir");
exit(EXIT_FAILURE);
}
struct dirent* dp = NULL;
while ((dp = readdir(dir)) != NULL) {
if ((dp->d_namlen == 1 && strncmp(".", dp->d_name, 1) == 0) ||
(dp->d_namlen == 2 && strncmp("..", dp->d_name, 2) == 0))
continue;
printf("%d %s\n", dp->d_fileno, dp->d_name);
if (unlink(dp->d_name) < 0) {
perror("unlink");
closedir(dir);
exit(EXIT_FAILURE);
}
}
closedir(dir);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment