home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / clock / settd.c < prev    next >
C/C++ Source or Header  |  1994-03-04  |  5KB  |  177 lines

  1. typedef int FILE;
  2. #define CMOS_ADR 0x70
  3. #define CMOS_DATA 0x71
  4. #define CMOS_BYTES 64
  5. extern char *index();
  6. extern int _rax, _rbx, _rcx, _rdx;
  7. int RAX, RBX, RCX, RDX;
  8. char *p; char cmosfilename[] = "\\CMOSINFO";
  9. char cmos[CMOS_BYTES]; 
  10. int hour,minute,second; 
  11. int day,month,year,century; 
  12. int dayofweek; 
  13. char *monthname[] = {"Jan", 
  14.                     "Feb",
  15.                     "Mar",
  16.                     "Apr",
  17.                     "May",
  18.                     "Jun",
  19.                     "Jul",
  20.                     "Aug",
  21.                     "Sep",
  22.                     "Oct",
  23.                     "Nov",
  24.                     "Dec"
  25.                    };
  26. char *dayname[] = {"Sunday",
  27.                   "Monday",
  28.                   "Tuesday",
  29.                   "Wednesday",
  30.                   "Thursday",
  31.                   "Friday",
  32.                   "Saturday"
  33.                  };
  34.  
  35. main(argc,argv)
  36. int argc;
  37. char *argv[];
  38. {int i,j,flag;
  39.  FILE *fp;
  40.  if (argc < 2)
  41.    {printf("\n");
  42.     printf("\nThis program will save and restore the AT's CMOS data, which");
  43.     printf("\nincludes, but is not limited to, the configuration, and the");
  44.     printf("\ntime and date.  This program will also let you change the time");
  45.     printf("\nand date (The DOS DATE and TIME commands do not work).");
  46.     printf("\n");
  47.     printf("\nTo save the configuration (do this before your battery dies)");
  48.     printf("\nC:>settd /s (will store configuration in file \\CMOSINFO.)");
  49.     printf("\n");
  50.     printf("\nTo restore the configuration (do this after you replace your battery)");
  51.     printf("\nC:>settd /r (will restore from file \\CMOSINFO.)");
  52.     printf("\n");
  53.     printf("\nTo set the time and date (either one is optional):");
  54.     printf("\nC:>settd hh:mm:ss dd-mm-yy");
  55.     printf("\n");
  56.     printf("\nTo display the current time and date:");
  57.     printf("\nC:>settd /d");
  58.     printf("\n");
  59.     exit(0);
  60.    }
  61.  for (i=1; i<argc; ++i)
  62.    {p = index(argv[i],'/');
  63.     if (p != 0)
  64.       {flag = toupper(*(p+1));
  65.        switch(flag)
  66.          {case 'S': /* Save CMOS info */
  67.                     for (j = 0; j<CMOS_BYTES; ++j)
  68.                        {_outb(j,CMOS_ADR);         /* Tell CMOS which byte */
  69.                         j = j;                     /* Kill some time */
  70.                         cmos[j] = _inb(CMOS_DATA); /* Read byte from CMOS */
  71.                        }
  72.                     fp = creat(cmosfilename);
  73.                     write(fp,cmos,CMOS_BYTES);
  74.                     close(fp);
  75.                     break;
  76.  
  77.           case 'R': /* Restore CMOS info */
  78.                     get_current_td();
  79.                     fp = open(cmosfilename,0);
  80.                     read(fp,cmos,CMOS_BYTES);
  81.                     close(fp);
  82.                     for (j = 0; j<CMOS_BYTES; ++j)
  83.                        {_outb(j,CMOS_ADR);         /* Tell CMOS which byte */
  84.                         j = j;                     /* Kill some time */
  85.                         _outb(cmos[j],CMOS_DATA);  /* Send byte to CMOS */
  86.                        }
  87.                     set_current_td();
  88.                     break;
  89.  
  90.           case 'D': /* Display current time and date */
  91.                     get_current_td();
  92.                     printf("\n\n%02d:%02d:%02d  %s  %d %s %d\n\n"
  93.                           ,hour
  94.                           ,minute
  95.                           ,second
  96.                           ,dayname[dayofweek]
  97.                           ,day
  98.                           ,monthname[month-1]
  99.                           ,century * 100 + year
  100.                           );
  101.                     break;
  102.          }
  103.       }
  104.     if (index(argv[i],':') != 0) /* Set time */
  105.       {sscanf(argv[i],"%d:%d:%d",&hour,&minute,&second);
  106.        set_current_t();
  107.       }
  108.     if (index(argv[i],'-') != 0) /* Set date */
  109.       {sscanf(argv[i],"%d-%d-%d",&day,&month,&year);
  110.        if (month > 12) {j = month; month = day; day = j;}
  111.        if (year < 100) year += 1900;
  112.        if (year < 1980) year += 100;
  113.        century = year / 100;
  114.        year %= 100;
  115.        set_current_d();
  116.       }
  117.    }
  118.  
  119. get_current_td()
  120. {
  121.  RAX = 0x2A00;
  122.  _rax = RAX;
  123.  _doint(0x21);
  124.  RAX = _rax;
  125.  RCX = _rcx;
  126.  RDX = _rdx;
  127.  dayofweek = RAX & 0x00FF;
  128.  century = RCX / 100;
  129.  year = RCX % 100;
  130.  month = (RDX >> 8) & 0x00FF;
  131.  day = RDX & 0x00FF;
  132.  RAX = 0x2C00;
  133.  _rax = RAX;
  134.  _doint(0x21);
  135.  RAX = _rax; RCX = _rcx; RDX = _rdx;
  136.  hour = (RCX >> 8) & 0x00FF;
  137.  minute = RCX & 0x00FF;
  138.  second = (RDX >> 8) & 0x00FF;
  139. }
  140.  
  141. set_current_td()
  142. {set_current_t();
  143.  set_current_t();
  144. }
  145.  
  146. set_current_t()
  147. {
  148.  RCX = (((hour/10) << 4) + (hour % 10)) << 8;
  149.  RCX += ((minute/10) << 4) + (minute % 10);
  150.  RDX = (((second/10) << 4) + (second % 10)) << 8;
  151.  RAX = 0x0300;
  152.  _rax = RAX; _rcx = RCX; _rdx = RDX;
  153.  _doint(0x1A);
  154.  RCX = (hour << 8) + minute;
  155.  RDX = second << 8;
  156.  RAX = 0x2D00;
  157.  _rax = RAX; _rcx = RCX; _rdx = RDX;
  158.  _doint(0x21);
  159. }
  160.  
  161. set_current_d()
  162. {
  163.  RCX = (((century/10) << 4) + (century % 10)) << 8;
  164.  RCX += ((year/10) << 4) + (year % 10);
  165.  RDX = (((month/10) << 4) + (month % 10)) << 8;
  166.  RDX += ((day/10) << 4) + (day % 10);
  167.  RAX = 0x0500;
  168.  _rax = RAX; _rcx = RCX; _rdx = RDX;
  169.  _doint(0x1A);
  170.  RCX = century * 100 + year;
  171.  RDX = (month << 8) + day;
  172.  RAX = 0x2B00;
  173.  _rax = RAX; _rcx = RCX; _rdx = RDX;
  174.  _doint(0x21);
  175. }
  176.