1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
/*
news foo prints /var/news/foo
news -a prints all news items, latest first
news -n lists names of new items
news -s tells count of new items only
news prints items changed since last news
*/
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <setjmp.h>
#include <signal.h>
#include <dirent.h>
#include <pwd.h>
#include <time.h>
#include <locale.h>
#define INDENT 3
#define RD_WR_ALL (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
#define DATE_FMT "%a %b %e %H:%M:%S %Y"
/*
* %a abbreviated weekday name
* %b abbreviated month name
* %e day of month
* %H hour (24-hour clock)
* %M minute
* %S second
* %Y year
*/
/*
The following items should not be printed.
*/
char *ignore[] = {
"core",
NULL
};
struct n_file {
long n_time;
char n_name[MAXNAMLEN];
} *n_list;
char NEWS[] = "/var/news"; /* directory for news items */
int aopt = 0; /* 1 say -a specified */
int n_count; /* number of items in NEWS directory */
int number_read; /* number of items read */
int nopt = 0; /* 1 say -n specified */
int optsw; /* for getopt */
int opt = 0; /* number of options specified */
int sopt = 0; /* 1 says -s specified */
char stdbuf[BUFSIZ];
char time_buf[50]; /* holds date and time string */
jmp_buf save_addr;
void all_news(void);
int ck_num(void);
void count(char *);
void initialize(void);
void late_news(void(*)(), int);
void notify(char *);
void print_item(char *);
void read_dir(void);
int
main(int argc, char **argv)
{
int i;
(void)setlocale(LC_ALL, "");
setbuf (stdout, stdbuf);
initialize();
read_dir();
if (argc <= 1) {
late_news (print_item, 1);
ck_num();
}
else while ((optsw = getopt(argc, argv, "ans")) != EOF)
switch(optsw) {
case 'a':
aopt++;
opt++;
break;
case 'n':
nopt++;
opt++;
break;
case 's':
sopt++;
opt++;
break;
default:
fprintf (stderr, "usage: news [-a] [-n] [-s] [items]\n");
exit (1);
}
if (opt > 1) {
fprintf(stderr, "news: options are mutually exclusive\n");
exit(1);
}
if (opt > 0 && argc > 2) {
fprintf(stderr, "news: options are not allowed with file names\n");
exit(1);
}
if (aopt) {
all_news();
ck_num();
exit(0);
}
if (nopt) {
late_news (notify, 0);
ck_num();
exit(0);
}
if (sopt) {
late_news (count, 0);
exit(0);
}
for (i=1; i<argc; i++) print_item (argv[i]);
return (0);
}
/*
* read_dir: get the file names and modification dates for the
* files in /var/news into n_list; sort them in reverse by
* modification date. We assume /var/news is the working directory.
*/
void
read_dir(void)
{
struct dirent *nf, *readdir();
struct stat sbuf;
char fname[MAXNAMLEN];
DIR *dirp;
int i, j;
/* Open the current directory */
if ((dirp = opendir(".")) == NULL) {
fprintf (stderr, "Cannot open %s\n", NEWS);
exit (1);
}
/* Read the file names into n_list */
n_count = 0;
while (nf = readdir(dirp)) {
strncpy (fname, nf->d_name, (unsigned) strlen(nf->d_name) + 1);
if (nf->d_ino != (ino_t)0 && stat (fname, &sbuf) >= 0
&& (sbuf.st_mode & S_IFMT) == S_IFREG) {
register char **p;
p = ignore;
while (*p && strncmp (*p, nf->d_name, MAXNAMLEN))
++p;
if (!*p) {
if (n_count++ > 0)
n_list = (struct n_file *)
realloc ((char *) n_list,
(unsigned)
(sizeof (struct n_file)
* n_count));
else
n_list = (struct n_file *) malloc
((unsigned)
(sizeof (struct n_file) *
n_count));
if (n_list == NULL) {
fprintf (stderr, "No storage\n");
exit (1);
}
n_list[n_count-1].n_time = sbuf.st_mtime;
strncpy (n_list[n_count-1].n_name,
nf->d_name, MAXNAMLEN);
}
}
}
/* Sort the elements of n_list in decreasing time order */
for (i=1; i<n_count; i++)
for (j=0; j<i; j++)
if (n_list[j].n_time < n_list[i].n_time) {
struct n_file temp;
temp = n_list[i];
n_list[i] = n_list[j];
n_list[j] = temp;
}
/* Clean up */
closedir(dirp);
}
void
initialize(void)
{
if (signal (SIGQUIT, SIG_IGN) != (void(*)())SIG_IGN)
signal (SIGQUIT, _exit);
umask (((~(S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)) & S_IAMB));
if (chdir (NEWS) < 0) {
fprintf (stderr, "Cannot chdir to %s\n", NEWS);
exit (1);
}
}
void
all_news(void)
{
int i;
for (i=0; i<n_count; i++)
print_item (n_list[i].n_name);
}
void
print_item(char *f)
{
FILE *fd;
char fname[MAXNAMLEN+1];
static int firstitem = 1;
void onintr();
struct passwd *getpwuid();
if (f == NULL) {
return;
}
strncpy (fname, f, MAXNAMLEN);
fname[MAXNAMLEN] = '\0';
if ((fd = fopen (fname, "r")) == NULL)
printf ("Cannot open %s/%s\n", NEWS, fname);
else {
register int c, ip, op;
struct stat sbuf;
struct passwd *pw;
fstat (fileno (fd), &sbuf);
if (firstitem) {
firstitem = 0;
putchar ('\n');
}
if (setjmp(save_addr))
goto finish;
if (signal(SIGINT, SIG_IGN) != (void(*)())SIG_IGN)
signal(SIGINT, onintr);
printf ("%s ", fname);
pw = getpwuid (sbuf.st_uid);
if (pw)
printf ("(%s)", pw->pw_name);
else
printf (".....");
cftime(time_buf, DATE_FMT, &sbuf.st_mtime);
printf (" %s\n", time_buf);
op = 0;
ip = INDENT;
while ((c = getc (fd)) != EOF) {
switch (c) {
case '\r':
case '\n':
putchar (c);
op = 0;
ip = INDENT;
break;
case ' ':
ip++;
break;
case '\b':
if (ip > INDENT)
ip--;
break;
case '\t':
ip = ((ip - INDENT + 8) & -8) + INDENT;
break;
default:
while (ip < op) {
putchar ('\b');
op--;
}
while ((ip & -8) > (op & -8)) {
putchar ('\t');
op = (op + 8) & -8;
}
while (ip > op) {
putchar (' ');
op++;
}
putchar (c);
ip++;
op++;
break;
}
}
fflush (stdout);
finish:
putchar ('\n');
fclose (fd);
number_read++;
if (signal(SIGINT, SIG_IGN) != (void(*)())SIG_IGN)
signal(SIGINT, SIG_DFL);
}
}
void
late_news(void(*emit)(), int update)
{
long cutoff;
int i;
char fname[50], *cp;
struct stat newstime;
int fd;
struct {
long actime, modtime;
} utb;
extern char *getenv();
/* Determine the time when last called */
cp = getenv ("HOME");
if (cp == NULL) {
fprintf (stderr, "Cannot find HOME variable\n");
exit (1);
}
strcpy (fname, cp);
strcat (fname, "/");
strcat (fname, ".news_time");
cutoff = stat (fname, &newstime) < 0? 0: newstime.st_mtime;
/* Print the recent items */
for (i=0; i<n_count && n_list[i].n_time > cutoff; i++) {
(*emit) (n_list[i].n_name);
number_read++;
}
(*emit) ((char *) NULL);
fflush (stdout);
if (update) {
/* Re-create the file and refresh the update time */
if (n_count > 0 && (fd = creat (fname, RD_WR_ALL)) >= 0) {
utb.actime = utb.modtime = n_list[0].n_time;
close (fd);
utime (fname, &utb);
}
}
}
void
notify(char *s)
{
static int first = 1;
if (s) {
if (first) {
first = 0;
printf ("news:", NEWS);
}
printf (" %.14s", s);
} else if (!first)
putchar ('\n');
}
/*ARGSUSED*/
void
count(char *s)
{
static int nitems = 0;
if (s)
nitems++;
else {
if (nitems) {
printf ("%d news item", nitems);
if (nitems != 1)
putchar ('s');
printf (".\n");
}
else printf("No news.\n");
}
}
void
onintr()
{
sleep(2);
longjmp(save_addr, 1);
}
int
ck_num(void)
{
if (sopt && !number_read) printf("No news.\n");
return(0);
}
|