/* A simple program to illustrate the fts(3) functionality */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fts.h>

int
main(int argc, char **argv)
{
  FTS *fstree;
  FTSENT *filelist, *cur, *p;

  if (argc < 2) {
    fprintf(stderr, "Usage: %s [PATH(s)]\n", argv[0]);
    exit(1);
  }

  argv += 1;                    /* remove the first parameter */
  fstree = fts_open(argv, FTS_PHYSICAL, NULL);
  while((p = fts_read(fstree)) != NULL) {
    switch(p->fts_info) {
    case FTS_DP:
      printf("%s/%s\n", p->fts_path,p->fts_name);
      break;
    }
    /* Print the children. */
    filelist = fts_children(fstree, FTS_NAMEONLY);
    for (cur = filelist; cur; cur = cur->fts_link) {
      printf("%s\n", cur->fts_name);
    }
  }
  fts_close(fstree);
  return(0);
}
