check.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // CTHTTPD - Simple Web Server - GPLv2
  2. // Chris Dorman, 2012-2014 (cddo@riseup.net)
  3. // Help from Nickolai Vurgaft (slipperygate@gmail.com)
  4. #include "check.h"
  5. #include "dep.h"
  6. // Check if a file exists (index.html)
  7. int file_exists(char *fname) {
  8. struct stat buffer;
  9. if (stat (fname, &buffer) == 0)
  10. return 0;
  11. else
  12. return 1;
  13. }
  14. // Check if the path is a directory (darklight)
  15. int is_dir(char* p) {
  16. char * stripslash;
  17. struct stat st;
  18. stripslash = p + 1; // strip the first forward 'slash' from the string
  19. if (stat(stripslash, &st) == 0 && (st.st_mode & S_IFDIR)) {
  20. return 1;
  21. }
  22. else if (stat(stripslash, &st) == 0 && (st.st_mode & S_IFREG)) {
  23. return 2;
  24. }
  25. return 0;
  26. }
  27. void ms_sleep(unsigned int ms)
  28. {
  29. struct timespec elapsed;
  30. struct timespec tv;
  31. int was_error;
  32. /* Set the timeout interval */
  33. elapsed.tv_sec = ms/1000;
  34. elapsed.tv_nsec = (ms%1000)*1000000;
  35. do {
  36. errno = 0;
  37. tv.tv_sec = elapsed.tv_sec;
  38. tv.tv_nsec = elapsed.tv_nsec;
  39. was_error = nanosleep(&tv, &elapsed);
  40. } while (was_error && (errno == EINTR));
  41. }