![]() |
| | #1 |
| Registered User Join Date: Aug 2001
Posts: 27
| that damn stat() 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 | |
| | #2 |
| and the hat of Jobseeking 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('-');
|
| Salem is offline | |
| | #3 |
| Blank Join Date: Aug 2001
Posts: 1,034
| Try reading man 2 stat to get all the defines |
| Nick is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |