home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / DATE.CMM < prev    next >
Text File  |  1995-11-21  |  5KB  |  161 lines

  1. /*
  2. **    date.cmm
  3. **
  4. **    This CMM example file is an implementation of the DOS date
  5. **    command. It is meant to be a part of the CMM windows command
  6. **    interpretor, while at the same time showing the power of CMM.
  7. **    It demonstrates the use of calls into DOS at the interupt level.
  8. **
  9. */
  10. main(argc,argv)
  11. {
  12.    // At least Print the date if it's not DOS or Windows
  13.    if(!defined(_DOS_)&&!defined(_DOS32_)&&!defined(_WINDOWS_))
  14.    {
  15.       strftime(buf,"%a %b %d %X %Z %Y\n",localtime(time()));
  16.       printf("%s",buf);
  17.       exit(0);
  18.    }
  19.  
  20.    /* get the current system date */
  21.    /* use DOS call 0x2a to get the date */
  22.    regs.ah = 0x2a;
  23.  
  24.    interrupt(0x21,regs, outregs);
  25.  
  26.    //   this is the set of return values from DOS call 2a
  27.    //   Day    = outregs.dl;
  28.    //   Month  = outregs.dh;
  29.    //   Year   = outregs.cx;
  30.    //   dayOfWeek = outregs.al;
  31.  
  32.    /* if the new date wasn't entered, prompt for it */
  33.    if(argc<2)
  34.    {
  35.       printf("Current date is %s %02d-%02d-%d\n",GetDay(outregs.al),
  36.       outregs.dh, outregs.dl, outregs.cx );
  37.  
  38.       printf("Enter new date (mm-dd-yy): ");
  39.       gets(dateString);
  40.    }
  41.    else if (argc == 2)
  42.    {
  43.       /* try the command line argument for a new date */
  44.       dateString = argv[1];
  45.    }
  46.    else 
  47.       GiveInstructionsAndExit();
  48.  
  49.    while(dateString[0] != '\0')
  50.    {
  51.       dateValid = IsDateValid(dateString);
  52.       
  53.       /* if the date isn't valid, let the user know */
  54.       if(dateValid == FALSE)
  55.       {
  56.          dateString[0] = '\0';
  57.          printf("\nInvalid date\n");
  58.          printf("Enter new date (mm-dd-yy): ");
  59.          gets(dateString);
  60.       }
  61.       else
  62.       {
  63.          /* get the pieces of the date */
  64.          sscanf(dateString,"%d-%d-%d",newMonth,newDay,newYear);
  65.  
  66.          /* print the new date */
  67.          printf("New date is %02d-%02d-%d\n",newMonth,newDay,newYear);
  68.  
  69.          /* use DOS call 0x2b to set the new date */
  70.          regs.dl = newDay;
  71.          regs.dh = newMonth;
  72.          regs.cx = newYear;
  73.          regs.ah = 0x2b;
  74.  
  75.          interrupt(0x21,regs);
  76.  
  77.          /* setting the string to null allows us to exit */
  78.          dateString[0] = '\0';
  79.       }
  80.    }
  81. }
  82. GiveInstructionsAndExit()  // Show some info about using this program
  83. {                          // and then exit this program. Do not return.
  84.    printf("date.cmm - CEnvi date utility.\n");
  85.    printf("           This utility requires no more than 1 argument. In general,\n");
  86.    printf("           either the new date mm-dd-yy or no argument. If no new date is \n");
  87.    printf("           the user will be prompted for one. \n");
  88.    printf("           For Example:\n");
  89.    printf("                        date\n");
  90.    printf("                        date 08-05-1966\n");
  91.    exit(EXIT_FAILURE);
  92.  
  93. }
  94.  
  95. /* This function is used to determine if a given date fits the format
  96.    mm-dd-yyyy. It is used for verifying a new system date. 
  97.    On PC's a valid year is between 1980 and 2099 */
  98. IsDateValid(dateString)
  99. {
  100.    dateValid = FALSE;
  101.  
  102.    /* use sscanf to get the parts of the date */
  103.    if(sscanf(dateString,"%d-%d-%d",newMonth,newDay,newYear) == 3)
  104.    {
  105.       /* check the month and the year */
  106.       if((newMonth>0)&&(newMonth<=12)&&(newYear>=1980)&&(newYear<2100))
  107.       {
  108.          /* now check the number of days in the month */
  109.          switch(newMonth)
  110.          {
  111.             /* first the 31 day months */
  112.             case 1:       // January
  113.             case 3:       // March
  114.             case 5:       // May
  115.             case 7:       // July
  116.             case 8:       // August
  117.             case 10:      // October
  118.             case 12:      // December
  119.                if((newDay > 0)&&(newDay <= 31))
  120.                dateValid = TRUE;
  121.                break;
  122.  
  123.             /* Now the 30 day months */
  124.             case 4:       // April
  125.             case 6:       // June
  126.             case 9:       // September
  127.             case 11:      // November
  128.                if((newDay > 0)&&(newDay <= 30))
  129.                dateValid = TRUE;
  130.                break;
  131.  
  132.             case 2:       // February
  133.                // account for leap year 
  134.                if(((newDay > 0)&&(newDay <= 28))||
  135.                   ((newDay > 0)&&(newDay<=29)&&((newYear % 4)==0)))
  136.                      dateValid = TRUE;
  137.                break;
  138.          }
  139.       }
  140.    }
  141.    return(dateValid);
  142. }
  143. /* convert a day represented from 0-6 to a string for display */
  144. GetDay(integerDay)
  145. {
  146.    if(integerDay == 0)
  147.       return("Sun");
  148.    if(integerDay == 1)
  149.       return("Mon");
  150.    if(integerDay == 2)
  151.       return("Tue");
  152.    if(integerDay == 3)
  153.       return("Wed");
  154.    if(integerDay == 4)
  155.       return("Thu");
  156.    if(integerDay == 5)
  157.       return("Fri");
  158.    if(integerDay == 6)
  159.       return("Sat");
  160. }
  161.