stat()

This is a discussion on stat() within the Linux Programming forums, part of the Platform Specific Boards category; I need someones help, i need to use the stat() command in a program that takes in a filename that, ...

  1. #1
    Blank
    Join Date
    Aug 2001
    Posts
    1,034

    Unhappy stat()

    I need someones help, i need to use the stat() command in a program that takes in a filename that, were the stat command show the permissions of that file, the program will keep going till you enter a null filename.

    I am stuck, i've tried looking up info on the stat, but not found any real good stuff that explains it.

    is there any brainy C/linux programmer out there that can help.
    -ali

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149

    What do you want to know?

    The man page for stat() lists the prototype and how to use it.
    Code:
    // I use Solaris 8 - may be different
    #include <sys/types.h>
    #include <sys/stat.h>
    
    int stat ( const char *path, struct stat *buf );
    
    // directories leading to file in path must be searchable.
    
    mode_t   st_mode; /* File mode (see mknod(2)) */
    ino_t    st_ino;        /* Inode number */
    dev_t    st_dev;      /* ID of device containing */
                                   /* a directory entry for this file */
    dev_t    st_rdev;     /* ID of device */
                                   /* This entry is defined only for */
                                   /* char special or block special files */
    nlink_t  st_nlink;     /* Number of links */
    uid_t    st_uid;        /* User ID of the file's owner */
    gid_t    st_gid;        /* Group ID of the file's group */
    off_t    st_size;       /* File size in bytes */
    time_t   st_atime;   /* Time of last access */
    time_t   st_mtime;  /* Time of last data modification */
    time_t   st_ctime;   /* Time of last file status change */
                                  /* Times measured in seconds since */
                                   /* 00:00:00 UTC, Jan. 1, 1970 */
    long     st_blksize;  /* Preferred I/O block size */
    blkcnt_t st_blocks; /* Number of 512 byte blocks allocated*/
    Here is an example of how the stat function can be used:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>
    
    #include <sys/types.h>
    #include <sys/stat.h>
    
    int main ( int argc, char *argv[])
    {
        struct stat FileAttrib;
    
        if (argc != 2)
            printf("Usage: <executable> <file>\n");
        else
        {
            if (stat(argv[(argc - 1)], &FileAttrib) < 0)
                printf("File Error Message = %s\n", strerror(errno));
            else
            {
                tm *pTm = gmtime(&FileAttrib.st_atime);
                printf("File last accessed on: %.2i/%.2i/%.2i at %.2i:%.2i:%.2i GMT\n",
                         (pTm->tm_mon + 1),
                         pTm->tm_mday,
    	     (pTm->tm_year % 100),
    	     pTm->tm_hour,
    	     pTm->tm_min,
    	     pTm->tm_sec);
            }
        }
        return 0;
    }
    David
    Last edited by Strider; 08-15-2001 at 06:33 AM.
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    It's not really me?

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    Wasn't cross posting i was told that this would be better place to ask a linux fucntion call, anyway there is somthing funny as it was i who posted this topic, somehow its attached itself to nick?

    sounds like the SQL database is playing up

    -ali

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    27
    ok going off what people have said and offered. i have wrote the following.
    No idea if it work,

    Does anyone see a problem with it!!!

    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/stat.h>

    #define BUFFER 10

    struct stat fileattrib;

    char file[BUFFER], *nl;

    int main(int argc, char *argv[])
    {
    // Get file name
    printf("Enter Filename....\n");
    while (gets(file) != NULL) // Loop getting filename while not null
    {
    *nl = strchr( buff, '\n' ); //Search out the null entry

    if ((buf[0] == '\n') // if a null is a 0 then exit
    break;

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

    if( stat( file, &fileattrib ) != 0 ) {
    {
    printf("Access Permissions of file: %s\n ",file);
    printf("----------------------------\n");
    printf( "%d\n", fileattrib.st_mode );
    }
    else
    printf("File Error Message = %s\n", strerror(errno));

    printf("Enter Filename....\n"); // for next filename input
    }
    }

  6. #6
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    There are some syntax errors but it is close.

    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <errno.h> 
    #include <string.h>  // ** don't need this twice
    #include <sys/stat.h> 
    
    #define BUFFER 100 // ** increased - file path can get pretty long
    
    struct stat fileattrib; 
    
    char file[BUFFER] = {'\0'}; char *nl; 
    
    int main() // ** don't need arguments if not used
    { 
        // Get file name 
        printf("Enter Filename....\n"); 
    
        // ** refered to buff instead of file
        while (gets(file) != NULL) // Loop getting filename while not null 
        { 
    	// ** 'nl = ' not '*nl = ' - don't dereference
    	// ** refered to buf instead of file
    	nl = strchr( file, '\n' ); //Search out the null entry 
    
    	if ((file[0] == '\0')) // if a null is a 0 then exit 
    	    break; 
    
    	if ( nl != NULL ) 
    	    *nl = '\0'; // remove the end of line 
    
    	// ** stat returns 0 if file exists
    	if( stat( file, &fileattrib ) == 0 )
    	{ 
    	    printf("Access Permissions of file: %s\n ",
    			file); 
    	    printf("----------------------------\n"); 
    	    printf( "%d\n", fileattrib.st_mode ); 
    	} 
    	else 
    	    printf("File Error Message = %s\n",
    			strerror(errno)); 
    
    	printf("Enter Filename....\n"); // for next filename input 
        } 
        return 0; // ** don't forget the return statement
    }
    David
    Last edited by Strider; 08-16-2001 at 02:46 PM.
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-06-2007, 05:17 AM
  2. Random Number Range Problem.
    By xamlit in forum C Programming
    Replies: 11
    Last Post: 01-26-2006, 12:55 PM
  3. Capturing file stat information
    By Sue Paterniti in forum C Programming
    Replies: 3
    Last Post: 04-15-2002, 06:47 AM
  4. Shortening main
    By pdstatha in forum C Programming
    Replies: 1
    Last Post: 04-03-2002, 04:56 PM
  5. that damn stat()
    By @licomb in forum Linux Programming
    Replies: 2
    Last Post: 08-22-2001, 05:24 PM

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