/* * Filename: md5_words.c * * File Created: 22.10.2017 * Last Modified: Mon 23 Oct 2017 02:46:23 PM MDT * Description: * 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: */ char pipe[2]; int cpid = Fork(); Dup2(pipe[0], 0); write(pipe[1], word, sizeof(word)); if (cpid == 0) { Dup2(pipe[1], 1); close(pipe[0]); char** arguments = {"/usr/bin/md5sum", NULL}; execv("/usr/bin/md5sum", arguments); return 0; } close(pipe[1]); close(pipe[0]); } return 0; }