1 #if HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4 
5 #ifndef HAVE_WC_FUNCS
6 
7 #include <sys/types.h>
8 #include <errno.h>
9 
10 #ifndef EILSEQ
11 #define EILSEQ EINVAL
12 #endif
13 
mutt_wctoutf8(char * s,unsigned int c,size_t buflen)14 int mutt_wctoutf8 (char *s, unsigned int c, size_t buflen)
15 {
16   if (c < (1 << 7))
17   {
18     if (s && buflen >= 1)
19       *s++ = c;
20     return 1;
21   }
22   else if (c < (1 << 11))
23   {
24     if (s && buflen >= 2)
25      {
26       *s++ = 0xc0 | (c >> 6);
27       *s++ = 0x80 | (c & 0x3f);
28     }
29     return 2;
30   }
31   else if (c < (1 << 16))
32   {
33     if (s && buflen >= 3)
34     {
35       *s++ = 0xe0 | (c >> 12);
36       *s++ = 0x80 | ((c >> 6) & 0x3f);
37       *s++ = 0x80 | (c & 0x3f);
38     }
39     return 3;
40   }
41   else if (c < (1 << 21))
42   {
43     if (s && buflen >= 4)
44     {
45       *s++ = 0xf0 | (c >> 18);
46       *s++ = 0x80 | ((c >> 12) & 0x3f);
47       *s++ = 0x80 | ((c >> 6) & 0x3f);
48       *s++ = 0x80 | (c & 0x3f);
49     }
50     return 4;
51   }
52   else if (c < (1 << 26))
53   {
54     if (s && buflen >= 5)
55     {
56       *s++ = 0xf8 | (c >> 24);
57       *s++ = 0x80 | ((c >> 18) & 0x3f);
58       *s++ = 0x80 | ((c >> 12) & 0x3f);
59       *s++ = 0x80 | ((c >> 6) & 0x3f);
60       *s++ = 0x80 | (c & 0x3f);
61     }
62     return 5;
63   }
64   else if (c < (1 << 31))
65   {
66     if (s && buflen >= 6)
67     {
68       *s++ = 0xfc | (c >> 30);
69       *s++ = 0x80 | ((c >> 24) & 0x3f);
70       *s++ = 0x80 | ((c >> 18) & 0x3f);
71       *s++ = 0x80 | ((c >> 12) & 0x3f);
72       *s++ = 0x80 | ((c >> 6) & 0x3f);
73       *s++ = 0x80 | (c & 0x3f);
74     }
75     return 6;
76   }
77   errno = EILSEQ;
78   return -1;
79 }
80 
81 #endif /* !HAVE_WC_FUNCS */
82