Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _XOPEN_SOURCE 700
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdint.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <string.h>
- #define osAssert(state, msg) osErrorFatal(state, msg, __LINE__, __FILE__)
- void osErrorFatal(bool state, const char *msg, int lineNum, const char *fname){
- if(!state){
- perror(msg);
- fprintf(stderr, "%s:%d\n",fname, lineNum);
- exit(EXIT_FAILURE);
- }
- }
- int osCalculateFolderSize(char *path, unsigned *size){
- DIR *dirFD= opendir(path);
- if(dirFD == NULL){
- return false;
- }
- struct dirent *folder;
- struct stat buf;
- while((folder=readdir(dirFD))!=NULL){
- //printf("%s\n", folder->d_name);
- // const char *pathname, struct stat *buf
- // *;
- if(strcmp(".",folder->d_name) || strcmp("..",folder->d_name)){
- continue;
- } else {
- char* newPath = strndup(path, strlen(path)+1+strlen(folder->d_name));
- strcat(newPath, "/");
- strcat(newPath, folder->d_name);
- printf("p: %s\n", newPath);
- osAssert(-1!=lstat(newPath, &buf),"lstat failed inwhile");
- if(S_ISDIR(buf.st_mode)){
- osAssert(-1!=osCalculateFolderSize(newPath, size),"failed to calculate dir size");
- } else {
- *size += buf.st_size;
- }
- }
- }
- return 0;
- }
- int main(int argc, char **argv){
- osAssert(2==argc,"wrong num of args");
- struct stat buf;
- osAssert(-1!=lstat(argv[1],&buf),"stat failed");
- osAssert(S_ISDIR(buf.st_mode), "this is not dir");
- unsigned size = 0;
- osAssert(-1!=osCalculateFolderSize(argv[1], &size),"failed to calculate dir size");
- printf("\n");
- printf("size: %u\n",size);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement