Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- http://stackoverflow.com/questions/3138600/correct-use-of-stat-on-c
- http://www.electro.fisica.unlp.edu.ar/temas/p5/files.html //explicatii directoare
- https://www.cs.cf.ac.uk/Dave/C/node20.html#SECTION002000000000000000000 //~directoare
- */
- #include <stdio.h>
- #include <dirent.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- long int computeDirectorySize(const char* name){
- DIR* dir = opendir(name);
- long int totalSize = 0;
- struct dirent* currentFile;
- while((currentFile = readdir(dir)) != NULL){
- if(strcmp(currentFile->d_name, "..") == 0 || strcmp(currentFile->d_name, ".") == 0){
- continue;
- }
- struct stat file_status;
- if (stat(currentFile->d_name, &file_status) == 0) {
- char dirName[30] = "";
- //strcat(dirName, name);
- strcat(dirName, "/");
- strcat(dirName, currentFile->d_name);
- printf("%s\n", dirName);
- if (S_ISDIR(file_status.st_mode)){
- printf("%s is a directory\n", currentFile->d_name);
- totalSize += computeDirectorySize(dirName);
- }
- else if(S_ISREG(file_status.st_mode)){
- printf("%s is a normal file\n", currentFile->d_name);
- //printf("%d \n", file_status.st_size);
- totalSize += file_status.st_size;
- }
- else{
- continue;
- }
- }
- else { /* stat() call failed and returned '-1'. */
- perror("stat call failed");
- }
- }
- closedir(dir);
- return totalSize;
- }
- int main(int argc, char *argv[])
- {
- if(argc == 1)
- {
- fprintf(stderr, "Please give one or more directory names! %d\r\n", argc);
- return 1;
- }
- int i;
- for(i=1; i<argc; i++){
- long int totalSize = computeDirectorySize(argv[i]);
- printf("Dimensiunea totala a directorului %s este : %d bytes.\n\n", argv[i], totalSize);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement