3
0

lsattr.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 FAST_FUNC lsattr_dir_proc(const char *dir_name,
  52. struct dirent *de,
  53. void *private UNUSED_PARAM)
  54. {
  55. struct stat st;
  56. char *path;
  57. path = concat_path_file(dir_name, de->d_name);
  58. if (lstat(path, &st) != 0)
  59. bb_perror_msg("stat %s", path);
  60. else if (de->d_name[0] != '.' || (option_mask32 & OPT_ALL)) {
  61. list_attributes(path);
  62. if (S_ISDIR(st.st_mode) && (option_mask32 & OPT_RECUR)
  63. && !DOT_OR_DOTDOT(de->d_name)
  64. ) {
  65. printf("\n%s:\n", path);
  66. iterate_on_dir(path, lsattr_dir_proc, NULL);
  67. bb_putchar('\n');
  68. }
  69. }
  70. free(path);
  71. return 0;
  72. }
  73. static void lsattr_args(const char *name)
  74. {
  75. struct stat st;
  76. if (lstat(name, &st) == -1) {
  77. bb_perror_msg("stat %s", name);
  78. } else if (S_ISDIR(st.st_mode) && !(option_mask32 & OPT_DIRS_OPT)) {
  79. iterate_on_dir(name, lsattr_dir_proc, NULL);
  80. } else {
  81. list_attributes(name);
  82. }
  83. }
  84. int lsattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  85. int lsattr_main(int argc UNUSED_PARAM, char **argv)
  86. {
  87. getopt32(argv, "Radlv");
  88. argv += optind;
  89. if (!*argv)
  90. *--argv = (char*)".";
  91. do lsattr_args(*argv++); while (*argv);
  92. return EXIT_SUCCESS;
  93. }