logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

filetype.c (992B)


  1. // Collection of Unix tools, comparable to coreutils
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 200809L
  5. #define _XOPEN_SOURCE 700 // S_ISVTX, S_IFMT, S_IFDIR, …
  6. #include "./mode.h"
  7. #include <sys/stat.h>
  8. const char *
  9. filetype(struct stat *st)
  10. {
  11. #ifdef S_TYPEISMQ
  12. if(S_TYPEISMQ(st)) return "message queue";
  13. #endif
  14. #ifdef S_TYPEISSEM
  15. if(S_TYPEISSEM(st)) return "semaphore";
  16. #endif
  17. #ifdef S_TYPEISSHM
  18. if(S_TYPEISSHM(st)) return "shared memory object";
  19. #endif
  20. #ifdef S_TYPEISTMO
  21. if(S_TYPEISTMO(st)) return "typed memory object";
  22. #endif
  23. switch(st->st_mode & S_IFMT)
  24. {
  25. case S_IFBLK:
  26. return "block special";
  27. case S_IFCHR:
  28. return "character special";
  29. case S_IFIFO:
  30. return "FIFO special";
  31. case S_IFREG:
  32. return "regular file";
  33. case S_IFLNK:
  34. return "symbolic link";
  35. case S_IFDIR:
  36. return "directory";
  37. case S_IFSOCK:
  38. return "socket";
  39. }
  40. return "unknown filetype";
  41. }