1 /* Copyright (c) 2002-2006 Lucent Technologies; see LICENSE */
6 * As of 2020, older systems like RHEL 6 and AIX still do not have C11 atomics.
7 * On those systems, make the code use volatile int accesses and hope for the best.
8 * (Most uses of fmtinstall are not actually racing with calls to print that lookup
9 * formats. The code used volatile here for years without too many problems,
10 * even though that's technically racy. A mutex is not OK, because we want to
11 * be able to call print from signal handlers.)
13 * RHEL is using an old GCC (atomics were added in GCC 4.9).
14 * AIX is using its own IBM compiler (XL C).
16 #if __IBMC__ || !__clang__ && __GNUC__ && (__GNUC__ < 4 || (__GNUC__==4 && __GNUC_MINOR__<9))
17 #warning not using C11 stdatomic on legacy system
18 #define _Atomic volatile
19 #define atomic_load(x) (*(x))
20 #define atomic_store(x, y) (*(x)=(y))
21 #define ATOMIC_VAR_INIT(x) (x)
23 #include <stdatomic.h>
35 typedef struct Convfmt Convfmt;
45 * lock updates to fmt by calling __fmtlock, __fmtunlock.
46 * reads can start at nfmt and work backward without
47 * further locking. later fmtinstalls take priority over earlier
48 * ones because of the backwards loop.
49 * once installed, a format is never overwritten.
67 {'C', __runefmt}, /* Plan 9 addition */
70 {'F', __efgfmt}, /* ANSI only */
74 {'L', __flagfmt}, /* ANSI only */
76 {'S', __runesfmt}, /* Plan 9 addition */
78 {'b', __ifmt}, /* Plan 9 addition */
86 {'i', __ifmt}, /* ANSI only */
103 int (*fmtdoquote)(int);
106 * __fmtlock() must be set
109 __fmtinstall(int c, Fmts f)
119 i = atomic_load(&fmtalloc.nfmt);
122 p = &fmtalloc.fmt[i];
125 atomic_store(&fmtalloc.nfmt, i+1);
131 fmtinstall(int c, int (*f)(Fmt*))
136 ret = __fmtinstall(c, f);
146 ep = &fmtalloc.fmt[atomic_load(&fmtalloc.nfmt)];
147 for(p=ep; p-- > fmtalloc.fmt; )
155 __fmtdispatch(Fmt *f, void *fmt, int isrunes)
161 f->width = f->prec = 0;
166 fmt = (Rune*)fmt + 1;
168 fmt = (char*)fmt + chartorune(&rune, (char*)fmt);
176 f->flags |= FmtWidth|FmtPrec;
179 if(!(f->flags & FmtWidth)){
184 case '1': case '2': case '3': case '4':
185 case '5': case '6': case '7': case '8': case '9':
187 while(r >= '0' && r <= '9'){
188 i = i * 10 + r - '0';
191 fmt = (Rune*)fmt + 1;
194 fmt = (char*)fmt + 1;
198 fmt = (Rune*)fmt - 1;
200 fmt = (char*)fmt - 1;
202 if(f->flags & FmtWidth){
206 f->flags |= FmtWidth;
211 i = va_arg(f->args, int);
214 * negative precision =>
215 * ignore the precision.
217 if(f->flags & FmtPrec){
218 f->flags &= ~FmtPrec;