1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

C System Programming (Directories/Files Status)

Discussion in 'Software Development' started by hashim, Apr 11, 2002.

Thread Status:
Not open for further replies.
  1. hashim

    hashim Thread Starter

    Joined:
    Jul 14, 2001
    Messages:
    75
    I am working on a Systems Programming Program in C. I hope that you can help me. I can open a directory and print the files inside the directory. However, for every file that it prints, it says that it is a "DIRECTORY" file and has permissions of "2700".

    1. I need to open a given COMMAND LINE argument directory and then print all of the files that are in the directory,
    2. print whether they are directory or regular, etc. and
    3. print their mode.

    I am getting the same output for each file though. Can you look at my code and see why it might be printing the same for all? These are not all directory files.

    THE OUTPUT when I run the file:

    ------------------------------------------
    The directory testme contains the following:

    File . is a Directory file
    The mode of . is 2700,
    File .. is a Directory file
    The mode of .. is 2700,
    File jane is a Directory file
    The mode of adam is 2700,
    File bob is a Directory file
    The mode of jim is 2700,
    File test.c is a Directory file
    The mode of adam.c is 2700,
    ------------------------------------------

    MY PROGRAM:


    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <dirent.h> /* readdir(), etc. */
    #include <sys/types.h>
    #include <sys/stat.h> /* stat(), etc. */
    #include <sys/dir.h>

    /* struct dirent *DirEntryPtr; */

    /* Globals */
    char *filename = NULL; /* Points to file name */
    struct dirent *entry; /* the address of a directory entry created
    by the os when you you read a directory
    structure it always contains a name field
    (d_name) & inode no */

    struct stat st; /* an inode structure to be filled in by the os
    when you make a stat() call. */

    /*************************************************/

    /* argc refers to number of args passed, and argv[] is a pointer array that points to each argument passed to main */

    main ( int argc, char *argv[] )

    {
    DIR *dirp; /* the address of a directory structure
    created by the operating system when
    you open a directory it remembers the
    last entry you read. */

    /* Check to make sure the user has entered a command line argument */

    if (argc != 2)
    {
    printf("Usage: You need to enter only one argument!\n");
    exit(1);
    }

    filename = argv[1]; /* set variable to command line arg*/

    /* Check to make sure there were no errors. */
    if((dirp=opendir(filename))==NULL)
    {
    fatalError();
    }

    printf ("\nThe directory %s contains the following:\n\n",filename);

    while(entry=readdir(dirp))
    {
    if (stat(filename, &st) == -1) {
    printf("Unable to obtain the status of %s\n", filename);
    }

    printf("\nFile %s is a ", entry->d_name);

    switch (st.st_mode & S_IFMT) {

    case S_IFREG : printf("Regular file \n");
    printf("The mode of %s is %o\n\n", entry->d_name, st.st_mode & 07777);
    break;

    case S_IFDIR : printf("Directory file \n");
    printf("The mode of %s is %o\n\n", entry->d_name, st.st_mode & 07777);
    break;

    case S_IFCHR : printf("Character special file \n");
    printf("The mode of %s is %o\n\n", entry->d_name, st.st_mode & 07777);
    break;

    case S_IFBLK : printf("Block special file \n");
    printf("The mode of %s is %o\n\n", entry->d_name, st.st_mode & 07777);
    break;

    default: fatalError();
    }
    printf("\n");
    }
    closedir(dirp);
    printf("\n");
    return (0); /* Done */
    }

    /*************************************************************************

    fatalError ()

    {
    perror ("Project 4: "); /* Describe error */
    exit(1);
    }
     
  2. codejockey

    codejockey

    Joined:
    Feb 11, 2002
    Messages:
    1,405
    I think the basic problem is that you always perform the stat call on the filename supplied as an argument, rather than on each filename read from the directory entry (entry->d_name). Rather than printing the filetype of each file in the directory, you are printing the filetype of the filename parameter for each file you find in the directory.

    Hope this helps.
     
  3. hashim

    hashim Thread Starter

    Joined:
    Jul 14, 2001
    Messages:
    75
    I think that I figured it out. I needed to use lstat() instead of stat() and only use 0777 instaed of 07777.

    Thanks for your reply!
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Similar Threads - System Programming (Directories
  1. Techrajxx
    Replies:
    1
    Views:
    291
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/75937

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice