DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T m

⟦f408b63c6⟧ TextFile

    Length: 5807 (0x16af)
    Types: TextFile
    Names: »motd.c«

Derivation

└─⟦9ae75bfbd⟧ Bits:30007242 EUUGD3: Starter Kit
    └─⟦653021b30⟧ »EurOpenD3/utils/downtime.tar.Z« 
        └─⟦946c717da⟧ 
            └─⟦this⟧ »motd.c« 

TextFile

/* 
 * Copyright (c) 1988 Michael A. Cooper, University of Southern California.
 * This program may be used, copied, modified, and redistributed freely
 * for noncommercial purposes, so long as this notice remains intact.
 */

#ifndef lint
static char *RCSid = "$Header: motd.c,v 4.2 88/07/05 16:00:45 mcooper Exp $";
#endif

/*
 *------------------------------------------------------------------
 *
 * $Source: /usr/skat3/src/common/usc/etc/downtime/RCS/motd.c,v $
 * $Revision: 4.2 $
 * $Date: 88/07/05 16:00:45 $
 * $State: Exp $
 *
 *------------------------------------------------------------------
 *
 * Michael A. Cooper
 * Research and Development Group
 * University Computing Services 
 * University of Southern California
 * (mcooper@oberon.USC.EDU)
 *
 *------------------------------------------------------------------
 *
 * $Log:	motd.c,v $
 * Revision 4.2  88/07/05  16:00:45  mcooper
 * Added copyright notice.
 * 
 * Revision 4.1  88/05/23  15:40:42  mcooper
 * All tm tm_mon members are now 0-11
 * instead of 1-12 for compat. with 
 * the normal BSD time routines.
 * 
 * Revision 4.0  88/04/20  15:43:03  mcooper
 * Version 4.
 * 
 * Revision 3.6  88/04/19  16:50:28  mcooper
 * Fixed problem in delmotd() that caused trailing 
 * blank lines to sometimes be deleted.
 * 
 * Revision 3.5  88/04/19  16:19:15  mcooper
 * Do more checking for preceeding and trailing blank
 * lines in the motd file.
 * 
 * Revision 3.4  88/03/02  16:10:50  mcooper
 * Cleanup time.
 * 
 * Revision 3.3  87/12/16  14:31:34  mcooper
 * Fixed bug in delmotd() that sometimes
 * caused random blank lines to be deleted
 * from /etc/motd.
 * 
 * Revision 3.2  87/12/16  13:56:51  mcooper
 * Stupid kludge in addmotd to make
 * sun4's happy.
 * 
 * Revision 3.1  87/12/11  18:12:55  mcooper
 * The first line of a motd message now 
 * contains the current date to be consistant
 * with the way we make /etc/motd entries.
 * 
 * Revision 3.0  87/07/24  14:20:51  mcooper
 * Version 3.
 * 
 *------------------------------------------------------------------
 */

#include "defs.h"

#define MOTD_MSG1  "\tSystem downtime scheduled for"
#define MOTD_MSG2  "\tThe system will be down for:"
#define MOTD_MSG3  "\tExpected back up"

#define MSG_LEN(s)  (70 - strlen(s))

/*
 * addmotd - Add a message to the message of the day.
 */

addmotd(dt)
struct downtime *dt;
{
  FILE *fd;
  char *ddate;
  int prepend;

  if (dt == NULL) {
    return(-1);
  }

  prepend = lastblank(MOTD);

  if ((fd = fopen(MOTD, "a")) == NULL) {
    perror(MOTD);
    return(-1);
  }

  ddate = mkdate(dt->dt_orderdown, D_MOTD);
  if (!prepend) {
    fprintf(fd, "\n");
  }
  fprintf(fd, "%s:", ddate);
  fprintf(fd, "%s %s.\n", MOTD_MSG1, mkdate(dt->dt_down, D_VERBOSE));

  if (strncmp(dt->dt_reason, EMPTY, strlen(EMPTY)))
    fprintf(fd, "%s %.*s.\n", 
	    MOTD_MSG2, 
	    MSG_LEN(MOTD_MSG2),
	    dt->dt_reason
	    );
  fprintf(fd, "%s %s.\n\n", MOTD_MSG3, mkdate(dt->dt_up, D_VERBOSE));

