Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6996789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:12:08+00:00 2026-05-27T20:12:08+00:00

I am trying to write a basic program that will sum up the filesizes

  • 0

I am trying to write a basic program that will sum up the filesizes of all files in the current directory and all subdirectories. I thought it would be a nice exercise. I am trying to use basic recursion for this. Here is what I have so far:

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>

long totalbytes = 0;

long dirsize(const char* directory, int verbose)
{
  struct dirent *de;
  struct stat s;
  DIR * dir; 
  //long total_items = 0;
  long filesize = 0;

  dir = opendir(directory);
  if (dir == NULL)
  {
     printf("Failed to open %s.\n", directory);
     return -1;
  }

  while ((de = readdir (dir)) != NULL)
  {
    stat(de->d_name, &s);
  if (S_ISLNK(s.st_mode)) 
  {
    printf("links are ignored.\n");
  }
  if (de->d_type == DT_REG)
  {
    filesize = 0; //be sure to reset this each time to avoid inaccuracy
    stat(de->d_name, &s); // get file info into our s structure
    if (verbose) 
    {
      printf("%s/%s : %ld bytes (%f MB)\n", directory, de->d_name, s.st_size, (float) s.st_size / 1024 / 1024);
    }
    filesize = s.st_size; //put file size into filesize variable
    totalbytes += filesize; //increment totalbytes
  }
  if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
  {
    char pathname[PATH_MAX];
    sprintf(pathname, "%s/%s", directory, de->d_name);
    dirsize(pathname, verbose); //recursion: keep looping until no more subdirs remain
    }
  }
  closedir(dir);
  return totalbytes;
}

long compute_size(const char* directory, int verbose)
{
  long space = dirsize(directory, verbose);
  return space;
}

int main(int argc, char* argv[])
{
  if (argc != 2)
  {
    printf("Usage: dirsize DIRECTORY\n");
return -1;
  }

  int verbose = 1; //show or hide individual computations

  long space = compute_size(argv[1], verbose);
  if (space != -1)
  {
    float space_mb = (float) space / 1024 / 1024;
    printf("space occupied: %ld bytes\n", space);
    printf("(%f MB)\n", space_mb);
  }
  return 0;
}

Here is my program output (I am also including an ls of the subdirectory)

08:35 PM@~/tmp$ ./dirsize .
./dirsize : 8369 bytes (0.007981 MB)
./diskstat.c : 1430 bytes (0.001364 MB)
./diskstat : 7993 bytes (0.007623 MB)
./ftw.c : 491 bytes (0.000468 MB)
./a.out : 8044 bytes (0.007671 MB)
./arrays.sh : 212 bytes (0.000202 MB)
./fileread/timestamp : 0 bytes (0.000000 MB)
./fileread/timestamp.c : 0 bytes (0.000000 MB)
./fileread/a.out : 8044 bytes (0.007671 MB)
./fileread/build.prop : 8044 bytes (0.007671 MB)
./fileread/read.c : 4096 bytes (0.003906 MB)
./fileread/read : 454656 bytes (0.433594 MB)
./local.fstab : 76 bytes (0.000072 MB)
./dirsize.c : 1857 bytes (0.001771 MB)
./echo.sh : 223 bytes (0.000213 MB)
./TEAMS.sh : 605 bytes (0.000577 MB)
space occupied: 504140 bytes
(0.480785 MB)
08:35 PM@~/tmp$ ls -l fileread/
total 40
-rwxr-xr-x 1 raidzero raidzero 8132 Dec 28 10:39 a.out
-rw-r--r-- 1 raidzero raidzero 2346 Dec 28 10:09 build.prop
-rwxr-xr-x 1 raidzero raidzero 8384 Dec 29 13:57 read
-rw-r--r-- 1 raidzero raidzero 1150 Dec 28 10:16 read.c
-rwxr-xr-x 1 raidzero raidzero 8132 Dec 29 13:57 timestamp
-rw-r--r-- 1 raidzero raidzero  659 Dec 28 10:39 timestamp.c
08:35 PM@~/tmp$

In my limited experience, getting random data is sign of improper memory allocation, but st_size in the stat struct is a long int..?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:12:08+00:00Added an answer on May 27, 2026 at 8:12 pm

    de->d_name only has the filename – it does not contain the path leading up to it. So you’re statting ./timestamp rather than ./fileread/timestamp, for example. You can either construct the full path to the file, use chdir() to enter directories as you go (not safe if you’re in a multi-threaded environment, and can be fragile if dirs are moved when you’re in them – fchdir() can be very helpful to get back to higher directories), or (on recent unixen) use the openat()/fstat() functions to traverse directories.

    Note that, had you checked for error returns from stat, you would have noticed this. Always check those error returns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple program that will compare the files in separate
I'm trying to write a basic Erlang program that reads in a file in
Hey, I'm trying to write a program that will accept new tasks from people,
So, my basic problem is that I'm trying to write a program for a
I am trying to write a program in Visual Basic (with VS 2010) that
Trying to write a PowerShell cmdlet that will mute the sound at start, unless
Trying to write a couple of functions that will encrypt or decrypt a file
im trying to write an app that will display a list off lines from
I'm trying to write a regex function that will identify and replace a single
Ok, I'm trying to code a basic program that could help user to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.