Solved

using stat in linux C program

Posted on 2004-10-17
2
9,963 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 and use conditional statements in the C programming language.

773 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