fp.c

Murat Arslan (arslanm@gate.marketweb.net.tr)
Mon, 31 Aug 1998 14:12:33 +0300 (EEST)


http://piranha.marketweb.net.tr/fp.c.gz:

UmarIm iSe yarar...

----------------------- 8< -----------------------
/*
* fp.c by Murat Arslan (arslanm@linux.org.tr)
* prints some information about the input file.
*
* $Id: fp.c,v 1.0 1998/08/31 13:42:20 arslanm Exp arslanm $
*
* ---------------------------------------------------------
* user> gcc -Wall -o fp fp.c
* user> su
* root's Password:
* root#> cp fp /usr/local/bin
* root#> chown root.root /usr/local/bin/fp
* root#> chmod 4755 /usr/local/bin/fp
* ---------------------------------------------------------
*
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/sysmacros.h>

struct passwd *getpwuid(uid_t);
struct group *getgrgid(gid_t);
int turkish=0;
int chr=0;

char *howlong(unsigned long old, unsigned long now) {
unsigned long diff;
static char diffbuf[100];
int sec,min,hour,day,year;
char buf[20];
char *ago;
sec = 0;
min = 0;
hour = 0;
day = 0;
year = 0;
diff = now - old;

bzero(diffbuf, sizeof(diffbuf));
if(turkish)
ago = "once";
else
ago = "ago";
while(1) {
if(diff < 60) {
sec = sec + diff;
break;
} else if(diff >= 60 &&
diff < 3600) {
unsigned long t;
unsigned long e;
e = diff/60;
min+=e;
t = diff - (e * 60);
diff = t;
continue;
} else if(diff >= 3600 &&
diff < 86400) {
unsigned long t;
unsigned long e;
e = diff/3600;
hour+=e;
t = diff - (e * 3600);
diff = t;
continue;
}
if(diff >= 86400 &&
diff < 31536000) {
unsigned long t;
unsigned long e;
e = diff/86400;
day+=e;
t = diff - (e * 86400);
diff = t;
continue;
} else if(diff >= 31536000) {
unsigned long t;
unsigned long e;
e=diff/32140800;
year+=e;
t = diff - (e * 32140800 );
diff = t;
continue;
}
}
if(year) {
sprintf(buf, "%d %s%s ", year, turkish ? "yil" : "year",
(year>1 && !turkish) ? "s" : "");
strcat(diffbuf, buf);
}
if(day) {
sprintf(buf, "%d %s%s ", day, turkish ? "gun" : "day",
(day>1 && !turkish) ? "s" : "");
strcat(diffbuf, buf);
}
if(hour) {
sprintf(buf, "%d %s%s ", hour, turkish ? "saat" : "hour",
(hour>1 && !turkish) ? "s" : "");
strcat(diffbuf, buf);
}
if(min) {
sprintf(buf, "%d %s%s ", min, turkish ? "dk" : "min",
(min>1 && !turkish) ? "s" : "");
strcat(diffbuf, buf);
}
if(!year && !day && !hour && !min && !sec) {
if(turkish)
sprintf(buf, "HEMEN SIMDI");
else
sprintf(buf, "RIGHT NOW");
} else
sprintf(buf, "%d %s%s %s", sec, turkish>0 ? "sn" : "sec",
(sec>1 && !turkish) ? "s" : "", ago);
strcat(diffbuf, buf);
return(diffbuf);
}
char *zaman(unsigned long num) {
time_t tf;
struct tm *t;
static char timebuf[50];
tf = num;
t = localtime(&tf);
if(turkish)
strftime(timebuf, 50, "%d %b %a %H:%M:%S %Y", t);
else
strftime(timebuf, 50, "%b %a %d %H:%M:%S %Y", t);
if(turkish) {
char day[4];
char month[4];
day[0] = timebuf[7];
day[1] = timebuf[8];
day[2] = timebuf[9];
day[3] = '\0';
month[0] = timebuf[3];
month[1] = timebuf[4];
month[2] = timebuf[5];
month[3] = '\0';
if(!strcmp(day, "Mon")) {
timebuf[7] = 'P'; timebuf[8] = 'z'; timebuf[9] = 't';
} else if(!strcmp(day, "Tue")) {
timebuf[7] = 'S'; timebuf[8] = 'a'; timebuf[9] = 'l';
} else if(!strcmp(day, "Wed")) {
timebuf[7] = 'C'; timebuf[8] = 'a'; timebuf[9] = 'r';
} else if(!strcmp(day, "Thu")) {
timebuf[7] = 'P'; timebuf[8] = 'e'; timebuf[9] = 'r';
} else if(!strcmp(day, "Fri")) {
timebuf[7] = 'C'; timebuf[8] = 'u'; timebuf[9] = 'm';
} else if(!strcmp(day, "Sat")) {
timebuf[7] = 'C'; timebuf[8] = 't'; timebuf[9] = 's';
} else {
timebuf[7] = 'P'; timebuf[8] = 'a'; timebuf[9] = 'z';
}
if(!strcmp(month, "Jan")) {
timebuf[3] = 'O'; timebuf[4] = 'c'; timebuf[5] = 'a';
} else if(!strcmp(month, "Feb")) {
timebuf[3] = 'S'; timebuf[4] = 'u'; timebuf[5] = 'b';
} else if(!strcmp(month, "Mar")) {
timebuf[3] = 'M'; timebuf[4] = 'a'; timebuf[5] = 'r';
} else if(!strcmp(month, "Apr")) {
timebuf[3] = 'N'; timebuf[4] = 'i'; timebuf[5] = 's';
} else if(!strcmp(month, "Jun")) {
timebuf[3] = 'H'; timebuf[4] = 'a'; timebuf[5] = 'z';
} else if(!strcmp(month, "Jul")) {
timebuf[3] = 'T'; timebuf[4] = 'e'; timebuf[5] = 'm';
} else if(!strcmp(month, "Aug")) {
timebuf[3] = 'A'; timebuf[4] = 'g'; timebuf[5] = 'u';
} else if(!strcmp(month, "Sep")) {
timebuf[3] = 'E'; timebuf[4] = 'y'; timebuf[5] = 'l';
} else if(!strcmp(month, "Oct")) {
timebuf[3] = 'E'; timebuf[4] = 'k'; timebuf[5] = 'i';
} else if(!strcmp(month, "Nov")) {
timebuf[3] = 'K'; timebuf[4] = 'a'; timebuf[5] = 's';
} else {
timebuf[3] = 'A'; timebuf[4] = 'r'; timebuf[5] = 'a';
}
}
return(timebuf);
}

void usage(char *prg) {
printf("usage: %s\t[-t] filename\n", prg);
printf("kullanim: %s\t[-t] dosya\n", prg);
exit(1);
}
void err(char *prg) {
printf("fp: Invalid argument\n");
usage(prg);
}
void banner(char *prg) {
printf("fp v1.0, prints some information about the input file.\n");
printf("fp s1.0, girilen dosya hakkinda bir takim bilgiler verir.\n");
usage(prg);
}
int main(int argc, char *argv[]) {
struct stat *st,*st1;
struct passwd *pwd;
struct group *grp;
char *fname,permbuf[10],modebuf[5],username[20],groupname[20];
int perm,sym=0;
uid_t uid,myuin,t;
gid_t gid;
time_t tf;
if((t = getuid()) != 0)
if((myuin = geteuid()) != 0) {
printf("error: uid=%d\teuid=%d\n",t,myuin);
printf("fp needs to be run as root.\n");
printf("enter the following commands as root:\n");
printf("chown root.root %s ; chmod 4755 %s\"\n", argv[0], argv[0]);
exit(1);
}
if(argc < 2)
banner(argv[0]);
if(argv[1][0] == '-') {
if(argv[1][1] == 't')
turkish=1;
else if(argv[1][1] == 'h')
usage(argv[0]);
else
err(argv[0]);
if(!argv[2]) {
printf("Need an argument.\n");
usage(argv[0]);
} else
fname = argv[2];
} else
fname=argv[1];
st1 = malloc(sizeof(st1));
if(lstat(fname,st1) <0) {
perror("stat()");
exit(1);
} else {
gid = st1->st_gid;
uid = st1->st_uid;
}
free(st1);
pwd = malloc(sizeof(pwd));
pwd = getpwuid(uid);
if(pwd != NULL)
strncpy(username, pwd->pw_name, sizeof(username));
else {
if(turkish)
strcpy(username, "hata");
else
strcpy(username, "error");
}
grp = malloc(sizeof(grp));
grp = getgrgid(gid);
if(grp != NULL)
strncpy(groupname, grp->gr_name, sizeof(groupname));
else {
if(turkish)
strcpy(groupname, "hata");
else
strcpy(groupname, "error");
}
st = malloc(sizeof(st));
if (lstat(fname, st) < 0) {
perror("stat()");
exit(1);
} else {
if(turkish)
printf(" Dosya: \"%s\"\n", fname);
else
printf(" File: \"%s\"\n", fname);
if(turkish)
printf(" Boyut: %lu", st->st_size);
else
printf(" Size: %lu", st->st_size);
if(turkish)
printf(" Dosya tipi: ");
else
printf(" Filetype: ");
if(S_ISDIR(st->st_mode)) {
if(turkish)
printf("Directory\n");
else
printf("Dizin\n");
permbuf[0] = 'd';
}

if(S_ISLNK(st->st_mode)) {
char readbuf[255];
if(readlink(fname, readbuf, sizeof(readbuf)) != -1) {
if(turkish)
printf("%s -> %s\n", fname,readbuf);
else
printf("%s -> %s\n",fname, readbuf);
} else {
if(turkish)
printf("Sembolik Link\n");
else
printf("Symbolic Link\n");
}
strcpy(permbuf, "lrwxrwxrwx");
sym=1;
}
if(S_ISREG(st->st_mode)) {
if(turkish)
printf("Normal Dosya\n");
else
printf("Regular File\n");
permbuf[0] = '-';
}
if(S_ISCHR(st->st_mode)) {
if(turkish)
printf("Karakter Aygiti\n");
else
printf("Character Device\n");
permbuf[0] = 'c';
chr=1;
}
if(S_ISBLK(st->st_mode)) {
if(turkish)
printf("Blok Aygiti\n");
else
printf("Block Device\n");
permbuf[0] = 'b';
chr=1;
}
if(S_ISFIFO(st->st_mode)) {
printf("Fifo\n");
permbuf[0] = 'p';
}
if(S_ISSOCK(st->st_mode)) {
if(turkish)
printf("Soket\n");
else
printf("Socket\n");
permbuf[0] = 's';
}
perm=0;
if(st->st_mode & S_IRUSR) {
perm+=400;
if(!sym)
permbuf[1] = 'r';
} else permbuf[1] = '-';
if(st->st_mode & S_IWUSR) {
perm+=200;
if(!sym)
permbuf[2] = 'w';
} else permbuf[2] = '-';
if(st->st_mode & S_IXUSR) {
perm+=100;
if(!sym)
permbuf[3] = 'x';
} else permbuf[3] = '-';
if(st->st_mode & S_IRGRP) {
perm+=40;
if(!sym)
permbuf[4] = 'r';
} else permbuf[4] = '-';
if(st->st_mode & S_IWGRP) {
perm+=20;
if(!sym)
permbuf[5] = 'w';
} else permbuf[5] = '-';
if(st->st_mode & S_IXGRP) {
perm+=10;
if(!sym)
permbuf[6] = 'x';
} else permbuf[6] = '-';
if(st->st_mode & S_IROTH) {
perm+=4;
if(!sym)
permbuf[7] = 'r';
} else permbuf[7] = '-';
if(st->st_mode & S_IWOTH) {
perm+=2;
if(!sym)
permbuf[8] = 'w';
} else permbuf[8] = '-';
if(st->st_mode & S_IXOTH) {
perm+=1;
if(!sym)
permbuf[9] = 'x';
} else permbuf[9] = '-';
if(st->st_mode & S_ISUID) {
perm+=4000;
if(!sym)
permbuf[3] = 's';
}
if(st->st_mode & S_ISGID) {
perm+=2000;
if(!sym)
permbuf[6] = 's';
}
if(st->st_mode & S_ISVTX) {
perm+=1000;
if(!sym)
permbuf[9] = 't';
}
permbuf[10] = '\0';
if(perm < 1000)
sprintf(modebuf, "0%d", perm);
else
sprintf(modebuf, "%4d", perm);
if(turkish)
printf(" Kip: (%s/%10s) ", modebuf, permbuf);
else
printf(" Mode: (%s/%10s) ", modebuf,permbuf);
printf(" UID: (%d/%s)", st->st_uid,username);
printf(" GID: (%d/%s)\n", st->st_gid,groupname);
if(turkish)
printf(" Aygit: %ld:%ld",
(long) major(st->st_dev),
(long) minor(st->st_dev));
else
printf("Device: %ld:%ld",
(long) major(st->st_dev),
(long) minor(st->st_dev));
if(turkish)
printf(" Inot: %lu", st->st_ino);
else
printf(" Inode: %lu", st->st_ino);
if(turkish)
printf(" Link: %d", st->st_nlink);
else
printf(" Links: %d", st->st_nlink);
if(chr) {
if(turkish)
printf(" Aygit Tipi: %ld:%ld\n",
(long) major(st->st_rdev),
(long) minor(st->st_rdev));
else
printf(" Device type: %ld:%ld\n",
(long) major(st->st_rdev),
(long) minor(st->st_rdev));
} else
printf("\n");
tf = time(NULL);
if(turkish) {
printf(" Erisim: %s (%s)\n", zaman(st->st_atime),
howlong(st->st_atime,tf));
printf(" Modife: %s (%s)\n", zaman(st->st_mtime),
howlong(st->st_mtime,tf));
printf("Degisim: %s (%s)\n", zaman(st->st_ctime),
howlong(st->st_ctime,tf));
} else {
printf("Access:\t%s (%s)\n", zaman(st->st_atime),
howlong(st->st_atime,tf));
printf("Modify:\t%s (%s)\n", zaman(st->st_mtime),
howlong(st->st_mtime,tf));
printf("Change:\t%s (%s)\n", zaman(st->st_ctime),
howlong(st->st_ctime,tf));
}
}
return(0);
}

----------------------- 8< -----------------------
__
Murat Arslan