From: Francois Fleuret Date: Tue, 23 Feb 2010 18:21:28 +0000 (+0100) Subject: Initial commit. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=d04ae65cca82b350617e81778e4e6d3b9f221b0f;p=dus.git Initial commit. --- d04ae65cca82b350617e81778e4e6d3b9f221b0f diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0642924 --- /dev/null +++ b/Makefile @@ -0,0 +1,54 @@ + +# +# selector is a simple command line utility for selection of strings +# with a dynamic pattern-matching. +# +# Copyright (c) 2009 Francois Fleuret +# Written by Francois Fleuret +# +# This file is part of selector. +# +# selector is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3 as +# published by the Free Software Foundation. +# +# selector is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with selector. If not, see . +# + +BINARY_PATH = /usr/bin +MAN_PATH = /usr/share/man/man1 + +LDFLAGS= + +REVISION_NUMBER=\"$(shell cat REVISION_NUMBER)\" + +ifeq ($(DEBUG),yes) + OPTIMIZE_FLAG = -ggdb3 -DDEBUG -fno-omit-frame-pointer +else + OPTIMIZE_FLAG = -O2 +endif + +CFLAGS = -Wall -ansi -pedantic -DREVISION_NUMBER=$(REVISION_NUMBER) $(OPTIMIZE_FLAG) + +all: dus + +dus: dus.o + $(CC) -o $@ $^ $(LDFLAGS) + +install: dus + install -m 755 dus $(BINARY_PATH) + # install -m 644 dus.1 $(MAN_PATH) + # gzip $(MAN_PATH)/dus.1 + +uninstall: + rm $(BINARY_PATH)/dus + # rm $(MAN_PATH)/dus.1.gz + +clean: + rm -f *.o dus diff --git a/REVISION_NUMBER b/REVISION_NUMBER new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/REVISION_NUMBER @@ -0,0 +1 @@ +1 diff --git a/dus.c b/dus.c new file mode 100644 index 0000000..4ad1fff --- /dev/null +++ b/dus.c @@ -0,0 +1,104 @@ + +/*************************************************************************/ +/* START_IP_HEADER */ +/* */ +/* This program is free software: you can redistribute it and/or modify */ +/* it under the terms of the version 3 of the GNU General Public License */ +/* as published by the Free Software Foundation. */ +/* */ +/* This program is distributed in the hope that it will be useful, but */ +/* WITHOUT ANY WARRANTY; without even the implied warranty of */ +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ +/* General Public License for more details. */ +/* */ +/* You should have received a copy of the GNU General Public License */ +/* along with this program. If not, see . */ +/* */ +/* Written by and Copyright (C) Francois Fleuret */ +/* Contact for comments & bug reports */ +/* */ +/* END_IP_HEADER */ +/*************************************************************************/ + +#define _BSD_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include + +struct file_with_size { + char *filename; + size_t size; + struct file_with_size *next; +}; + +size_t file_or_dir_size(char *name) { + DIR *dir; + struct dirent *dir_e; + struct stat dummy; + size_t result; + char subname[1024]; + + result = 0; + + if(lstat(name, &dummy) != 0) { + printf("Can not stat %s (%s).\n", name, strerror(errno)); + exit (1); + } + + if(S_ISLNK(dummy.st_mode)) { + return 0; + } + + dir = opendir(name); + + if(dir) { + while((dir_e = readdir(dir))) { + if(strcmp(dir_e->d_name, ".") && + strcmp(dir_e->d_name, "..")) { + sprintf(subname, "%s/%s", name, dir_e->d_name); + result += file_or_dir_size(subname); + } + } + closedir(dir); + } else { + if(S_ISREG(dummy.st_mode)) { + result += dummy.st_size; + } + } + + return result; +} + +struct file_with_size *create(char *name, struct file_with_size *current) { + struct file_with_size *result; + result = malloc(sizeof(struct file_with_size)); + result->filename = strdup(name); + result->size = file_or_dir_size(name); + result->next = current; + return result; +} + +int main(int argc, char **argv) { + int k; + struct file_with_size *root; + + root = 0; + for(k = 1; k < argc; k++) { + root = create(argv[k], root); + } + + while(root) { + printf("%u %s\n", + root->size, + root->filename); + root = root->next; + } + + exit(0); +}