2323#define USAGE (p ) fprintf(stderr, \
2424 "Tiny Encryption Algorithm implementation, with 128 bit key.\n" \
2525 "Performs Encryption/Decruption of multiple flies.\n" \
26- "usage:\n%s [-e [-D] |-d [-N] ] [-v] -k '16 byte key' -I <...>\n" \
26+ "usage:\n%s [-e |-d [-N] ] [-D ] [-v] -k '16 byte key' -I <...>\n" \
2727 "-e - Encrypt\n" \
2828 " Encrypts the input files and the output files of each" \
2929 " will be placed in the same directory with extension .3\n" \
30- "-D - Will delete parent files after encryption\n" \
3130 "-d - Decrypt\n" \
3231 " Decrypts the input files and the output files of each" \
3332 " will be placed in the same directory excluding extension .3\n" \
3433 "-N - When decrypting, display output to stdout.\n" \
34+ "-D - Deletes source files after encryption or decryption.\n" \
3535 "-v - Verbose\n" \
3636 "-k - 16 byte key.\n" \
3737 "-I - Files that need to be processed.\n" \
38+ "\nNotes:\n"\
39+ " - Cannot use -D (Delete file), -N (stdout output) together.\n" \
40+ " - Cannot use -e (encryption), -N (stdout output) together.\n" \
41+ " - When using -N (stdout output), -v (Verbose) is ignored.\n" \
3842 , p)
3943
4044#define MAX_FILENAME_LENGTH 255 // Length of file path
4145#define MAX_INPUT_FILES 50 // number of files
4246#define ENCRYPTED_FILE_EXTENSION ".3"
4347
44- #define FLAG_DELETE_AFTER_ENCRYPTION (1 << 0)
48+ #define FLAG_DELETE_SOURCE_FILE (1 << 0)
4549#define FLAG_VERBOSE (1 << 1)
4650#define FLAG_OUTPUT_TO_STDOUT (1 << 2)
4751
@@ -90,10 +94,9 @@ int main(int argc,char *argv[])
9094
9195 // -- 1. Perform Encryption and Decryption --
9296
93- output_filename [0 ] = '\0' ;
94-
9597 if (prm .mode == ENCRYPT ) {
9698 // Output file name: InputfilePath + ".3"
99+ output_filename [0 ] = '\0' ;
97100 strcat (output_filename , prm .files [i ]);
98101 strcat (output_filename , ENCRYPTED_FILE_EXTENSION );
99102 }
@@ -110,7 +113,7 @@ int main(int argc,char *argv[])
110113 // Feature: Output to stdout
111114 if (prm .flags & FLAG_OUTPUT_TO_STDOUT ) {
112115 strcpy (output_filename ,"(stdout)" );
113- enc_dec_mode = TEA_FLAG_OUTPUT_STDOUT ;
116+ enc_dec_mode | = TEA_FLAG_OUTPUT_STDOUT ;
114117 printf ("-------------- %u: %s --------------\n" ,
115118 i + 1 , prm .files [i ]);
116119 }
@@ -137,10 +140,8 @@ int main(int argc,char *argv[])
137140 // -- 3. Delete the file if -D option is set --
138141
139142 // Feature: Delete file is -D was provided.
140- // Delete only if Encryption worked
141- if (prm .mode == ENCRYPT &&
142- ed_status == true &&
143- prm .flags & FLAG_DELETE_AFTER_ENCRYPTION ) {
143+ if (ed_status == true &&
144+ prm .flags & FLAG_DELETE_SOURCE_FILE ) {
144145 // Feature: verbosity
145146 printf ("Deletion: %s : %s\n" , prm .files [i ],
146147 delete (prm .files [i ]) ? "Success" : "Failed" );
@@ -212,7 +213,7 @@ int readargs(char *argv[], struct op *out)
212213 out -> flags |= FLAG_VERBOSE ;
213214 break ;
214215 case 'D' :
215- out -> flags |= FLAG_DELETE_AFTER_ENCRYPTION ;
216+ out -> flags |= FLAG_DELETE_SOURCE_FILE ;
216217 break ;
217218 default :
218219 fprintf (stderr ,"Error: Invaid argument: %s\n" ,arg );
@@ -229,21 +230,25 @@ int readargs(char *argv[], struct op *out)
229230
230231bool args_is_valid (struct op * out )
231232{
232- if (out -> flags & FLAG_DELETE_AFTER_ENCRYPTION && out -> mode == DECRYPT ){
233- fprintf (stderr ,
234- "Error: Delete option is only for Encryption mode.\n" );
235- return false;
236- }
237233
238234 if (out -> flags & FLAG_OUTPUT_TO_STDOUT && out -> mode == ENCRYPT ){
239235 fprintf (stderr ,
240236 "Error: -N option can only be used in Decrypt mode.\n" );
241237 return false;
242238 }
243239
240+ if (out -> flags & FLAG_OUTPUT_TO_STDOUT &&
241+ out -> flags & FLAG_DELETE_SOURCE_FILE ) {
242+ fprintf (stderr ,
243+ "Warning: -D is ignored when outputing to stdout.\n" );
244+
245+ // Remove -D flag
246+ out -> flags &= ~FLAG_DELETE_SOURCE_FILE ;
247+ }
248+
244249 if (out -> flags & FLAG_OUTPUT_TO_STDOUT && out -> flags & FLAG_VERBOSE ) {
245250 fprintf (stderr ,
246- "Warning: -v option has no effect with -N option .\n" );
251+ "Warning: -v is ignored when outputing to stdout .\n" );
247252
248253 // Clear verbose flag
249254 out -> flags &= ~FLAG_VERBOSE ;
0 commit comments