lsattr.c 2.8 KB

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