Thread: stat()

  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 05: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 01: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, 04: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, 05: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, 04:24 PM