]> granicus.if.org Git - strace/commitdiff
open: implement sprint_open_modes using sprintflags_ex
authorEugene Syromyatnikov <evgsyr@gmail.com>
Fri, 31 Aug 2018 04:21:21 +0000 (06:21 +0200)
committerEugene Syromyatnikov <evgsyr@gmail.com>
Thu, 18 Jul 2019 13:58:07 +0000 (15:58 +0200)
* defs.h (sprintflags_ex): Add "sep" argument.
(sprintflags): Pass '\0' in "sep" argument.
* open.c (sprint_open_modes): Use sprintflags_ex for printing
open_mode_flags.
* xlat.c (sprintflags_ex): Add "sep" argument, use it as initial
separator (if not nul).

defs.h
open.c
xlat.c

diff --git a/defs.h b/defs.h
index 4f3b8f3e984c8b7e7a37b9214a04d09a884d82c5..742c01ec2ccbff4842f584f7649395a13b18a398 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -806,12 +806,12 @@ extern int printflags_ex(uint64_t flags, const char *dflt,
                         enum xlat_style, const struct xlat *, ...)
        ATTRIBUTE_SENTINEL;
 extern const char *sprintflags_ex(const char *prefix, const struct xlat *,
-                                 uint64_t flags, enum xlat_style);
+                                 uint64_t flags, char sep, enum xlat_style);
 
 static inline const char *
 sprintflags(const char *prefix, const struct xlat *xlat, uint64_t flags)
 {
-       return sprintflags_ex(prefix, xlat, flags, XLAT_STYLE_DEFAULT);
+       return sprintflags_ex(prefix, xlat, flags, '\0', XLAT_STYLE_DEFAULT);
 }
 
 extern const char *sprinttime(long long sec);
diff --git a/open.c b/open.c
index a65e26657caf50b241e7965955695f66171902b7..8ac6de8319e25231df2a2b66a17c4139ecad7b15 100644 (file)
--- a/open.c
+++ b/open.c
@@ -54,11 +54,10 @@ print_dirfd(struct tcb *tcp, int fd)
 const char *
 sprint_open_modes(unsigned int flags)
 {
-       static char outstr[(1 + ARRAY_SIZE(open_mode_flags)) * sizeof("O_LARGEFILE")];
+       static char outstr[sizeof("flags O_ACCMODE")];
        char *p;
        char sep;
        const char *str;
-       const struct xlat *x;
 
        sep = ' ';
        p = stpcpy(outstr, "flags");
@@ -71,21 +70,10 @@ sprint_open_modes(unsigned int flags)
                        return outstr;
                sep = '|';
        }
+       *p = '\0';
 
-       for (x = open_mode_flags; x->str; x++) {
-               if ((flags & x->val) == x->val) {
-                       *p++ = sep;
-                       p = stpcpy(p, x->str);
-                       flags &= ~x->val;
-                       if (!flags)
-                               return outstr;
-                       sep = '|';
-               }
-       }
-       /* flags is still nonzero */
-       *p++ = sep;
-       p = xappendstr(outstr, p, "%#x", flags);
-       return outstr;
+       return sprintflags_ex(outstr, open_mode_flags, flags, sep,
+                             XLAT_STYLE_ABBREV) ?: outstr;
 }
 
 void
diff --git a/xlat.c b/xlat.c
index 62245f9cb2900e47e47394abfef63b135711516e..d00a0e2198567ae0ae42a2c14992d020e0e4a6c5 100644 (file)
--- a/xlat.c
+++ b/xlat.c
@@ -292,7 +292,7 @@ printxval_indexn_ex(const struct xlat *xlat, size_t xlat_size, uint64_t val,
  */
 const char *
 sprintflags_ex(const char *prefix, const struct xlat *xlat, uint64_t flags,
-              enum xlat_style style)
+              char sep, enum xlat_style style)
 {
        static char outstr[1024];
        char *outptr;
@@ -305,6 +305,8 @@ sprintflags_ex(const char *prefix, const struct xlat *xlat, uint64_t flags,
                if (!flags)
                        return NULL;
 
+               if (sep)
+                       *outptr++ = sep;
                outptr = xappendstr(outstr, outptr, "%s",
                                    sprint_xlat_val(flags, style));
 
@@ -312,6 +314,8 @@ sprintflags_ex(const char *prefix, const struct xlat *xlat, uint64_t flags,
        }
 
        if (flags == 0 && xlat->val == 0 && xlat->str) {
+               if (sep)
+                       *outptr++ = sep;
                if (xlat_verbose(style) == XLAT_STYLE_VERBOSE) {
                        outptr = xappendstr(outstr, outptr, "0 /* %s */",
                                            xlat->str);
@@ -322,26 +326,33 @@ sprintflags_ex(const char *prefix, const struct xlat *xlat, uint64_t flags,
                return outstr;
        }
 
-       if (xlat_verbose(style) == XLAT_STYLE_VERBOSE && flags)
+       if (xlat_verbose(style) == XLAT_STYLE_VERBOSE && flags) {
+               if (sep) {
+                       *outptr++ = sep;
+                       sep = '\0';
+               }
                outptr = xappendstr(outstr, outptr, "%s",
                                    sprint_xlat_val(flags, style));
+       }
 
        for (; flags && xlat->str; xlat++) {
                if (xlat->val && (flags & xlat->val) == xlat->val) {
-                       if (found)
-                               *outptr++ = '|';
-                       else if (xlat_verbose(style) == XLAT_STYLE_VERBOSE)
+                       if (sep) {
+                               *outptr++ = sep;
+                       } else if (xlat_verbose(style) == XLAT_STYLE_VERBOSE) {
                                outptr = stpcpy(outptr, " /* ");
+                       }
 
                        outptr = stpcpy(outptr, xlat->str);
                        found = 1;
+                       sep = '|';
                        flags &= ~xlat->val;
                }
        }
 
        if (flags) {
-               if (found)
-                       *outptr++ = '|';
+               if (sep)
+                       *outptr++ = sep;
                if (found || xlat_verbose(style) != XLAT_STYLE_VERBOSE)
                        outptr = xappendstr(outstr, outptr, "%s",
                                            sprint_xlat_val(flags, style));