  fclose(fd);
  return(0);
}

/*
 * from date.c
 */
extern char *days[];
extern char *months[];

/*
 * delmotd - Delete a motd.
 */

delmotd(dt)
struct downtime *dt;
{
  FILE *fdmotd, *fdtmp;
  char *odate;
  char tmpfile[200], buf[BUFSIZ], firstline[BUFSIZ];
  char format[BUFSIZ], mon[100];
  int minute, hour, mday, year;
  register struct downtime *tmpdt;
  register int z;
  int gotnl;

  tmpdt = (struct downtime *) xmalloc(sizeof(struct downtime));
  tmpdt->dt_down = (struct tm *) xmalloc(sizeof(struct tm));

  if ((fdmotd = fopen(MOTD, "r")) == NULL) {
    perror(MOTD);
    return(-1);
  }

  sprintf(tmpfile, "%s.tmpXXXXXX", MOTD);
  mktemp(tmpfile);
  if ((fdtmp = fopen(tmpfile, "w")) == NULL) {
    perror(tmpfile);
    return(-1);
  }
  
  odate = (char *) mkdate(dt->dt_orderdown, D_MOTD);
  sprintf(firstline, "%s:%s %s.", odate, MOTD_MSG1, 
	  mkdate(dt->dt_down, D_VERBOSE));

  while (fgets(buf, sizeof(buf), fdmotd)) {
    if (isblank(buf)) {
      gotnl = TRUE;
      if (!fgets(buf, sizeof(buf), fdmotd)) {
	fputs("\n", fdtmp);
	break;
      }
    } else {
      gotnl = FALSE;
    }

    if (strncmp(firstline, buf, strlen(firstline))) {
      if (gotnl && !(isblank(buf))) {
	gotnl = FALSE;
	fputs("\n", fdtmp);
      }
      fputs(buf, fdtmp);
      continue;
    }

    sprintf(format, "%s: %s %%*s %%s %%d, %%d at %%d:%%d", odate, MOTD_MSG1);
    sscanf(buf, format, 
	   mon, &mday, &year, &hour, &minute
	   );

    for (z = 0; z < 12; z++) {
      if (strncmp(mon, months[z], strlen(months[z])) == 0)
        tmpdt->dt_down->tm_mon = z;
    }

    tmpdt->dt_down->tm_mday = mday;
    tmpdt->dt_down->tm_year = year;
    tmpdt->dt_down->tm_hour = hour;
    tmpdt->dt_down->tm_min = minute;
    
    if (strcmp(ttod(tmpdt->dt_down), ttod(dt->dt_down)) == 0) {
      /*
       * nuke the "down for" line.
       */
      fgets(buf, sizeof(buf), fdmotd);
      if (strncmp(buf, MOTD_MSG2, strlen(MOTD_MSG2)) &&
	  strncmp(buf, MOTD_MSG3, strlen(MOTD_MSG3))) {
        fputs(buf, fdtmp);
      }
      /*
       * nuke the "Expected up" line.
       */
      fgets(buf, sizeof(buf), fdmotd);
      if (strncmp(buf, MOTD_MSG2, strlen(MOTD_MSG2)) &&
	  strncmp(buf, MOTD_MSG3, strlen(MOTD_MSG3))) {
        fputs(buf, fdtmp);
      }
      continue;
    }
    fputs(buf, fdtmp);
  }

  fclose(fdmotd);
  fclose(fdtmp);

  if (unlink(MOTD) < 0) {
    perror(MOTD);
    return(-1);
  }

  if (rename(tmpfile, MOTD) < 0) {
    fprintf(stderr, "%s --> ", tmpfile);
    perror(MOTD);
    return(-1);
  }

  return(0);
}

/*
 * lastblank - If the last line of file is blank, return 0, else 1.
 */
lastblank(file)
char *file;
{
  FILE *fd;
  char buf[BUFSIZ];

  if ((fd = fopen(file, "r")) == NULL) {
    perror(file);
    return(-1);
  }

  while (fgets(buf, sizeof(buf), fd));

  fclose(fd);

  return(isblank(buf));
}