Extraction of folder size in Linux C program

Source: Internet
Author: User

Http://lizzy115.blog.163.com/blog/static/36491958201010254255320/

 

In Embedded Linux systems, some real-time data is often stored. However, when the storage space is limited, it is often necessary to determine the size of folders in the storage directory, there are few programs that can be used to obtain the folder size through the C language on the Internet. Now we provide a program for everyone to share and share. In fact, the program extracts the size of all files in the folder, extract the sum of the file sizes in the running program folder, excluding the folder sizes in the folder directory. The specific procedure is as follows:

# Include <stdio. h>
# Include <dirent. h>
# Include <sys/STAT. h>
 
Main ()
{
Dir * D;
Struct dirent * de;
Struct stat Buf;
Int exists;
Int total_size;
 
D = opendir (".");
If (D = NULL ){
Perror ("prsize ");
Exit (1 );
}
 
Total_size = 0;
 
For (DE = readdir (d); De! = NULL; De = readdir (D )){
Exists = Stat (de-> d_name, & BUF );
If (exists <0 ){
Fprintf (stderr, "couldn't stat % s \ n", de-> d_name );
} Else {
Total_size + = Buf. st_size;
}
}
Closedir (d );
Printf ("% d \ n", total_size );
}

The following is a program for extracting the size of another folder:

# Include <stdio. h>
# Include <errno. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/STAT. h>

Static unsigned int Total = 0;

Int sum (const char * fpath, const struct stat * Sb, int typeflag)
{

Total + = Sb-> st_size;
Return 0;

}
 

Int main (INT argc, char ** argv)
{

If (! Argv [1] | access (argv [1], r_ OK )){

Return 1;

}

If (FTW (argv [1], & sum, 1 )){

Perror ("FTW ");

Return 2;

}

Printf ("% s: % u \ n", argv [1], total );

Return 0;

}

 

Compile a program using gcc
Gcc-O dir_size dir_size.c

Run the program
./Dir_size/licy/

 

The following program uses the statfs function to extract hard disk size data and extract the remaining space, and print out the hard disk size and remaining space.

# Include <stdio. h>;
# Include <sys/VFS. h>;
# Include <error. h>;

# Define gsize (1024.00*1024.00*1024.00)
# Define msize (1024.00*1024.00)

# Ifndef ext2_super_magic
# Define ext2_super_magic 0xef53
# Endif

Int main ()
{
Long long blocks, bfree;
Struct statfs FS;
If (statfs ("/", & FS) <0)
{
Perror ("statfs ");
Exit (0 );
}

Printf ("% x \ n", FS. f_type);/* type of filesystem (see below )*/
Printf ("% LD \ n", FS. f_bsize);/* optimal transfer block size */
Printf ("% LD \ n", FS. f_blocks);/* Total data blocks in File System */
Printf ("% LD \ n", FS. f_bfree);/* free blocks in FS */
Printf ("% LD \ n", FS. f_bavail);/* free blocks avail to non-superuser */
Printf ("% LD \ n", FS. f_files);/* total file nodes in File System */
Printf ("% LD \ n", FS. f_ffree);/* free file nodes in FS */
Printf ("% d \ n", FS. f_fsid);/* File System ID */
Printf ("% LD \ n", FS. f_namelen);/* Maximum length of filenames */

Blocks = FS. f_blocks;
Bfree = FS. f_bfree;
Printf ("% LLD \ n", blocks );
If (FS. f_type = ext2_super_magic)
{
Printf ("total size of/is % f g \ n", blocks * fs. f_bsize/gsize );
Printf ("Free size of/is % f g \ n", bfree * fs. f_bsize/gsize );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.