?
Solved

using stat in linux C program

Posted on 2004-10-17
2
Medium Priority
?
9,980 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
van_dy earned 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.
Suggested Courses

801 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