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;
}