This assignment consists of two programming questions.
Please use the gnu (Free Software
Foundation) C compiler, gcc
. To help with debugging, use
the gnu debugger, gdb
. valgrind
DUE DATE: 11:59 pm, Friday October 6.
NO EXTENSIONS.
Watch the discussion group for possible updates.
#include <stdio.h> #include <stdlib.h> void get_line(char *s,int offset, char mode) { /* your code here */ } void put_line(char *s) { /* your code here */ } int main(int argc,char **argv) { char s[300], mode; int offset; if ((argc < 2) || (argc > 3)) { printf("Usage: %s {e|d|p} n\n where n is an integer from 0 to 255.\n",argv[0]); return 1; } else { mode=*argv[1]; if ((mode == 'e') || (mode == 'd')) { if (argc < 3) { printf("Please supply an offset.\n"); return 1; } } offset=atoi(argv[2]); if ((offset < 0) || (offset > 255)) { printf("Usage: %s {e|d|p} n\n where n is an integer from 0 to 255.\n",argv[0]); return 1; } } while(1) { get_line(s,offset,mode); if(!s[0]) break; put_line(s); } }You should modify only the places where
/* your code here
*/
appears.
Your code for get_line
and put_line
should
duplicate the functionality of the standard gets
and
puts
library routines, except that (1) you need not return a
useful value, (2) get_line
should not strip the
newline characters off the end of its input lines, and
put_line
should not add a newline to its output lines
(This behavior with respect to newlines is the same as displayed by the
file-specific fgets
and fputs
routines.), and
(3) you will be adding encoding/decoding functionality.
get_line
should do the following:
mode
is "e" (encrypt) then convert each
character except the terminal newline of each line to a string of 3
characters. These three characters represent the digits (the
ASCII representation) of the decimal value of the original
character plus offset
, all modulo 256. For example,
given the line "abcd\n" ("\n" is the terminal newline character), the
string s
returned by get_line
should be "098099100101\n" if
offset
is 1 (because 97 is the ASCII code for "a"...).
mode
is "d" (decrypt) then convert each triple
of digits into the
corresponding character after converting it into an integer and subtracting
offset
, modulo 256. The terminal newline
should be left intact by this process. So, given the input
"098099100101\n" and offset
1, the string s
should be "abcd\n".
mode
is "p" (plain text) then do not modify the
input/output.
Within get_line
and put_line
you should use the getchar
and putchar
library
routines. You are not permitted (for this one assignment) to use
gets
, puts
, scanf
, or
printf
in order to read from/to stdin/stdout.
You may, however, use sprintf
and atoi
for internal manipulation of your characters.
Your code must reside in a file named codec.c
in a
directory named codec
, which must be an immediate
subdirectory of the directory in which you run the
turnin
script.
Finally, in your README.txt
or README.pdf
file, you must answer the following questions: What may happen if you
feed your codec
program input containing a line longer
than 100 characters? What might you do about this problem?
tail
when
run with the -r
command-line switch
(except for the encryption/decryption).)
You should store your strings internally in a linked list.
You may place a (large) fixed limit on the length of an input line, but
you should try not to use that much space for every line: once
read into memory, a line should be copied to dynamically allocated space
of the appropriate size.
Your code should handle an arbitrary number of input lines.
You may use any of the functions in the <string.h> library package,
with the exception of strdup
;
type "man 3 string
" to learn more.
Prior to terminating, your program must deallocate (free
)
any dynamically allocated (malloc
ed) space. (Your program
will appear to run correctly even if you don't, but your code would then
"leak storage" if used as part of a larger system. The TAs will be
checking for proper memory cleanup.
Your code must reside in a directory named reverselines
,
which must be an immediate subdirectory of the directory in which you
run the turnin
script.
Documentation for the programming
questions must appear in your README.txt
or
README.pdf
file. This file
must be located in the directory in which you run the
turnin
script.
You should also provide a Makefile
(this tutorial about Make might be helpful) in order to
facilitate the compilation of your code.
The only other things in this directory should be the two subdirectories
codec
and reverselines
.