Created
September 24, 2012 19:04
-
-
Save dce/3777665 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int index_of(char* needle, char* haystack[], int length) { | |
int i; | |
for (i = 0; i < length; i++) { | |
if (strcmp(haystack[i], needle) == 0) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
int decrypt(int argc, char* argv[]) { | |
if (index_of("-d", argv, argc) > -1) { | |
return 1; | |
} | |
return 0; | |
} | |
int offset(int argc, char* argv[]) { | |
int delta = 1; | |
int flag = index_of("-n", argv, argc); | |
if (flag > -1) { | |
delta = atoi(argv[flag + 1]); | |
} | |
if (decrypt(argc, argv)) { | |
delta *= -1; | |
} | |
return delta; | |
} | |
FILE* input(int argc, char* argv[]) { | |
FILE* file = stdin; | |
int flag = index_of("-f", argv, argc); | |
if (flag > -1) { | |
file = fopen(argv[flag + 1], "r"); | |
if (!file) { | |
printf("Invalid filename."); | |
exit(1); | |
} | |
} | |
return file; | |
} | |
void encode(FILE* data, int delta) { | |
char c; | |
setbuf(stdout, NULL); // print immediately | |
while ((c = fgetc(data)) != EOF) { | |
printf("%c", c + delta); | |
} | |
} | |
int main(int argc, char* argv[]) { | |
FILE* data = input(argc, argv); | |
encode(data, offset(argc, argv)); | |
fclose(data); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment