/* The original version of this program can be found at http://damb.dk */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* #include <dirent.h> */
#ifdef __GNUC__
/* GCC compilers tends to have getcwd in sys/unistd.h */
#include <sys/unistd.h>
#else
/* #include <dir.h> */
#endif
#include <sys/stat.h>

void scan(const char *dir, const char *ext)
{
  unsigned int ext_len = ext ? strlen(ext) : 0;
  DIR *d = opendir(dir);
  struct stat stat_struct;
  char full_name[256];

  if(d)
  {
    struct dirent *dirent;
    while((dirent = readdir(d)) != NULL)
    {
      if(strcmp(dirent->d_name, ".") && strcmp(dirent->d_name, ".."))
      {
        sprintf(full_name, "%s/%s", dir, dirent->d_name);
        if(!stat(full_name, &stat_struct))
        {
          if(stat_struct.st_mode & S_IFDIR)
          { /* It's a directory entry */
            scan(full_name, ext);
          }
          else
          { /* It's probably a file */
            if(ext == NULL ||
               (strlen(dirent->d_name) >= ext_len && !strcmp(&dirent->d_name[strlen(dirent->d_name) - ext_len], ext)))
            {
              printf("%sn", full_name);
            }
          }
        }
      }
    }
    closedir(d);
  }
}

/* Arguments are assumed by extensions to list, eg. .c .html */
/* Wildcards are not supported                               */
/* If no arguments are given we will list everything         */
int main(int argc, char *argv[])
{
  char dir[256];
  int i;

  getcwd(dir, sizeof(dir));

  if(argc == 1)
  {
    scan(dir, NULL);
  }
  else
  {
    for(i = 1; i < argc; i++)
      scan(dir, argv[i]);
  }

  return EXIT_SUCCESS;
}