summaryrefslogtreecommitdiff
path: root/28_fix_vg_encr
diff options
context:
space:
mode:
authorHaidong Ji2022-04-15 15:51:30 -0500
committerHaidong Ji2022-04-15 15:51:30 -0500
commit442a49ad5a48d417345959b903ae6a6d32d55759 (patch)
treec7127bb497e5e439018b1915e0136eec2c9cb124 /28_fix_vg_encr
Great C programming funHEADmaster
Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git
Diffstat (limited to '28_fix_vg_encr')
-rw-r--r--28_fix_vg_encr/Makefile5
-rw-r--r--28_fix_vg_encr/README22
-rw-r--r--28_fix_vg_encr/encrypt.c60
-rw-r--r--28_fix_vg_encr/grade.txt28
-rw-r--r--28_fix_vg_encr/input.txt2
-rw-r--r--28_fix_vg_encr/input.txt.enc2
6 files changed, 119 insertions, 0 deletions
diff --git a/28_fix_vg_encr/Makefile b/28_fix_vg_encr/Makefile
new file mode 100644
index 0000000..f08edbe
--- /dev/null
+++ b/28_fix_vg_encr/Makefile
@@ -0,0 +1,5 @@
+CFLAGS=-ggdb3 -Wall -Werror -std=gnu99 -pedantic
+encrypt: encrypt.c
+ gcc $(CFLAGS) -o encrypt encrypt.c
+clean:
+ rm -f encrypt *~
diff --git a/28_fix_vg_encr/README b/28_fix_vg_encr/README
new file mode 100644
index 0000000..5d44bb1
--- /dev/null
+++ b/28_fix_vg_encr/README
@@ -0,0 +1,22 @@
+In a previous problem, you executed a Ceaser Cipher encryption program by hand.
+That program read an input file, and wrote to standard output.
+
+In this problem, you are going to work on a slightly modified version of that program,
+which is is included in encrypt.c. The major difference between this version
+of the program and the previous one is that rather than printing to stdout,
+this program appends ".enc" to the input file name, and writes its output to that
+file (for example, if your input file is called "input.txt", it will write to "input.txt.enc").
+Additionally, rather than using fgetc to read one character at a time, this version
+of the program uses getline to read an entire line at a time.
+
+This program has the basic algorithm correct, but makes a variety of errors---all of which
+valgrind will detect. Your job for this problem is to fix the program by making it
+valgrind cleanly.
+
+Hint: Start from the first valgrind error. Read and understand the error. It will
+ tell you on what line of code valgrind detected the problem. Understand the
+ error, and why it is occuring (drawing some pictures will likely help here).
+ Fix the error, make, and re-run. Repeat the process until all valgrind errors are
+ gone. Don't forget that gdb may be useful as well.
+
+
diff --git a/28_fix_vg_encr/encrypt.c b/28_fix_vg_encr/encrypt.c
new file mode 100644
index 0000000..71b7156
--- /dev/null
+++ b/28_fix_vg_encr/encrypt.c
@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+
+void encrypt(FILE * f, int key, FILE * outfile){
+ char * line=NULL;
+ size_t sz=0;
+ while (getline(&line,&sz, f) >= 0) {
+ char * ptr = line;
+ while (*ptr != '\0') {
+ int c = *ptr;
+ if (isalpha(c)) {
+ c = tolower(c);
+ c -= 'a';
+ c += key;
+ c %= 26;
+ c += 'a';
+ }
+ *ptr = c;
+ ptr++;
+ }
+ fprintf(outfile, "%s", line);
+ }
+ free(line);
+}
+
+int main(int argc, char ** argv) {
+ if (argc != 3) {
+ fprintf(stderr,"Usage: encrypt key inputFileName\n");
+ return EXIT_FAILURE;
+ }
+ int key = atoi(argv[1]);
+ if (key == 0) {
+ fprintf(stderr,"Invalid key (%s): must be a non-zero integer\n", argv[1]);
+ return EXIT_FAILURE;
+ }
+ FILE * f = fopen(argv[2], "r");
+ if (f == NULL) {
+ perror("Could not open file");
+ return EXIT_FAILURE;
+ }
+ //outfileNAme is argv[2] + ".txt", so add 4 to its length.
+ char * outFileName = malloc((strlen(argv[2]) + 5) * sizeof(*outFileName));
+ strcpy(outFileName, argv[2]);
+ strcat(outFileName, ".enc");
+ FILE * outFile = fopen(outFileName, "w");
+ encrypt(f,key, outFile);
+ if (fclose(outFile) != 0) {
+ perror("Failed to close the input file!");
+ return EXIT_FAILURE;
+ }
+ if (fclose(f) != 0) {
+ perror("Failed to close the input file!");
+ return EXIT_FAILURE;
+ }
+ free(outFileName);
+
+ return EXIT_SUCCESS;
+}
diff --git a/28_fix_vg_encr/grade.txt b/28_fix_vg_encr/grade.txt
new file mode 100644
index 0000000..389f7f2
--- /dev/null
+++ b/28_fix_vg_encr/grade.txt
@@ -0,0 +1,28 @@
+Grading at Tue 14 Dec 2021 02:37:37 AM UTC
+Attempting to compile encrypt.c
+testcase1:
+your output was correct
+ - Valgrind was clean (no errors, no memory leaks)
+valgrind was clean
+#################################################
+testcase2:
+ - Valgrind was clean (no errors, no memory leaks)
+valgrind was clean
+#################################################
+testcase3:
+ - Valgrind was clean (no errors, no memory leaks)
+valgrind was clean
+#################################################
+testcase4:
+ - Valgrind was clean (no errors, no memory leaks)
+valgrind was clean
+#################################################
+testcase5:
+ - Valgrind was clean (no errors, no memory leaks)
+valgrind was clean
+#################################################
+testcase6:
+ - Valgrind was clean (no errors, no memory leaks)
+valgrind was clean
+
+Overall Grade: A
diff --git a/28_fix_vg_encr/input.txt b/28_fix_vg_encr/input.txt
new file mode 100644
index 0000000..76a327a
--- /dev/null
+++ b/28_fix_vg_encr/input.txt
@@ -0,0 +1,2 @@
+An example
+Of the input.
diff --git a/28_fix_vg_encr/input.txt.enc b/28_fix_vg_encr/input.txt.enc
new file mode 100644
index 0000000..4d4e357
--- /dev/null
+++ b/28_fix_vg_encr/input.txt.enc
@@ -0,0 +1,2 @@
+hu lehtwsl
+vm aol puwba.