From: Eugene Syromyatnikov Date: Fri, 31 Aug 2018 04:21:21 +0000 (+0200) Subject: open: implement sprint_open_modes using sprintflags_ex X-Git-Tag: v5.3~110 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=def1bd82dc034bf1ee1d33445dba2210d58e756d;p=strace open: implement sprint_open_modes using sprintflags_ex * 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). --- diff --git a/defs.h b/defs.h index 4f3b8f3e..742c01ec 100644 --- 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 a65e2665..8ac6de83 100644 --- 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 62245f9c..d00a0e21 100644 --- 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));