home *** CD-ROM | disk | FTP | other *** search
/ Zodiac Super OZ / MEDIADEPOT.ISO / FILES / 16 / FREEDOS.ZIP / FD_A4PRE.ZIP / SOURCE / MICROC.ZIP / DATE.C < prev    next >
C/C++ Source or Header  |  1995-05-25  |  4KB  |  160 lines

  1. /*
  2.    DATE.C - Program to change system date
  3.    Original code by: James Hall
  4.    Ported to MICRO-C by: M. "Hannibal" Toal
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.    Changes:
  21.  
  22.    JH    Jan 95        Original Program in MS-C
  23.    MT   Apr 95        Ported to MICRO-C
  24.             Added support for international date formats
  25.  
  26. */
  27.  
  28. #include <stdio.h>
  29. #include "freedos.h"
  30. #include "getopt.c"
  31. #include "country.c"
  32.  
  33. char *Months[] = 
  34.     {"January", "February", "March", "April", "May", "June", "July",
  35.     "August", "September", "October", "November", "December"};
  36.  
  37. char *Weekdays[] = 
  38.     {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", 
  39.     "Friday", "Saturday"};
  40.  
  41. char *Datestyle[] = {"mm%syy%sdd", "dd%smm%syy", "yy%smm%sdd"};
  42.  
  43. void usage (void);
  44. void F_putdate (struct country Cinfo);
  45. void F_setdate (struct country Cinfo, char *Buf);
  46.  
  47. main (int argc, char **argv)
  48.     {
  49.     int i;
  50.     char Buf[MAX_STR];
  51.     struct country Cinfo;
  52.  
  53.     get_country (Cinfo);
  54.  
  55.     /* Scan the command line */
  56.     while ((i = getopt (argc, argv, "D?", "")) != EOF)
  57.         {
  58.         switch (i)
  59.             {
  60.             case 'D':
  61.                 F_putdate (Cinfo);
  62.                 exit (0);        
  63.             default:
  64.                 usage ();
  65.                 break;
  66.             }
  67.         }
  68.  
  69.     /* Check if there are enough args left */
  70.     if ((argc - optind) != 0)
  71.         {
  72.         strcpy (Buf, "");
  73.         for (i = optind; i < argc; i++)
  74.             {
  75.             strcat (Buf, argv[i]);
  76.             strcat (Buf, " ");
  77.             }
  78.         F_setdate (Cinfo, Buf);
  79.         F_putdate (Cinfo);
  80.         }
  81.  
  82.     else
  83.         {
  84.         /* Get the current date */
  85.         F_putdate (Cinfo);
  86.  
  87.         sprintf (Buf, Datestyle[Cinfo.co_datefmt], 
  88.             Cinfo.co_dtsep, Cinfo.co_dtsep);
  89.  
  90.         /* Get the new date */
  91.         printf ("New date (%s): ", Buf);
  92.  
  93.         if (strlen(gets (Buf)))
  94.             {
  95.             F_setdate (Cinfo, Buf);
  96.             F_putdate (Cinfo);
  97.             }
  98.         }
  99.     exit (0);
  100.     }
  101.  
  102. void F_setdate (struct country Cinfo, char *Buf)
  103.  
  104.     {
  105.     int n1, n2, n3, Month, Day, Year;
  106.  
  107.     n1 = atoi(strtok(Buf, " .,-/\\\t"));
  108.     n2 = atoi(strtok(NULL, " .,-/\\\t"));
  109.     n3 = atoi(strtok(NULL, " .,-/\\\t"));
  110.  
  111.     switch (Cinfo.co_datefmt)
  112.         {
  113.         case 1:
  114.         Day = n1; Month = n2; Year = n3; break;
  115.  
  116.         case 2:
  117.         Year = n1; Month = n2; Day = n3; break;
  118.  
  119.         default:
  120.         Month = n1; Day = n2; Year = n3; break;
  121.         }
  122.  
  123.     /* Is it a 2 digit date? */
  124.     if (Year <= 79) Year += 2000; 
  125.     else if (Year <= 99) Year += 1900;
  126.  
  127.     /* Set the date */
  128.     if ((set_date (Day, Month, Year)) == -1)
  129.         {
  130.         printf ("Invalid Date: %s\n", Buf);
  131.         abort (1);
  132.         }
  133.  
  134.         return TRUE;
  135.     }
  136.  
  137. void F_putdate (struct country Cinfo)
  138.  
  139.     {
  140.     int Year, Month, Day, Hour, Min, Sec, Dayofweek;
  141.  
  142.     Dayofweek = Weekdays[get_date (&Day, &Month, &Year)];
  143.     get_time (&Hour, &Min, &Sec);
  144.  
  145.     printf ("%s %02d %s %04d %02d%s%02d%s%02d\n",
  146.         Dayofweek, Day, Months[Month-1], Year,
  147.         Hour, Cinfo.co_tmsep, Min, Cinfo.co_tmsep, Sec);
  148.     }
  149.  
  150.  
  151. void usage (void)
  152.  
  153.     {
  154.     printp ("DATE", "Display and change system date");
  155.     printc ("1995", "James Hall and M. \"Hannibal\" Toal");
  156.     printu ("DATE", "[/D] [mm/dd/yy]");
  157.     printo ("D", "Display date only, do not change");
  158.     exit (1);
  159.     }
  160.