lsattr.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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"
  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(lsattr, BB_DIR_BIN, BB_SUID_DROP))
  19. //kbuild:lib-$(CONFIG_LSATTR) += lsattr.o e2fs_lib.o
  20. //usage:#define lsattr_trivial_usage
  21. //usage: "[-Radlv] [FILE]..."
  22. //usage:#define lsattr_full_usage "\n\n"
  23. //usage: "List ext2 file attributes\n"
  24. //usage: "\n -R Recurse"
  25. //usage: "\n -a Don't hide entries starting with ."
  26. //usage: "\n -d List directory entries instead of contents"
  27. //usage: "\n -l List long flag names"
  28. //usage: "\n -v List version/generation number"
  29. #include "libbb.h"
  30. #include "e2fs_lib.h"
  31. enum {
  32. OPT_RECUR = 0x1,
  33. OPT_ALL = 0x2,
  34. OPT_DIRS_OPT = 0x4,
  35. OPT_PF_LONG = 0x8,
  36. OPT_GENERATION = 0x10,
  37. };
  38. static void list_attributes(const char *name)
  39. {
  40. unsigned long fsflags;
  41. unsigned long generation;
  42. if (fgetflags(name, &fsflags) != 0)
  43. goto read_err;
  44. if (option_mask32 & OPT_GENERATION) {
  45. if (fgetversion(name, &generation) != 0)
  46. goto read_err;
  47. printf("%5lu ", generation);
  48. }
  49. if (option_mask32 & OPT_PF_LONG) {
  50. printf("%-28s ", name);
  51. print_e2flags(stdout, fsflags, PFOPT_LONG);
  52. bb_putchar('\n');
  53. } else {
  54. print_e2flags(stdout, fsflags, 0);
  55. printf(" %s\n", name);
  56. }
  57. return;
  58. read_err:
  59. bb_perror_msg("reading %s", name);
  60. }
  61. static int FAST_FUNC lsattr_dir_proc(const char *dir_name,
  62. struct dirent *de,
  63. void *private UNUSED_PARAM)
  64. {
  65. struct stat st;
  66. char *path;
  67. path = concat_path_file(dir_name, de->d_name);
  68. if (lstat(path, &st) != 0)
  69. bb_perror_msg("stat %s", path);
  70. else if (de->d_name[0] != '.' || (option_mask32 & OPT_ALL)) {
  71. list_attributes(path);
  72. if (S_ISDIR(st.st_mode) && (option_mask32 & OPT_RECUR)
  73. && !DOT_OR_DOTDOT(de->d_name)
  74. ) {
  75. printf("\n%s:\n", path);
  76. iterate_on_dir(path, lsattr_dir_proc, NULL);
  77. bb_putchar('\n');
  78. }
  79. }
  80. free(path);
  81. return 0;
  82. }
  83. static void lsattr_args(const char *name)
  84. {
  85. struct stat st;
  86. if (lstat(name, &st) == -1) {
  87. bb_perror_msg("stat %s", name);
  88. } else if (S_ISDIR(st.st_mode) && !(option_mask32 & OPT_DIRS_OPT)) {
  89. iterate_on_dir(name, lsattr_dir_proc, NULL);
  90. } else {
  91. list_attributes(name);
  92. }
  93. }
  94. int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  95. int lsattr_main(int argc UNUSED_PARAM, char **argv)
  96. {
  97. getopt32(argv, "Radlv");
  98. argv += optind;
  99. if (!*argv)
  100. *--argv = (char*)".";
  101. do lsattr_args(*argv++); while (*argv);
  102. return EXIT_SUCCESS;
  103. }