View Full Version : that damn stat()

08-22-2001, 10:34 AM
Ok my program works, only problem is i am unsure of the stat command

say from the linux terminal
stat -l txtfile

it would display lots of info about that file, such as its permissions something like -rw-r--r--

How can you do this with the stat function, i think its something to do with either .st_mode or st_dev
does anyone know, i searched the net and can't find anything, other than the same repeted cat description from linux. can anyone help me its doing my head.

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

#define BUFFER 30

int status;

struct stat statinfo;

int main ( ) {
char buff[BUFFER];
printf("Enter a Filename\n");
while ( fgets(buff,BUFFER,stdin) != NULL )
char *nl = strchr( buff, '\n' );

if ( buff[0] == '\n' )
break; /* blank line entered */

if ( nl != NULL )
*nl = '\0'; /* remove the end of line */

status = stat( buff, &statinfo );

if ( status == 0 )
printf("File: '%s' \n", buff);
printf("Size: %d bytes\n", statinfo.st_size);
printf("%o\n", statinfo.st_dev)ge;
printf("System Permission: %m\n", statinfo.st_mode);
printf("Inode:%d Links %d\n",statinfo.st_ino,statinfo.st_nlink);

printf("User ID %u\n", statinfo.st_uid);
printf("Group ID %u\n", statinfo.st_gid);
printf("Last Accessed %s",ctime(&statinfo.st_atime));
printf("Last changed %s", ctime(&statinfo.st_ctime));
printf("Last Modified: %s", ctime(&statinfo.st_mtime));
perror( "Failed to stat" );

printf("Enter a Filename\n");
printf("Program terminated\n");
return 0;

08-22-2001, 12:50 PM
> printf("System Permission: %m\n", statinfo.st_mode);
There's no such thing as %m.
If you want to print the mode, then you need to do it on a per bit basis.

st_mode is a series of bits which are set or clear depending on whether that particular permission is allowed.


if ( statinfo.st_mode & S_IRUSR ) putchar('r'); else putchar('-');

08-22-2001, 04:24 PM
Try reading man 2 stat to get all the defines