Solved

using stat in linux C program

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

792 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