4.2 inode status
Rick Rowe
rick at wjvax.UUCP
Sat Dec 8 00:52:31 AEST 1984
I have been working on various programs which require going
through directories and getting useful information on inodes.
Here is a short and quick program that does just that. Since I
am relatively new to usenet, please forgive any posting frammishes....
No makefile was required, just try:
cc -o looker looker.c
(`looker' was used for lack of anything better....)
-------------------------- cut here -------------------------------------
.TH LOOKER 1 "5 December 1984"
.SH NAME
looker \- determine file status
.SH SYNOPSIS
.B looker
file ...
.SH DESCRIPTION
.I Looker
performs a series of checks on each argument
in an attempt to get the necessary inode information on the argument.
.I Looker
file information is given on each argument specified. When no argument
exists, information on each file in the current directory is given.
.SH SEE ALSO
stat(2), sys/stat.h, directory(3)
------------------------- cut here -------------------------------------
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <grp.h>
#include <pwd.h>
/*
looker-
Obtain as much info as possible about the file
Specify the file or will default and display information on
each file in the current directory
Author: Richard Rowe Date: 12-5-84
Compile with: cc -o looker looker.c
*/
main(argc,argv)
int argc;
char *argv[];
{
if( argc == 1 )
disdir();
else
while( argc > 1 )
gstatus(argv[--argc]);
}
/* disdir()
opens current directory and
inputs all of the files.
*/
disdir()
{
int i, cnt;
DIR *opendir(), *ffp;
struct direct *readdir(), *dp;
/* open present directory if possible */
if((ffp = opendir(".")) == NULL){
printf("files: can't open directory .\n") ;
return(-1) ; /* error */
}
for(dp=readdir(ffp); dp != NULL; dp=readdir(ffp))
gstatus(dp->d_name);
closedir(ffp);
}
/*
Uses stat to obtain file information from the system
*/
gstatus(name)
char *name;
{
struct group *getgrgid(), *grp;
struct passwd *getpwuid(), *pwd;
char *ctime();
struct stat buf;
if( (stat(name,&buf)) !=0 ) perror("STAT");
pwd = getpwuid(buf.st_uid);
grp = getgrgid(buf.st_gid);
printf("\n\nFilename %s\n",name);
printf("Number of Links to file: %d\n",buf.st_nlink);
printf("Total Size of File: %d\n",buf.st_size);
printf("Optimal blocksize for filesystem i/o ops: %ld\n",buf.st_blksize);
printf("Actual blocksize of File: %ld\n\n",buf.st_blocks);
printf("Group is %s\n",grp->gr_name);
printf("Owner is %s\n\n",pwd->pw_name);
printf("Time file status last changed: %s",ctime(&buf.st_ctime));
printf("Time file data last read: %s",ctime(&buf.st_atime));
printf("Time file data last modified: %s",ctime(&buf.st_mtime));
fileinfo(buf.st_mode);
}
/*
Prints out the file mode status
*/
fileinfo(mode)
u_short mode;
{
printf("File has the following status:\n\n");
switch( mode & S_IFMT ) {
case S_IFDIR:
printf("\t\t\tDirectory\n");
break;
case S_IFCHR:
printf("\t\t\tCharacter Special\n");
break;
case S_IFBLK:
printf("\t\t\tBlock Special\n");
break;
case S_IFREG:
printf("\t\t\tRegular File\n");
break;
case S_IFLNK:
printf("\t\t\tSymbolic Link\n");
break;
case S_IFSOCK:
printf("\t\t\tSocket\n");
break;
default:
printf("FILE TYPE UNKNOWN\n");
}
printf("\t\t\tExecution Mode: %d\n",mode&07777);
}
------------------------- cut here --------------------------------------
More information about the Comp.sources.unix
mailing list