stat - display file or filesystem status

Name
       stat -  Report file details in a form more flexible than ls

Syntax
       stat  [-name]  [-dev]  [-ino]  [-mode]  [-nlink]  [-uid] [-gid] [-size]
       [-atime_dec] [-mtime_dec]  [-ctime_dec]  [-blocks]  [-fmt  date_format]
       [-atime] [-mtime] [-ctime]  file...

       ustat devno...


Description
       (options can appear in any order. Output is tab separated)


Arguments
       filename  -  a  filename, either relative to the current working direc-
       tory, or fully specified.


Options
       -name  The name of the file.


       -Xtime_dec
              Where X is a (access), m (modification)  or  c  (status  change)
              time  expressed as a decimal number. This raw format may be use-
              ful in scripts when monitoring for a change, or comparing.


       -Xtime As above, but formatted as a data. The default format is YY-mon-
              DD:hh:mm:ss.


       -fmt   overrides  the  default date format. The format string is in the
              format given in strftime(3C).


Example
       $ stat -size -ctime_dec -name stat.c
       2601    830998915       stat.c

       $ ustat `stat -dev /var`
       1797    Device number
       86100   Total blocks available to non-super-user
       60152   Number of free files
               Filsys name
               Filsys pack name


See Also
       stat(2), ustat(2), strftime(3C)

Environment
       PATH=$PATH:/home/horton/bin/{HPPA,RISC,sun4m}

       MANPATH=$MANPATH:/home/joppa/fastpac/release_2/doc/60.Manuals

Known Bugs
       None

Author
       David Horton - CiTR

Copyright
       Centre for Information Technology Research, 1996


/**************************************************************************
*
* COPYRIGHT (C) 1996, CiTR
* This document is confidential information.
*
* Produced by: CiTR
*
* Project: NEC ATM LAN (Phase 4)
*
* File: stat.c
*$Source$
*
* Reference:
*
* Description: Report file details in a form more flexible than ls
*
* History:
* 02-May-1996 David & (horton) : created
* 29-May-2001 Add named UID support
*
*$Log$
*
*************************************************************************/

#if !defined(lint)
static char *rcsid = "@(#)$RCSfile$ $Revision$";
#endif

#define _INCLUDE_POSIX_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>

