#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>

int filter(const struct dirent *ent)
{
  if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) return 0;
  return 1;
}


int main(int argc, char *argv[])
{
  char path[PATH_MAX];
  struct dirent **namelist;
  int n;
  char *dir = ".";

  if (argc >= 2) dir = argv[1];

  n = scandir(dir, &namelist, filter, alphasort);
  if (n == -1) {
      perror("scandir");
      return 1;
  }

  for(int i = 0; i < n; i++) {
    printf("%s\n", namelist[i]->d_name);
    sprintf(path, "%s/%s", dir, namelist[i]->d_name);
    // Now you can use 'path' to stat the file at it's real location.
    free(namelist[i]);
  }
  free(namelist);

  return 0;
}
