/* * Codifica rotX (X == 13 di default) * * Davide Alberani (C) 1999 * * Licenza: GPL * * Compile with: cc -O2 -o rotx rotX.c * */ #include #include // buffer #define BUFFER 4096 // (de)codifica la stringa secondo il rot desiderato unsigned char rotX (unsigned char input, int displ) { if (input > 64 && input < 91) { input = input + displ; if (input > 90) { input = input - 26; } } else { if (input > 96 && input < 123) { input = input + displ; if (input > 122) { input = input - 26; } } } return input; } // se e` un intero, restituisce 1 int isnumber(char *argomento) { int j; for (j=0; j < strlen(argomento); ++j) { if (!isdigit(argomento[j])) return 0; } return 1; } // help ed errori void help(int exitcode, char *myname) { printf("Usage: %s [number]\n", myname); printf(" number : type of rot code (>= 0).\n"); exit(exitcode); } int main (int argc, char *argv[]) { int j; char stringa[BUFFER]; // rot di default int displ = 13; // max un argomento if (argc > 2) { fprintf(stderr,"Only one operand\n") ; help(1, argv[0]); } if (argc == 2) { // se si e` chiesto aiuto if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "-h")) { help(2, argv[0]); } // se non e` un numero valido if (!isnumber(argv[1])) { fprintf(stderr,"\"%s\" is not a valid number\n", argv[1]) ; help(3, argv[0]); } // converte il numero displ = atoi(argv[1]); displ = displ % 26; } // il filtro while (fgets(stringa, BUFFER, stdin)) { for (j=0; j < strlen(stringa); ++j) { fputc(rotX(stringa[j], displ), stdout); } } return 0; }