// Derived from independent code by // Chris Stone , High Mountain Software Internet Gateway // and KnighA@tetraworld.com (Andy Knight) // // Refactored by Chris.McDonald@uwa.edu.au // // Compile me with: // gcc -std=c99 -Wall -o creditcard creditcard.c // #include #include #include #include static int IsCardNumberValid(const char *CardNum) { unsigned int sum = 0 ; int dbl = !(strlen(CardNum) % 2) ; int c ; while ((c = *CardNum++) != 0) { if(isspace(c)) { dbl = !dbl; continue; } if(!isdigit(c)) { return(0); } if (dbl) { unsigned int n = (c - '0') * 2 ; if (n > 9) { n -= 9 ; } sum += n ; dbl = 0 ; } else { sum += (c - '0') ; dbl = 1 ; } } return (sum % 10) == 0 ; } static char *DescribeCard(const char *CardNum) { static struct _t { char *prefix; int length; char *name; int uses_checksum; } table[] = { { "1800", 15, "Japanese Credit Bureau", 1 }, { "2014", 15, "enRoute", 0 }, { "2131", 15, "Japanese Credit Bureau", 1 }, { "2149", 15, "enRoute", 0 }, { "300", 14, "Diners Club/Carte Blanche", 1 }, { "301", 14, "Diners Club/Carte Blanche", 1 }, { "302", 14, "Diners Club/Carte Blanche", 1 }, { "303", 14, "Diners Club/Carte Blanche", 1 }, { "304", 14, "Diners Club/Carte Blanche", 1 }, { "305", 14, "Diners Club/Carte Blanche", 1 }, { "34", 15, "American Express", 1 }, { "36", 14, "Diners Club/Carte Blanche", 1 }, { "37", 15, "American Express", 1 }, { "38", 14, "Diners Club/Carte Blanche", 1 }, { "3", 16, "Japanese Credit Bureau", 1 }, { "4", 13, "Visa", 1 }, { "4", 16, "Visa", 1 }, { "51", 16, "Mastercard", 1 }, { "52", 16, "Mastercard", 1 }, { "53", 16, "Mastercard", 1 }, { "54", 16, "Mastercard", 1 }, { "55", 16, "Mastercard", 1 }, { "56", 16, "Australian Bankcard", 1 }, { "6011", 16, "Discover", 1 } }; #define NCARDS (sizeof(table) / sizeof(table[0])) int n; for(n=0 ; n 0) { printf("%s - %s\n", *argv, DescribeCard(*argv)); ++argv; --argc; } exit(EXIT_SUCCESS); }