so, here comes `date'.  I'll post `init.c' when I find it back.

I hope I won't have too much trouble with algorithms owners.  hey! we
can't reinvent the wheel each time any more, we have to buy it!
ciao,
Mario.

/*
 * date  small utility to check and set system time.
 *
 * Usage: /bin/date
 *       date [?[?]] | <date>
 *
 * 1999-11-07  [EMAIL PROTECTED]
 *
 *  Copyright 1999 Mario Frasca
 *
 *  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.
 *
 */

#include <time.h>
#include <sys/time.h>

/* EarlyDate is 1992-01-01T00:00:00 */
#define EARLYDATE 694288800L

/* our own happy mktime() replacement, with the following drawbacks: */
/*    doesn't check boundary conditions */
/*    doesn't set wday or yday */
/*    doesn't return the local time */
time_t utc_mktime(t)
struct tm *t;
{
 static int moffset[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273,
304, 304 };
 time_t ret;

  /* calculate days from years */
 ret = t->tm_year - 1970;
 ret *= 365L;

  /* count leap days in preceding years */
 ret += ((t->tm_year -1969) >> 2);

  /* calculate days from months */
 ret += moffset[t->tm_mon];

  /* add in this year's leap day, if any */
   if (((t->tm_year & 3) == 0) && (t->tm_mon > 1)) {
  ret ++;
   }

  /* add in days in this month */
   ret += (t->tm_mday - 1);

  /* convert to hours */
 ret *= 24L;
   ret += t->tm_hour;

  /* convert to minutes */
   ret *= 60L;
   ret += t->tm_min;

  /* convert to seconds */
   ret *= 60L;
   ret += t->tm_sec;

  /* return the result */
   return ret;
}

void usage()
{
 fputs("date: read or modify current system date\n", stdout);
 fputs("usage: date ?[?]|[yy]yy-m-dTh:m:s\n", stdout);
 exit(1);
}

int main(argc, argv)
char ** argv;
int argc;
{
 time_t systime;
 time(&systime);

 if(argc==1)
 {
  fputs(ctime(&systime), stdout);
 }
 else if (argc>2) usage();
 else
 {
  char *p, buf[32];
  struct tm tm;
  struct timeval tv;

  if(argv[1][0] == '?')
  {
   if((argv[1][1] == '?') && (systime > EARLYDATE))
    return 0;

   /* ask user for current date */
   fputs("insert current date: ", stdout);
   fgets(buf, 31, stdin);
   p = strtok(buf,"-");
  }
  else
   p = strtok(argv[1],"-");

  tm.tm_year= tm.tm_mon= tm.tm_mday= tm.tm_hour= tm.tm_min= tm.tm_sec=0;

  do{
   tm.tm_year = atoi(p);
   if(!(p = strtok(NULL, "-"))) usage();
   tm.tm_mon = atoi(p);tm.tm_mon--;
   if(!(p = strtok(NULL, "T"))) usage();
   tm.tm_mday = atoi(p);
   p = strtok(NULL, ":");
   if(!p) break;
   tm.tm_hour = atoi(p);
   p = strtok(NULL, ":");
   if(!p) break;
   tm.tm_min = atoi(p);
   p = strtok(NULL, "\n");
   if(!p) break;
   tm.tm_sec = atoi(p);
  }while(0); /* only to have that 'break' */
  if(tm.tm_year<70) tm.tm_year+=2000;
  else if(tm.tm_year<100)tm.tm_year+=1900;
  else if(tm.tm_year<1970)
   usage();
  systime = utc_mktime(&tm);
      tv.tv_sec = systime;
      tv.tv_usec = 0;
      if (settimeofday (&tv, NULL) != 0)
  {
   fprintf (stderr,
    "Unable to set time -- probably you are not root\n");
   exit (1);
  }

 }

   return 0;
}

Reply via email to