lsattr.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <sys/types.h>
  20. #include <dirent.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <getopt.h>
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/param.h>
  29. #include <sys/stat.h>
  30. #include "ext2fs/ext2_fs.h"
  31. #include "e2fsbb.h"
  32. #include "e2p/e2p.h"
  33. #define OPT_RECUR 1
  34. #define OPT_ALL 2
  35. #define OPT_DIRS_OPT 4
  36. #define OPT_PF_LONG 8
  37. #define OPT_GENERATION 16
  38. static int flags;
  39. static void list_attributes(const char *name)
  40. {
  41. unsigned long fsflags;
  42. unsigned long generation;
  43. if (fgetflags(name, &fsflags) == -1)
  44. goto read_err;
  45. if (flags & OPT_GENERATION) {
  46. if (fgetversion(name, &generation) == -1)
  47. goto read_err;
  48. printf("%5lu ", generation);
  49. }
  50. if (flags & OPT_PF_LONG) {
  51. printf("%-28s ", name);
  52. print_flags(stdout, fsflags, PFOPT_LONG);
  53. puts("");
  54. } else {
  55. print_flags(stdout, fsflags, 0);
  56. printf(" %s\n", name);
  57. }
  58. return;
  59. read_err:
  60. bb_perror_msg("reading %s", name);
  61. }
  62. static int lsattr_dir_proc(const char *, struct dirent *, void *);
  63. static void lsattr_args(const char *name)
  64. {
  65. struct stat st;
  66. if (lstat(name, &st) == -1) {
  67. bb_perror_msg("stating %s", name);
  68. } else {
  69. if (S_ISDIR(st.st_mode) && !(flags & OPT_DIRS_OPT))
  70. iterate_on_dir(name, lsattr_dir_proc, NULL);
  71. else
  72. list_attributes(name);
  73. }
  74. }
  75. static int lsattr_dir_proc(const char *dir_name, struct dirent *de,
  76. void *private)
  77. {
  78. struct stat st;
  79. char *path;
  80. path = concat_path_file(dir_name, de->d_name);
  81. if (lstat(path, &st) == -1)
  82. bb_perror_msg(path);
  83. else {
  84. if (de->d_name[0] != '.' || (flags & OPT_ALL)) {
  85. list_attributes(path);
  86. if (S_ISDIR(st.st_mode) && (flags & OPT_RECUR) &&
  87. (de->d_name[0] != '.' && (de->d_name[1] != '\0' ||
  88. (de->d_name[1] != '.' && de->d_name[2] != '\0')))) {
  89. printf("\n%s:\n", path);
  90. iterate_on_dir(path, lsattr_dir_proc, NULL);
  91. puts("");
  92. }
  93. }
  94. }
  95. free(path);
  96. return 0;
  97. }
  98. int lsattr_main(int argc, char **argv)
  99. {
  100. int i;
  101. flags = getopt32(argc, argv, "Radlv");
  102. if (optind > argc - 1)
  103. lsattr_args(".");
  104. else
  105. for (i = optind; i < argc; i++)
  106. lsattr_args(argv[i]);
  107. return EXIT_SUCCESS;
  108. }