Created
July 9, 2011 23:19
-
-
Save quag/1074043 to your computer and use it in GitHub Desktop.
Find the size of a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <errno.h> | |
#include <string.h> | |
#include <fts.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
int main(int argc, char** argv) | |
{ | |
char * paths[] = {argv[1], NULL}; | |
FTS *fts = fts_open(&paths[0], FTS_NOCHDIR, NULL); | |
if (fts == NULL) | |
{ | |
fprintf(stderr, "fts_open error: %s", strerror(errno)); | |
return EXIT_FAILURE; | |
} | |
long size = 0; | |
for (;;) | |
{ | |
FTSENT *ftsent = fts_read(fts); | |
if (ftsent == NULL) | |
{ | |
if (errno) | |
{ | |
fprintf(stderr, "fts_read error: %s", strerror(errno)); | |
return EXIT_FAILURE; | |
} | |
break; | |
} | |
if (ftsent->fts_info & FTS_F) | |
{ | |
size += ftsent->fts_statp->st_size; | |
} | |
} | |
if (fts_close(fts)) | |
{ | |
fprintf(stderr, "fts_close error: %s", strerror(errno)); | |
return EXIT_FAILURE; | |
} | |
printf("%ld\n", size); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment