3
0

lsattr.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * lsattr.c - List file attributes on an ext2 file system
  4. *
  5. * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
  6. * Laboratoire MASI, Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * This file can be redistributed under the terms of the GNU General
  10. * Public License
  11. */
  12. //config:config LSATTR
  13. //config: bool "lsattr (5.5 kb)"
  14. //config: default y
  15. //config: select PLATFORM_LINUX
  16. //config: help
  17. //config: lsattr lists the file attributes on a second extended file system.
  18. //applet:IF_LSATTR(APPLET_NOEXEC(lsattr, lsattr, BB_DIR_BIN, BB_SUID_DROP, lsattr))
  19. /* ls is NOEXEC, so we should be too! ;) */
  20. //kbuild:lib-$(CONFIG_LSATTR) += lsattr.o e2fs_lib.o
  21. //usage:#define lsattr_trivial_usage
  22. //usage: "[-Radlv] [FILE]..."
  23. //usage:#define lsattr_full_usage "\n\n"
  24. //usage: "List ext2 file attributes\n"
  25. //usage: "\n -R Recurse"
  26. //usage: "\n -a Don't hide entries starting with ."
  27. //usage: "\n -d List directory entries instead of contents"
  28. //usage: "\n -l List long flag names"
  29. //usage: "\n -v List version/generation number"
  30. #include "libbb.h"
  31. #include "e2fs_lib.h"
  32. enum {
  33. OPT_RECUR = 0x1,
  34. OPT_ALL = 0x2,
  35. OPT_DIRS_OPT = 0x4,
  36. OPT_PF_LONG = 0x8,
  37. OPT_GENERATION = 0x10,
  38. };
  39. static void list_attributes(const char *name)
  40. {
  41. unsigned long fsflags;
  42. unsigned long generation;
  43. if (fgetflags(name, &fsflags) != 0)
  44. goto read_err;
  45. if (option_mask32 & OPT_GENERATION) {
  46. if (fgetversion(name, &generation) != 0)
  47. goto read_err;
  48. printf("%5lu ", generation);
  49. }
  50. if (option_mask32 & OPT_PF_LONG) {
  51. printf("%-28s ", name);
  52. print_e2flags(stdout, fsflags, PFOPT_LONG);
  53. bb_putchar('\n');
  54. } else {
  55. print_e2flags(stdout, fsflags, 0);
  56. printf(" %s\n", name);
  57. }
  58. return;
  59. read_err:
  60. bb_perror_msg("reading %s", name);
  61. }
  62. static int FAST_FUNC lsattr_dir_proc(const char *dir_name,
  63. struct dirent *de,
  64. void *private UNUSED_PARAM)
  65. {
  66. struct stat st;
  67. char *path;
  68. path = concat_path_file(dir_name, de->d_name);
  69. if (lstat(path, &st) != 0)
  70. bb_perror_msg("stat %s", path);
  71. else if (de->d_name[0] != '.' || (option_mask32 & OPT_ALL)) {
  72. list_attributes(path);
  73. if (S_ISDIR(st.st_mode) && (option_mask32 & OPT_RECUR)
  74. && !DOT_OR_DOTDOT(de->d_name)
  75. ) {
  76. printf("\n%s:\n", path);
  77. iterate_on_dir(path, lsattr_dir_proc, NULL);
  78. bb_putchar('\n');
  79. }
  80. }
  81. free(path);
  82. return 0;
  83. }
  84. static void lsattr_args(const char *name)
  85. {
  86. struct stat st;
  87. if (lstat(name, &st) == -1) {
  88. bb_perror_msg("stat %s", name);
  89. } else if (S_ISDIR(st.st_mode) && !(option_mask32 & OPT_DIRS_OPT)) {
  90. iterate_on_dir(name, lsattr_dir_proc, NULL);
  91. } else {
  92. list_attributes(name);
  93. }
  94. }
  95. int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  96. int lsattr_main(int argc UNUSED_PARAM, char **argv)
  97. {
  98. getopt32(argv, "Radlv");
  99. argv += optind;
  100. if (!*argv)
  101. *--argv = (char*)".";
  102. do lsattr_args(*argv++); while (*argv);
  103. return EXIT_SUCCESS;
  104. }