LINUX TIPS AND TRICKS --- July 27, 2001
Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
_________________________________________________________________

Calling a File's Information
By Danny Kalev

An inode is a data structure that contains information about a 
file. Linux provides three syscalls for obtaining this 
information. The retrieved information is stored in a struct 
called stat. The syscalls and the stat struct are declared in 
the <sys/stat.h> header.

The first of these three syscalls is stat(), which has the 
following prototype:

    int stat(const char * pathname, struct stat *info);

It takes a pathname as the first argument and a pointer to an 
empty stat struct, which is, in turn, filled with the file's 
information. A zero return value indicates success, whereas a 
nonzero value indicates an error. If the pathname is a symbolic 
link (symlink), then stat() follows it.

The second syscall, lstat(), has the following prototype:

    int lstat(const char * pathname, struct stat *info);

lstat() is similar to stat() except that it doesn't follow 
symlinks. Thus, you can use it to determine whether a file name 
is a symlink.

Finally, the fstat() function takes a file descriptor, rather 
than a name, as its first argument. fd must be an open file's 
descriptor. Fstat () has the following prototype: 

    int fstat(int fd, struct stat *info);

The struct stat contains the following fields:

    st_dev -- A device number on which the file resides.
    st_ino -- A file's on-disk inode number.
    st_mode -- A file's mode.
    st_nlink -- The number of pathnames that reference this inode.
    st_uid -- The file owner's UID.
    st_gid -- A file's group.
    st_rdev -- For a character or block device, a device's major and 
               minor numbers.
    st_size -- A file's size in bytes. 
    st_blksize -- The block size of the file system that stores the 
                  file.
    st_blocks -- The number of blocks allocated for the file.
    st_atime -- Most recent file access time (i.e., the last time it 
                was opened).
    st_mtime -- Most recent modification time. Updated whenever the 
                file's data is changed.
    st_ctime -- Most recent metadata modification (e.g., an owner or 
                group change).

The following program retrieves the size of a file whose name is
passed as a command line argument and prints it on the screen:

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

    int main(int argc, char ** argv)
    {
     struct stat info;
     if(argc<1)
     {
      printf("insufficient number of command line arguments\n");
      exit(1);
     }
     if (stat(argv[1], &info))
     {
      printf("stat() error, exit forced");
      strerror(errno);
      exit(1);
     }
     printf("size of file %s is: %d /n", argv[1], info.st_size);
     return 0;
    }

About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with more 
than 10 years of experience, specializing in C++ and 
object-oriented analysis and design on various platforms 
including VMS, DOS, Windows, Unix, and Linux. His technical 
interests involve code optimization, networking, and distributed
computing. He is also a member of the ANSI C++ standardization 
committee and the author of ANSI/ISO C++ Professional 
Programmer's Handbook (Que, 1999). Danny can be reached at 
linuxnl@excite.com.
_________________________________________________________________

ADDITIONAL RESOURCES

Syscall specifications of Linux
http://www.infran.ru/TechInfo/syscalls/

Linux Specific System Calls
http://www.adventnet.com/products/v5products/stack/docs/
port_gd/system_contents.html


Linux/i386 system calls
http://www.linuxassembly.org/syscall.html
_________________________________________________________________

CUSTOMER SERVICE

SUBSCRIBE/UNSUBSCRIBE:
- Go to: http://reg.itworld.com/cgi-bin/subcontent12.cgi
- Enter your email address under "Current subscriber" to log in
- Uncheck the box next to the newsletter you want to
  unsubscribe from
- Or check the box next to the newsletter you want to
  subscribe to
- Submit

If you have questions, please send email to customer service at:
mailto:support@itworld.com
_________________________________________________________________

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate
  Editor, Newsletters at: andrew_santosusso@itworld.com

* For advertising information, write Dan Chupka, Account
  Executive at: dan_chupka@itworld.com

* For recruitment advertising information, write Jamie Swartz,
  Eastern Regional Sales Manager at: jamie_swartz@itworld.com
  or Paul Duthie, Western Regional Sales Manager at:
  paul_duthie@itworld.com

* For all other inquiries, write Jodie Naze, Product Manager, 
  Newsletters at: jodie_naze@itworld.com
_________________________________________________________________

PRIVACY POLICY

http://www.itworld.com/Privacy/

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
