fcount

count files in a directory
git clone git://z3bra.org/fcount
Log | Files | Refs | README | LICENSE

commit 86daa4d2950565621b05135335870ada4808e1af
Author: Willy Goiffon <w.goiffon@gmail.com>
Date:   Mon Nov  4 08:52:16 2013

mcount: count files within a directory

Diffstat:
 mcount.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+), 0 deletions(-)

diff --git a/mcount.c b/mcount.c @@ -0,0 +1,57 @@ +/* + * mntotify.c -- Wily Goiffon 2013 <willy at mailoo dot org> + * + * Opens a directory and output the number of file found in this directory. + * Usefull to watch the number of mails in a folder and output it to a status + * bar. + * + */ +#include<stdio.h> +#include<stdlib.h> +#include<dirent.h> +#include<string.h> +#include<errno.h> + +#define INBOX_DIRECTORY "/home/z3bra/var/mail/INBOX/new" + +int +main(int argc, char **argv) +{ + int count = 0; + DIR* rep = NULL; + struct dirent *readFile = NULL; + + if (argc > 1) { + rep = opendir(argv[1]); + } else { + rep = opendir(INBOX_DIRECTORY); + } + + if(rep != NULL) { + + while( (readFile = readdir(rep)) != NULL){ + if(strcmp(readFile->d_name, ".") && strcmp(readFile->d_name, "..")){ + count++; + } + } + + if(closedir(rep) == -1){ + exit (-1); + } + } else { + switch (errno) { + case EACCES: + fprintf(stderr,"bad permissions : %s/\n", INBOX_DIRECTORY); + break; + case ENOTDIR: + fprintf(stderr, "not a directory : %s.\n", INBOX_DIRECTORY); + break; + default: + fprintf(stderr, "erreur #%d\n",errno); + } + } + + printf("%u\n",count); + return 0; +} +