#include <ftw.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#define UNUSED_PARAM(x)	(void)x

static int nftw_cb(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf)
{
	UNUSED_PARAM(fpath);
	UNUSED_PARAM(sb);
	UNUSED_PARAM(tflag);
	UNUSED_PARAM(ftwbuf);
	return 1;
}

int main(void)
{
	size_t cnt = 0;
	while(cnt<1024*1024){
//		nftw(".", nftw_cb, 256, FTW_DEPTH | FTW_PHYS);
		nftw(".", nftw_cb, 256, FTW_PHYS);
		cnt++;
	}
	const char * path = ".";
	int fd = open(path, O_RDONLY);
	if(fd==-1){
		fprintf(stderr, "open(\"%s\", O_RDONLY); error %d %s\n", path, errno, strerror(errno));
		if(errno==EMFILE){
			fprintf(stderr, "Yup, we exhausted the file descriptor stock!\n");
		}
		return EXIT_FAILURE;
	}else{
		fprintf(stdout, "File open succeeded fd=%d\n", fd);
		if(fd!=3){
			fprintf(stderr, "fd was supposed to be 3! What happened?\n");
		}
	}
	close(fd);
	return EXIT_SUCCESS;
}
