xref: /illumos-gate/usr/src/cmd/news/news.c (revision 0d8b5334)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*0d8b5334Sceastha /*
23*0d8b5334Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0d8b5334Sceastha  * Use is subject to license terms.
25*0d8b5334Sceastha  */
26*0d8b5334Sceastha 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*0d8b5334Sceastha #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate 	news foo	prints /var/news/foo
347c478bd9Sstevel@tonic-gate 	news -a		prints all news items, latest first
357c478bd9Sstevel@tonic-gate 	news -n		lists names of new items
367c478bd9Sstevel@tonic-gate 	news -s		tells count of new items only
377c478bd9Sstevel@tonic-gate 	news		prints items changed since last news
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <setjmp.h>
467c478bd9Sstevel@tonic-gate #include <signal.h>
477c478bd9Sstevel@tonic-gate #include <dirent.h>
487c478bd9Sstevel@tonic-gate #include <pwd.h>
497c478bd9Sstevel@tonic-gate #include <time.h>
507c478bd9Sstevel@tonic-gate #include <locale.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define INDENT 3
537c478bd9Sstevel@tonic-gate #define	RD_WR_ALL	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	DATE_FMT	"%a %b %e %H:%M:%S %Y"
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  *	%a	abbreviated weekday name
587c478bd9Sstevel@tonic-gate  *	%b	abbreviated month name
597c478bd9Sstevel@tonic-gate  *	%e	day of month
607c478bd9Sstevel@tonic-gate  *	%H	hour (24-hour clock)
617c478bd9Sstevel@tonic-gate  *	%M	minute
627c478bd9Sstevel@tonic-gate  *	%S	second
637c478bd9Sstevel@tonic-gate  *	%Y	year
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate 	The following items should not be printed.
677c478bd9Sstevel@tonic-gate */
687c478bd9Sstevel@tonic-gate char	*ignore[] = {
697c478bd9Sstevel@tonic-gate 		"core",
707c478bd9Sstevel@tonic-gate 		NULL
717c478bd9Sstevel@tonic-gate };
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate struct n_file {
747c478bd9Sstevel@tonic-gate 	long n_time;
757c478bd9Sstevel@tonic-gate 	char n_name[MAXNAMLEN];
767c478bd9Sstevel@tonic-gate } *n_list;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate char	NEWS[] = "/var/news";	/* directory for news items */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate int	aopt = 0;	/* 1 say -a specified */
817c478bd9Sstevel@tonic-gate int	n_count;	/* number of items in NEWS directory */
827c478bd9Sstevel@tonic-gate int	number_read;	/* number of items read */
837c478bd9Sstevel@tonic-gate int	nopt = 0;	/* 1 say -n specified */
847c478bd9Sstevel@tonic-gate int	optsw;		/* for getopt */
857c478bd9Sstevel@tonic-gate int	opt = 0;	/* number of options specified */
867c478bd9Sstevel@tonic-gate int	sopt = 0;	/* 1 says -s specified */
877c478bd9Sstevel@tonic-gate char	stdbuf[BUFSIZ];
887c478bd9Sstevel@tonic-gate char	time_buf[50];	/* holds date and time string */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate jmp_buf	save_addr;
917c478bd9Sstevel@tonic-gate 
92*0d8b5334Sceastha void all_news(void);
93*0d8b5334Sceastha int ck_num(void);
94*0d8b5334Sceastha void count(char *);
95*0d8b5334Sceastha void initialize(void);
96*0d8b5334Sceastha void late_news(void(*)(), int);
97*0d8b5334Sceastha void notify(char *);
98*0d8b5334Sceastha void print_item(char *);
99*0d8b5334Sceastha void read_dir(void);
100*0d8b5334Sceastha 
101*0d8b5334Sceastha int
main(int argc,char ** argv)102*0d8b5334Sceastha main(int argc, char **argv)
1037c478bd9Sstevel@tonic-gate {
104*0d8b5334Sceastha 	int i;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	(void)setlocale(LC_ALL, "");
1077c478bd9Sstevel@tonic-gate 	setbuf (stdout, stdbuf);
1087c478bd9Sstevel@tonic-gate 	initialize();
1097c478bd9Sstevel@tonic-gate 	read_dir();
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (argc <= 1) {
1127c478bd9Sstevel@tonic-gate 		late_news (print_item, 1);
1137c478bd9Sstevel@tonic-gate 		ck_num();
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	else while ((optsw = getopt(argc, argv, "ans")) != EOF)
1167c478bd9Sstevel@tonic-gate 		switch(optsw) {
1177c478bd9Sstevel@tonic-gate 		case 'a':
1187c478bd9Sstevel@tonic-gate 			aopt++;
1197c478bd9Sstevel@tonic-gate 			opt++;
1207c478bd9Sstevel@tonic-gate 			break;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 		case 'n':
1237c478bd9Sstevel@tonic-gate 			nopt++;
1247c478bd9Sstevel@tonic-gate 			opt++;
1257c478bd9Sstevel@tonic-gate 			break;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 		case 's':
1287c478bd9Sstevel@tonic-gate 			sopt++;
1297c478bd9Sstevel@tonic-gate 			opt++;
1307c478bd9Sstevel@tonic-gate 			break;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		default:
1337c478bd9Sstevel@tonic-gate 			fprintf (stderr, "usage: news [-a] [-n] [-s] [items]\n");
1347c478bd9Sstevel@tonic-gate 			exit (1);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate         if (opt > 1) {
1387c478bd9Sstevel@tonic-gate         	fprintf(stderr, "news: options are mutually exclusive\n");
1397c478bd9Sstevel@tonic-gate         	exit(1);
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate         if (opt > 0 && argc > 2) {
1437c478bd9Sstevel@tonic-gate         	fprintf(stderr, "news: options are not allowed with file names\n");
1447c478bd9Sstevel@tonic-gate         	exit(1);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (aopt) {
1487c478bd9Sstevel@tonic-gate 		all_news();
1497c478bd9Sstevel@tonic-gate 		ck_num();
1507c478bd9Sstevel@tonic-gate 		exit(0);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (nopt) {
1547c478bd9Sstevel@tonic-gate 		late_news (notify, 0);
1557c478bd9Sstevel@tonic-gate 		ck_num();
1567c478bd9Sstevel@tonic-gate 		exit(0);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (sopt) {
1607c478bd9Sstevel@tonic-gate 		late_news (count, 0);
1617c478bd9Sstevel@tonic-gate 		exit(0);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	for (i=1; i<argc; i++) print_item (argv[i]);
1657c478bd9Sstevel@tonic-gate 
166*0d8b5334Sceastha 	return (0);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  *	read_dir: get the file names and modification dates for the
1717c478bd9Sstevel@tonic-gate  *	files in /var/news into n_list; sort them in reverse by
1727c478bd9Sstevel@tonic-gate  *	modification date. We assume /var/news is the working directory.
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate 
175*0d8b5334Sceastha void
read_dir(void)176*0d8b5334Sceastha read_dir(void)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	struct dirent *nf, *readdir();
1797c478bd9Sstevel@tonic-gate 	struct stat sbuf;
1807c478bd9Sstevel@tonic-gate 	char fname[MAXNAMLEN];
1817c478bd9Sstevel@tonic-gate 	DIR *dirp;
1827c478bd9Sstevel@tonic-gate 	int i, j;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/* Open the current directory */
1857c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(".")) == NULL) {
1867c478bd9Sstevel@tonic-gate 		fprintf (stderr, "Cannot open %s\n", NEWS);
1877c478bd9Sstevel@tonic-gate 		exit (1);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	/* Read the file names into n_list */
1917c478bd9Sstevel@tonic-gate 	n_count = 0;
1927c478bd9Sstevel@tonic-gate 	while (nf = readdir(dirp)) {
1937c478bd9Sstevel@tonic-gate 		strncpy (fname, nf->d_name, (unsigned) strlen(nf->d_name) + 1);
1947c478bd9Sstevel@tonic-gate 		if (nf->d_ino != (ino_t)0 && stat (fname, &sbuf) >= 0
1957c478bd9Sstevel@tonic-gate 		 && (sbuf.st_mode & S_IFMT) == S_IFREG) {
1967c478bd9Sstevel@tonic-gate 			register char **p;
1977c478bd9Sstevel@tonic-gate 			p = ignore;
1987c478bd9Sstevel@tonic-gate 			while (*p && strncmp (*p, nf->d_name, MAXNAMLEN))
1997c478bd9Sstevel@tonic-gate 				++p;
2007c478bd9Sstevel@tonic-gate 			if (!*p) {
2017c478bd9Sstevel@tonic-gate 				if (n_count++ > 0)
2027c478bd9Sstevel@tonic-gate 					n_list = (struct n_file *)
2037c478bd9Sstevel@tonic-gate 						realloc ((char *) n_list,
2047c478bd9Sstevel@tonic-gate 						(unsigned)
2057c478bd9Sstevel@tonic-gate 						(sizeof (struct n_file)
2067c478bd9Sstevel@tonic-gate 						    * n_count));
2077c478bd9Sstevel@tonic-gate 				else
2087c478bd9Sstevel@tonic-gate 					n_list = (struct n_file *) malloc
2097c478bd9Sstevel@tonic-gate 						((unsigned)
2107c478bd9Sstevel@tonic-gate 						(sizeof (struct n_file) *
2117c478bd9Sstevel@tonic-gate 						n_count));
2127c478bd9Sstevel@tonic-gate 				if (n_list == NULL) {
2137c478bd9Sstevel@tonic-gate 					fprintf (stderr, "No storage\n");
2147c478bd9Sstevel@tonic-gate 					exit (1);
2157c478bd9Sstevel@tonic-gate 				}
2167c478bd9Sstevel@tonic-gate 				n_list[n_count-1].n_time = sbuf.st_mtime;
2177c478bd9Sstevel@tonic-gate 				strncpy (n_list[n_count-1].n_name,
2187c478bd9Sstevel@tonic-gate 					nf->d_name, MAXNAMLEN);
2197c478bd9Sstevel@tonic-gate 			}
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/* Sort the elements of n_list in decreasing time order */
2247c478bd9Sstevel@tonic-gate 	for (i=1; i<n_count; i++)
2257c478bd9Sstevel@tonic-gate 		for (j=0; j<i; j++)
2267c478bd9Sstevel@tonic-gate 			if (n_list[j].n_time < n_list[i].n_time) {
2277c478bd9Sstevel@tonic-gate 				struct n_file temp;
2287c478bd9Sstevel@tonic-gate 				temp = n_list[i];
2297c478bd9Sstevel@tonic-gate 				n_list[i] = n_list[j];
2307c478bd9Sstevel@tonic-gate 				n_list[j] = temp;
2317c478bd9Sstevel@tonic-gate 			}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/* Clean up */
2347c478bd9Sstevel@tonic-gate 	closedir(dirp);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
237*0d8b5334Sceastha void
initialize(void)238*0d8b5334Sceastha initialize(void)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	if (signal (SIGQUIT, SIG_IGN) != (void(*)())SIG_IGN)
2417c478bd9Sstevel@tonic-gate 		signal (SIGQUIT, _exit);
2427c478bd9Sstevel@tonic-gate 	umask (((~(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)) & S_IAMB));
2437c478bd9Sstevel@tonic-gate 	if (chdir (NEWS) < 0) {
2447c478bd9Sstevel@tonic-gate 		fprintf (stderr, "Cannot chdir to %s\n", NEWS);
2457c478bd9Sstevel@tonic-gate 		exit (1);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
249*0d8b5334Sceastha void
all_news(void)250*0d8b5334Sceastha all_news(void)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	int i;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	for (i=0; i<n_count; i++)
2557c478bd9Sstevel@tonic-gate 		print_item (n_list[i].n_name);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
258*0d8b5334Sceastha void
print_item(char * f)259*0d8b5334Sceastha print_item(char *f)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	FILE *fd;
2627c478bd9Sstevel@tonic-gate 	char fname[MAXNAMLEN+1];
2637c478bd9Sstevel@tonic-gate 	static int firstitem = 1;
2647c478bd9Sstevel@tonic-gate 	void onintr();
2657c478bd9Sstevel@tonic-gate 	struct passwd *getpwuid();
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if (f == NULL) {
2687c478bd9Sstevel@tonic-gate 		return;
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 	strncpy (fname, f, MAXNAMLEN);
2717c478bd9Sstevel@tonic-gate 	fname[MAXNAMLEN] = '\0';
2727c478bd9Sstevel@tonic-gate 	if ((fd = fopen (fname, "r")) == NULL)
2737c478bd9Sstevel@tonic-gate 		printf ("Cannot open %s/%s\n", NEWS, fname);
2747c478bd9Sstevel@tonic-gate 	else {
2757c478bd9Sstevel@tonic-gate 		register int c, ip, op;
2767c478bd9Sstevel@tonic-gate 		struct stat sbuf;
2777c478bd9Sstevel@tonic-gate 		struct passwd *pw;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		fstat (fileno (fd), &sbuf);
2807c478bd9Sstevel@tonic-gate 		if (firstitem) {
2817c478bd9Sstevel@tonic-gate 			firstitem = 0;
2827c478bd9Sstevel@tonic-gate 			putchar ('\n');
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 		if (setjmp(save_addr))
2857c478bd9Sstevel@tonic-gate 			goto finish;
2867c478bd9Sstevel@tonic-gate 		if (signal(SIGINT, SIG_IGN) != (void(*)())SIG_IGN)
2877c478bd9Sstevel@tonic-gate 			signal(SIGINT, onintr);
2887c478bd9Sstevel@tonic-gate 		printf ("%s ", fname);
2897c478bd9Sstevel@tonic-gate 		pw = getpwuid (sbuf.st_uid);
2907c478bd9Sstevel@tonic-gate 		if (pw)
2917c478bd9Sstevel@tonic-gate 			printf ("(%s)", pw->pw_name);
2927c478bd9Sstevel@tonic-gate 		else
2937c478bd9Sstevel@tonic-gate 			printf (".....");
2947c478bd9Sstevel@tonic-gate 		cftime(time_buf, DATE_FMT, &sbuf.st_mtime);
2957c478bd9Sstevel@tonic-gate 		printf (" %s\n", time_buf);
2967c478bd9Sstevel@tonic-gate 		op = 0;
2977c478bd9Sstevel@tonic-gate 		ip = INDENT;
2987c478bd9Sstevel@tonic-gate 		while ((c = getc (fd)) != EOF) {
2997c478bd9Sstevel@tonic-gate 			switch (c) {
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 			case '\r':
3027c478bd9Sstevel@tonic-gate 			case '\n':
3037c478bd9Sstevel@tonic-gate 				putchar (c);
3047c478bd9Sstevel@tonic-gate 				op = 0;
3057c478bd9Sstevel@tonic-gate 				ip = INDENT;
3067c478bd9Sstevel@tonic-gate 				break;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 			case ' ':
3097c478bd9Sstevel@tonic-gate 				ip++;
3107c478bd9Sstevel@tonic-gate 				break;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 			case '\b':
3137c478bd9Sstevel@tonic-gate 				if (ip > INDENT)
3147c478bd9Sstevel@tonic-gate 					ip--;
3157c478bd9Sstevel@tonic-gate 				break;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 			case '\t':
3187c478bd9Sstevel@tonic-gate 				ip = ((ip - INDENT + 8) & -8) + INDENT;
3197c478bd9Sstevel@tonic-gate 				break;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 			default:
3227c478bd9Sstevel@tonic-gate 				while (ip < op) {
3237c478bd9Sstevel@tonic-gate 					putchar ('\b');
3247c478bd9Sstevel@tonic-gate 					op--;
3257c478bd9Sstevel@tonic-gate 				}
3267c478bd9Sstevel@tonic-gate 				while ((ip & -8) > (op & -8)) {
3277c478bd9Sstevel@tonic-gate 					putchar ('\t');
3287c478bd9Sstevel@tonic-gate 					op = (op + 8) & -8;
3297c478bd9Sstevel@tonic-gate 				}
3307c478bd9Sstevel@tonic-gate 				while (ip > op) {
3317c478bd9Sstevel@tonic-gate 					putchar (' ');
3327c478bd9Sstevel@tonic-gate 					op++;
3337c478bd9Sstevel@tonic-gate 				}
3347c478bd9Sstevel@tonic-gate 				putchar (c);
3357c478bd9Sstevel@tonic-gate 				ip++;
3367c478bd9Sstevel@tonic-gate 				op++;
3377c478bd9Sstevel@tonic-gate 				break;
3387c478bd9Sstevel@tonic-gate 			}
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 		fflush (stdout);
3417c478bd9Sstevel@tonic-gate finish:
3427c478bd9Sstevel@tonic-gate 		putchar ('\n');
3437c478bd9Sstevel@tonic-gate 		fclose (fd);
3447c478bd9Sstevel@tonic-gate 		number_read++;
3457c478bd9Sstevel@tonic-gate 		if (signal(SIGINT, SIG_IGN) != (void(*)())SIG_IGN)
3467c478bd9Sstevel@tonic-gate 			signal(SIGINT, SIG_DFL);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
350*0d8b5334Sceastha void
late_news(void (* emit)(),int update)351*0d8b5334Sceastha late_news(void(*emit)(), int update)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	long cutoff;
3547c478bd9Sstevel@tonic-gate 	int i;
3557c478bd9Sstevel@tonic-gate 	char fname[50], *cp;
3567c478bd9Sstevel@tonic-gate 	struct stat newstime;
3577c478bd9Sstevel@tonic-gate 	int fd;
3587c478bd9Sstevel@tonic-gate 	struct {
3597c478bd9Sstevel@tonic-gate 		long actime, modtime;
3607c478bd9Sstevel@tonic-gate 	} utb;
3617c478bd9Sstevel@tonic-gate 	extern char *getenv();
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	/* Determine the time when last called */
3647c478bd9Sstevel@tonic-gate 	cp = getenv ("HOME");
3657c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
3667c478bd9Sstevel@tonic-gate 		fprintf (stderr, "Cannot find HOME variable\n");
3677c478bd9Sstevel@tonic-gate 		exit (1);
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 	strcpy (fname, cp);
3707c478bd9Sstevel@tonic-gate 	strcat (fname, "/");
3717c478bd9Sstevel@tonic-gate 	strcat (fname, ".news_time");
3727c478bd9Sstevel@tonic-gate 	cutoff = stat (fname, &newstime) < 0? 0: newstime.st_mtime;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/* Print the recent items */
3757c478bd9Sstevel@tonic-gate 	for (i=0; i<n_count && n_list[i].n_time > cutoff; i++) {
3767c478bd9Sstevel@tonic-gate 		(*emit) (n_list[i].n_name);
3777c478bd9Sstevel@tonic-gate 		number_read++;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 	(*emit) ((char *) NULL);
3807c478bd9Sstevel@tonic-gate 	fflush (stdout);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if (update) {
3837c478bd9Sstevel@tonic-gate 		/* Re-create the file and refresh the update time */
3847c478bd9Sstevel@tonic-gate 		if (n_count > 0 && (fd = creat (fname, RD_WR_ALL)) >= 0) {
3857c478bd9Sstevel@tonic-gate 			utb.actime = utb.modtime = n_list[0].n_time;
3867c478bd9Sstevel@tonic-gate 			close (fd);
3877c478bd9Sstevel@tonic-gate 			utime (fname, &utb);
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
392*0d8b5334Sceastha void
notify(char * s)393*0d8b5334Sceastha notify(char *s)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate 	static int first = 1;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (s) {
3987c478bd9Sstevel@tonic-gate 		if (first) {
3997c478bd9Sstevel@tonic-gate 			first = 0;
4007c478bd9Sstevel@tonic-gate 			printf ("news:", NEWS);
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 		printf (" %.14s", s);
4037c478bd9Sstevel@tonic-gate 	} else if (!first)
4047c478bd9Sstevel@tonic-gate 		putchar ('\n');
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate /*ARGSUSED*/
408*0d8b5334Sceastha void
count(char * s)409*0d8b5334Sceastha count(char *s)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	static int nitems = 0;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	if (s)
4147c478bd9Sstevel@tonic-gate 		nitems++;
4157c478bd9Sstevel@tonic-gate 	else {
4167c478bd9Sstevel@tonic-gate 		if (nitems) {
4177c478bd9Sstevel@tonic-gate 			printf ("%d news item", nitems);
4187c478bd9Sstevel@tonic-gate 			if (nitems != 1)
4197c478bd9Sstevel@tonic-gate 				putchar ('s');
4207c478bd9Sstevel@tonic-gate 			printf (".\n");
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 		else printf("No news.\n");
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate void
onintr()4277c478bd9Sstevel@tonic-gate onintr()
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	sleep(2);
4307c478bd9Sstevel@tonic-gate 	longjmp(save_addr, 1);
4317c478bd9Sstevel@tonic-gate }
432*0d8b5334Sceastha 
433*0d8b5334Sceastha int
ck_num(void)434*0d8b5334Sceastha ck_num(void)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	if (sopt && !number_read) printf("No news.\n");
4377c478bd9Sstevel@tonic-gate 	return(0);
4387c478bd9Sstevel@tonic-gate }
439