logininit: login information for /etc/profile (source)

Lenny Tropiano lenny at icus.UUCP
Wed Jun 1 13:15:01 AEST 1988


Here's a small utility program that I use here for login information...

Enjoy,
Lenny

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  Makefile README chtime.c logininit.c
# Wrapped by lenny at icus on Tue May 31 23:13:14 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f Makefile -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"Makefile\"
else
echo shar: Extracting \"Makefile\" \(633 characters\)
sed "s/^X//" >Makefile <<'END_OF_Makefile'
X#
X# Makefile to compile logininit.c  (Login Information)
X# By Lenny Tropiano
X# (c)1988 ICUS Computer Group     UUCP:  ...icus!lenny
X#
XCFLAGS=-v -O -DMAXPERCENT=10 -DMAXINODES=500
XLDFLAGS=-s
XLIBS=/lib/crt0s.o /lib/shlib.ifile
XDEST=/usr/lbin/
X#
Xlogininit:  logininit.o chtime.o
X	@echo "Loading ..."
X	$(LD) $(LDFLAGS) -o logininit logininit.o chtime.o $(LIBS) 
X#
Xlogininit.o:
X	$(CC) $(CFLAGS) -c logininit.c
X#
X# You need to be super-user to do this.
X#
X$(DEST):
X	mkdir $(DEST)
X	chmod 755 $(DEST)
X#
Xinstall: logininit $(DEST)
X	cp logininit $(DEST)
X	chown root $(DEST)/logininit
X	chgrp bin  $(DEST)/logininit
X	chmod 4755 $(DEST)/logininit
END_OF_Makefile
if test 633 -ne `wc -c <Makefile`; then
    echo shar: \"Makefile\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f README -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"README\"
else
echo shar: Extracting \"README\" \(2491 characters\)
sed "s/^X//" >README <<'END_OF_README'
X
XThis is a little ditty I whipped up one day... Basically it tells you all
Xkinds of neat stuff when you login.  It can be called from inside 
X/etc/profile and will print this kind of information...
X---
XIt's Tuesday, May 31, 1988 at 8:00pm ... 
XNode: icus, up for 22 hours, 38 minutes, and 27 seconds.
X21% available disk storage in the root filesystem. (26968 blocks, 8692 i-nodes)
X1.29MB of main memory currently available. (48%)
X
XNo mail.
X---
XIt's Tuesday, May 31, 1988 at 11:02pm ... 
XNode: icus, up for 1 day, 2 hours, 36 minutes, and 1 second.
X21% available disk storage in the root filesystem. (24968 blocks, 8777 i-nodes)
X1.45MB of main memory currently available. (54%)
X
X       **************************************************************
X       * This is my message of the day.  It can say whatever I like *
X       **************************************************************
X
XYou have mail! (13 mail messages in your mailbox)
X
X---
XIt will tell you when you have mail (and how many mail messages you have). It
Xprints the /etc/motd out so the call to "cat /etc/motd" can be removed from
X/etc/profile.
X
XAnother nice feature is that it monitors the space left on the hard disk
Xwhen you login.  It checks the percentage (%) left to MAXPERCENT (compile
Xtime option [defaults to 10%]) and the inodes left with MAXINODES [defaults
Xto 500].
X
XSnipped from /etc/profile
X
X...
X# Print login initialization information
X/usr/lbin/logininit
X
X#The following line (if uncommented) will set TERM and TERMCAP (see ttytype)
X#eval `tset -s -Q`
X#For now, just set TERM to s4
XNAME=`tty`
Xif [ \( "`expr $NAME : '/dev/w' `"      != "0" -o \
X	"`expr $NAME : '/dev/sys' `"    != "0" \) ]
Xthen
X...
X
Xlogininit is called before the question to ask what terminal you are
Xrunning on...  It can be put almost anywhere... Even in your /etc/localprofile
Xor wherever.   Remember to take out redundant processing from /etc/profile
Xlike the "if..fi" statement to check the mailbox status, and the 
X"cat /etc/motd"
X
XEnjoy,
XLenny
X---
XUS MAIL  : Lenny Tropiano, ICUS Computer Group        IIIII  CCC U   U  SSS
X           PO Box 1                                     I   C    U   U S
X           Islip Terrace, New York  11752               I   C    U   U  SS 
XPHONE    : (516) 968-8576 [H] (516) 582-5525 [W]        I   C    U   U    S
XTELEX    : 154232428 [ICUS]                           IIIII  CCC  UUU  SSS 
XAT&T MAIL: ...attmail!icus!lenny  
XUUCP     : ...{mtune, ihnp4, boulder, talcott, sbcs, bc-cis}!icus!lenny 
END_OF_README
if test 2491 -ne `wc -c <README`; then
    echo shar: \"README\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f chtime.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"chtime.c\"
else
echo shar: Extracting \"chtime.c\" \(1648 characters\)
sed "s/^X//" >chtime.c <<'END_OF_chtime.c'
X/*
X * ch_time - format elapsed time into a character string.
X *
X * Mikel Manitius - 85-01-08 - (mikel at codas.att.com.uucp)
X * Lenny Tropiano - 88-04-14 - (lenny at icus.UUCP) - modified slightly
X *
X */
X
X#define			NULL		(0)
X#define			SECOND		1L
X#define			MINUTE		60L
X#define			HOUR		(60L * 60L)
X#define			DAY		(24L * 60L * 60L)
X#define			WEEK		(7L  * 24L * 60L * 60L)
X#define			BUFSIZE		1024
X
Xchar	*malloc();
X
Xchar	*day[] =
X{
X	"Sunday", "Monday", "Tuesday",
X	"Wednesday", "Thursday", "Friday",
X	"Saturday", 0
X};
X
Xchar	*mon[] =
X{
X	"January", "February", "March", "April", "May",
X	"June", "July", "August", "September", "October",
X	"November", "December", 0
X};
X
X
Xchar *ch_time(sec)
Xint	sec;
X{
X	unsigned long	hrs = 0L;
X	unsigned long	days = 0L;
X	unsigned long	mins = 0L;
X	unsigned long	weeks = 0L;
X	char	*buff;
X
X	buff = malloc(BUFSIZE);
X	if(buff == NULL)
X		return(NULL);
X	weeks = sec / WEEK;
X	sec -= WEEK * weeks;
X	days = sec / DAY;
X	sec -= DAY * days;
X	hrs = sec / HOUR;
X	sec -= HOUR * hrs;
X	mins = sec / MINUTE;
X	sec -= MINUTE * mins;
X
X	if(weeks)
X		sprintf(buff, "%d week%s, ", weeks, (weeks == 1) ? "" : "s");
X	if(days)
X		sprintf(buff, "%s%d day%s, ", (weeks) ? buff : "",
X			days, (days == 1L) ? "" : "s");
X	if(hrs || days || weeks)
X		sprintf(buff, "%s%d hour%s, ", (days || weeks) ? buff : "",
X			hrs, (hrs == 1L) ? "" : "s");
X	if(mins || hrs || days || weeks)
X		sprintf(buff, "%s%d minute%s, ",
X			(hrs || days || weeks) ? buff : "",
X			mins, (mins == 1L) ? "" : "s");
X	sprintf(buff, "%s%s%d second%s.",
X			(mins || hrs || days || weeks) ? buff : "",
X			(mins || hrs || days || weeks) ? "and " : "",
X			sec, (sec == 1L) ? "" : "s");
X	return(buff);
X}
END_OF_chtime.c
if test 1648 -ne `wc -c <chtime.c`; then
    echo shar: \"chtime.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f logininit.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"logininit.c\"
else
echo shar: Extracting \"logininit.c\" \(6088 characters\)
sed "s/^X//" >logininit.c <<'END_OF_logininit.c'
X/************************************************************************\
X**                                                                      **
X** Program name: logininit.c (Login Initialization)                     **
X** Programmer:   Lenny Tropiano            UUCP: ...icus!lenny          **
X** Organization: ICUS Computer Group       (c)1988                      **
X** Date:         April 3, 1988                                          **
X**                                                                      **
X**************************************************************************
X**                                                                      **
X** Program use:  Program is run as information apon login called from   **
X**               the /etc/profile. This program the filesystem          **
X**               information, available main memory and mail messages.  **
X**                                                                      **
X**   This programs must be setuid root (ie. 4755) and can be placed     **
X**   in any directory you choose.                                       **
X**                                                                      **
X**************************************************************************
X** Permission is granted to distribute this program by any means as     **
X** long as credit is given for my work.     I would also like to see    **
X** any modifications or enhancements to this program.                   **
X\************************************************************************/
X
X#include <stdio.h>
X#include <fcntl.h>
X#include <errno.h>
X#include <signal.h>
X#include <sys/types.h>
X#include <sys/filsys.h>
X#include <sys/param.h>
X#include <sys/utsname.h>
X#include <utmp.h>
X#include <time.h>
X#include <nlist.h>
X
X#ifndef ctob			
X#include <sys/sysmacros.h>
X#endif
X
X#define UNIX	"/unix"
X#define KMEM	"/dev/kmem"
X
Xstruct nlist unixsym[] = {
X#define X_MAXMEM	0
X	{ "maxmem" },
X#define X_FREEMEM	1
X	{ "freemem" },
X	{ NULL }
X};
X
X#define	FILESYS	"/dev/rfp002"
X#define MEGABYTE 1048576.0
X
X#ifndef MAXPERCENT
X# define MAXPERCENT	10
X#endif
X
X#ifndef MAXINODES
X# define MAXINODES	500
X#endif
X
Xchar	*progname;			/* program name			*/
Xchar	mailfile[30];			/* mailbox file name            */
Xstruct  filsys fs;			/* Filesystem superblock struct */
Xstruct	tm	*today, *localtime();
X
X/************************************************************************/
X
Xmain(argc,argv)
Xint	argc;
Xchar	*argv[];
X{
X	char	*getenv();
X
X    	progname = *argv;
X	sprintf(mailfile,"%s",getenv("MAIL"));
X
X	printf("\n");
X
X	showtime();		/* what time is it?    */
X	uptime();		/* get system uptime   */
X	filestatus();		/* get filesystem info */
X	memory();		/* get current memory  */
X	show_motd();		/* print the /etc/motd */
X	mailcheck();		/* get mailbox status  */
X
X	printf("\n");
X	exit(0);
X}
X
Xfilestatus()
X{
X	int	fd, percleft;
X
X	if ((fd = open(FILESYS, O_RDONLY)) == -1) {
X		fprintf(stderr,"%s: cannot open %s for read\n",
X			progname, FILESYS);
X		exit(1);
X	}
X	
X	if (lseek(fd, 512, 0) == -1) {
X		fprintf(stderr,"%s: cannot lseek to superblock\n", progname);
X		exit(1);
X	}
X
X	if (read(fd, &fs, sizeof(struct filsys)) == -1) {
X		fprintf(stderr,"%s: cannot read the superblock\n", progname);
X		exit(1);
X	}
X
X	close(fd);
X
X	percleft = (int)((((float)fs.s_tfree / (float)fs.s_fsize) + 0.005) 
X			* 100.0);
X	printf("%2d%% available disk storage in the root filesystem. (%d blocks, %d i-nodes)\n",
X		percleft, fs.s_tfree * 2, fs.s_tinode);
X
X	if (percleft < MAXPERCENT) 
X		printf("    WARNING: Disk space is low.  Please clean up unnecessary storage.\007\n");
X
X	if (fs.s_tinode < MAXINODES) 
X		printf("    WARNING: I-nodes are low. Please archive your files.\007\n");
X
X}
X
Xmailcheck()
X{
X	FILE	*fp;
X	char	buffer[BUFSIZ], *strrchr();
X	int	msgs = 0, forward = 0;
X
X	if (access(mailfile,4) == 0) {
X		msgs = 0;
X		if ((fp = fopen(mailfile,"r")) != NULL) {
X			while (fgets(buffer, BUFSIZ, fp) != NULL) {
X				if (strncmp(buffer,"Forward",7) == 0) {
X					forward = 1;
X					break;
X				}
X				if (strncmp(buffer,"From ",5) == 0)
X					msgs++;
X			}
X		}
X		fclose(fp);
X	} else  msgs = 0;
X
X	if (forward)
X		printf("\nYour mail is being forwarded to%s\n",
X			strrchr(buffer,' '));
X	else if (msgs != 0)
X		printf("You have mail! (%d mail message%s in your mailbox)\007\n",
X			msgs, (msgs == 1) ? "" : "s");
X	else
X		printf("No mail.\n");
X}
X
X
Xmemory()
X{
X	int	kmem;
X	int	maxmem, freemem;
X	float	megs, max, free, perc;
X
X	if (nlist(UNIX, unixsym) < 0) {
X		fprintf(stderr, "%s: no namelist.\n", UNIX);
X		exit(1);
X	}
X
X	if ((kmem = open(KMEM, 0)) < 0) {
X		perror(KMEM);
X		exit(1);
X	}
X
X	lseek(kmem, (long) unixsym[X_MAXMEM].n_value, 0);
X	read(kmem, (char *) &maxmem, sizeof(int));
X	lseek(kmem, (long) unixsym[X_FREEMEM].n_value, 0);
X	read(kmem, (char *) &freemem, sizeof(int));
X
X	close(kmem);
X
X	free = ctob(freemem);
X	max  = ctob(maxmem);
X	perc = free / max;
X	perc *= 100.0;
X
X	printf("%4.2fMB of main memory currently available. (%-2.0f%%)\n",
X		(ctob(freemem) / MEGABYTE), perc);
X		
X}
X
Xuptime()
X{
X	int	now, boottime;
X	char	*up, *ch_time();
X	struct	utmp	*utent, *getutent();
X	struct	utsname	utsname;
X
X	time(&now);
X	today = localtime(&now);
X
X	setutent();
X	while ((utent = getutent()) != (struct utmp *)NULL) {
X		if (utent->ut_type == BOOT_TIME) {
X			boottime = utent->ut_time;
X			break;
X		}
X	}
X	endutent();
X
X	up = ch_time(now - boottime);
X	uname(&utsname);
X	printf("Node: %s, up for %s\n", utsname.nodename, up);
X
X}
X
Xshowtime()
X{
X	int	now, ampm;
X	extern	char	*day[], *mon[];
X
X	time(&now);
X	today = localtime(&now);
X
X	if (today->tm_hour == 0) {
X		ampm = 0;
X		today->tm_hour += 12;
X	} else if (today->tm_hour > 12) {
X		ampm = 1;
X		today->tm_hour -= 12;
X	} else  ampm = 0;
X
X	printf("It's %s, %s %d, %d at %2d:%.2d%s ... \n",
X		day[today->tm_wday], mon[today->tm_mon], today->tm_mday,
X		today->tm_year + 1900, today->tm_hour, today->tm_min,
X		(ampm == 0) ? "am" : "pm");
X
X}
X
Xshow_motd()
X{
X	FILE	*fp;
X	char	buffer[BUFSIZ];
X
X	printf("\n");
X	if ((fp = fopen("/etc/motd","r")) != NULL) {
X		while (fgets(buffer, BUFSIZ, fp) != NULL) 
X			printf("%s", buffer);
X	
X		fclose(fp);
X	}
X	printf("\n");
X}
END_OF_logininit.c
if test 6088 -ne `wc -c <logininit.c`; then
    echo shar: \"logininit.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of shell archive.
exit 0
-- 
US MAIL  : Lenny Tropiano, ICUS Computer Group        IIIII  CCC U   U  SSS
           PO Box 1                                     I   C    U   U S
           Islip Terrace, New York  11752               I   C    U   U  SS 
PHONE    : (516) 968-8576 [H] (516) 582-5525 [W]        I   C    U   U    S
TELEX    : 154232428 [ICUS]                           IIIII  CCC  UUU  SSS 
AT&T MAIL: ...attmail!icus!lenny  
UUCP     : ...{mtune, ihnp4, boulder, talcott, sbcs, bc-cis}!icus!lenny 



More information about the Unix-pc.sources mailing list