#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#include <sys/types.h> 
#include <sys/stat.h> 
#include <dirent.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>

/* als: list files of specific type, size or by permission in the specified
   directory. Print type, size, owner, group */
/* Usage:
	als [-trdl] [-s{+|-}size] [-p{+|-}rwx] directory [directory...]
*/

typedef enum {SIZE_NONE, SIZE_GREATER, SIZE_LESS} size_comp;  
typedef enum {PERM_NONE, PERM_HAS, PERM_HASNOT} perm_comp;  

int main (int argc, char *argv[]) {
int show_regs = 0, show_dirs = 0, show_links = 0;
size_comp size_operator = SIZE_NONE; 
off_t size_limit = 0;
perm_comp perm_operator = PERM_NONE;
int need_read = 0, need_write = 0, need_exec = 0;
int argcount, i;
char *opt_ptr;
char *desc_str;
char buff[PATH_MAX + 1];

struct stat stat_info;
DIR *theDir;
struct dirent *dentry;
struct passwd *userinfo;
struct group *groupinfo;


for (argcount = 1; argcount < argc; argcount++) {
	if (argv[argcount][0] != '-')
		break; /* end of options */
	switch (argv[argcount][1]) {
		case 't':
			for (opt_ptr = argv[argcount] + 2; *opt_ptr; opt_ptr++) {
				switch (*opt_ptr) {
					case 'r':
						show_regs = 1;
						break;
					case 'd':
						show_dirs = 1;
						break;
					case 'l':
						show_links = 1;
						break;
					default:
						fprintf(stderr, "Unknown filetype %c\n", *opt_ptr);
						exit(1);
				}
			}
			break;
		case 's':
			opt_ptr = argv[argcount] + 2;
			if (*opt_ptr == '+')
				size_operator = SIZE_GREATER;
			else if (*opt_ptr == '-')
				size_operator = SIZE_LESS;
			else {
				fprintf(stderr, "size option: no + or minus specified\n");
				exit(1);
			}
			size_limit = atoi(opt_ptr + 1);
			break;
		case 'p':
			opt_ptr = argv[argcount] + 2;
			if (*opt_ptr == '+')
				perm_operator = PERM_HAS;
			else if (*opt_ptr == '-')
				perm_operator = PERM_HASNOT;
			else {
				fprintf(stderr, "perm option: no + or minus specified\n");
				exit(1);
			}
			for (opt_ptr++; *opt_ptr; opt_ptr++) {
				switch (*opt_ptr) {
					case 'r':
						need_read = 1;
						break;
					case 'w':
						need_write = 1;
						break;
					case 'x':
						need_exec = 1;
						break;
					default:
						fprintf(stderr, "Unknown perm %c\n", *opt_ptr);
						exit(1);
				}
			}
			break;
		case '\0':
			break;
		default:
			fprintf(stderr, "Unknown option %c\n", argv[argcount][1]);
			exit(1);
	}
}

for (; argcount < argc; argcount++) {
	theDir = opendir(argv[argcount]);
	if (theDir == NULL) {
		fprintf(stderr, "als: %s: %s\n", argv[argcount], strerror(errno));
		continue;
	}
	while ((dentry = readdir(theDir)) != NULL) {


		/* EDW O KALOS KWDIKAS */

		userinfo = getpwuid(stat_info.st_uid);
		groupinfo = getgrgid(stat_info.st_gid);
		printf("%s\t%s\t%ld\t%s\t%s\n", buff, desc_str,
			stat_info.st_size,
			((userinfo != NULL) ? (userinfo->pw_name) : "UNKNOWN"),
			((groupinfo != NULL) ? (groupinfo->gr_name) : "UNKNOWN"));

	}
		
	
}


return 0;
}
