Thread: that damn stat()

  1. #1
    Registered User
    Join Date
    Aug 2001

    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.

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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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('-');
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Join Date
    Aug 2001
    Try reading man 2 stat to get all the defines

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-06-2007, 04:17 AM
  2. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  3. Random Number Range Problem.
    By xamlit in forum C Programming
    Replies: 11
    Last Post: 01-26-2006, 12:55 PM
  4. Capturing file stat information
    By Sue Paterniti in forum C Programming
    Replies: 3
    Last Post: 04-15-2002, 05:47 AM
  5. Shortening main
    By pdstatha in forum C Programming
    Replies: 1
    Last Post: 04-03-2002, 04:56 PM