home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 21 / CD_ASCQ_21_040595.iso / dos / prg / c / freedos3 / source / jh_utils / time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-07  |  2.3 KB  |  114 lines

  1. /*
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.  
  7.    This program is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.    GNU General Public License for more details.
  11.  
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program; if not, write to the Free Software
  14.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.    */
  16.  
  17. /***************************************************************************
  18.  * This program displays the time and allows the user to change it.
  19.  *
  20.  * Author: James Hall
  21.  */
  22.  
  23. #include <dos.h>
  24. #include <stdio.h>
  25. #include <conio.h>
  26. #include <string.h>
  27. #include "getopt.h"
  28. #include "freedos.h"
  29.  
  30.  
  31. void usage (void);
  32.  
  33.  
  34. /* Quick-C structure for the time */
  35.  
  36. struct dostime_t dtime;
  37.  
  38.  
  39. main (int argc, char **argv)
  40. {
  41.   int i;
  42.   unsigned uHour, uMin, uSec;
  43.   char szBuf[MAX_STR];
  44.  
  45.   /* Scan the command line */
  46.  
  47.   while ((i = getopt (argc, argv, "?")) != EOF)
  48.     {
  49.       switch (i)
  50.     {
  51.     default:
  52.       usage ();
  53.       break;
  54.     }
  55.     }
  56.  
  57.   /* Get the current time */
  58.  
  59.   if ((argc - optind) > 0)
  60.     strcpy (szBuf, argv[optind]);
  61.  
  62.   else
  63.     {
  64.       _dos_gettime (&dtime);
  65.       printf ("Current time is: %u:%02u:%02u\n", dtime.hour,
  66.           dtime.minute, dtime.second);
  67.  
  68.       /* Get the new time */
  69.  
  70.       printf ("Enter new time: ");
  71.       gets (szBuf);
  72.     }
  73.  
  74.   /* Parse the new time */
  75.  
  76.   if (strlen (szBuf))
  77.     {
  78.       sscanf (szBuf, "%u:%u:%u", &uHour, &uMin, &uSec);
  79.  
  80.       if (uHour < 24)
  81.     {
  82.       dtime.hour = (unsigned char) uHour;
  83.     }
  84.  
  85.       if (uMin < 60)
  86.     {
  87.       dtime.minute = (unsigned char) uMin;
  88.     }
  89.  
  90.       if (uSec < 60)
  91.     {
  92.       dtime.second = (unsigned char) uSec;
  93.     }
  94.  
  95.       /* Set the time */
  96.       /* _dos_settime from Quick-C sets the time from a structure */
  97.  
  98.       _dos_settime (&dtime);
  99.       _dos_gettime (&dtime);
  100.  
  101.       printf ("New time: %u:%02u:%02u\n",
  102.           dtime.hour, dtime.minute, dtime.second);
  103.     }
  104.   exit (0);
  105. }
  106.  
  107. void 
  108. usage (void)
  109. {
  110.   printp ("TIME", "Displays the time and allows the user to change it.");
  111.   printu ("TIME", "[hh:mm:ss]");
  112.   exit (1);
  113. }
  114.