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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:50:02+00:00 2026-06-17T09:50:02+00:00

Compiler: Code::Blocks(GNU GCC) Platform: Windows(x86) Update: I have solved the problem by using chdir()

  • 0
  • Compiler: Code::Blocks(GNU GCC)
  • Platform: Windows(x86)

Update: I have solved the problem by using chdir() to change the current working directory before I call opendir(). So I am assuming that opendir() can only open a directory that is in the current working directory. So my new question is, am I correct?

I am currently writing a basic imitation of window’s dir command. My program works properly when the “.” wildcard is used as an argument for opendir(). But when I don’t use the wildcard and specify a directory instead. My program will not open the directory specified to it. For example if I type c:\windows it will open c:\ instead and each file’s st_mode will be the same. At least I assume that they are all the same because all of the file types(DIR, FILE, OTHER) are the same.

#include <stdio.h>
#include <string.h>

#include <sys/stat.h>
#include <dirent.h>

int main(int argc, char* argv[])
{
//'directory' points to the directory | 'directory_contents' is used with readdir() to read the directory's('directory') contents.
DIR *directory;
struct dirent *directory_contents;
struct stat file_info;

//IF no argument is present display the contents of the current directory | IF there is an arugment display the contents of that argument | ELSE Too many arguments
if (argc == 1)
{
    directory = opendir(".");
}
else if (argc == 2)
{
    //New Code
    chdir(argv[1]); directory = opendir(".");

    //Old Code
    directory = opendir(argv[1]);
}
else
{
    printf("ERROR: Extra arguments\n");
}

//Checks to see if the directory opened above was actually opened.
if (directory == NULL)
{
    printf("ERROR: Failed to open '%s'.\n", argv[1]);
    return 2;
}
else
{
    //WHILE there are file names to be read THEN read the file names
    while (directory_contents = readdir(directory))
    {
        stat(directory_contents->d_name, &file_info);

        //Test for directory
        if(S_ISDIR(file_info.st_mode))
        {
            //File type
            printf("<DIR>   ");

            //File name
            if(strlen(directory_contents->d_name) <= 15)
            {
                printf("%-15s", directory_contents->d_name);
            }
            else if(strlen(directory_contents->d_name) > 15)
            {
                printf("%.12s.. ", directory_contents->d_name);
            }

            //File premissions
            printf("<%c%c%c>\n", ((file_info.st_mode & S_IRUSR)==0) ? '-' : 'r', ((file_info.st_mode & S_IWUSR)==0) ? '-' : 'w', ((file_info.st_mode & S_IXUSR)==0) ? '-' : 'x');
        }
        //Test for a regular file.
        else if(S_ISREG(file_info.st_mode))
        {
            //File type
            printf("<FILE>  ");

            //File name
            if(strlen(directory_contents->d_name) <= 15)
            {
                printf("%-15s", directory_contents->d_name);
            }
            else if(strlen(directory_contents->d_name) > 15)
            {
                printf("%.12s.. ", directory_contents->d_name);
            }

            //File premissions
            printf("<%c%c%c> ", ((file_info.st_mode & S_IRUSR)==0) ? '-' : 'r', ((file_info.st_mode & S_IWUSR)==0) ? '-' : 'w', ((file_info.st_mode & S_IXUSR)==0) ? '-' : 'x');

            //File size
            if (file_info.st_size < 1000)
            {
                printf("<%-3i B>\n", file_info.st_size);
            }
            else if ( (file_info.st_size > 1000) && (file_info.st_size < 1000000) )
            {
                printf("<%-3i KB>\n", file_info.st_size/1000);
            }
            else if ( (file_info.st_size > 1000000) && (file_info.st_size < 1000000000) )
            {
                printf("<%-3i MB>\n", file_info.st_size/1000000);
            }
            else
            {
                printf("<%-3i GB>\n", file_info.st_size/1000000000);
            }
        }
        //Symbolic Link etc.
        else
        {
            //File type
            printf("<OTHER> ");

            //File name
            if(strlen(directory_contents->d_name) <= 15)
            {
                printf("%-15s", directory_contents->d_name);
            }
            else if(strlen(directory_contents->d_name) > 15)
            {
                printf("%.12s.. ", directory_contents->d_name);
            }

            //File premissions
            printf("<%c%c%c>\n", ((file_info.st_mode & S_IRUSR)==0) ? '-' : 'r', ((file_info.st_mode & S_IWUSR)==0) ? '-' : 'w', ((file_info.st_mode & S_IXUSR)==0) ? '-' : 'x');
        }
    }
}
}

And yes I do know that the permissions I am outputting are completely irrelevant because of Window’s use of ACLs. I am only writing this program on windows because I have no choice right now but it is intended for a Linux OS.

  • 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-06-17T09:50:03+00:00Added an answer on June 17, 2026 at 9:50 am

    stat(directory_contents->d_name,

    This line is the problem. The d_name field is just the name of the file without any directory. So unless the directory happens to be the current directory, the call to stat() will not find the file.

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

Sidebar

Related Questions

Compiler: Code::Blocks(GNU GCC) Platform: Windows(x86) Includes: winsock.h winsock2.h (ws2_32 is linked as well) I
I'm using the Code::Blocks IDE with the GNU GCC compiler. struct test { char
I am using Code::Blocks 10.05, and the GNU GCC Compiler. Basically, I ran into
I'm using code blocks with gnu gcc compiler. But when I try to compile
I have been using the gcc compiler with code::blocks ide, and have noticed there
I am using Code-Blocks with the mingw GCC compiler for windows to code my
I'm compiling this code with GNU GCC Compiler in Code Blocks but for some
I'm trying to get sqlite3 c-api to compile under windows with code:blocks/gcc mingw compiler
I am running C in Code:Blocks IDE with the compiler GNU GCC . And
I am using Code Block with GNU GCC Compiler. And I am trying this

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.