[project @ 1996-06-12 10:18:24 by partain]
[nofib.git] / utils / md4tools / pipeit.c
1 /*
2 ** Utility routines to interface to the MD4 program as a filter.
3 */
4 #include <stdio.h>
5 #include "md4_def.h"
6
7 #ifdef USE_STRCHR
8 #define RDX strrchr
9 #else
10 #define RDX rindex
11 #endif /* USE_STRCHR */
12
13 static char OutputFile[] = "/tmp/hashcodeXXXXXX";
14 static char ChecksumBuffer[HDRTEXTSIZE + 2];
15 static FILE *Stream;
16
17 extern char *RDX();
18 extern char *mktemp();
19 #ifdef CHARPSPRINTF
20 extern char *sprintf();
21 #endif /* CHARPSPRINTF */
22
23
24 /*
25 ** Spawn a MD4 that has its output redirected.
26 */
27 FILE *
28 Md4Open()
29 {
30 char buff[sizeof OutputFile + 20];
31
32 /* Open stream to md4. */
33 (void)mktemp(OutputFile);
34 (void)sprintf(buff, "md4 >%s", OutputFile);
35 if ((Stream = popen(buff, "w")) == NULL)
36 (void)unlink(OutputFile);
37 return Stream;
38 }
39
40
41 /*
42 ** Close the pipe and read in the Md4's output.
43 */
44 char *
45 Md4Close()
46 {
47 FILE *F;
48 char *p;
49
50 (void)pclose(Stream);
51
52 /* Open the output file, read the one line. */
53 if ((F = fopen(OutputFile, "r")) == NULL)
54 return NULL;
55 p = fgets(ChecksumBuffer, sizeof ChecksumBuffer, F);
56 (void)fclose(F);
57 (void)unlink(OutputFile);
58 if (p == NULL)
59 return NULL;
60
61 /* Kill the newline. */
62 if ((p = RDX(ChecksumBuffer, '\n')) == NULL)
63 return NULL;
64 *p = '\0';
65 return ChecksumBuffer;
66 }