mountpoint.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* See LICENSE file for copyright and license details. */
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <sys/sysmacros.h>
  5. #include <mntent.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include "util.h"
  11. static void
  12. usage(void)
  13. {
  14. eprintf("usage: %s [-dqx] target\n", argv0);
  15. }
  16. int
  17. main(int argc, char *argv[])
  18. {
  19. int dflag = 0, qflag = 0, xflag = 0;
  20. int ret = 0;
  21. struct mntent *me = NULL;
  22. FILE *fp;
  23. struct stat st1, st2;
  24. ARGBEGIN {
  25. case 'd':
  26. dflag = 1;
  27. break;
  28. case 'q':
  29. qflag = 1;
  30. break;
  31. case 'x':
  32. xflag = 1;
  33. break;
  34. default:
  35. usage();
  36. } ARGEND;
  37. if (argc < 1)
  38. usage();
  39. if (stat(argv[0], &st1) < 0) {
  40. if (qflag)
  41. return 1;
  42. eprintf("stat %s:", argv[0]);
  43. }
  44. if (xflag) {
  45. if (!S_ISBLK(st1.st_mode)) {
  46. if (qflag)
  47. return 1;
  48. eprintf("stat: %s: not a block device\n",
  49. argv[0]);
  50. }
  51. printf("%u:%u\n", major(st1.st_rdev),
  52. minor(st1.st_rdev));
  53. return 0;
  54. }
  55. if (!S_ISDIR(st1.st_mode)) {
  56. if (qflag)
  57. return 1;
  58. eprintf("stat %s: not a directory\n", argv[0]);
  59. }
  60. if (dflag) {
  61. printf("%u:%u\n", major(st1.st_dev),
  62. minor(st1.st_dev));
  63. return 0;
  64. }
  65. fp = setmntent("/proc/mounts", "r");
  66. if (!fp) {
  67. if (qflag)
  68. return 1;
  69. eprintf("setmntent %s:", "/proc/mounts");
  70. }
  71. while ((me = getmntent(fp)) != NULL) {
  72. if (stat(me->mnt_dir, &st2) < 0) {
  73. if (qflag)
  74. return 1;
  75. eprintf("stat %s:", me->mnt_dir);
  76. }
  77. if (st1.st_dev == st2.st_dev &&
  78. st1.st_ino == st2.st_ino)
  79. break;
  80. }
  81. endmntent(fp);
  82. if (me == NULL)
  83. ret = 1;
  84. if (!qflag)
  85. printf("%s %s a mountpoint\n", argv[0],
  86. !ret ? "is" : "is not");
  87. return ret;
  88. }