Solved

using stat in linux C program

Posted on 2004-10-17
2
9,958 Views
Last Modified: 2008-01-09
Hi, I have an assignment to make a program which list files and directories in the current directory (a simple version of ls).  I can not finure out how to convert structures in the stat type to readable form. Here is the code:

****************************************************
//dirlist by Josh McCullough
//finds a dir within the current dir

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>

char* getProtection(mode_t);
char* getUsername(uid_t);
char* getGroup(gid_t);
char* getDateString(time_t);

int main(int argc, char * argv[])
{
        DIR *objDirPnt;
        struct dirent *objDir;
        struct stat objFile;

        objDirPnt=opendir(get_current_dir_name());

        printf("Mode      Owner ID\tSize (b)\tModify Time\tName\n");

        while((objDir=readdir(objDirPnt))!=NULL)
        {
                stat(objDir->d_name,&objFile);

                char *cProtection=getProtection(objFile.st_mode);
                char *cOwner=getUsername(objFile.st_uid);
                char *cModifyTime=getDateString(objFile.st_mtime);

                printf("%s ",cProtection);
                printf("%s\t",cOwner);
                printf("%d\t\t",objFile.st_size);
                printf("%s\t",cModifyTime);
                printf("%s\n",objDir->d_name);
        }

        closedir(objDirPnt);

        return 0;
}

char*  getProtection(mode_t vProtection)
{
        char *cProtection="---------";

        if(S_IRUSR & vProtection)
                cProtection[0]='R';
        if(S_IWUSR & vProtection)
                cProtection[1]='W';
        if(S_IXUSR & vProtection)
                cProtection[2]='X';

        return cProtection;
}

char* getUsername(uid_t vUsername)
{
        return "no user";
}

char* getGroup(gid_t vGroup)
{
        return "no group";
}

char* getDateString(time_t vTime)
{
        struct tm *tmTime=localtime(&vTime);
        static char cTime[25];

        strftime(cTime,sizeof(cTime),"%h %e %H:%M",tmTime);

        return cTime;
}
****************************************************

I've created a few functions, which i want to convert the info in the stat structure to readable form.  For instance, getProtection should accept a mode_t type and return rwxr--r-- or something like that.

If this is unclear let me know.
Thanks,
Josh
0
Comment
Question by:jsm11482
2 Comments
 
LVL 5

Accepted Solution

by:
van_dy earned 125 total points
ID: 12334194
Hi Josh,

char *getprotection(mode)
             mode_t mode;
{
              char *mstring = calloc(10, 1);
               memset(mstring, '-', 9);
                if(mode & S_IRUSR)
                          mstring[0] = 'r';
                 if(mode & S_IWUSR)
                           mstring[1] = 'w';
                 ......
                  if(mode & S_IXOTH)
                            mstring[8] = x;

                  return mstring;
}


for printing the user name, you can call getpwuid(), and getting the groupname u can call getgrgid(). look into the man pages of each to learn how to use them.

hope this helps,
van_dy
0
 

Author Comment

by:jsm11482
ID: 12334785
perfect! thanks a bunch!
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mixing C++ and C code elegantly 10 153
How to design, and implement simple interface 5 134
In C, why can't I shift a 64-bit integer more than 32 bits? 13 206
valid enum? 6 73
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now