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