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

  • Home
  • SEARCH
  • 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 6643807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:05:37+00:00 2026-05-26T00:05:37+00:00

I got this question in a Cisco interview: write a function to find the

  • 0

I got this question in a Cisco interview: write a function to find the size of a directory?

Following is the pseudocode for such a function, that follows a recursive approach. Please tell me if there can be any other approach also.

int directorySize(DirectoryHandle dh)
{
    int size=0;
    if (!dh)
    {
        DirectoryHandle dh1 = directoryOpen("Directory_path");
    }
    else
    {
        dh1 = dh;
    }

    while (dh1)
    {
        if (TRUE=IsDirectory(dh1))
        {
            size += directorysize(dh1);
        }
        else if (TRUE == IsFile(dh1))
        {
            FileHandle fh = dh1;
            while (EOF != fh)
            {
                size++;
            }
        }
    }
}
  • 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-26T00:05:37+00:00Added an answer on May 26, 2026 at 12:05 am

    Canonical example of using nftw:

    Note that as interview questions go, they will probably want to see you thinking about

    • traversal order
    • permission (inaccessible subfolder etc.)
    • size ondisk vs. apparent size
    • symlinks, hardlinks (outside the tree? duplicate counting?)
    • sparse files
    • performance

    The following code does address most of these issues in a pragmatic fashion:

    .

    #define _XOPEN_SOURCE 500
    #include <ftw.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    
    static uintmax_t total        = 0ul;
    static uintmax_t files        = 0ul;
    static uintmax_t directories  = 0ul;
    static uintmax_t symlinks     = 0ul;
    static uintmax_t inaccessible = 0ul;
    static uintmax_t blocks512    = 0ul;
    
    static int
    display_info(const char *fpath, const struct stat *sb,
                 int tflag, struct FTW *ftwbuf)
    {
        switch(tflag)
        {
            case FTW_D:
            case FTW_DP:  directories++;  break;
            case FTW_NS:
            case FTW_SL:
            case FTW_SLN: symlinks++;     break;
            case FTW_DNR: inaccessible++; break;
            case FTW_F:   files++;        break;
        }
        total += sb->st_size;
        blocks512 += sb->st_blocks;
        return 0; /* To tell nftw() to continue */
    }
    
    int
    main(int argc, char *argv[])
    {
        int flags = FTW_DEPTH | FTW_MOUNT | FTW_PHYS;
    
        if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags) == -1)
        {
            perror("nftw");
            exit(EXIT_FAILURE);
        }
    
        printf("Total size: %7jd\n", total);
        printf("In %jd files and %jd directories (%jd symlinks and %jd inaccessible directories)\n", files, directories, symlinks, inaccessible);
        printf("Size on disk %jd * 512b = %jd\n", blocks512, blocks512<<9);
    
        exit(EXIT_SUCCESS);
    }
    

    This was posted as Fastest ways to get a directory Size and Size on disk before. Typical output:

    Total size: 28433001733
    In 878794 files and 87047 directories (73318 symlinks and 0 inaccessible directories)
    Size on disk 59942192 * 512b = 30690402304
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this question at an interview and at the end was told there
I got this question from this discussion . A method call like object.m does
So I got this question from one of the developers in my team: What
I've got this small question - given a bitmask of weekdays (e.g., Sunday =
The responses I've got to this question have solved the problem I had in
This question on archiving PDF's got me wondering -- if I wanted to compress
This question asking for a phone number format API in Java got me wondering
(I asked this question in another way , and got some interesting responses but
When I asked this question I got almost always a definite yes you should
In this question, I asked about breaking a dataset into subsets, and got a

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.