C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-22-2001, 10:34 AM   #1
Registered User
 
Join Date: Aug 2001
Posts: 27
that damn stat()

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.

Code:
#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));
			}
			else 
        perror( "Failed to stat" );
		
		  printf("Enter a Filename\n");			        
    }
		printf("Program terminated\n");
    return 0;
}
@licomb is offline   Reply With Quote
Old 08-22-2001, 12:50 PM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,680
> 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.

eg
Code:
if ( statinfo.st_mode & S_IRUSR ) putchar('r'); else putchar('-');
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 08-22-2001, 04:24 PM   #3
Blank
 
Join Date: Aug 2001
Posts: 1,034
Try reading man 2 stat to get all the defines
Nick is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the error code of stat for non existent file ? jabka C Programming 1 09-06-2007 04:17 AM
help with stat() and fopen() movl0x1 C Programming 6 07-25-2007 05:28 AM
Random Number Range Problem. xamlit C Programming 11 01-26-2006 12:55 PM
Capturing file stat information Sue Paterniti C Programming 3 04-15-2002 05:47 AM
Shortening main pdstatha C Programming 1 04-03-2002 04:56 PM


All times are GMT -6. The time now is 04:11 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22