print_flags.c 808 B

1234567891011121314151617181920212223242526272829303132
  1. /* vi: set sw=4 ts=4: */
  2. /* Print string that matches bit masked flags
  3. *
  4. * Copyright (C) 2008 Natanael Copa <natanael.copa@gmail.com>
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  7. */
  8. #include <libbb.h>
  9. /* returns a set with the flags not printed */
  10. int FAST_FUNC print_flags_separated(const int *masks, const char *labels, int flags, const char *separator)
  11. {
  12. const char *need_separator = NULL;
  13. while (*labels) {
  14. if (flags & *masks) {
  15. printf("%s%s",
  16. need_separator ? need_separator : "",
  17. labels);
  18. need_separator = separator;
  19. flags &= ~ *masks;
  20. }
  21. masks++;
  22. labels += strlen(labels) + 1;
  23. }
  24. return flags;
  25. }
  26. int FAST_FUNC print_flags(const masks_labels_t *ml, int flags)
  27. {
  28. return print_flags_separated(ml->masks, ml->labels, flags, NULL);
  29. }