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

  1. /*
  2.    TIME.C - Program to change system time
  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.  
  25. */
  26.  
  27. #include <stdio.h>
  28. #include "freedos.h"
  29. #include "getopt.c"
  30. #include "country.c"
  31.  
  32. char *Months[] = 
  33.     {"January", "February", "March", "April", "May", "June", "July",
  34.     "August", "September", "October", "November", "December"};
  35.  
  36. char *Weekdays[] = 
  37.     {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", 
  38.     "Friday", "Saturday"};
  39.  
  40. void usage (void);
  41. void F_puttime (struct country Cinfo);
  42. void F_settime (char *Buf);
  43.  
  44. main (int argc, char **argv)
  45. {
  46.     int i;
  47.     char Buf[MAX_STR];
  48.     struct country Cinfo;
  49.  
  50.     get_country (Cinfo);
  51.  
  52.     /* Scan the command line */
  53.     while ((i = getopt (argc, argv, "D?", "")) != EOF)
  54.     {
  55.     switch (i)
  56.         {
  57.         case 'D':
  58.             F_puttime (Cinfo);
  59.             exit (0);        
  60.         default:
  61.             usage ();
  62.             break;
  63.         }
  64.     }
  65.  
  66.     /* Set time if specified on command line */
  67.     if ((argc - optind) != 0)
  68.         {
  69.         strcpy (Buf, "");
  70.         for (i = optind; i < argc; i++)
  71.             {
  72.             strcat (Buf, argv[i]);    /* Get all args on command line */
  73.             strcat (Buf, " ");    /* We have to keep args separated */
  74.             }
  75.         F_settime (Buf);
  76.         F_puttime (Cinfo);
  77.         }
  78.  
  79.     /* Input a string for time and process that, then */
  80.     else
  81.         {
  82.         F_puttime (Cinfo);        /* Get the current date */
  83.  
  84.         /* Get the new date, make prompt comform to country style*/
  85.         printf ("New time (HH%sMM%sSS): ", 
  86.             Cinfo.co_tmsep, Cinfo.co_tmsep);
  87.  
  88.         if (strlen(gets (Buf)))
  89.             {
  90.             F_settime (Buf);
  91.             F_puttime (Cinfo);
  92.             }
  93.         }
  94.     exit (0);
  95. }
  96.  
  97. void F_puttime (struct country Cinfo)
  98.  
  99.     {
  100.     int Year, Month, Day, Hour, Min, Sec, Dayofweek;
  101.  
  102.     Dayofweek = Weekdays[get_date (&Day, &Month, &Year)];
  103.     get_time (&Hour, &Min, &Sec);
  104.  
  105.     printf ("%s %02d %s %04d %02d%s%02d%s%02d\n",
  106.         Dayofweek, Day, Months[Month-1], Year,
  107.         Hour, Cinfo.co_tmsep, Min, Cinfo.co_tmsep, Sec);
  108.     }
  109.  
  110. void F_settime (char *Buf)
  111.  
  112. {
  113.  
  114. int Hrs, Min, Sec, PM;
  115.  
  116. PM = FALSE;
  117.  
  118. /* Determine if time is AM or PM */
  119. if (strchr (Buf, 'P') || strchr (Buf, 'p'))
  120.     PM = TRUE;
  121.  
  122. /* Peel off each argument from the string */
  123. Hrs = atoi(strtok(Buf, " .,-:/\\\t"));
  124. Min = atoi(strtok(NULL, " .,-:/\\\tAPMapm"));
  125. Sec = atoi(strtok(NULL, " .,-:/\\\tAPMapm"));
  126.  
  127. /* if we have a PM time, convert it to 24 hour format */
  128. if (PM)
  129.     {
  130.     if (Hrs < 12) Hrs = Hrs + 12;
  131.     if (Hrs == 12) Hrs = 0;
  132.     }
  133.  
  134. /* Set the date */
  135.     if ((set_time (Hrs, Min, Sec)) == -1)
  136.     {
  137.     printf ("Invalid Time: %s\n", Buf);
  138.     abort (1);
  139.     }
  140.  
  141. return TRUE;
  142. }
  143.  
  144. void usage (void)
  145.  
  146.     {
  147.     printp ("TIME", "Display and change system date");
  148.     printc ("1995", "James Hall and M. \"Hannibal\" Toal");
  149.     printu ("TIME", "[/D] [mm/dd/yy]");
  150.     printo ("D", "Display time only, do not change");
  151.     exit (1);
  152.     }
  153.