Debian Codegraph @

src/atoms.c

raw
1
2 #ifdef HAVE_CONFIG_H
3 #include "config.h"
4 #endif
5
6 /* Rely on vasprintf (GNU extension) instead of vsnprintf if
7 possible... */
8 #define _GNU_SOURCE
9 #include <stdio.h>
10
11 #include <xcb/xcb.h>
12 #include <stdlib.h>
13 #include <stdarg.h>
14 #include "xcb_atom.h"
15
16 #ifndef __has_attribute
17 # define __has_attribute(x) 0 /* Compatibility with older compilers. */
18 #endif
19
20 #if __has_attribute(__format__) \
21 || defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)
22 __attribute__((__format__(__printf__,1,2)))
23 #endif
24 static char *makename(const char *fmt, ...)
25 {
26 char *ret;
27 int n;
28 va_list ap;
29
30 #ifndef HAVE_VASPRINTF
31 char *np;
32 int size = 64;
33
34 /* First allocate 'size' bytes, should be enough usually */
35 if((ret = malloc(size)) == NULL)
36 return NULL;
37
38 while(1)
39 {
40 va_start(ap, fmt);
41 n = vsnprintf(ret, size, fmt, ap);
42 va_end(ap);
43
44 if(n < 0)
45 return NULL;
46
47 if(n < size)
48 return ret;
49
50 size = n + 1;
51 if((np = realloc(ret, size)) == NULL)
52 {
53 free(ret);
54 return NULL;
55 }
56
57 ret = np;
58 }
59 #else
60 va_start(ap, fmt);
61 n = vasprintf(&ret, fmt, ap);
62 va_end(ap);
63
64 if(n < 0)
65 return NULL;
66
67 return ret;
68 #endif
69 }
70
71 char *xcb_atom_name_by_screen(const char *base, uint8_t screen)
72 {
73 return makename("%s_S%u", base, screen);
74 }
75
76 char *xcb_atom_name_by_resource(const char *base, uint32_t resource)
77 {
78 return makename("%s_R%08X", base, resource);
79 }
80
81 char *xcb_atom_name_unique(const char *base, uint32_t id)
82 {
83 if(base)
84 return makename("%s_U%u", base, id);
85 else
86 return makename("U%u", id);
87 }