Beispiel #1
0
int date(char* argstr)
{
  date_rec* date_p;
  int err = 0, d, m, y;
  char *tokenPtr;
  char *months[12] = {"January","February","March","April","May","June","July", "August","September","October","November","December"};
  char *day = sys_alloc_mem(2);
  char *month = sys_alloc_mem(2);
  char *year = sys_alloc_mem(4);

  tokenPtr = strtok(argstr," "); // tokenize those args

  if(tokenPtr == NULL){
    sys_get_date(date_p);
    printf("%s %d %d\n", months[date_p->month], date_p->day, date_p->year);
    return err;
  }
  
  while (tokenPtr != NULL) {    
    if(!strcmp(tokenPtr, "-s\0")){
      tokenPtr = strtok(NULL, " "); // next token plz
      
      if(strlen(tokenPtr) != 8){ // if it ain't 8, we know they f****d up
	printf("Invalid date string.\n");
	break;
      }

      // grab those strings
      strncpy(year, tokenPtr, 4);
      strncpy(month, tokenPtr + 4, 2);
      strncpy(day, tokenPtr + 6, 2);

      // leading zeros? eh, let's make em ints anyway...
      m = atoi(month)-1;
      d = atoi(day);
      y = atoi(year);

      // are they even valid?
      if(validDate(d,m,y)){
	date_p->day = d;
	date_p->month = m;
	date_p->year = y;
	err = sys_set_date(date_p);
	printf("Changing system date to %s %d %d\n", months[m], d, y);
	}
      else{
	printf("Invalid date string.\n");
	break;
      }
    }
    
    if(!strcmp(tokenPtr, "-h\0")){
      printf("Usage of date:\n\tdate -s yyyymmdd\tChange the system date to the date specified.\n");
    }
    
    tokenPtr = strtok(NULL, " ");
  }
  return err;
}
Beispiel #2
0
/**
	Procedure: handler_get_date

	Purpose: Displays System Date

	Parameters: None  

	Return value: Error Code; Zero If Ok

	Calls: sys_get_date    

	Globals: None

	Errors: None
**/
int handler_get_date(){
	//Declare new date_rec variable
	date_rec date;
	
	//Call system function with date address passed in
	sys_get_date(&date);
	
	//Print date by accessing the attributes of the pointer to the struct
	printf("The current date is: %d/%d/%d\n", date.month, date.day, date.year);

	return 0;
}//end handler_get_Date
Beispiel #3
0
void mpxcmd_date( int argc, char *argv[] )
{
	/* Temporary storage for the return value of sys_ functions. */
	int retval;	

	/* Structure to hold a date (day, month, and year). */
	date_rec date;	

	/* Called with no arguments, we should just print the date: */
	if ( argc == 1 ){
		sys_get_date(&date);
		printf("Current MPX system date (yyyy-mm-dd): %04d-%02d-%02d\n",
				date.year, date.month, date.day);
		return;
	}

	/* Called with three arguments, we need to try to set the date
	 * from the values provided by the user. */
	if ( argc == 4 ){

		/* Convert string arguments to integer values: */
		date.year  = atoi(argv[1]);
		date.month = atoi(argv[2]);
		date.day   = atoi(argv[3]);

		/* Check that the user's input represents a valid date: */
		if ( ! mpx_validate_date(date.year, date.month, date.day) ) {
			/* Lines broken confusingly due to 80-column limit: */
			printf("ERROR: Invalid date specified; ");
			printf("MPX system date is unchanged.\n");
			printf("       Valid dates are between 1900-01-01 ");
			printf("and 2999-12-31, inclusive.\n");
			return;
		}

		/* Attempt to set that date, catching errors: */
		retval = sys_set_date(&date);
		if ( retval != 0 ) {
			printf("ERROR: sys_set_date() returned an error.\n");
			return;
		}

		/* Success! */
		printf("The MPX system date has been changed.\n");
		return;
	}

	/* If we get to here, the user invoked the date command incorrectly. */
	printf("ERROR: Wrong number of arguments to 'date'.\n");
	printf("       Type 'help date' for usage information.\n");
}
Beispiel #4
0
/**
	Procedure: handler_set_date

	Purpose: Sets System Date

	Parameters: None 

	Return value: 0

	Calls: keyboardInput, sys_set_date, sys_get_date, errorCheck

	Globals: None

	Errors: Invalid Date, Date Not Changed
**/
int handler_set_date(){
	char *Buffer;
	int month;
	int day;
	int year;
	int flag = 0;
	int leapYear;
	int i = 0;
	
	printf("Enter the date in mmddyyyy format with no slashes or dashes: \n");
	Buffer = keyboardInput(0);
	

	while(i<8){
		if(Buffer[i] < 48 || Buffer[i] > 57){
			//printf("The date you entered is invalid!");
			flag = 1;
		}
		i++;		
		
	}
	if(flag == 0){
		month = (Buffer[0] - 48)*10 + Buffer[1] - 48;
		day = (Buffer[2] - 48)*10 + Buffer[3] - 48;
		year = (Buffer[4] - 48)*1000 + (Buffer[5] - 48)*100 + (Buffer[6] - 48)*10 + Buffer[7] - 48;
		if(year%4 == 0 && year%100 ==0 && year% 400 == 0){
			leapYear = 1;
		}
		if(month > 12|| day < 1 || month < 1){
			flag = 1;
		}
		if(leapYear==0){
			if(month < 13 && month == 02 && day > 28){
				flag = 1;
			}else if(month <13 && month == 1 && day > 31){
				flag = 1;
			}else if(month <13 && month == 3 && day > 31){
				flag = 1;
			}else if(month <13 && month == 5 && day > 31){
				flag = 1;
			}else if(month <13 && month == 7 && day > 31){
				flag = 1;		
			}else if(month <13 && month == 8 && day > 31){
				flag = 1;	
			}else if(month <13 && month == 10 && day > 31){
				flag = 1;	
			}else if(month <13 && month == 12 && day > 31){
				flag = 1;	
			}else if(month <13 && month == 4 && day > 30){
				flag = 1;
			}else if(month <13 && month == 6 && day > 30){
				flag = 1;	
			}else if(month <13 && month == 9 && day > 30){
				flag = 1;
			}else if(month <13 && month == 11 && day > 30){
				flag = 1;	
			}
		}else{
			if(month < 13 && month == 02 && day > 29){
				flag = 1;
			}else if(month <13 && month == 1 && day > 31){
				flag = 1;
			}else if(month <13 && month == 3 && day > 31){
				flag = 1;
			}else if(month <13 && month == 5 && day > 31){
				flag = 1;
			}else if(month <13 && month == 7 && day > 31){
				flag = 1;		
			}else if(month <13 && month == 8 && day > 31){
				flag = 1;	
			}else if(month <13 && month == 10 && day > 31){
				flag = 1;	
			}else if(month <13 && month == 12 && day > 31){
				flag = 1;	
			}else if(month <13 && month == 4 && day > 30){
				flag = 1;
			}else if(month <13 && month == 6 && day > 30){
				flag = 1;	
			}else if(month <13 && month == 9 && day > 30){
				flag = 1;
			}else if(month <13 && month == 11 && day > 30){
				flag = 1;	
			}
		}
		
	}	
	
	if(flag == 0){
		date_rec date;
		
		date.month = month;
		date.day = day;
		date.year = year;
		
		sys_set_date(&date);
		
		sys_get_date(&date);
		if(date.month != month || date.day != day || date.year != year){
			errorCheck(-109);
		}	
	}
	else{
		errorCheck(-108);
	}
	return 0;
}