3
0

lsattr.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * lsattr.c - List file attributes on an ext2 file system
  3. *
  4. * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
  5. * Laboratoire MASI, Institut Blaise Pascal
  6. * Universite Pierre et Marie Curie (Paris VI)
  7. *
  8. * This file can be redistributed under the terms of the GNU General
  9. * Public License
  10. */
  11. /*
  12. * History:
  13. * 93/10/30 - Creation
  14. * 93/11/13 - Replace stat() calls by lstat() to avoid loops
  15. * 94/02/27 - Integrated in Ted's distribution
  16. * 98/12/29 - Display version info only when -V specified (G M Sipe)
  17. */
  18. #include <sys/types.h>
  19. #include <dirent.h>
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #include <getopt.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/param.h>
  28. #include <sys/stat.h>
  29. #include <ext2fs/ext2_fs.h>
  30. #include "e2fsbb.h"
  31. #include "e2p/e2p.h"
  32. #ifdef __GNUC__
  33. # define EXT2FS_ATTR(x) __attribute__(x)
  34. #else
  35. # define EXT2FS_ATTR(x)
  36. #endif
  37. #define OPT_RECUR 1
  38. #define OPT_ALL 2
  39. #define OPT_DIRS_OPT 4
  40. #define OPT_PF_LONG 8
  41. #define OPT_GENERATION 16
  42. static int flags;
  43. #ifdef CONFIG_LFS
  44. # define LSTAT lstat64
  45. # define STRUCT_STAT struct stat64
  46. #else
  47. # define LSTAT lstat
  48. # define STRUCT_STAT struct stat
  49. #endif
  50. static void list_attributes(const char *name)
  51. {
  52. unsigned long fsflags;
  53. unsigned long generation;
  54. if (fgetflags(name, &fsflags) == -1)
  55. goto read_err;
  56. if (flags & OPT_GENERATION) {
  57. if (fgetversion(name, &generation) == -1)
  58. goto read_err;
  59. printf("%5lu ", generation);
  60. }
  61. if (flags & OPT_PF_LONG) {
  62. printf("%-28s ", name);
  63. print_flags(stdout, fsflags, PFOPT_LONG);
  64. printf("\n");
  65. } else {
  66. print_flags(stdout, fsflags, 0);
  67. printf(" %s\n", name);
  68. }
  69. return;
  70. read_err:
  71. bb_perror_msg("reading %s", name);
  72. }
  73. static int lsattr_dir_proc(const char *, struct dirent *, void *);
  74. static void lsattr_args(const char *name)
  75. {
  76. STRUCT_STAT st;
  77. if (LSTAT(name, &st) == -1) {
  78. bb_perror_msg("stating %s", name);
  79. } else {
  80. if (S_ISDIR(st.st_mode) && !(flags & OPT_DIRS_OPT))
  81. iterate_on_dir(name, lsattr_dir_proc, NULL);
  82. else
  83. list_attributes(name);
  84. }
  85. }
  86. static int lsattr_dir_proc(const char *dir_name, struct dirent *de,
  87. void *private EXT2FS_ATTR((unused)))
  88. {
  89. STRUCT_STAT st;
  90. char *path;
  91. path = concat_path_file(dir_name, de->d_name);
  92. if (LSTAT(path, &st) == -1)
  93. bb_perror_msg(path);
  94. else {
  95. if (de->d_name[0] != '.' || (flags & OPT_ALL)) {
  96. list_attributes(path);
  97. if (S_ISDIR(st.st_mode) && (flags & OPT_RECUR) &&
  98. (de->d_name[0] != '.' && (de->d_name[1] != '\0' ||
  99. (de->d_name[1] != '.' && de->d_name[2] != '\0')))) {
  100. printf("\n%s:\n", path);
  101. iterate_on_dir(path, lsattr_dir_proc, NULL);
  102. printf("\n");
  103. }
  104. }
  105. }
  106. free(path);
  107. return 0;
  108. }
  109. int lsattr_main(int argc, char **argv)
  110. {
  111. int i;
  112. flags = bb_getopt_ulflags(argc, argv, "Radlv");
  113. if (optind > argc - 1)
  114. lsattr_args(".");
  115. else
  116. for (i = optind; i < argc; i++)
  117. lsattr_args(argv[i]);
  118. return EXIT_SUCCESS;
  119. }