Merge branch 'vendor/GCC44'
[dragonfly.git] / bin / cpdup / compat_sun.c
1 /*
2  * Implement some functions which are unknown to Solaris 10.
3  */
4
5 #include "compat_sun.h"
6 #include <stdlib.h>
7
8 int
9 vasprintf(char **str, const char *format, va_list ap)
10 {
11     char *newstr;
12     char dummy[2];
13     int result;
14
15     if ((result = vsnprintf(dummy, 2, format, ap)) < 0) {
16         *str = NULL;
17         return (result);
18     }
19     if ((*str = malloc(result + 1)) == NULL)
20         return (-1);
21     if ((result = vsnprintf(*str, result + 1, format, ap)) < 0) {
22         free(*str);
23         *str = NULL;
24     }
25     return (result);
26 }
27
28 int
29 asprintf(char **str, const char *format, ...)
30 {
31     va_list ap;
32     int result;
33
34     va_start(ap, format);
35     result = vasprintf(str, format, ap);
36     va_end(ap);
37     return (result);
38 }
39
40 #ifndef NOMD5
41
42 #include <sys/types.h>
43 #include <sys/uio.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46
47 char *
48 MD5File(const char *filename, char *buf)
49 {
50     unsigned char dbuf[8192];
51     MD5_CTX ctx;
52     int fd;
53     int count;
54
55     if ((fd = open(filename, O_RDONLY)) < 0)
56         return (NULL);
57     MD5Init(&ctx);
58     while ((count = read(fd, dbuf, sizeof(dbuf))) > 0)
59         MD5Update(&ctx, dbuf, count);
60     close(fd);
61     if (count < 0)
62         return (NULL);
63     if (buf == NULL)
64         if ((buf = malloc(33)) == NULL)
65             return NULL;
66     MD5Final(dbuf, &ctx);
67     for (count = 0; count < 16; count++)
68         sprintf(buf + count * 2, "%02x", dbuf[count]);
69     return (buf);
70 }
71
72 #endif  /* ifndef NOMD5 */