stat.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* vim: ts=4 sw=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2013-2018 K. Lange
  5. *
  6. * stat
  7. *
  8. * Display file status.
  9. */
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <errno.h>
  15. #include <sys/stat.h>
  16. #include <sys/time.h>
  17. static void show_usage(int argc, char * argv[]) {
  18. printf(
  19. "stat - display file status\n"
  20. "\n"
  21. "usage: %s [-Lq] PATH\n"
  22. "\n"
  23. " -L \033[3mdereference symlinks\033[0m\n"
  24. " -q \033[3mdon't print anything, just return 0 if file exists\033[0m\n"
  25. " -? \033[3mshow this help text\033[0m\n"
  26. "\n", argv[0]);
  27. }
  28. static int dereference = 0, quiet = 0;
  29. static int stat_file(char * file) {
  30. struct stat _stat;
  31. int result;
  32. if (dereference) {
  33. result = stat(file, &_stat);
  34. } else {
  35. result = lstat(file, &_stat);
  36. }
  37. if (result == -1) {
  38. if (!quiet) {
  39. fprintf(stderr, "stat: %s: %s\n", file, strerror(errno));
  40. }
  41. return 1;
  42. }
  43. if (quiet) return 0;
  44. printf("0x%x bytes\n", (unsigned int)_stat.st_size);
  45. if (S_ISDIR(_stat.st_mode)) {
  46. printf("Is a directory.\n");
  47. } else if (S_ISFIFO(_stat.st_mode)) {
  48. printf("Is a pipe.\n");
  49. } else if (S_ISLNK(_stat.st_mode)) {
  50. printf("Is a symlink.\n");
  51. } else if (_stat.st_mode & 0111) {
  52. printf("Is executable.\n");
  53. }
  54. struct stat * f = &_stat;
  55. printf("st_dev 0x%x %d\n", (unsigned int)f->st_dev , (unsigned int)sizeof(f->st_dev ));
  56. printf("st_ino 0x%x %d\n", (unsigned int)f->st_ino , (unsigned int)sizeof(f->st_ino ));
  57. printf("st_mode 0x%x %d\n", (unsigned int)f->st_mode , (unsigned int)sizeof(f->st_mode ));
  58. printf("st_nlink 0x%x %d\n", (unsigned int)f->st_nlink , (unsigned int)sizeof(f->st_nlink ));
  59. printf("st_uid 0x%x %d\n", (unsigned int)f->st_uid , (unsigned int)sizeof(f->st_uid ));
  60. printf("st_gid 0x%x %d\n", (unsigned int)f->st_gid , (unsigned int)sizeof(f->st_gid ));
  61. printf("st_rdev 0x%x %d\n", (unsigned int)f->st_rdev , (unsigned int)sizeof(f->st_rdev ));
  62. printf("st_size 0x%x %d\n", (unsigned int)f->st_size , (unsigned int)sizeof(f->st_size ));
  63. printf("0x%x\n", (unsigned int)((uint32_t *)f)[0]);
  64. return 0;
  65. }
  66. int main(int argc, char ** argv) {
  67. int opt;
  68. while ((opt = getopt(argc, argv, "?Lq")) != -1) {
  69. switch (opt) {
  70. case 'L':
  71. dereference = 1;
  72. break;
  73. case 'q':
  74. quiet = 1;
  75. break;
  76. case '?':
  77. show_usage(argc,argv);
  78. return 1;
  79. }
  80. }
  81. if (optind >= argc) {
  82. show_usage(argc, argv);
  83. return 1;
  84. }
  85. int ret = 0;
  86. while (optind < argc) {
  87. ret |= stat_file(argv[optind]);
  88. optind++;
  89. }
  90. return ret;
  91. }