Browse Source

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).
Eugene Syromyatnikov 1 year ago
parent
commit
def1bd82dc
3 changed files with 24 additions and 25 deletions
  1. 2
    2
      defs.h
  2. 4
    16
      open.c
  3. 18
    7
      xlat.c

+ 2
- 2
defs.h View File

@@ -806,12 +806,12 @@ extern int printflags_ex(uint64_t flags, const char *dflt,
806 806
 			 enum xlat_style, const struct xlat *, ...)
807 807
 	ATTRIBUTE_SENTINEL;
808 808
 extern const char *sprintflags_ex(const char *prefix, const struct xlat *,
809
-				  uint64_t flags, enum xlat_style);
809
+				  uint64_t flags, char sep, enum xlat_style);
810 810
 
811 811
 static inline const char *
812 812
 sprintflags(const char *prefix, const struct xlat *xlat, uint64_t flags)
813 813
 {
814
-	return sprintflags_ex(prefix, xlat, flags, XLAT_STYLE_DEFAULT);
814
+	return sprintflags_ex(prefix, xlat, flags, '\0', XLAT_STYLE_DEFAULT);
815 815
 }
816 816
 
817 817
 extern const char *sprinttime(long long sec);

+ 4
- 16
open.c View File

@@ -54,11 +54,10 @@ print_dirfd(struct tcb *tcp, int fd)
54 54
 const char *
55 55
 sprint_open_modes(unsigned int flags)
56 56
 {
57
-	static char outstr[(1 + ARRAY_SIZE(open_mode_flags)) * sizeof("O_LARGEFILE")];
57
+	static char outstr[sizeof("flags O_ACCMODE")];
58 58
 	char *p;
59 59
 	char sep;
60 60
 	const char *str;
61
-	const struct xlat *x;
62 61
 
63 62
 	sep = ' ';
64 63
 	p = stpcpy(outstr, "flags");
@@ -71,21 +70,10 @@ sprint_open_modes(unsigned int flags)
71 70
 			return outstr;
72 71
 		sep = '|';
73 72
 	}
73
+	*p = '\0';
74 74
 
75
-	for (x = open_mode_flags; x->str; x++) {
76
-		if ((flags & x->val) == x->val) {
77
-			*p++ = sep;
78
-			p = stpcpy(p, x->str);
79
-			flags &= ~x->val;
80
-			if (!flags)
81
-				return outstr;
82
-			sep = '|';
83
-		}
84
-	}
85
-	/* flags is still nonzero */
86
-	*p++ = sep;
87
-	p = xappendstr(outstr, p, "%#x", flags);
88
-	return outstr;
75
+	return sprintflags_ex(outstr, open_mode_flags, flags, sep,
76
+			      XLAT_STYLE_ABBREV) ?: outstr;
89 77
 }
90 78
 
91 79
 void

+ 18
- 7
xlat.c View File

@@ -292,7 +292,7 @@ printxval_indexn_ex(const struct xlat *xlat, size_t xlat_size, uint64_t val,
292 292
  */
293 293
 const char *
294 294
 sprintflags_ex(const char *prefix, const struct xlat *xlat, uint64_t flags,
295
-	       enum xlat_style style)
295
+	       char sep, enum xlat_style style)
296 296
 {
297 297
 	static char outstr[1024];
298 298
 	char *outptr;
@@ -305,6 +305,8 @@ sprintflags_ex(const char *prefix, const struct xlat *xlat, uint64_t flags,
305 305
 		if (!flags)
306 306
 			return NULL;
307 307
 
308
+		if (sep)
309
+			*outptr++ = sep;
308 310
 		outptr = xappendstr(outstr, outptr, "%s",
309 311
 				    sprint_xlat_val(flags, style));
310 312
 
@@ -312,6 +314,8 @@ sprintflags_ex(const char *prefix, const struct xlat *xlat, uint64_t flags,
312 314
 	}
313 315
 
314 316
 	if (flags == 0 && xlat->val == 0 && xlat->str) {
317
+		if (sep)
318
+			*outptr++ = sep;
315 319
 		if (xlat_verbose(style) == XLAT_STYLE_VERBOSE) {
316 320
 			outptr = xappendstr(outstr, outptr, "0 /* %s */",
317 321
 					    xlat->str);
@@ -322,26 +326,33 @@ sprintflags_ex(const char *prefix, const struct xlat *xlat, uint64_t flags,
322 326
 		return outstr;
323 327
 	}
324 328
 
325
-	if (xlat_verbose(style) == XLAT_STYLE_VERBOSE && flags)
329
+	if (xlat_verbose(style) == XLAT_STYLE_VERBOSE && flags) {
330
+		if (sep) {
331
+			*outptr++ = sep;
332
+			sep = '\0';
333
+		}
326 334
 		outptr = xappendstr(outstr, outptr, "%s",
327 335
 				    sprint_xlat_val(flags, style));
336
+	}
328 337
 
329 338
 	for (; flags && xlat->str; xlat++) {
330 339
 		if (xlat->val && (flags & xlat->val) == xlat->val) {
331
-			if (found)
332
-				*outptr++ = '|';
333
-			else if (xlat_verbose(style) == XLAT_STYLE_VERBOSE)
340
+			if (sep) {
341
+				*outptr++ = sep;
342
+			} else if (xlat_verbose(style) == XLAT_STYLE_VERBOSE) {
334 343
 				outptr = stpcpy(outptr, " /* ");
344
+			}
335 345
 
336 346
 			outptr = stpcpy(outptr, xlat->str);
337 347
 			found = 1;
348
+			sep = '|';
338 349
 			flags &= ~xlat->val;
339 350
 		}
340 351
 	}
341 352
 
342 353
 	if (flags) {
343
-		if (found)
344
-			*outptr++ = '|';
354
+		if (sep)
355
+			*outptr++ = sep;
345 356
 		if (found || xlat_verbose(style) != XLAT_STYLE_VERBOSE)
346 357
 			outptr = xappendstr(outstr, outptr, "%s",
347 358
 					    sprint_xlat_val(flags, style));

Loading…
Cancel
Save