/* Compute and print the MD5 checksum of each word in the system dictionary. We can use "/usr/bin/md5sum" to compute one checksum. */ #include "csapp.h" int main() { FILE *f; char word[256]; size_t len; f = fopen("/usr/share/dict/words", "r"); while (fgets(word, sizeof(word), f)) { len = strlen(word); if (len && word[len-1] == '\n') word[--len] = 0; /* We want to print the 32-character MD5 sum after each word and on the same line as the word: */ printf("%s\n", word); { char *args[] = { "/usr/bin/md5sum", NULL }; pid_t pid; int status, fds[2]; Pipe(fds); pid = Fork(); if (pid == 0) { Write(fds[1], word, strlen(word)); Close(fds[1]); Dup2(fds[0], 0); Execve("/usr/bin/md5sum", args, environ); } Close(fds[0]); Close(fds[1]); Waitpid(pid, &status, 0); } } return 0; }