/*
 *Problem 2-5
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

	
int main()
{	
	char *file="test_file";
	int open_ret, i;	/* Return values variables and counter */

	/* 
	 * The program asks us to open many files but 
	 * this only aims to using up all the possible number 
	 * of file descriptors which can be accomplished much 
	 * easily by just opening one file multiple times
	 */
	open_ret=open(file, O_CREAT, 00644);
	close(3);
	for(i=3;;i++)
	{
			open_ret=open(file,O_RDONLY);
			if(open_ret==-1) break;
	}

	printf("number of possible fd: %d\n", i);
	
	for(i=3;;i++)
		if(close(i)==-1) break;

	unlink(file);		
	return(0);
}	

/*
##### OUTPUT ######

adimitro@alef:/student/adimitro/cwork> ./a.out
number of possible fd: 256
adimitro@alef:/student/adimitro/cwork>
*/
