All pastes #183797 Raw Edit

siz.c

public c v1 · immutable
#183797 ·published 2006-09-27 07:35 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/dir.h>#include <sys/stat.h>int dirsiz (char *);void help ();main (int a, char** v){	if (!--a)		help ();	++v;	int b;	struct stat tmp;	for (b=0;b<a;++b)	{		stat (v[b], &tmp);		printf ("%s is ", v[b]);		if (S_ISDIR (tmp.st_mode))			printf ("a directory, size: %d\n", dirsiz (v[b]));		else		if (S_ISREG (tmp.st_mode))			printf ("a file, size: %d\n", tmp.st_size);		else			printf ("is not a file!\n");	}	putchar ('\n');	return 0;}void help (){	printf ("Usage: siz [file/dir name]\n");	exit (0);}int dirsiz (char *fn){	DIR *di = opendir (fn);	struct dirent *tmp;	if (!di)		exit (1);	struct stat tp;	int ret=0;	char tstr[256];	while (tmp = readdir (di))	{		if (!strcmp (tmp->d_name, ".") || !strcmp (tmp->d_name, ".."))			continue;		strcpy (tstr, fn);		strcat (tstr, "/");		strcat (tstr, tmp->d_name);		stat (tstr, &tp);		if (S_ISDIR (tp.st_mode))			ret += dirsiz (tstr);		else			ret += tp.st_size;	}	return ret;}