mntopts.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * mountopts.c --- convert between default mount options and strings
  4. *
  5. * Copyright (C) 2002 Theodore Ts'o <tytso@mit.edu>
  6. *
  7. * This file can be redistributed under the terms of the GNU Library General
  8. * Public License
  9. *
  10. */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. #include <errno.h>
  16. #include "e2p.h"
  17. struct mntopt {
  18. unsigned int mask;
  19. const char *string;
  20. };
  21. static const struct mntopt mntopt_list[] = {
  22. { EXT2_DEFM_DEBUG, "debug" },
  23. { EXT2_DEFM_BSDGROUPS, "bsdgroups" },
  24. { EXT2_DEFM_XATTR_USER, "user_xattr" },
  25. { EXT2_DEFM_ACL, "acl" },
  26. { EXT2_DEFM_UID16, "uid16" },
  27. { EXT3_DEFM_JMODE_DATA, "journal_data" },
  28. { EXT3_DEFM_JMODE_ORDERED, "journal_data_ordered" },
  29. { EXT3_DEFM_JMODE_WBACK, "journal_data_writeback" },
  30. { 0, 0 },
  31. };
  32. const char *e2p_mntopt2string(unsigned int mask)
  33. {
  34. const struct mntopt *f;
  35. static char buf[20];
  36. int fnum;
  37. for (f = mntopt_list; f->string; f++) {
  38. if (mask == f->mask)
  39. return f->string;
  40. }
  41. for (fnum = 0; mask >>= 1; fnum++);
  42. sprintf(buf, "MNTOPT_%d", fnum);
  43. return buf;
  44. }
  45. int e2p_string2mntopt(char *string, unsigned int *mask)
  46. {
  47. const struct mntopt *f;
  48. char *eptr;
  49. int num;
  50. for (f = mntopt_list; f->string; f++) {
  51. if (!strcasecmp(string, f->string)) {
  52. *mask = f->mask;
  53. return 0;
  54. }
  55. }
  56. if (strncasecmp(string, "MNTOPT_", 8))
  57. return 1;
  58. if (string[8] == 0)
  59. return 1;
  60. num = strtol(string+8, &eptr, 10);
  61. if (num > 32 || num < 0)
  62. return 1;
  63. if (*eptr)
  64. return 1;
  65. *mask = 1 << num;
  66. return 0;
  67. }
  68. static char *skip_over_blanks(char *cp)
  69. {
  70. while (*cp && isspace(*cp))
  71. cp++;
  72. return cp;
  73. }
  74. static char *skip_over_word(char *cp)
  75. {
  76. while (*cp && !isspace(*cp) && *cp != ',')
  77. cp++;
  78. return cp;
  79. }
  80. /*
  81. * Edit a mntopt set array as requested by the user. The ok
  82. * parameter, if non-zero, allows the application to limit what
  83. * mntopts the user is allowed to set or clear using this function.
  84. */
  85. int e2p_edit_mntopts(const char *str, __u32 *mntopts, __u32 ok)
  86. {
  87. char *cp, *buf, *next;
  88. int neg;
  89. unsigned int mask;
  90. buf = xstrdup(str);
  91. cp = buf;
  92. while (cp && *cp) {
  93. neg = 0;
  94. cp = skip_over_blanks(cp);
  95. next = skip_over_word(cp);
  96. if (*next == 0)
  97. next = 0;
  98. else
  99. *next = 0;
  100. switch (*cp) {
  101. case '-':
  102. case '^':
  103. neg++;
  104. case '+':
  105. cp++;
  106. break;
  107. }
  108. if (e2p_string2mntopt(cp, &mask))
  109. return 1;
  110. if (ok && !(ok & mask))
  111. return 1;
  112. if (mask & EXT3_DEFM_JMODE)
  113. *mntopts &= ~EXT3_DEFM_JMODE;
  114. if (neg)
  115. *mntopts &= ~mask;
  116. else
  117. *mntopts |= mask;
  118. cp = next ? next+1 : 0;
  119. }
  120. return 0;
  121. }