Void usagechar s printfusage s 01 pwd filen s exit1

Info icon This preview shows pages 20–25. Sign up to view the full content.

View Full Document Right Arrow Icon
void usage(char *s) { printf(‘‘Usage: %s 0|1 pwd < file\n’’, s); exit(1); } int main(int argc, char *argv[]) { int opt; CSC180 Fall 2008, University of Toronto 19
Image of page 20

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
if (argc < 3) usage(argv[0]); opt = atoi(argv[1]); if (opt == 1) encrypt(argv[2]); else if (opt == 0) decrypt(argv[2]); else { printf(‘‘Unrecognized option %d\n’’, opt); usage(argv[0]); } return 0; } Take a look at the function encrypt() . ( decrypt() is just a reverse function of encrypt() .) CSC180 Fall 2008, University of Toronto 20
Image of page 21
In while ((c = getchar()) != EOF) , the expression (c = getchar()) != EOF is evaluated. If the expression is non-zero, the loop body is executed, and the expression is re-evaluated. This cycle repeats until (c = getchar()) != EOF evaluates to zero. The expression (c = getchar()) != EOF reads a character from standard input, assigns this character to c , and tests whether c is EOF (end-of-file signal). Since the equality operator != has higher precedence than the assignment operator = , we put c = getchar() in parentheses. The loop body has only one statement: putchar(c + pwd[count++ % len]); so we can omit curly braces for clarity. Using pwd[count++ % len] , we get a character, indexed by count++ % len , from the password, pwd , where len is the length of pwd , and CSC180 Fall 2008, University of Toronto 21
Image of page 22

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
count is the number of characters that have been read so far. Suppose our password is ‘‘12ab’’ , hence len is 4. When count is 0, we get character ’1’ . When count is 1, we get ’2’ . When count is 3, we get ’b’. When count is 4, we get ’1’, starting over from the leftmost character. Suppose c is ’B’ and pwd[count++ % len] is ’1’, the expression c + pwd[count++ % len]) becomes ’B’ + ’1’ = 66 + 49 = 115 . Adding a value derived from the password to the original character, we encode the original character. Subtracting the value from the encoded character, we recover the original character ( decrypt() ’s job). Let a.out be the program derived from encrypt.c . Encoding command: CSC180 Fall 2008, University of Toronto 22
Image of page 23
./a.out 1 ‘‘12ab’’ < a.txt > b.txt , where the option 1 means encoding, ‘‘12ab’’ is password, < redirects a.txt to a.out , and > redirects a.out ’s output to b.txt . Decoding command: ./a.out 0 ‘‘12ab’’ < b.txt , where the option 0 means decoding. Exercise: modify encrypt.c such that one function can either encode or decode. The function’s prototype may look like this: void code(char *pwd, char mode); CSC180 Fall 2008, University of Toronto 23
Image of page 24

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Infinite loops Infinite for loop: for (;;) { /* loop body */ } Infinite while loop: while (1) { /* loop body */ } CSC180 Fall 2008, University of Toronto 24
Image of page 25
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern