]> arthur.barton.de Git - netatalk.git/blob - libatalk/compat/misc.c
Spotlight: use async Tracker SPARQL API
[netatalk.git] / libatalk / compat / misc.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif /* HAVE_CONFIG_H */
4
5 #include <stdio.h>
6 #include <stdarg.h>
7
8 #include <atalk/compat.h>
9
10 #if !defined HAVE_DIRFD && defined SOLARIS
11 #include <dirent.h>
12 int dirfd(DIR *dir)
13 {
14     return dir->d_fd;
15 }
16 #endif
17
18 #ifndef HAVE_STRNLEN
19 size_t strnlen(const char *s, size_t max)
20 {
21     size_t len;
22   
23     for (len = 0; len < max; len++) {
24         if (s[len] == '\0') {
25             break;
26         }
27     }
28     return len;  
29 }
30 #endif
31
32 #ifndef HAVE_VASPRINTF
33 int vasprintf(char **ret, const char *fmt, va_list ap)
34 {
35     int n, size = 64;
36     char *p, *np;
37
38     if ((p = malloc(size)) == NULL)
39         return NULL;
40
41     while (1) {
42         /* Try to print in the allocated space. */
43         n = vsnprintf(p, size, fmt, ap);
44         /* If that worked, return the string. */
45         if (n > -1 && n < size) {
46             *ret = p;
47             return n;
48         }
49         /* Else try again with more space. */
50         if (n > -1)    /* glibc 2.1 */
51             size = n+1; /* precisely what is needed */
52         else           /* glibc 2.0 */
53             size *= 2;  /* twice the old size */
54         if ((np = realloc (p, size)) == NULL) {
55             free(p);
56             *ret = NULL;
57             return -1;
58         } else {
59             p = np;
60         }
61     }
62 }
63 #endif
64
65 #ifndef HAVE_ASPRINTF
66 int asprintf(char **strp, const char *fmt, ...)
67 {
68     va_list ap;
69     int len;
70
71     va_start(ap, fmt);
72     len = vasprintf(strp, fmt, ap);
73     va_end(ap);
74
75     return len;
76 }
77 #endif