/* Richard A. DeVenezia
 * 5/21/1997
 *
 * Usage: dir [directory]
 *
 * Output: list file information to stdout --- size, mod date and filename
 *
 * Compiled: c89 dir.c -o dir
 */

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>

int  main (argc,argv)
 int argc;
 char **argv;
{
	DIR *dirp;
	struct dirent *dp;
	struct stat buf;
	struct tm result;
	const char path[1000];
	char date[30];
	int l;

	if (strcmp ("-?", argv[1]) == 0) {
		printf ("Usage: dir [directory]\n");
		printf ("directory defaults to current if not specified\n");
		return 0;
	}

	if (argc == 1)
		strcpy ((char *)path, ".");
	else
		strcpy ((char *)path, argv[1]);

	dirp = opendir (path);

	l = strlen (path);
	strcpy ((char *)path+(l++), "/");

	while ((dp = readdir(dirp)) != NULL) {
		strcpy ((char *)path+l,dp->d_name);

		if (0==stat(path,&buf)) {
			strftime (date,29,"%m/%d/%Y %T", gmtime (&buf.st_mtime));
			printf ("%12d %s %s\n",buf.st_size,date,path);
		}
		else
			fprintf (stderr, "errno=%d\n",errno);
	}

	(void) closedir(dirp);

	return 0;
}
