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 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
|
/* news.c - display system news
* Copyright (C) 1993-6 Charles (int@link.xs4all.nl)
* Copyright (C) 1996 Dominik Kubla, <dominik@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef lint
static char rcsid[] = "$Id: news.c,v 1.12 1997/01/10 17:09:19 int Exp $";
#endif
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <utime.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include "getopt.h"
#include "news.h"
char *progname;
const char *newsdir = NEWSDIR;
const char *dformat = DATEFORMAT;
char **exclude_list = 0;
int nr_exclude = 0;
static struct option long_options[] =
{
{ "all", no_argument, 0, 'a' },
{ "datestamp", no_argument, 0, 'd' },
{ "datefmt", required_argument, 0, 'D' },
{ "expire", required_argument, 0, 'e' },
{ "newsdir", required_argument, 0, 'f' },
{ "articles", no_argument, 0, 's' },
{ "names", no_argument, 0, 'n' },
{ "oneperline", no_argument, 0, 'l' },
{ "page", no_argument, 0, 'p' },
{ "pager", required_argument, 0, 'P' },
{ "verbose", no_argument, 0, 'v' },
{ "version", no_argument, 0, 'V' },
{ "exclude", required_argument, 0, 'x' },
{ "help", no_argument, 0, 'h' },
{ NULL, 0, 0, 0 },
};
/*
* MAIN
*/
int main(int argc, char **argv)
{
int ac = 0, n, i;
char *av[32], **nav, *env;
progname = (char *)argv[0];
f.expire = -1;
if((env = getenv("SYSNEWSFLAGS")))
{
ac = implode(env, av, 32);
nav = (char **)malloc( sizeof(char *) * (ac + argc) );
for(i = n = 0; n < argc; n++)
nav[n] = argv[i++];
for(i = 0; n < (argc + ac); n++)
nav[n] = av[i++];
}
else
nav = argv;
parse_args(argc + ac, nav);
if(f.expire >= 0)
do_expire(f.expire);
else
read_sysnews(argc, argv);
exit(EXIT_SUCCESS);
}
int implode(char *str, char *av[], int max)
{
char c;
int ac;
for(ac = 0; ac < max; )
{
while(isspace(*str))
str++;
if(!*str)
break;
av[ac++] = str;
while(*str && !isspace(*str))
str++;
c = *str;
*str++ = '\0';
if(!c)
break;
}
return ac;
}
void parse_args(int argc, char **argv)
{
int ch;
while ((ch = getopt_long(argc, argv, "adD:e:f:snlpPvx:h",
long_options, 0)) != EOF)
switch(ch)
{
case 'a': /* show all articles */
f.all = 1;
break;
case 'd': /* show date of article */
f.datestamp = 1;
break;
case 'D': /* date format */
dformat = optarg;
break;
case 'e': /* expire articles */
f.expire = atoi(optarg);
break;
case 'f': /* use alternate newsdir */
newsdir = optarg;
break;
case 's': /* show # of articles */
f.items = 1;
break;
case 'n': /* show names of articles */
f.names = 1;
break;
case 'l': /* show one article per line */
f.oneperline = 1;
break;
case 'p': /* pipe through $PAGER */
f.page = 1;
break;
case 'P':
f.pager = optarg;
f.page = 1;
break;
case 'v':
f.verbose = 1;
break;
case 'x':
add_exclude(optarg);
break;
case 'V':
printf("news version %s\n", VERSION);
exit(EXIT_SUCCESS);
case 'h': /* hm. what could this be? */
print_usage();
exit(EXIT_SUCCESS);
default:
fprintf(stderr, "Type `%s --help' for more information.\n", progname);
exit(EXIT_FAILURE);
break;
}
}
void read_sysnews(int argc, char **argv)
{
DIR *dfp;
FILE *pfp = 0;
struct dirent *dir;
struct stat st;
struct passwd *pw;
time_t news_time = 0;
char *home, line[1024];
char tmp[512];
char *newsnames;
int art_cnt = 0, avc;
unsigned short cols = 80;
int ret;
/* Clearout temporary data space. */
(void) memset(tmp, 0, 512);
home = getenv("HOME");
if(!home)
{
fprintf(stderr, "%s: error: you are homeless!\n", progname);
exit(EXIT_FAILURE);
}
sprintf(line, "%s/%s", home, NEWSTIME);
ret = stat(line, &st);
if(ret < 0)
{
if(errno != ENOENT)
exit(EXIT_FAILURE);
if(!f.all && !f.items && !f.names && !argv[optind])
{
ret = creat(line, 0600);
close(ret);
}
}
else
news_time = st.st_mtime;
#ifdef TIOCGWINSZ
if(!f.names && !f.oneperline)
{
struct winsize win;
if(ioctl(1, TIOCGWINSZ, &win) != -1)
cols = win.ws_col;
}
#endif
errno = 0;
newsnames = malloc((size_t)cols);
if (newsnames == NULL)
{
perror("news: ");
exit(EXIT_FAILURE);
}
memset(newsnames, 0, cols);
if(!f.all && !f.items && !f.names && !argv[optind])
{
ret = utime(line, 0);
if(ret < 0)
{
fprintf(stderr, "%s: (%s) %s\n", progname, line, ERRMSG);
}
}
dfp = opendir(newsdir);
if(!dfp)
{
fprintf(stderr, "%s: opendir(%s): %s\n", progname, newsdir, ERRMSG);
exit(EXIT_FAILURE);
}
while((dir = readdir(dfp)))
{
if(dir->d_name[0] == '.') /* skip all names starting with '.' */
continue;
if(dir->d_name[ strlen(dir->d_name) - 1 ] == '~')
continue; /* skip all names ending with '~' */
if(argv[optind])
{
avc = optind;
for(avc = optind; avc < argc; avc++)
{
if(strstr(dir->d_name, argv[avc]))
break;
}
if(!argv[avc])
continue;
}
sprintf(line, "%s/%s", newsdir, dir->d_name);
ret = stat(line, &st);
if(ret < 0)
{
fprintf(stderr, "%s: (%s) %s\n", progname, line, ERRMSG);
continue;
}
if (S_ISDIR(st.st_mode)) /* Ignore directories */
continue;
if(news_time < st.st_mtime || f.all || argv[optind])
{
if(!f.names && !f.items)
{
if(f.page && (!pfp))
{
pfp = open_pager();
if(!pfp)
exit(EXIT_FAILURE);
}
pw = getpwuid(st.st_uid);
fprintf(pfp ? pfp : stdout, "\n** %s (%s) %s\n", dir->d_name,
pw ? pw->pw_name : "unknown", ctime(&st.st_mtime));
fflush(pfp ? pfp : stdout);
if(f.page)
more(pfp, line);
else
cat(line);
}
if(f.names)
{
if(f.oneperline)
{
if(f.datestamp)
{
strftime(tmp, 512, dformat, gmtime(&st.st_mtime));
printf("NEWS: %s %s\n", tmp, dir->d_name);
}
else
{
printf("NEWS: %s\n", dir->d_name);
}
}
else
{
if((strlen(dir->d_name) +
strlen(newsnames) +
strlen(progname) + 1 + 2) > cols)
{
printf("NEWS: %s\n", newsnames);
strcpy(newsnames, "");
}
strcat(newsnames, dir->d_name);
strcat(newsnames, " ");
}
}
art_cnt++;
}
}
if(f.names && art_cnt && newsnames[0])
printf("NEWS: %s\n", newsnames);
if(f.items)
printf("NEWS: %d news article%s\n", art_cnt, (art_cnt == 1) ? "" : "s");
closedir(dfp);
if(pfp)
pclose(pfp);
exit(art_cnt ? 0 : 2);
}
/**
*** print a file to stdout
**/
int cat(char *file)
{
FILE *fp;
fp = fopen(file, "r");
if(!fp)
{
fprintf(stderr, "%s: (%s) %s\n", progname, file, ERRMSG);
return ERR;
}
fcat(fileno(fp), fileno(stdout));
fclose(fp);
return OK;
}
/**
*** pipe a file through $PAGER
**/
int more(FILE *pfp, char *file)
{
FILE *fp;
fp = fopen(file, "r");
if(!fp)
{
fprintf(stderr, "%s: (%s) %s\n", progname, file, ERRMSG);
return ERR;
}
fcat(fileno(fp), fileno(pfp));
fclose(fp);
return OK;
}
/**
*** setup a pipe to $PAGER, f.pager or DEF_PAGER
**/
FILE *open_pager()
{
FILE *pfp;
const char *pager;
if(!(pager = f.pager) && !(pager = getenv("PAGER")))
pager = DEF_PAGER;
pfp = popen(pager, "w");
if(!pfp)
{
fprintf(stderr, "%s: popen(%s) failed: %s\n", progname, pager, ERRMSG);
return 0;
}
return pfp;
}
int fcat(int fpin, int fpout)
{
int i;
char buf[1024];
while((i = read(fpin, buf, 1024)))
{
if(i == -1)
{
fprintf(stderr, "%s: %s\n", progname, ERRMSG);
return ERR;
}
write(fpout, buf, i);
}
return OK;
}
/**
*** expire old news
**/
void do_expire(int expire)
{
DIR *dfp;
struct stat st;
struct dirent *dir;
time_t cur_t, exp_t;
int i, ret;
dfp = opendir(newsdir);
if(!dfp)
{
fprintf(stderr, "Error opening newsdir (%s): %s\n", newsdir, ERRMSG);
exit(EXIT_FAILURE);
}
chdir(newsdir);
create_exclude_list();
#ifdef DEBUG
for(i = 0; i < nr_exclude; i++)
printf("#%02d: \"%s\"\n", i, exclude_list[i]);
#endif
time(&cur_t);
exp_t = cur_t - (expire * (60 * 60 * 24));
while((dir = readdir(dfp)))
{
if(dir->d_name[0] == '.')
continue;
for(i = 0; i < nr_exclude; i++)
if(!strcmp(exclude_list[i], dir->d_name))
break;
if(i<nr_exclude)
continue;
if(stat(dir->d_name, &st))
continue;
if(st.st_mtime < exp_t)
{
ret = unlink(dir->d_name);
if(ret < 0)
{
fprintf(stderr, "Error: remove (%s): %s\n", dir->d_name, ERRMSG);
}
else
{
if(f.verbose)
printf("Expiring article: %s\n", dir->d_name);
}
}
}
closedir(dfp);
}
void create_exclude_list(void)
{
FILE *fp;
char buf[BUFSIZ];
fp = fopen(NOEXPFILE, "r");
if(!fp)
return;
while(fgets(buf, BUFSIZ, fp))
{
if(buf[0] == '#' || buf[0] == ';') /* skip comments */
continue;
add_exclude(buf);
}
fclose(fp);
}
void add_exclude(char *str)
{
char line[NAME_MAX];
int lineidx = 0;
int st = 0;
while(1)
{
switch(*str)
{
case ' ':
break;
case '\0':
case '\r':
case '\n':
case ',':
if(st)
{
line[lineidx] = 0;
lineidx = 0;
st = 0;
errno = 0;
if(!nr_exclude)
exclude_list = (char **)malloc(sizeof(char *));
else
exclude_list = (char **)realloc(exclude_list,
sizeof(char *) * (nr_exclude + 1));
if (exclude_list == NULL) {
perror("news: ");
exit(EXIT_FAILURE);
}
exclude_list[nr_exclude++] = strdup(line);
}
if(!*str)
return;
break;
default:
st = 1;
line[lineidx++] = *str;
if(lineidx >= NAME_MAX)
{
fprintf(stderr, "add_exclude: filename too long (%d)\n", lineidx);
exit(EXIT_FAILURE);
}
break;
}
str++;
}
}
void print_usage(void)
{
printf("news v%s - display system news - (C) 1993-96 int@link.xs4all.nl\n", VERSION);
printf("Usage: %s [-flags] [[article1] [article2] .. ]\n\n", progname);
printf(" -a, --all display all news\n");
printf(" -d, --datestamp add datestamp\n");
printf(" -D, --datefmt=FMT date format (see strftime(3) for details)\n");
printf(" -f, --newsdir=DIR use alternate newsdir\n");
printf(" -n, --names display news article names only\n");
printf(" -l, --oneperline one article name per line\n");
printf(" -p, --page pipe article through $PAGER\n");
printf(" -P, --pager=PAGER pipe article through PAGER\n");
printf(" -s, --articles display number of news articles\n");
if(!access(newsdir, W_OK))
{
printf("\n");
printf(" -e, --expire=DAYS expire news older than # DAYS\n");
printf(" -x, --exclude=A,B list of articles to exclude when expiring\n");
}
printf("\n");
printf(" --help display this help text\n");
printf(" --version display version\n");
}
|