A static site generator, written in C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cms_err.c 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "cms_err.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. static char* get_message(cms_err err)
  6. {
  7. switch (err)
  8. {
  9. case CMS_ERR_NONE:
  10. return "";
  11. case CMS_ERR_UNKNOWN:
  12. return "Unknown error.";
  13. case CMS_ERR_ALLOC:
  14. return "Memory allocation failed.";
  15. case CMS_ERR_PARSE:
  16. return "Parse error.";
  17. case CMS_ERR_FILENOENT:
  18. return "File doesn't exist.";
  19. case CMS_ERR_DIRNOENT:
  20. return "Directory doesn't exist.";
  21. case CMS_ERR_NOTFILE:
  22. return "Not a file.";
  23. case CMS_ERR_NOTDIR:
  24. return "Not a directory.";
  25. case CMS_ERR_PERM:
  26. return "Permission denied.";
  27. case CMS_ERR_INITED:
  28. return "Already initiated.";
  29. case CMS_ERR_NOTINITED:
  30. return "Not initiated.";
  31. }
  32. }
  33. void cms_err_panic(cms_err err, char* msg)
  34. {
  35. if (err == CMS_ERR_NONE)
  36. return;
  37. if (msg == NULL)
  38. fprintf(stderr, "Error: %s\n", get_message(err));
  39. else
  40. fprintf(stderr, "Error: %s (%s)\n", get_message(err), msg);
  41. exit(1);
  42. }
  43. cms_err cms_err_from_std_err(int err)
  44. {
  45. switch (err)
  46. {
  47. case EACCES:
  48. return CMS_ERR_PERM;
  49. case EEXIST:
  50. return CMS_ERR_FILEEXISTS;
  51. case EFAULT:
  52. return CMS_ERR_PERM;
  53. case EISDIR:
  54. return CMS_ERR_NOTFILE;
  55. case ENOENT:
  56. return CMS_ERR_FILENOENT;
  57. case ENOMEM:
  58. return CMS_ERR_ALLOC;
  59. case ENOTDIR:
  60. return CMS_ERR_NOTDIR;
  61. case EROFS:
  62. return CMS_ERR_PERM;
  63. default:
  64. return CMS_ERR_UNKNOWN;
  65. }
  66. }