10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22*239Sceastha /*
23*239Sceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24*239Sceastha * Use is subject to license terms.
25*239Sceastha */
26*239Sceastha
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
30*239Sceastha #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate news foo prints /var/news/foo
340Sstevel@tonic-gate news -a prints all news items, latest first
350Sstevel@tonic-gate news -n lists names of new items
360Sstevel@tonic-gate news -s tells count of new items only
370Sstevel@tonic-gate news prints items changed since last news
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #include <unistd.h>
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate #include <setjmp.h>
460Sstevel@tonic-gate #include <signal.h>
470Sstevel@tonic-gate #include <dirent.h>
480Sstevel@tonic-gate #include <pwd.h>
490Sstevel@tonic-gate #include <time.h>
500Sstevel@tonic-gate #include <locale.h>
510Sstevel@tonic-gate
520Sstevel@tonic-gate #define INDENT 3
530Sstevel@tonic-gate #define RD_WR_ALL (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
540Sstevel@tonic-gate
550Sstevel@tonic-gate #define DATE_FMT "%a %b %e %H:%M:%S %Y"
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate * %a abbreviated weekday name
580Sstevel@tonic-gate * %b abbreviated month name
590Sstevel@tonic-gate * %e day of month
600Sstevel@tonic-gate * %H hour (24-hour clock)
610Sstevel@tonic-gate * %M minute
620Sstevel@tonic-gate * %S second
630Sstevel@tonic-gate * %Y year
640Sstevel@tonic-gate */
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate The following items should not be printed.
670Sstevel@tonic-gate */
680Sstevel@tonic-gate char *ignore[] = {
690Sstevel@tonic-gate "core",
700Sstevel@tonic-gate NULL
710Sstevel@tonic-gate };
720Sstevel@tonic-gate
730Sstevel@tonic-gate struct n_file {
740Sstevel@tonic-gate long n_time;
750Sstevel@tonic-gate char n_name[MAXNAMLEN];
760Sstevel@tonic-gate } *n_list;
770Sstevel@tonic-gate
780Sstevel@tonic-gate char NEWS[] = "/var/news"; /* directory for news items */
790Sstevel@tonic-gate
800Sstevel@tonic-gate int aopt = 0; /* 1 say -a specified */
810Sstevel@tonic-gate int n_count; /* number of items in NEWS directory */
820Sstevel@tonic-gate int number_read; /* number of items read */
830Sstevel@tonic-gate int nopt = 0; /* 1 say -n specified */
840Sstevel@tonic-gate int optsw; /* for getopt */
850Sstevel@tonic-gate int opt = 0; /* number of options specified */
860Sstevel@tonic-gate int sopt = 0; /* 1 says -s specified */
870Sstevel@tonic-gate char stdbuf[BUFSIZ];
880Sstevel@tonic-gate char time_buf[50]; /* holds date and time string */
890Sstevel@tonic-gate
900Sstevel@tonic-gate jmp_buf save_addr;
910Sstevel@tonic-gate
92*239Sceastha void all_news(void);
93*239Sceastha int ck_num(void);
94*239Sceastha void count(char *);
95*239Sceastha void initialize(void);
96*239Sceastha void late_news(void(*)(), int);
97*239Sceastha void notify(char *);
98*239Sceastha void print_item(char *);
99*239Sceastha void read_dir(void);
100*239Sceastha
101*239Sceastha int
main(int argc,char ** argv)102*239Sceastha main(int argc, char **argv)
1030Sstevel@tonic-gate {
104*239Sceastha int i;
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate (void)setlocale(LC_ALL, "");
1070Sstevel@tonic-gate setbuf (stdout, stdbuf);
1080Sstevel@tonic-gate initialize();
1090Sstevel@tonic-gate read_dir();
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate if (argc <= 1) {
1120Sstevel@tonic-gate late_news (print_item, 1);
1130Sstevel@tonic-gate ck_num();
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate else while ((optsw = getopt(argc, argv, "ans")) != EOF)
1160Sstevel@tonic-gate switch(optsw) {
1170Sstevel@tonic-gate case 'a':
1180Sstevel@tonic-gate aopt++;
1190Sstevel@tonic-gate opt++;
1200Sstevel@tonic-gate break;
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate case 'n':
1230Sstevel@tonic-gate nopt++;
1240Sstevel@tonic-gate opt++;
1250Sstevel@tonic-gate break;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate case 's':
1280Sstevel@tonic-gate sopt++;
1290Sstevel@tonic-gate opt++;
1300Sstevel@tonic-gate break;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate default:
1330Sstevel@tonic-gate fprintf (stderr, "usage: news [-a] [-n] [-s] [items]\n");
1340Sstevel@tonic-gate exit (1);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate if (opt > 1) {
1380Sstevel@tonic-gate fprintf(stderr, "news: options are mutually exclusive\n");
1390Sstevel@tonic-gate exit(1);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate if (opt > 0 && argc > 2) {
1430Sstevel@tonic-gate fprintf(stderr, "news: options are not allowed with file names\n");
1440Sstevel@tonic-gate exit(1);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate if (aopt) {
1480Sstevel@tonic-gate all_news();
1490Sstevel@tonic-gate ck_num();
1500Sstevel@tonic-gate exit(0);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate if (nopt) {
1540Sstevel@tonic-gate late_news (notify, 0);
1550Sstevel@tonic-gate ck_num();
1560Sstevel@tonic-gate exit(0);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate if (sopt) {
1600Sstevel@tonic-gate late_news (count, 0);
1610Sstevel@tonic-gate exit(0);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate for (i=1; i<argc; i++) print_item (argv[i]);
1650Sstevel@tonic-gate
166*239Sceastha return (0);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * read_dir: get the file names and modification dates for the
1710Sstevel@tonic-gate * files in /var/news into n_list; sort them in reverse by
1720Sstevel@tonic-gate * modification date. We assume /var/news is the working directory.
1730Sstevel@tonic-gate */
1740Sstevel@tonic-gate
175*239Sceastha void
read_dir(void)176*239Sceastha read_dir(void)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate struct dirent *nf, *readdir();
1790Sstevel@tonic-gate struct stat sbuf;
1800Sstevel@tonic-gate char fname[MAXNAMLEN];
1810Sstevel@tonic-gate DIR *dirp;
1820Sstevel@tonic-gate int i, j;
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate /* Open the current directory */
1850Sstevel@tonic-gate if ((dirp = opendir(".")) == NULL) {
1860Sstevel@tonic-gate fprintf (stderr, "Cannot open %s\n", NEWS);
1870Sstevel@tonic-gate exit (1);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate /* Read the file names into n_list */
1910Sstevel@tonic-gate n_count = 0;
1920Sstevel@tonic-gate while (nf = readdir(dirp)) {
1930Sstevel@tonic-gate strncpy (fname, nf->d_name, (unsigned) strlen(nf->d_name) + 1);
1940Sstevel@tonic-gate if (nf->d_ino != (ino_t)0 && stat (fname, &sbuf) >= 0
1950Sstevel@tonic-gate && (sbuf.st_mode & S_IFMT) == S_IFREG) {
1960Sstevel@tonic-gate register char **p;
1970Sstevel@tonic-gate p = ignore;
1980Sstevel@tonic-gate while (*p && strncmp (*p, nf->d_name, MAXNAMLEN))
1990Sstevel@tonic-gate ++p;
2000Sstevel@tonic-gate if (!*p) {
2010Sstevel@tonic-gate if (n_count++ > 0)
2020Sstevel@tonic-gate n_list = (struct n_file *)
2030Sstevel@tonic-gate realloc ((char *) n_list,
2040Sstevel@tonic-gate (unsigned)
2050Sstevel@tonic-gate (sizeof (struct n_file)
2060Sstevel@tonic-gate * n_count));
2070Sstevel@tonic-gate else
2080Sstevel@tonic-gate n_list = (struct n_file *) malloc
2090Sstevel@tonic-gate ((unsigned)
2100Sstevel@tonic-gate (sizeof (struct n_file) *
2110Sstevel@tonic-gate n_count));
2120Sstevel@tonic-gate if (n_list == NULL) {
2130Sstevel@tonic-gate fprintf (stderr, "No storage\n");
2140Sstevel@tonic-gate exit (1);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate n_list[n_count-1].n_time = sbuf.st_mtime;
2170Sstevel@tonic-gate strncpy (n_list[n_count-1].n_name,
2180Sstevel@tonic-gate nf->d_name, MAXNAMLEN);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate /* Sort the elements of n_list in decreasing time order */
2240Sstevel@tonic-gate for (i=1; i<n_count; i++)
2250Sstevel@tonic-gate for (j=0; j<i; j++)
2260Sstevel@tonic-gate if (n_list[j].n_time < n_list[i].n_time) {
2270Sstevel@tonic-gate struct n_file temp;
2280Sstevel@tonic-gate temp = n_list[i];
2290Sstevel@tonic-gate n_list[i] = n_list[j];
2300Sstevel@tonic-gate n_list[j] = temp;
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate /* Clean up */
2340Sstevel@tonic-gate closedir(dirp);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate
237*239Sceastha void
initialize(void)238*239Sceastha initialize(void)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate if (signal (SIGQUIT, SIG_IGN) != (void(*)())SIG_IGN)
2410Sstevel@tonic-gate signal (SIGQUIT, _exit);
2420Sstevel@tonic-gate umask (((~(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)) & S_IAMB));
2430Sstevel@tonic-gate if (chdir (NEWS) < 0) {
2440Sstevel@tonic-gate fprintf (stderr, "Cannot chdir to %s\n", NEWS);
2450Sstevel@tonic-gate exit (1);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
249*239Sceastha void
all_news(void)250*239Sceastha all_news(void)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate int i;
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate for (i=0; i<n_count; i++)
2550Sstevel@tonic-gate print_item (n_list[i].n_name);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate
258*239Sceastha void
print_item(char * f)259*239Sceastha print_item(char *f)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate FILE *fd;
2620Sstevel@tonic-gate char fname[MAXNAMLEN+1];
2630Sstevel@tonic-gate static int firstitem = 1;
2640Sstevel@tonic-gate void onintr();
2650Sstevel@tonic-gate struct passwd *getpwuid();
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate if (f == NULL) {
2680Sstevel@tonic-gate return;
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate strncpy (fname, f, MAXNAMLEN);
2710Sstevel@tonic-gate fname[MAXNAMLEN] = '\0';
2720Sstevel@tonic-gate if ((fd = fopen (fname, "r")) == NULL)
2730Sstevel@tonic-gate printf ("Cannot open %s/%s\n", NEWS, fname);
2740Sstevel@tonic-gate else {
2750Sstevel@tonic-gate register int c, ip, op;
2760Sstevel@tonic-gate struct stat sbuf;
2770Sstevel@tonic-gate struct passwd *pw;
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate fstat (fileno (fd), &sbuf);
2800Sstevel@tonic-gate if (firstitem) {
2810Sstevel@tonic-gate firstitem = 0;
2820Sstevel@tonic-gate putchar ('\n');
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate if (setjmp(save_addr))
2850Sstevel@tonic-gate goto finish;
2860Sstevel@tonic-gate if (signal(SIGINT, SIG_IGN) != (void(*)())SIG_IGN)
2870Sstevel@tonic-gate signal(SIGINT, onintr);
2880Sstevel@tonic-gate printf ("%s ", fname);
2890Sstevel@tonic-gate pw = getpwuid (sbuf.st_uid);
2900Sstevel@tonic-gate if (pw)
2910Sstevel@tonic-gate printf ("(%s)", pw->pw_name);
2920Sstevel@tonic-gate else
2930Sstevel@tonic-gate printf (".....");
2940Sstevel@tonic-gate cftime(time_buf, DATE_FMT, &sbuf.st_mtime);
2950Sstevel@tonic-gate printf (" %s\n", time_buf);
2960Sstevel@tonic-gate op = 0;
2970Sstevel@tonic-gate ip = INDENT;
2980Sstevel@tonic-gate while ((c = getc (fd)) != EOF) {
2990Sstevel@tonic-gate switch (c) {
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate case '\r':
3020Sstevel@tonic-gate case '\n':
3030Sstevel@tonic-gate putchar (c);
3040Sstevel@tonic-gate op = 0;
3050Sstevel@tonic-gate ip = INDENT;
3060Sstevel@tonic-gate break;
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate case ' ':
3090Sstevel@tonic-gate ip++;
3100Sstevel@tonic-gate break;
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate case '\b':
3130Sstevel@tonic-gate if (ip > INDENT)
3140Sstevel@tonic-gate ip--;
3150Sstevel@tonic-gate break;
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate case '\t':
3180Sstevel@tonic-gate ip = ((ip - INDENT + 8) & -8) + INDENT;
3190Sstevel@tonic-gate break;
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate default:
3220Sstevel@tonic-gate while (ip < op) {
3230Sstevel@tonic-gate putchar ('\b');
3240Sstevel@tonic-gate op--;
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate while ((ip & -8) > (op & -8)) {
3270Sstevel@tonic-gate putchar ('\t');
3280Sstevel@tonic-gate op = (op + 8) & -8;
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate while (ip > op) {
3310Sstevel@tonic-gate putchar (' ');
3320Sstevel@tonic-gate op++;
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate putchar (c);
3350Sstevel@tonic-gate ip++;
3360Sstevel@tonic-gate op++;
3370Sstevel@tonic-gate break;
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate fflush (stdout);
3410Sstevel@tonic-gate finish:
3420Sstevel@tonic-gate putchar ('\n');
3430Sstevel@tonic-gate fclose (fd);
3440Sstevel@tonic-gate number_read++;
3450Sstevel@tonic-gate if (signal(SIGINT, SIG_IGN) != (void(*)())SIG_IGN)
3460Sstevel@tonic-gate signal(SIGINT, SIG_DFL);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate
350*239Sceastha void
late_news(void (* emit)(),int update)351*239Sceastha late_news(void(*emit)(), int update)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate long cutoff;
3540Sstevel@tonic-gate int i;
3550Sstevel@tonic-gate char fname[50], *cp;
3560Sstevel@tonic-gate struct stat newstime;
3570Sstevel@tonic-gate int fd;
3580Sstevel@tonic-gate struct {
3590Sstevel@tonic-gate long actime, modtime;
3600Sstevel@tonic-gate } utb;
3610Sstevel@tonic-gate extern char *getenv();
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate /* Determine the time when last called */
3640Sstevel@tonic-gate cp = getenv ("HOME");
3650Sstevel@tonic-gate if (cp == NULL) {
3660Sstevel@tonic-gate fprintf (stderr, "Cannot find HOME variable\n");
3670Sstevel@tonic-gate exit (1);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate strcpy (fname, cp);
3700Sstevel@tonic-gate strcat (fname, "/");
3710Sstevel@tonic-gate strcat (fname, ".news_time");
3720Sstevel@tonic-gate cutoff = stat (fname, &newstime) < 0? 0: newstime.st_mtime;
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate /* Print the recent items */
3750Sstevel@tonic-gate for (i=0; i<n_count && n_list[i].n_time > cutoff; i++) {
3760Sstevel@tonic-gate (*emit) (n_list[i].n_name);
3770Sstevel@tonic-gate number_read++;
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate (*emit) ((char *) NULL);
3800Sstevel@tonic-gate fflush (stdout);
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate if (update) {
3830Sstevel@tonic-gate /* Re-create the file and refresh the update time */
3840Sstevel@tonic-gate if (n_count > 0 && (fd = creat (fname, RD_WR_ALL)) >= 0) {
3850Sstevel@tonic-gate utb.actime = utb.modtime = n_list[0].n_time;
3860Sstevel@tonic-gate close (fd);
3870Sstevel@tonic-gate utime (fname, &utb);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate
392*239Sceastha void
notify(char * s)393*239Sceastha notify(char *s)
3940Sstevel@tonic-gate {
3950Sstevel@tonic-gate static int first = 1;
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate if (s) {
3980Sstevel@tonic-gate if (first) {
3990Sstevel@tonic-gate first = 0;
4000Sstevel@tonic-gate printf ("news:", NEWS);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate printf (" %.14s", s);
4030Sstevel@tonic-gate } else if (!first)
4040Sstevel@tonic-gate putchar ('\n');
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate /*ARGSUSED*/
408*239Sceastha void
count(char * s)409*239Sceastha count(char *s)
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate static int nitems = 0;
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate if (s)
4140Sstevel@tonic-gate nitems++;
4150Sstevel@tonic-gate else {
4160Sstevel@tonic-gate if (nitems) {
4170Sstevel@tonic-gate printf ("%d news item", nitems);
4180Sstevel@tonic-gate if (nitems != 1)
4190Sstevel@tonic-gate putchar ('s');
4200Sstevel@tonic-gate printf (".\n");
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate else printf("No news.\n");
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate
4260Sstevel@tonic-gate void
onintr()4270Sstevel@tonic-gate onintr()
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate sleep(2);
4300Sstevel@tonic-gate longjmp(save_addr, 1);
4310Sstevel@tonic-gate }
432*239Sceastha
433*239Sceastha int
ck_num(void)434*239Sceastha ck_num(void)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate if (sopt && !number_read) printf("No news.\n");
4370Sstevel@tonic-gate return(0);
4380Sstevel@tonic-gate }
439