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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:32:33+00:00 2026-05-14T05:32:33+00:00

I am trying to simulate linux command ls using linux api from c. Looking

  • 0

I am trying to simulate linux command ls using linux api from c. Looking at the code it does make sense, but when I run it I get “stat error: No such file or directory”. I have checked that opendir is working ok. I think the problem is in stat, which is returning -1 even though I think it should return 0.

What am I missing?

Thanks for your help.

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

int main(int argc, char *argv[])
{
 DIR *dirp;
 struct dirent *direntp;
 struct stat stat_buf;
 char *str;

 if (argc != 2)
 {
  fprintf( stderr, "Usage: %s dir_name\n", argv[0]);
  exit(1);
 }

 if ((dirp = opendir( argv[1])) == NULL)
 {
  perror(argv[1]);
  exit(2);
 }

 while ((direntp = readdir( dirp)) != NULL)
 {
  if (stat(direntp->d_name, &stat_buf)==-1)
  {
   perror("stat ERROR");
   exit(3);
  }
  if (S_ISREG(stat_buf.st_mode)) str = "regular";
  else if (S_ISDIR(stat_buf.st_mode)) str = "directory";
  else str = "other";
  printf("%-25s - %s\n", direntp->d_name, str);
 }

 closedir(dirp);
 exit(0);
}
  • 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-14T05:32:33+00:00Added an answer on May 14, 2026 at 5:32 am

    It’s because you aren’t stating the actual file. It’s in a different directory. If you want the real filename, combine argv[1] and direntp->d_name with a ‘/’ between them.

    Also, hungarian naming is icky, even the minor bit like ‘p’ on the end. If you have so many variables you need to keep track of their types in their names you’re doing something wrong.

    Here is a revised version of your program:

    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    #include <string.h>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <errno.h>
    #include <limits.h>
    #include <sys/param.h>
    
    int main(int argc, char *argv[])
    {
     DIR *dirp;
     struct dirent *direntp;
     struct stat stat_buf;
     char *str;
     char fullpath[MAXPATHLEN + 1];
     size_t dirnamelen;
    
     if (argc != 2)
     {
      fprintf( stderr, "Usage: %s dir_name\n", argv[0]);
      exit(1);
     }
     strncpy(fullpath, argv[1], MAXPATHLEN - 1); /* account for trailing '/' */
     fullpath[MAXPATHLEN - 1] = '\0';
     dirnamelen = strlen(fullpath);
     if (strlen(argv[1]) > dirnamelen) {
      fprintf( stderr, "Directory name is too long: %s", argv[1] );
      exit(2);
     }
    
     fullpath[dirnamelen++] = '/';
     fullpath[dirnamelen] = '\0';
    
     if ((dirp = opendir( argv[1])) == NULL)
     {
      perror(argv[1]);
      exit(2);
     }
    
     while ((direntp = readdir( dirp)) != NULL)
     {
      fullpath[dirnamelen] = '\0';
      if ((dirnamelen + strlen(direntp->d_name)) > MAXPATHLEN) {
       fprintf(stderr, "File %s + directory %s is too long.", direntp->d_name, fullpath);
       continue;
      } else {
       /* strncpy is mild overkill because the if statement has verified that
          there's enough space.  */
       strncpy(fullpath + dirnamelen, direntp->d_name, MAXPATHLEN - dirnamelen);
       fullpath[MAXPATHLEN] = '\0';
      }
      if (stat(fullpath, &stat_buf)==-1)
      {
       perror("stat ERROR");
       exit(3);
      }
      if (S_ISREG(stat_buf.st_mode)) str = "regular";
      else if (S_ISDIR(stat_buf.st_mode)) str = "directory";
      else str = "other";
      printf("%-25s - %s\n", direntp->d_name, str);
     }
    
     closedir(dirp);
     exit(0);
    }
    

    Note that I use MAXPATHLEN (from <limits.h>) and carefully check to make sure there aren’t any buffer overflows. You should do the same in your code.

    Edit: Changed code to use strn family functions for added safety.

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

Sidebar

Related Questions

I'm trying to simulate some code that I have working with SQL but using
I'm trying to simulate a particle system using OpenGl but I can't get it
I'm trying to simulate n games of craps. The code seems to make sense
I am trying to simulate a simple MIPS processor using behavior code in Verilog.
I'm trying to simulate a token ring using python with sockets, but I have
I'm trying to port python code from linux to windows right now. In various
I'm trying to simulate rotating box using Newton Physics and OpenGL. This is what
I am trying to simulate a session using FactoryGirl / shoulda (it worked with
I am trying to simulate a keypress for enter key in selenium using the
I'm trying to simulate the Matrix code rain with the canvas element and javascript.

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.