/*
	 * finfo 0.1
	 * Copyright (c) 2001m Christine Maxwell
	 * All rights reserved.
	 *
	 * Redistribution and use in source and binary forms, with or without
	 * modification, are permitted provided that the following conditions
	 * are met:
	 * 1. Redistributions of source code must retain the above copyright
	 *    notice, this list of conditions and the following disclaimer.
	 * 2. Redistributions in binary form must reproduce the above copyright
	 *    notice, this list of conditions and the following disclaimer in the
	 *    documentation and/or other materials provided with the distribution.
	 *
	 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
	 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
	 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
	 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
	 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
	 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
	 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
	 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
	 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
	 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
	 * SUCH DAMAGE.
	 *
*/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/param.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

extern int errno;

int main(int argc, char **argv)
{
	 int x;
	 char fmode[12], fpath[MAXPATHLEN], *a;
	 struct stat finfo;

	 if(argc != 2 && argc !=3)
	 {
			printf("\nFinfo v0.1\n");
			printf("Usage : finfo [-l] file\n\n");
			exit(0);
	 }

	 if(argc==3) /* Be lazy, assume second argument is -l.  Get info on the link */
	 {
			x=lstat(argv[2], &finfo);
			a=realpath(argv[2],fpath);
	 }
	 else /* The second argument is a filename. */
	 {
			x=stat(argv[1], &finfo);
			a=realpath(argv[1], fpath);
	 }
	 if(x || !a)
	 {
			perror("finfo ");
			exit(errno);
	 }

	 strmode(finfo.st_mode,fmode);

	 printf("File  : %s\n",fpath);
	 printf("Type  : ");
	 switch(fmode[0])
	 {
			case           '-':
				 printf("Regular File\n");
				 break;
			case           'b':
				 printf("Block Special\n");
				 break;
			case           'c':
				 printf("Character Special\n");
				 break;
			case           'd':
				 printf("Directory\n");
				 break;
			case           'l':
				 printf("Symbolic Link\n");
				 break;
			case           'p':
				 printf("Fifo\n");
				 break;
			case           's':
				 printf("Socket\n");
				 break;
			case          'w':
				 printf("Whiteout\n");
				 break;
			case           '?':
			default:
				 printf("Unknown Inode Type");
				 break;
	 }
	 printf("Inode : %d\n", finfo.st_ino);
	 printf("Modes : %s\n", &fmode[1]);
	 a=fflagstostr(finfo.st_flags);
	 if(a && strlen(a) >=1 )
			printf("Flags : %s\n", a);
	 printf("Links : %d\n", finfo.st_nlink);
	 printf("Size  : %qd Bytes (%.2fK) (%.2fM)\n", finfo.st_size, (float)finfo.st_size/1024, (float)finfo.st_size/1000000);
	 printf("Blocks: %qd\n",finfo.st_blocks);
	 printf("Mt Ma : %03d\n", major(finfo.st_dev));
	 printf("Mt Mi : 0x%08x\n", minor(finfo.st_dev));
	 printf("IO BS : %d\n",finfo.st_blksize);
	 if (S_ISCHR(finfo.st_mode) || S_ISBLK(finfo.st_mode))
	 {
			if (minor(finfo.st_rdev) > 255 || minor(finfo.st_rdev) < 0)
			{
				 printf("Major : %03d\n", major(finfo.st_rdev));
				 printf("Minor : 0x%08x\n", minor(finfo.st_rdev));
			}
			else
			{
				 printf("Major : %03d\n", major(finfo.st_rdev));
				 printf("Minor : %03d\n", minor(finfo.st_rdev));
			}
	 }
	 printf("User  : %s (%d)\n", user_from_uid(finfo.st_uid, 0), finfo.st_uid);
	 printf("Group : %s (%d)\n", group_from_gid(finfo.st_gid, 0), finfo.st_gid);
	 printf("Last Accessed  : %s", ctime(&finfo.st_atime));
	 printf("Last Modifed   : %s", ctime(&finfo.st_mtime));
	 printf("Status Changed : %s", ctime(&finfo.st_ctime));
	 exit(0);
}



syntax highlighted by Code2HTML, v. 0.9.1