/***************************************************************************
 *   Copyright (C) 2006 by Brian Lauber   *
 *   bml8@case.edu   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/



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

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <dirent.h>

int main(int argc, char *argv[])
{
  struct stat FileInfo;
  DIR       * Directory;
  struct dirent    * DirInfo;

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

  if(stat(argv[1], &FileInfo) == -1)
  {
    fprintf(stderr, "File not found, or general mayhem\n\n");
    exit(EXIT_FAILURE);
  }

  if(!S_ISDIR(FileInfo.st_mode))
  {
    fprintf(stderr, "Not a directory\n\n");
    exit(EXIT_FAILURE);
  }

  printf("Directory contents:\n");
  Directory = opendir(argv[1]);
  while( (DirInfo = readdir(Directory)) != NULL)
  {
    printf("%s\n", DirInfo->d_name);
  }
  closedir(Directory);

  return EXIT_SUCCESS;
}
