]> granicus.if.org Git - strace/blob - tests/printflags.c
Update copyright headers
[strace] / tests / printflags.c
1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 2005-2018 Dmitry V. Levin <ldv@altlinux.org>
7  * All rights reserved.
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11
12 #include "tests.h"
13 #include "xlat.h"
14 #include <stdio.h>
15
16 int
17 printflags(const struct xlat *xlat, unsigned long long flags,
18            const char *const dflt)
19 {
20         if (flags == 0 && xlat->val == 0 && xlat->str) {
21                 fputs(xlat->str, stdout);
22                 return 1;
23         }
24
25         int n;
26         char sep = 0;
27         for (n = 0; xlat->str; xlat++) {
28                 if (xlat->val && (flags & xlat->val) == xlat->val) {
29                         if (sep)
30                                 putc(sep, stdout);
31                         else
32                                 sep = '|';
33                         fputs(xlat->str, stdout);
34                         flags &= ~xlat->val;
35                         n++;
36                 }
37         }
38
39         if (n) {
40                 if (flags) {
41                         if (sep)
42                                 putc(sep, stdout);
43                         printf("%#llx", flags);
44                         n++;
45                 }
46         } else {
47                 if (flags) {
48                         printf("%#llx", flags);
49                         if (dflt)
50                                 printf(" /* %s */", dflt);
51                 } else {
52                         if (dflt)
53                                 putc('0', stdout);
54                 }
55         }
56
57         return n;
58 }