void main (argc, argv)
int argc;
char *argv[]; {
int arg;
int opt;
char *sep;
struct stat buf;
char str[100];
struct tm *tm;
char *fmt = "%y-%b-%d:%H:%M:%S";
time_t now;
int errors = 0;
struct passwd *pwent = 0;
struct group *grent = 0;
int i,o;

if (argc < 3) {
fprintf(stderr,"%s [-name] [-dev] [-ino] [-mode] [-nlink] [-uid] [-gid] [-uid_name] [-gid_name] [-size] [-atime_dec] [-mtime_dec] [-ctime_dec] [-blocks] [-fmt date_format] [-atime] [-mtime] [-ctime] file...\n",
argv[0]);
fprintf(stderr,"(options can appear in any order. Output is tab separated)\n");
}

for (arg = 1; arg < argc; arg ++) {
if (argv[arg][0] == '-') {
if (strcmp(argv[arg], "-fmt") == 0) {
arg ++;
}
continue;
}
if (stat(argv[arg], &buf) != 0) {
perror(argv[arg]);
errors += 1;
continue;
}
for (opt = 1, sep = ""; opt < arg; opt++) {
if (strcmp("-fmt", argv[opt]) == 0) {
opt += 1;
fmt = argv[opt];
}
if(strcmp("-name", argv[opt]) == 0) {
printf("%s%s", sep, argv[arg]);
sep = "\t";
}
if(strcmp("-dev", argv[opt]) == 0) {
printf("%s%u", sep, buf.st_dev);
sep = "\t";
}
if(strcmp("-ino", argv[opt]) == 0) {
printf("%s%d", sep, buf.st_ino);
sep = "\t";
}
if(strcmp("-mode", argv[opt]) == 0) {
printf("%s%o", sep, buf.st_mode);
sep = "\t";
}
if(strcmp("-nlink", argv[opt]) == 0) {
printf("%s%d", sep, buf.st_nlink);
sep = "\t";
}
if(strcmp("-uid", argv[opt]) == 0) {
printf("%s%d", sep, buf.st_uid);
sep = "\t";
}
if(strcmp("-gid", argv[opt]) == 0) {
printf("%s%d", sep, buf.st_gid);
sep = "\t";
}
if(strcmp("-uid_name", argv[opt]) == 0) {
pwent = getpwuid(buf.st_uid);
if (pwent && pwent->pw_name) {
if (pwent->pw_gecos) {
for(i = 0, o=0; pwent->pw_gecos[i]; i++) {
switch(pwent->pw_gecos[i]) {
case 0:
str[o++] = 0;
break;
case ',':
str[o++] = 0;
break;
case '&':
strcpy(&str[o],pwent->pw_name);
o += strlen(pwent->pw_name);
break;
default:
str[o++] = pwent->pw_gecos[i];
break;
}
}
str[o] = 0;
printf("%s%s", sep, str);
} else {
printf("%s%s", sep, pwent->pw_name);
}
sep = "\t";
} else {
printf("%s%d", sep, buf.st_uid);
sep = "\t";
}
}
if(strcmp("-gid_name", argv[opt]) == 0) {
grent = getgrgid(buf.st_gid);
if (grent && grent->gr_name) {
printf("%s%s", sep, grent->gr_name);
sep = "\t";
} else {
printf("%s%d", sep, buf.st_gid);
sep = "\t";
}
}
if(strcmp("-size", argv[opt]) == 0) {
printf("%s%u", sep, buf.st_size);
sep = "\t";
}
if(strcmp("-atime_dec", argv[opt]) == 0) {
printf("%s%u", sep, buf.st_atime);
sep = "\t";
}
if(strcmp("-mtime_dec", argv[opt]) == 0) {
printf("%s%u", sep, buf.st_mtime);
sep = "\t";
}
if(strcmp("-ctime_dec", argv[opt]) == 0) {
printf("%s%u", sep, buf.st_ctime);
sep = "\t";
}
#ifdef unix
if(strcmp("-blocks", argv[opt]) == 0) {
printf("%s%d", sep, buf.st_blocks);
sep = "\t";
}
#endif
if(strcmp("-atime", argv[opt]) == 0) {
now = buf.st_atime;
tm = localtime(&now);
str[0] = 0;
(void) strftime(str, sizeof(str), fmt, tm);
printf("%s%s", sep, str);
sep = "\t";
}
if(strcmp("-mtime", argv[opt]) == 0) {
now = buf.st_mtime;
tm = localtime(&now);
str[0] = 0;
(void) strftime(str, sizeof(str), fmt, tm);
printf("%s%s", sep, str);
sep = "\t";
}
if(strcmp("-ctime", argv[opt]) == 0) {
now = buf.st_ctime;
tm = localtime(&now);
str[0] = 0;
(void) strftime(str, sizeof(str), fmt, tm);
printf("%s%s", sep, str);
sep = "\t";
}
}
printf("\n");
}
exit (errors);
}



/* ustat - get file system statistics
Author: David Horton
*/
#define _INCLUDE_POSIX_SOURCE
#define _INCLUDE_XOPEN_SOURCE
#define _INCLUDE_HPUX_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ustat.h>
#include <math.h>

#ifdef sun
#define strtoul strtol
#endif

main(argc, argv)
int argc; char *argv[]; {

dev_t dev;
int ret;
struct ustat buf;
int arg;
char ebuf[100];

for (arg = 1; arg < argc; arg ++) {
dev = strtoul(argv[arg], NULL, 10);
ret = ustat(dev, &buf);
if (ret != 0) {
sprintf(ebuf,"%d <- ustat(devno=%u)", ret, dev);
perror(ebuf);
continue;
}
printf("%u\tDevice number\n", dev);
printf("%d\tTotal blocks available to non-super-user \n",buf.f_tfree);
printf("%d\tNumber of free files\n",buf.f_tinode);
printf("%s\tFilsys name\n", buf.f_fname);
printf("%s\tFilsys pack name \n", buf.f_fpack);
#ifdef __hpux
printf("%d\tBlock Size\n", buf.f_blksize);
#endif
}
}