/******************************************************************************
* drip.c: dir /p for Linux                                                    *
* Copyright (C) 2007  Adam D. Ruppe  ( destructionator@gmail.com )            *
* Version: 1.0 (Mar 27, 2007)                                                 *
*                                                                             *
* 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
******************************************************************************/


#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<time.h>

// FIXME: race condition issues may happen here
// FIXME: int overflows and signed problems on sizes below

int main(int argc, char** argv){
	DIR* d;
	struct dirent * e;
	int num_file = 0, num_dir = 0, total_size = 0;

	struct tm * ti;
	time_t t;

	struct stat s;

	char buffer[256];

	int counter;

//	int a;

// FIXME: add command line args for help and version
//	for(a = 1; a < argc; a++){
//		
//	}

	if(argc > 1)
		if(chdir(argv[1]) == -1){
			printf("Couldn't open %s.", argv[1]);
			return 1;
		}



	getcwd(buffer, 256);

	d = opendir(".");

	if(d == NULL){
		printf("Unable to open directory %s.\n", buffer); // should never happen I think...
		return 1;
	}

	printf(" Directory of %s\n\n", buffer);

	counter = 2;
	while((e = readdir(d)) != NULL){
		// Name   <DIR> or size   Last access date last access time
		printf("%-24s",e->d_name);

		stat(e->d_name, &s);

		if(S_ISREG(s.st_mode)){
			printf("%12i", (int)s.st_size);
			num_file++;
			total_size += s.st_size;
		}
			// add number of files and total size
		else if(S_ISDIR(s.st_mode)){
			printf("%12s","<DIR>");
			num_dir++;
		}
			// add number of directories

		t = s.st_atime;
		ti = localtime(&t);

		printf("  %02i-%02i-%02i %2i:%2i%c",
			ti->tm_mon, ti->tm_mday, ti->tm_year%100,
			ti->tm_hour%12, ti->tm_min, ti->tm_hour >= 12 ? 'p' : 'a'
		);

		printf("\n");
		counter++;
		if(counter == 23){
			printf("Press any key to continue . . .");
			fgets(buffer, 10, stdin);
			counter = 0;
		}
	}

	closedir(d);

	printf("    %4i file%c %15i bytes\n", num_file, num_file == 1 ? ' ' : 's', total_size);
	printf("    %4i dir%c  \n", num_dir, num_dir == 1 ? ' ' : 's');

	return 0;
}

