3
0

fgetsetflags.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * fgetflags.c - Get a file flags on an ext2 file system
  4. * fsetflags.c - Set a file flags on an ext2 file system
  5. *
  6. * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
  7. * Laboratoire MASI, Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. *
  10. * This file can be redistributed under the terms of the GNU Library General
  11. * Public License
  12. */
  13. /*
  14. * History:
  15. * 93/10/30 - Creation
  16. */
  17. #ifdef HAVE_ERRNO_H
  18. #include <errno.h>
  19. #endif
  20. #ifdef HAVE_UNISTD_H
  21. #include <unistd.h>
  22. #endif
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #ifdef HAVE_EXT2_IOCTLS
  26. #include <fcntl.h>
  27. #include <sys/ioctl.h>
  28. #endif
  29. #include "e2p.h"
  30. #ifdef O_LARGEFILE
  31. #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
  32. #else
  33. #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
  34. #endif
  35. int fgetsetflags (const char * name, unsigned long * get_flags, unsigned long set_flags)
  36. {
  37. #ifdef HAVE_EXT2_IOCTLS
  38. struct stat buf;
  39. int fd, r, f, save_errno = 0;
  40. if (!stat(name, &buf) &&
  41. !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
  42. goto notsupp;
  43. }
  44. fd = open (name, OPEN_FLAGS);
  45. if (fd == -1)
  46. return -1;
  47. if (!get_flags) {
  48. f = (int) set_flags;
  49. r = ioctl (fd, EXT2_IOC_SETFLAGS, &f);
  50. } else {
  51. r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
  52. *get_flags = f;
  53. }
  54. if (r == -1)
  55. save_errno = errno;
  56. close (fd);
  57. if (save_errno)
  58. errno = save_errno;
  59. return r;
  60. notsupp:
  61. #endif /* HAVE_EXT2_IOCTLS */
  62. errno = EOPNOTSUPP;
  63. return -1;
  64. }