Solved

using stat in linux C program

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

743 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

11 Experts available now in Live!

Get 1:1 Help Now