raju
raju

Reputation: 21

How to get the directory size using c standard library

Is there is way to get the size of a directory using c standard library functions?

Upvotes: 2

Views: 5691

Answers (4)

Stefan Steiger
Stefan Steiger

Reputation: 82136

This should get you going.

See here for full program: https://stackoverflow.com/questions/3948116/how-to-integrate-two-different-processes-together-using-two-different-programs-in/3953873#3953873

For windows, see: http://code.google.com/p/portaputty/source/browse/trunk/windows/dirent.c?r=8

or this: http://www.softagalleria.net/dirent.php

or just use the MinGW compiler.

#include <unistd.h>
#include <dirent.h>
#include <sys/types.h> // for opendir(), readdir(), closedir()
#include <sys/stat.h> // for stat()


dir_proc = opendir(PROC_DIRECTORY) ;
    if (dir_proc == NULL)
    {
        perror("Couldn't open the " PROC_DIRECTORY " directory") ;
        return (pid_t) -2 ;
    }

    // Loop while not NULL
    while ( (de_DirEntity = readdir(dir_proc)) )
    {
        if (de_DirEntity->d_type == DT_DIR)
        {
            if (IsNumeric(de_DirEntity->d_name))
            {
                strcpy(chrarry_CommandLinePath, PROC_DIRECTORY) ;
                strcat(chrarry_CommandLinePath, de_DirEntity->d_name) ;
                strcat(chrarry_CommandLinePath, "/cmdline") ;
                FILE* fd_CmdLineFile = fopen (chrarry_CommandLinePath, "rt") ;  // open the file for reading text
                if (fd_CmdLineFile)
                {
                    fscanf(fd_CmdLineFile, "%s", chrarry_NameOfProcess) ; // read from /proc/<NR>/cmdline
                    fclose(fd_CmdLineFile);  // close the file prior to exiting the routine

                    if (strrchr(chrarry_NameOfProcess, '/'))
                        chrptr_StringToCompare = strrchr(chrarry_NameOfProcess, '/') +1 ;
                    else
                        chrptr_StringToCompare = chrarry_NameOfProcess ;

                    //printf("Process name: %s\n", chrarry_NameOfProcess);
                    //printf("Pure Process name: %s\n", chrptr_StringToCompare );

                    if ( CompareFunction(chrptr_StringToCompare, cchrptr_ProcessName, intCaseSensitiveness) )
                    {
                        pid_ProcessIdentifier = (pid_t) atoi(de_DirEntity->d_name) ;
                        closedir(dir_proc) ;
                        return pid_ProcessIdentifier ;
                    }
                }
            }
        }
    }
    closedir(dir_proc) ;

Upvotes: 1

ezpresso
ezpresso

Reputation: 8126

Check out this post with regard to how to get the size of a file. You may need to sum up the sizes of the files in a directory to get the "directory size".

If you are using Linux these posts may be of interest to you:

  1. How do I get the size of a directory in C?
  2. Find directory-size through C ??? LINUX ???

Upvotes: 1

Bart van Ingen Schenau
Bart van Ingen Schenau

Reputation: 15758

No. The C and C++ standard libraries do not explicitly support the concept of a directory.
As far as they are concerned, the backslashes in "C:\test\test.txt" have no special meaning. That is for the OS to handle.

Upvotes: 4

Didier Trosset
Didier Trosset

Reputation: 37427

What do you mean by the 'size of a directory'?

  • Is it the total size of the files contained into this directory?
  • ... plus the size of the sub-directories?
  • Is it only linked to the number of files contained in this directory?
  • ... plus the number of sub-directories?
  • ... plus the size of sub-directories themselves?

None of these are possible with a single C library or system call.

Upvotes: 2

Related Questions