mntopts.c 2.5 KB

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