/* The actual read function. */ void readin() { char* fname = ( char* )malloc( sizeof( char ) * 26 ) ; // The extra length for the null terminator. readName( fname, 0, 26 ) ; char* lname = ( char* )malloc( sizeof( char ) * 31 ) ; readName( lname, 1, 31 ) ; char* cardnum = ( char* )malloc( sizeof( char ) * 17 ) ; readNums( cardnum, 0, 17 ) ; char* month = ( char* )malloc( sizeof( char ) * 3 ) ; readNums( month, 1, 3 ) ; while( !checkMonth( month ) ) { printf( "The entered month is not valid. Please try again.\n" ) ; readNums( month, 1, 3 ) ; } char* year = ( char* )malloc( sizeof( char ) * 5 ) ; readNums( year, 2, 5 ) ; while( !checkYear( year ) ) { printf( "The entered year is not valid. Please try again.\n" ) ; readNums( year, 2, 5 ) ; } char* cvv = ( char* )malloc( sizeof( char ) * 4 ) ; readNums( cvv, 3, 4 ) ; if( strlen( cvv ) != 3 ) // String already checked for numbers. Check if all three are there. { printf( "Not enough digits for 3 digit CVV security code. Please try again. \n" ) ; readNums( cvv, 3, 4 ) ; } char* zip = ( char* )malloc( sizeof( char ) * 6 ) ; readNums( zip, 4, 6 ) ; if( strlen( zip ) != 5 ) { printf( "Not enough numbers for 5 digit zipcode. Please try again.\n" ) ; readNums( zip, 4, 6 ) ; } char* amount = ( char* )malloc( sizeof( char ) * 10 ) ; readAmount( amount, 10 ) ; char* email = ( char* )malloc( sizeof( char ) * 25 ) ; readEmail( email, 31 ) ; display( fname, lname, cardnum, month, year, cvv, zip, amount, email ) ; // Free up all the memory pointers used here. free( fname ) ; free( lname ) ; free( cardnum ) ; free( month ) ; free( year ) ; free( cvv ) ; free( zip ) ; free( amount ) ; free( email ) ; }
int main(void ) { ListName *names = NULL; ListYear *years = NULL; int choice = 0; int year; char name[512]; readFile(&years, &names); do { printf("Do you want to find a name or a year?\n1) Year 2)Name 0) Exit\nChoice : "); scanf("%d", &choice); switch( choice ) { case 1: printf("Insert the year that you will look for : "); scanf("%d", &year); printf(" The year %d %s\n", year, ( checkYear(years, year) == 1 ) ? "was found!" : "was not found!"); printYear(years); break; case 2: printf("Insert the name that you will look for : "); scanf("%s", name); printf(" The name %s %s\n", name, ( checkName(names, name) == 1 ) ? "was found!" : "was not found!"); break; case 0: printf("You are exiting...\n"); break; } }while ( choice != 0 ); getchar(); return EXIT_SUCCESS; }
static int getPtime (char *toptarg, char endTime, time_t sbtime, char flag, time_t *Ptime) { #define MINU 1 #define HOUR 2 #define DAY 4 #define MONTH 8 #define YEAR 16 char *cp, *cp1, *cp2, *cp3; time_t ptimev; struct tm *tmPtr; int ptimef = 0, m, tempInt; if ( ! checkChar (toptarg, &cp)) { lserrno = LSE_BAD_TIME; return -1; } checkThree (toptarg, '/', &cp1, &cp2, &cp3); if ( cp2 != NULL ) { if ( (cp1+1) == cp2 ) { lserrno = LSE_BAD_TIME; return -1; } *cp2 = '0'; checkThree (toptarg, '/', &cp1, &cp, &cp3); *cp2 = '/'; if ( cp3 != NULL ) { lserrno = LSE_BAD_TIME; return -1; } } checkThree (toptarg, ':', &cp1, &cp2, &cp3); if ( cp1 == NULL ) { checkThree (toptarg, '/', &cp1, &cp2, &cp3); if (( cp3 != NULL ) && ( *(cp3+1) != '0' )) { lserrno = LSE_BAD_TIME; return -1; } } checkThree (toptarg, ':', &cp1, &cp2, &cp3); if ( cp2 != NULL ) { lserrno = LSE_BAD_TIME; return -1; } if ( (cp1 != NULL) && ((cp1[-1] == '/') || (cp1[1] == '/'))) { lserrno = LSE_BAD_TIME; return -1; } if (flag == 'w') { tmPtr = malloc (sizeof (struct tm)); tmPtr->tm_year = 70; tmPtr->tm_mon = 0; tmPtr->tm_mday = 1; tmPtr->tm_hour = 0; tmPtr->tm_min = 0; } else { ptimev = time(0); tmPtr = localtime(&ptimev); } tmPtr->tm_sec = 0; tmPtr->tm_isdst = -1; cp = cp1; checkThree (toptarg, '/', &cp1, &cp2, &cp3); if ( (cp == NULL) && (cp1 != NULL) && (cp2 == NULL) && (cp1 > toptarg) ) { *cp1 = '\000'; tempInt = atoi(toptarg); if (tempInt > 12) { ptimef |= YEAR; tmPtr->tm_year = tempInt; if (checkYear(&tmPtr->tm_year) == -1) { *cp1 = '/'; return -1; } if (( *(cp1+1) != '\000')) { ptimef |= MONTH; tmPtr->tm_mon = atoi(cp1+1); if (checkTime(MONTH, tmPtr->tm_mon) == -1) { *cp1 = '/'; return -1; } } else tmPtr->tm_mon = ( endTime ? 12 : 1 ); if (endTime) { m = tmPtr->tm_mon; if ((m==1) || (m==3) || (m==5) || (m==7) || (m==8) || (m==10) || (m==12)) tmPtr->tm_mday = 31; else if (m != 2) tmPtr->tm_mday = 30; else if (tmPtr->tm_year%4 == 0) tmPtr->tm_mday = 29; else tmPtr->tm_mday = 28; } else tmPtr->tm_mday = 1; } else { ptimef |= MONTH; tmPtr->tm_mon = tempInt; if (checkTime(MONTH, tmPtr->tm_mon) == -1) { *cp1 = '/'; return -1; } if ( *(cp1+1) != '\000') { ptimef |= DAY; tmPtr->tm_mday = atoi(cp1+1); if (checkTime(DAY, tmPtr->tm_mday) == -1) { *cp1 = '/'; return -1; } } else if (endTime) { m = tmPtr->tm_mon; if ((m==1) || (m==3) || (m==5) || (m==7) || (m==8) || (m==10) || (m==12)) tmPtr->tm_mday = 31; else if (m != 2) tmPtr->tm_mday = 30; else if (tmPtr->tm_year%4 == 0) tmPtr->tm_mday = 29; else tmPtr->tm_mday = 28; } else tmPtr->tm_mday = 1; } *cp1 = '/'; tmPtr->tm_mon--; tmPtr->tm_hour = ( endTime ? 23 : 0 ); tmPtr->tm_min = ( endTime ? 59 : 0 ); tmPtr->tm_sec = ( endTime ? 59 : 0 ); if (flag == 'w') { if ((ptimef & YEAR) == YEAR) { lserrno = LSE_BAD_TIME; return -1; } if ((ptimef & MONTH) == MONTH) tmPtr->tm_year--; if ((ptimef & DAY) == DAY) tmPtr->tm_year -= 2; } *Ptime = mkTime (tmPtr, ptimef, sbtime); if (*Ptime < 0) return -1; return 0; } if ((cp == NULL) && (cp1 == toptarg) && (cp2 != NULL) ) { *cp2 = '\000'; cp2 = NULL; } if ((cp == NULL) && (cp1 == toptarg) && (cp2 == NULL)) { ptimef |= DAY; toptarg += 1; cp1 = NULL; } if ((cp == NULL) && (cp1 == NULL)) { ptimef |= DAY; tmPtr->tm_mday = atoi(toptarg); if (checkTime(DAY, tmPtr->tm_mday) == -1) return -1; tmPtr->tm_hour = ( endTime ? 23 : 0 ); tmPtr->tm_min = ( endTime ? 59 : 0 ); tmPtr->tm_sec = ( endTime ? 59 : 0 ); if (flag == 'w') { if ((ptimef & YEAR) == YEAR) { lserrno = LSE_BAD_TIME; return -1; } if ((ptimef & MONTH) == MONTH) tmPtr->tm_year--; if ((ptimef & DAY) == DAY) tmPtr->tm_year -= 2; } *Ptime = mkTime (tmPtr, ptimef, sbtime); if (*Ptime < 0) return -1; return 0; } if ( (cp == NULL) && (cp2 != NULL) && (cp1 != toptarg) ) { if ((cp3 != NULL) || (*(cp2+1) != '\000')) { ptimef |= YEAR; ptimef |= MONTH; ptimef |= DAY; *cp1 = '\000'; tmPtr->tm_year = atoi(toptarg); if (checkYear(&tmPtr->tm_year) == -1) { *cp1 = '/'; return -1; } *cp1 = '/'; *cp2 = '\000'; tmPtr->tm_mon = atoi(cp1 + 1); *cp2 = '/'; if (cp3 != NULL) { *cp3 = '\000'; tmPtr->tm_mday = atoi(cp2 + 1); *cp3 = '/'; } else tmPtr->tm_mday = atoi(cp2 + 1); if (checkTime(DAY, tmPtr->tm_mday) == -1) return -1; } else { *cp1 = '\000'; tempInt = atoi(toptarg); if (tempInt > 12) { ptimef |= YEAR; tmPtr->tm_year = tempInt; if (checkYear(&tmPtr->tm_year) == -1) { *cp1 = '/'; return -1; } ptimef |= MONTH; *cp2 = '\000'; tmPtr->tm_mon = atoi(cp1+1); if (endTime) { m = tmPtr->tm_mon; if ((m==1) || (m==3) || (m==5) || (m==7) || (m==8) || (m==10) || (m==12)) tmPtr->tm_mday = 31; else if (m != 2) tmPtr->tm_mday = 30; else if (tmPtr->tm_year%4 == 0) tmPtr->tm_mday = 29; else tmPtr->tm_mday = 28; } else tmPtr->tm_mday = 1; } else { ptimef |= MONTH; tmPtr->tm_mon = tempInt; ptimef |= DAY; *cp2 = '\000'; tmPtr->tm_mday = atoi(cp1+1); } if (checkTime(DAY, tmPtr->tm_mday) == -1) { *cp1 = '/'; *cp2 = '/'; return -1; } } if (checkTime(MONTH, tmPtr->tm_mon) == -1) return -1; tmPtr->tm_mon--; *cp1 = '/'; *cp2 = '/'; tmPtr->tm_hour = ( endTime ? 23 : 0 ); tmPtr->tm_min = ( endTime ? 59 : 0 ); tmPtr->tm_sec = ( endTime ? 59 : 0 ); if (flag == 'w') { if ((ptimef & YEAR) == YEAR) { lserrno = LSE_BAD_TIME; return -1; } if ((ptimef & MONTH) == MONTH) tmPtr->tm_year--; if ((ptimef & DAY) == DAY) tmPtr->tm_year -= 2; } *Ptime = mkTime (tmPtr,ptimef, sbtime); if (*Ptime < 0) return -1; return 0; } if ( (cp !=NULL) && (cp2 != NULL) && (cp3 != NULL) ) { if (cp3 != NULL) { ptimef |= YEAR; ptimef |= MONTH; ptimef |= DAY; *cp1 = '\000'; tmPtr->tm_year = atoi(toptarg); *cp1 = '/'; if (checkYear(&tmPtr->tm_year) == -1) return -1; *cp2 = '\000'; tmPtr->tm_mon = atoi(cp1+1); *cp2 = '/'; if (checkTime(MONTH, tmPtr->tm_mon) == -1) return -1; tmPtr->tm_mon--; *cp3 = '\000'; tmPtr->tm_mday = atoi(cp2+1); *cp3 = '/'; if (checkTime(DAY, tmPtr->tm_mday) == -1) return -1; *cp = '\000'; tmPtr->tm_hour = atoi(cp3+1); *cp = ':'; if (checkTime(HOUR, tmPtr->tm_hour) == -1) return -1; if (*(cp+1) != '\000') { tmPtr->tm_min = atoi(cp+1); if (checkTime(MINU, tmPtr->tm_min) == -1) return -1; } else { tmPtr->tm_min = ( endTime ? 59 : 0 ); tmPtr->tm_sec = ( endTime ? 59 : 0 ); } if (flag == 'w') { if ((ptimef & YEAR) == YEAR) { lserrno = LSE_BAD_TIME; return -1; } if ((ptimef & MONTH) == MONTH) tmPtr->tm_year--; if ((ptimef & DAY) == DAY) tmPtr->tm_year -= 2; } *Ptime = mkTime (tmPtr, ptimef, sbtime); if (*Ptime < 0) return -1; return 0; } } if ( (cp !=NULL) && (cp2 != NULL) && (cp3 == NULL) ) { if ( cp1 > toptarg ) { ptimef |= MONTH; *cp1 = '\000'; tmPtr->tm_mon = atoi(toptarg); *cp1 = '/'; if (checkTime(MONTH, tmPtr->tm_mon) == -1) return -1; tmPtr->tm_mon--; toptarg = cp1; } if ( toptarg[0] == '/' ) { toptarg +=1; } cp1 = cp2; cp2 = NULL; } if ( (cp !=NULL) && (cp1 != NULL) && (cp2 == NULL) ) { if ( (cp1 > toptarg) && (cp1 < cp) ) { ptimef |= DAY; *cp1 = '\000'; tmPtr->tm_mday = atoi(toptarg); *cp1 = '/'; if (checkTime(DAY, tmPtr->tm_mday) == -1) return -1; toptarg = cp1+1; } else if (cp1 > cp) { lserrno = LSE_BAD_TIME; return -1; } } checkThree (toptarg, '/', &cp1, &cp2, &cp3); if ( (cp != NULL) && (cp1 == toptarg) && (cp2 == NULL)) { toptarg +=1; } if ( toptarg[0] == ':' ) { ptimef |= MINU; tmPtr->tm_min = atoi(cp+1); if (checkTime(MINU, tmPtr->tm_min) == -1) return -1; if (flag == 'w') { if ((ptimef & YEAR) == YEAR) { lserrno = LSE_BAD_TIME; return -1; } if ((ptimef & MONTH) == MONTH) tmPtr->tm_year--; if ((ptimef & DAY) == DAY) tmPtr->tm_year -= 2; } *Ptime = mkTime (tmPtr, ptimef, sbtime); if (*Ptime < 0) return -1; return 0; } ptimef |= HOUR; if ( *(cp+1) != '\000') { ptimef |= MINU; tmPtr->tm_min = atoi(cp+1); if (checkTime(MINU, tmPtr->tm_min) == -1) return -1; } else tmPtr->tm_min = ( endTime ? 59 : 0 ); *cp = '\000'; tmPtr->tm_hour = atoi(toptarg); if (checkTime(HOUR, tmPtr->tm_hour) == -1) { *cp = ':'; return -1; } *cp = ':'; if (flag == 'w') { if ((ptimef & YEAR) == YEAR) { lserrno = LSE_BAD_TIME; return -1; } if ((ptimef & MONTH) == MONTH) tmPtr->tm_year--; if ((ptimef & DAY) == DAY) tmPtr->tm_year -= 2; } *Ptime = mkTime (tmPtr, ptimef, sbtime); if (*Ptime < 0) return -1; return 0; }