PDA

View Full Version : stat()



Nick
08-13-2001, 05:40 PM
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

Strider
08-14-2001, 05:42 AM
The man page for stat() lists the prototype and how to use it.


// 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:


#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

Nick
08-14-2001, 01:11 PM
It's not really me?

@licomb
08-14-2001, 07:57 PM
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

@licomb
08-15-2001, 08:42 PM
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
}
}

Strider
08-16-2001, 01:09 PM
There are some syntax errors but it is close.



#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