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 6563999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:54:46+00:00 2026-05-25T13:54:46+00:00

I am trying the listing directories example from K&R section 8.6, but when testing

  • 0

I am trying the listing directories example from K&R section 8.6, but when testing the code it, says “can’t access” for every single file in the directory. It is because it’s designed for Unix? Below is what I think the code should look like when pieced together, what am I missing? ADD:How do I fix this code to make it work?

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "dirent.h"
#include <sys/dir.h>
#define NAME_MAX 14

typedef struct {
  long ino;
  char name[NAME_MAX+1];
} Dirent;

void fsize(char *);

/* print file sizes */
main(int argc, char **argv)
{
  if (argc == 1)
    fsize(".");
  else
    while (--argc > 0)
      fsize(*++argv);
  return 0;
}

void dirwalk(char *, void (*fcn)(char *));

/* fsize: print size of file "name" */
void fsize(char *name)
{
  struct stat stbuf;

  if (stat(name, &stbuf) == -1) {
    fprintf(stderr, "fsize: can't access %s\n", name);
    return;
  }
  if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
    dirwalk(name, fsize);
  printf("%81d %s\n", stbuf.st_size, name);
}

#define MAX_PATH 1024

/* dirwalk: apply fn to all files in dir */
void dirwalk(char *dir, void (*fcn)(char *))
{
  char name[MAX_PATH];
  Dirent *dp;
  DIR *dfd;

  if ((dfd = opendir(dir)) == NULL) {
    fprintf(stderr, "dirwalk: can't open %s\n", dir);
    return;
  }

  while ((dp = readdir(dfd)) != NULL) {
    if (strcmp(dp->name, ".") == 0
    || strcmp(dp->name, "..") == 0)
      continue;
    if (strlen(dir)+strlen(dp->name)+2 > sizeof(name))
      fprintf(stderr, "dirwalk: name %s/%s too long\n",
          dir, dp->name);
    else {
      sprintf(name, "%s/%s", dir, dp->name);
      (*fcn)(name);
    }
  }
  closedir(dfd);
}
  • 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-25T13:54:46+00:00Added an answer on May 25, 2026 at 1:54 pm

    The structure Dirent defined in your example probably doesn’t match struct dirent for your system (it doesn’t on mine).

    On a Linux system the following will fix the problem:

    /* dirwalk: apply fn to all files in dir */
    void dirwalk(char *dir, void (*fcn)(char *))
    {
        char name[MAX_PATH];
        struct dirent *dp;
    
        DIR *dfd;
    
        if ((dfd = opendir(dir)) == NULL) {
                fprintf(stderr, "dirwalk: can't open %s\n", dir);
                return;
        }
    
        while ((dp = readdir(dfd)) != NULL) {
                if (strcmp(dp->d_name, ".") == 0
                                || strcmp(dp->d_name, "..") == 0)
                        continue;
                if (strlen(dir)+strlen(dp->d_name)+2 > sizeof(name))
                        fprintf(stderr, "dirwalk: name %s/%s too long\n",
                                        dir, dp->d_name);
                else {
                        sprintf(name, "%s/%s", dir, dp->d_name);
                        (*fcn)(name);
                }
        }
        closedir(dfd);
    }
    

    The readdir function is system-specific, so you should consult the documentation for yours. On a Linux system you can type man 2 readdir in a terminal to see the online manual for the readdir function.

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

Sidebar

Related Questions

I am trying to to get all directories' name from an FTP server and
I am trying to pull the price and seller from the amazon offer listing
Im listing all wordpress posts in the blog category but trying to add a
i'm trying to make this address: http://www.mysite.co.uk/Listing/London/34 really look for: http://www.mysite.co.uk/Listing/listed.php?area=London&list=34 behind the scenes.
I am trying to get the source code for the following page: http://www.amazon.com/gp/offer-listing/082470732X/ref=dp_olp_0?ie=UTF8&redirect=true&condition=all (Please
I'm trying to implement Maximum Rectangle Algorithm from Dr. Dobbs (Listing four) with Python.
I am learning process forking and I'm trying the code of listing 3: sunbox$
I am trying to generate a hierarchical directory listing in pyGTK. Currently, I have
so i'm trying to do a data migration where i take the listings from
Trying to make a make generic select control that I can dynamically add elements

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.