This repository was archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
335 lines (271 loc) · 9.34 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
*
* File: main.c
*
* Program to encrypt and decrypt
* a given string or file using
* Caesar cipher
*
* Copyright (C) 2019 Jithin Renji
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/* For getopt_long() */
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ENCRYPT 1
#define DECRYPT 0
/*
Size for different buffers
in the program
*/
#define BUF_SIZE 2048
int option_index = 0;
/* Long options */
static struct option long_options[] = {
{"encrypt", no_argument, 0, 'e'},
{"decrypt", no_argument, 0, 'd'},
{"file", required_argument, 0, 'f'},
{"help", no_argument, 0, 'h'},
{"out", required_argument, 0, 'o'},
{"input", no_argument, 0, 'I'},
{"version", no_argument, 0, 'V'},
{0, 0, 0, 0}
};
static char ret_str[BUF_SIZE] = "";
/* Print usage message */
void usage(char* prog_name);
/* Print version information */
void version(void);
/* Caesar cipher function */
void caesar(char* str, int shift_size, int encr_decr, int print);
/* Apply Caesar cipher on a file */
void caesar_f(char* fname, char* ofname, int shift_size, int encr_decr);
int main (int argc, char** argv)
{
if (argc < 2) {
fprintf(stderr, "%s: Not enough arguments!\n", argv[0]);
fprintf(stderr, "Try '%s --help'\n", argv[0]);
} else {
int opt;
/* To encrypt or not */
int encrypt = 0;
/* To decrypt or not */
int decrypt = 0;
/* File name provided or not */
int fname_provided = 0;
/* Usage message or not */
int help = 0;
/* Provided file name */
char fname[FILENAME_MAX];
/* Provided output file name */
char ofname[FILENAME_MAX] = "";
/* Input from stdin or not */
int in_from_stdin = 0;
while ((opt = getopt_long(argc, argv, "edf:ho:IV", long_options, &option_index)) != -1) {
switch (opt){
case 'e':
encrypt = 1;
break;
case 'd':
decrypt = 1;
break;
case 'f':
fname_provided = 1;
strcpy(fname, optarg);
break;
case 'h':
usage(argv[0]);
help = 1;
break;
case 'o':
strcpy(ofname, optarg);
break;
case 'I':
in_from_stdin = 1;
break;
case 'V':
version();
default:
exit(EXIT_FAILURE);
}
}
if (help == 1);
else if (encrypt == 1 && decrypt == 1) {
fprintf(stderr, "Error: Cannot encrypt and decrypt at the same time\n");
}
else if (encrypt == 0 && decrypt == 0 && help == 0) {
fprintf(stderr, "Error: Don't know whether to encrypt or decrypt\n");
exit(EXIT_FAILURE);
} else if (fname_provided) {
if(argv[optind] == NULL) {
fprintf(stderr, "Error: Shift size not provided\n");
exit(EXIT_FAILURE);
} else {
if (encrypt == 1)
caesar_f(fname, ofname, atoi(argv[optind]), ENCRYPT);
else
caesar_f(fname, ofname, atoi(argv[optind]), DECRYPT);
}
} else if (in_from_stdin == 1) {
if (argv[optind] == NULL) {
fprintf(stderr, "Error: Shift size not provided\n");
} else{
char input[BUF_SIZE];
while (fgets(input, BUF_SIZE, stdin)) {
input[strlen(input) - 1] = '\0';
if (encrypt == 1)
caesar(input, atoi(argv[optind]), ENCRYPT, 1);
else
caesar(input, atoi(argv[optind]), DECRYPT, 1);
}
}
} else if ((encrypt == 1) ^ (decrypt == 1)){
if (argv[optind] == NULL) {
fprintf(stderr, "Error: No text provided\n");
exit(EXIT_FAILURE);
} else if (argv[optind + 1] == NULL) {
fprintf(stderr, "Error: Shift size not provided\n");
exit(EXIT_FAILURE);
} else {
if (encrypt == 1)
caesar(argv[optind], atoi(argv[optind + 1]), ENCRYPT, 1);
else
caesar(argv[optind], atoi(argv[optind + 1]), DECRYPT, 1);
}
}
}
return 0;
}
void usage (char* prog_name)
{
printf("Usage: %s <-e | -d> -f <file name> [-o <output file name>] <shift size>\n", prog_name);
printf(" %s <-e | -d> <plain text> <shift size>\n", prog_name);
printf(" %s <-e | -d> -I <shift size>\n", prog_name);
printf("Options:\n");
printf("\t-e, --encrypt\t\tEncrypt plain text\n");
printf("\t-d, --decrypt\t\tDecrypt Caesar cipher text\n");
printf("\t-I, --input\t\tTake input from stdin\n");
printf("\t-f, --file <file name>\tEncrypt/Decrypt given file\n\n");
printf("\t-o, --out <file name>\tName of the output file in which\n\
the encrypted/decrypted file is going\n\
to be stored\n\n");
printf("\t-h, --help\t\tShow this help message\n");
printf("\t-V, --version\t\tShow version information\n");
}
void version (void)
{
printf("caesar 1.1\n");
printf("Copyright (C) 2019 Jithin Renji.\n");
printf("License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n");
printf("This is free software: you are free to change and redistribute it.\n");
printf("There is NO WARRANTY, to the extent permitted by law.\n\n");
printf("Written by Jithin Renji.\n");
}
void caesar (char* str, int shift_size, int encr_decr, int print)
{
if (encr_decr == ENCRYPT) {
char cipher[2048];
int i = 0;
while (i < strlen(str)) {
cipher[i] = str[i] + shift_size;
i += 1;
}
/*
strlen() does not include the null byte.
We have to add it ourselves.
*/
cipher[i] = '\0';
if (print == 1)
printf("%s\n", cipher);
strcpy(ret_str, cipher);
} else {
char decr_str[2048];
int i = 0;
while (i < strlen(str)) {
decr_str[i] = str[i] - shift_size;
i += 1;
}
/*
strlen() does not include the null byte.
We have to add it ourselves.
*/
decr_str[i] = '\0';
if (print == 1)
printf("%s\n", decr_str);
strcpy(ret_str, decr_str);
}
}
void caesar_f (char* fname, char* ofname, int shift_size, int encr_decr)
{
/* Stream to handle input file */
FILE* in_file = fopen(fname, "r");
/* Using default file name or not */
int default_file_name = 0;
/* Output file name */
char ofname_default[FILENAME_MAX];
if (in_file == NULL) {
char msg[BUF_SIZE];
strcpy(msg, "caesar: cannot open ");
strcat(msg, "'");
strcat(msg, fname);
strcat(msg, "'");
perror(msg);
exit(EXIT_FAILURE);
}
if (encr_decr == ENCRYPT) {
if (strcmp(ofname, "") == 0) {
strcpy(ofname_default, fname);
strcat(ofname_default, "_encr");
default_file_name = 1;
} else {
strcpy(ofname_default, ofname);
}
FILE* out_file = fopen(ofname_default, "w");
char line[BUF_SIZE];
while (fgets(line, BUF_SIZE, in_file)) {
char encr_line[BUF_SIZE];
caesar(line, shift_size, ENCRYPT, 0);
strcpy(encr_line, ret_str);
fprintf(out_file, "%s", encr_line);
strcpy(ret_str, "");
}
fclose(out_file);
printf("Done!\n");
} else if (encr_decr == DECRYPT) {
if (strcmp(ofname, "") == 0) {
strcpy(ofname_default, fname);
strcat(ofname_default, "_decr");
default_file_name = 1;
} else {
strcpy(ofname_default, ofname);
}
FILE* out_file = fopen(ofname_default, "w");
char line[BUF_SIZE];
while (fgets(line, BUF_SIZE, in_file)) {
char decr_line[BUF_SIZE];
caesar(line, shift_size, DECRYPT, 0);
strcpy(decr_line, ret_str);
fprintf(out_file, "%s", decr_line);
strcpy(ret_str, "");
}
fclose(out_file);
printf("Done!\n");
}
if (default_file_name == 1)
printf("Output file: %s\n", ofname_default);
}