fcount

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

commit b7551bb0ff8906c1b87eb5271fbd3a64e19b1397
parent 86daa4d2950565621b05135335870ada4808e1af
Author: z3bra <willy@mailoo.org>
Date:   Mon Feb 10 10:58:32 2014

Added makefile and renamed source

Diffstat:
 Makefile | 40 ++++++++++++++++++++++++++++++++++++-
 fcount.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 mcount.c | 57 +---------------------------------------------------
 3 files changed, 113 insertions(+), 57 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,40 @@ +#irc.freenode.net# BEGINNING OF THE FILE + +# Compilation settings +CC=gcc +CFLAGS+=-Wall -I inc -pedantic `cups-config --cflags` +LDFLAGS+=`cups-config --libs` + +# Command paths +RM=/bin/rm + + +PREFIX:=/usr +MANPREFIX:=${PREFIX}/share/man + + +.SUFFIXES: +.SUFFIXES: .c .h .o +.PHONY : all clean init install uninstall + +.c.o: + @echo -e "CC $<" + @${CC} -c ${CFLAGS} $< -o $@ + +fcount: mcount.o + @echo -e "LD fcount" + @${CC} $^ -o $@ ${LDFLAGS} + +all : fcount + +clean : + ${RM} -f fcount + ${RM} -f *.o + ${RM} -f *~ + +install: ${BIN} + install -D -m 0755 bin/fcount ${DESTDIR}${PREFIX}/bin/mcount + +uninstall: + ${RM} ${DESTDIR}${PREFIX}/bin/fcount +## EOF diff --git a/fcount.c b/fcount.c @@ -0,0 +1,73 @@ +/* + * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + * Version 2, December 2004 + * + * Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> + * + * Everyone is permitted to copy and distribute verbatim or modified + * copies of this license document, and changing it is allowed as long + * as the name is changed. + * + * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + * + * 0. You just DO WHAT THE FUCK YOU WANT TO. + */ +#include<stdio.h> +#include<stdlib.h> +#include<dirent.h> +#include<string.h> +#include<errno.h> + +#define DEFAULT_DIRECTORY "." + +void usage () +{ + fputs("mcount [-h] <directory>", stdout); +} + +int main(int argc, char **argv) +{ + int new = 0; + DIR* rep = NULL; + char directory[255]; + struct dirent *readFile = NULL; + + if (argc > 1) { + if ( !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { + usage(); + return 1; + } + strncpy(directory, argv[1], 255); + } else { + strncpy(directory, DEFAULT_DIRECTORY, 255); + } + + rep = opendir(directory); + if(rep != NULL) { + + while( (readFile = readdir(rep)) != NULL){ + if(strcmp(readFile->d_name, ".") && strcmp(readFile->d_name, "..")){ + new++; + } + } + + if(closedir(rep) == -1){ + exit (-1); + } + } else { + switch (errno) { + case EACCES: + fprintf(stderr,"bad permissions : %s/\n", directory); + break; + case ENOTDIR: + fprintf(stderr, "not a directory : %s.\n", directory); + break; + default: + fprintf(stderr, "erreur #%d\n",errno); + } + } + + printf("%u\n",new); + return 0; +} diff --git a/mcount.c b/mcount.c @@ -1,57 +0,0 @@ -/* - * 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; -} -