Skip to content

Instantly share code, notes, and snippets.

@theasder
Last active December 25, 2015 00:39
Show Gist options
  • Save theasder/6889384 to your computer and use it in GitHub Desktop.
Save theasder/6889384 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
DIR *catalog = opendir(argv[1]);
struct dirent* fp;
struct stat *buf;
long long int sum = 0;
char name[PATH_MAX] = { 0 };
while(1) {
fp = readdir(catalog);
if(fp == NULL) break;
if(argv[1][strlen(argv[1]) - 1] != '/')
snprintf(name, PATH_MAX, "%s/%s", argv[1], fp -> d_name);
else
snprintf(name, PATH_MAX, "%s%s", argv[1], fp -> d_name);
if(lstat(name, buf) != -1)
if(S_ISREG(buf -> st_mode) && !S_ISLNK(buf -> st_mode))
sum += buf -> st_size;
}
closedir(catalog);
printf("%lld\n", sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment