How to put something on Status line

4197,ATTT rbr at bonnie.ATT.COM
Fri Nov 2 05:05:01 AEST 1990


Bob Rager

-------------------- CUT HERE ---------------------
/* cc -O -s -o ${HOME}/bin/time_line timeline.c -lcurses  */

/* Name:	time_line
**
** INVOCATION:	time_line [-d] [-m]
**
** DESCRIPTION:
**
**	"time_line" is a program to print the current date and time
**	on the 25'th line - status line - of terminals that have that
**	capability. If "time_line" is called and the terminal does not
**	have a status line capability, the program exits gracefully.
**	Otherwise, the status line is cleared and the date (mm/dd) time
**	(hh:mm) is printed on the status line. The time is updated every
**	minute. Also the MAILPATH environment variable is accessed on each
**	cycle to determine if you have mail. The first time mail is detected
**	the message "MAIL" is appended to the date and time. The next cycle
**	the message "mail" is appended. When you process the mail, the message
**	disappears on the next cycle.
**
**	The program illustrates the use of curses control of the cursor to
**	print on the 25'th line; and to replace the cursor for the user.
**
**	Note that the program forks a child process after supressing interupt
**	and lets the parent process die. This is the way to do the UNIX
**	equivalent of an MSDOS TSR. The child stays in the system as an
**	orphaned process but supressing SIGINT and SIGQUIT keeps the system
**	from eventually killing the process. SIGHUP (hangup) kills the process
**	but allows the process to clean up before leaving.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/signal.h>
#include <time.h>
#include <fcntl.h>

/* Global variables */
char to_status_line[80];
char from_status_line[80];
char dis_status_line[80];
char clr_eol[100];
char rev_out[20];
char rev_end[20];
int eslok, columns ;
int quiet = 0;
int mail = 1;
int debug = 0;
short uid, pid, lpid;
void clr_status_line();

/* Global Functions */
outc(c) char c; { putchar(c);} 

main(argc, argv)
	int argc;
	char *argv[];
{
	int c, i;
	struct tm *tm, *localtime();
	time_t now, tenmin;
	char msg[BUFSIZ], *p, *host, *strchr();
	char *mfile, *getenv();
	struct stat new_st, old_st;

	void (*status_fmt)();
	void prt_status_line(), ctrm_status();

	extern char *optarg;
	extern int optind;

	FILE *fp = NULL;

	signal(SIGINT,SIG_IGN);
	signal(SIGQUIT,SIG_IGN);
	signal(SIGHUP,clr_status_line);
	signal(SIGTERM,clr_status_line);
	status_fmt = prt_status_line;
	while((c = getopt(argc, argv, "cdm")) != EOF) {
		switch(c) {
		case 'd':
			debug = 1 ;
			signal(SIGINT, clr_status_line);
			signal(SIGQUIT, SIG_DFL);
			break;
		case 'm':
			mail = 0 ;
			break;
		default:
			exit(1);
			break;
		}
	}
	initterm();
	/* This will allow starting from .profile         */
	/* immediately fork and let the parent die */
	if(!debug) {
		switch(fork()) {

		case -1:	/* fork failed */
			perror("fork");
			exit(1);

		case  0:	/* child */
			break;

		default:	/* parent dies */
			exit(0);
			break;
		}
	}
	uid= getuid();
	pid= getpid();
	mfile = getenv("MAIL");
	if( debug ) printf("%s\n", mfile);
	close(0);
	close(1);
	fcntl(2, F_DUPFD, 1);
	if(stat(mfile, &old_st) < 0) {
		old_st.st_mtime = 0;
		old_st.st_atime = 0;
	}
 while( 1 ){
	time(&now);
	tm = localtime(&now);
	sprintf(msg, "%d/%d %d:%02d ",
		tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
	tenmin = now - (10*60);
	if( mail ){
		if(stat(mfile, &new_st) >= 0) {
			if((new_st.st_size > 0)
			&& (new_st.st_mtime > new_st.st_atime)) {
				if(new_st.st_mtime > old_st.st_mtime) {
					putc('\007', stdout);
					strcat(msg, "  MAIL");
				} else {
					strcat(msg, "  mail");
				}
			}
			old_st = new_st;
		}
	}
	(*status_fmt)(msg);
 	sleep(60 - (now%60));
    }
}

void
prt_status_line(msg)
char *msg;
{
	int adj = 0;
 	tputs(tparm(to_status_line, 0), 1, outc);
	tputs(clr_eol, 1, outc);
	tputs(from_status_line, 1, outc);
	fflush(stdout);
 	tputs(tparm(to_status_line, 0), 1, outc);
	tputs(msg, 1, outc);
	tputs(from_status_line, 1, outc);
	fflush(stdout);
}
void
clr_status_line()
{
	printf("%s%s%s%s",
		tparm(to_status_line, 0),
		clr_eol,
		from_status_line,
		dis_status_line);
	fflush(stdout);
	exit(0);
}
initterm() {
	char *term, cp[256], *r, *tgetstr();
	char tbuf[2*1024];

	if ((term=getenv("TERM")) == NULL) {
		if (!quiet)
			fprintf(stderr, "sysline: No TERM variable in enviroment\n");
		exit(1);
	}
	if (tgetent(tbuf, term) <= 0) {
		if (!quiet)
			fprintf(stderr, "sysline: Unknown terminal type: %s\n", term);
		exit(1);
	}
	if (tgetflag("hs") <= 0) {
		if (!quiet)
			fprintf(stderr, "sysline: No status capability for %s\n", term);
		exit(1);
	}

	if(r = tgetstr("i1", cp)) {
		tputs(r, 1, outc);
	}
	if(r = tgetstr("is", cp)) {
		tputs(r, 1, outc);
	}
	if(r = tgetstr("i3", cp)) {
		tputs(r, 1, outc);
	}
	fflush(stdout);

	/* the "-1" below is to avoid cursor wraparound problems */
	if((columns=tgetnum("ws")) <= 0) { /* special width of status line */
		columns = tgetnum("co");
	}
	columns -= 1;
	if(r = tgetstr("ts", cp)) {
		strcpy(to_status_line, r);
	}
	if(r = tgetstr("fs", cp)) {
		strcpy(from_status_line, r);
	}
	if(r = tgetstr("ds", cp)) {
		strcpy(dis_status_line, r);
	}
	eslok = tgetflag("es");
	if(eslok) {
		if(r = tgetstr("ce", cp)) {
			strcpy(clr_eol, r);
		}
		if(r = tgetstr("so", cp)) {
			strcpy(rev_out, r);
		}
		if(r = tgetstr("se", cp)) {
			strcpy(rev_end, r);
		}
	} else {
		int i;
		for(i=0;i < columns ; i++) clr_eol[i] = ' ';
		strcpy(rev_out,"");
		strcpy(rev_end,"");
	}
}
------------------------ END OF C SOURCE ----------------------



More information about the Comp.unix.shell mailing list