lsattr.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include "libbb.h"
  20. #include "e2fs_lib.h"
  21. enum {
  22. OPT_RECUR = 0x1,
  23. OPT_ALL = 0x2,
  24. OPT_DIRS_OPT = 0x4,
  25. OPT_PF_LONG = 0x8,
  26. OPT_GENERATION = 0x10,
  27. };
  28. static void list_attributes(const char *name)
  29. {
  30. unsigned long fsflags;
  31. unsigned long generation;
  32. if (fgetflags(name, &fsflags) != 0)
  33. goto read_err;
  34. if (option_mask32 & OPT_GENERATION) {
  35. if (fgetversion(name, &generation) != 0)
  36. goto read_err;
  37. printf("%5lu ", generation);
  38. }
  39. if (option_mask32 & OPT_PF_LONG) {
  40. printf("%-28s ", name);
  41. print_e2flags(stdout, fsflags, PFOPT_LONG);
  42. bb_putchar('\n');
  43. } else {
  44. print_e2flags(stdout, fsflags, 0);
  45. printf(" %s\n", name);
  46. }
  47. return;
  48. read_err:
  49. bb_perror_msg("reading %s", name);
  50. }
  51. static int lsattr_dir_proc(const char *dir_name, struct dirent *de,
  52. void *private ATTRIBUTE_UNUSED)
  53. {
  54. struct stat st;
  55. char *path;
  56. path = concat_path_file(dir_name, de->d_name);
  57. if (lstat(path, &st) != 0)
  58. bb_perror_msg("stat %s", path);
  59. else if (de->d_name[0] != '.' || (option_mask32 & OPT_ALL)) {
  60. list_attributes(path);
  61. if (S_ISDIR(st.st_mode) && (option_mask32 & OPT_RECUR)
  62. && !DOT_OR_DOTDOT(de->d_name)
  63. ) {
  64. printf("\n%s:\n", path);
  65. iterate_on_dir(path, lsattr_dir_proc, NULL);
  66. bb_putchar('\n');
  67. }
  68. }
  69. free(path);
  70. return 0;
  71. }
  72. static void lsattr_args(const char *name)
  73. {
  74. struct stat st;
  75. if (lstat(name, &st) == -1) {
  76. bb_perror_msg("stat %s", name);
  77. } else if (S_ISDIR(st.st_mode) && !(option_mask32 & OPT_DIRS_OPT)) {
  78. iterate_on_dir(name, lsattr_dir_proc, NULL);
  79. } else {
  80. list_attributes(name);
  81. }
  82. }
  83. int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  84. int lsattr_main(int argc ATTRIBUTE_UNUSED, char **argv)
  85. {
  86. getopt32(argv, "Radlv");
  87. argv += optind;
  88. if (!*argv)
  89. *--argv = (char*)".";
  90. do lsattr_args(*argv++); while (*argv);
  91. return EXIT_SUCCESS;
  92. }