summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Lasseter <nathan.je.lasseter@googlemail.com>2012-06-05 21:21:15 +0100
committerNathan Lasseter <nathan.je.lasseter@googlemail.com>2012-06-05 21:21:15 +0100
commit96e91e89ed0acb1a0002bd7b8b27aa85109d4ddb (patch)
tree39bc2f5c84bfe22a162af9ebf7b61bcf8e1e4e07
parentbce6e5dfbeff23d6701b6fc4c159cefcfb0006ea (diff)
Code tidy and check file exists!
-rw-r--r--Makefile2
-rw-r--r--encrypt.c22
2 files changed, 14 insertions, 10 deletions
diff --git a/Makefile b/Makefile
index 677ba6a..dfb3231 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
CFLAGS=-Wall -Wextra
encrypt: encrypt.c
- gcc ${CFLAGS} ${EFLAGS} -o $@ $^
+ gcc ${CFLAGS} -o $@ $^
install:
cp encrypt /usr/local/bin/encrypt
diff --git a/encrypt.c b/encrypt.c
index 056d0da..880e2c9 100644
--- a/encrypt.c
+++ b/encrypt.c
@@ -3,36 +3,40 @@
#include <string.h>
int main(int argc, char** argv) {
-
+
// We need a file to encrypt
- if(argc==1) {
+ if (argc != 2) {
fprintf(stderr, "Usage: encrypt <file>");
- return 1;
+ return EXIT_FAILURE;
}
-
+
// Now lets open that file
FILE* file = fopen(argv[1], "r");
+ if (file == NULL) {
+ fprintf(stderr, "No file specified\n");
+ return EXIT_FAILURE;
+ }
// Now we need the passphrase. Get it
char *passphrase = malloc(256);
do {
printf("Enter Passphrase: ");
- if ( fgets(passphrase, 255, stdin) == NULL ) {
+ if (fgets(passphrase, 255, stdin) == NULL) {
fclose(file);
fprintf(stderr, "Read error or end of file");
- exit(EXIT_FAILURE);
+ return EXIT_FAILURE;
}
} while (strlen(passphrase) <= 0);
-
+
// And a temporary file to hold the encrypted data
char tpath[] = "SimCr.XXXXXX";
int tempfd = mkstemp(tpath);
FILE* temp = fdopen(tempfd, "w");
-
+
// Now we encrypt!
int passlen = strlen(passphrase) - 1;
int c,j=0;
- while ( (c=fgetc(file)) != EOF ) {
+ while ((c=fgetc(file)) != EOF) {
if (j == passlen) {
fputc(c ^ passphrase[0], temp);
j=1;