2
0

util.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "orasel.h"
  2. //-----------------------------------------------------------------------------
  3. void __attribute__((noreturn)) donegood(const char *msg)
  4. {
  5. OCITerminate(OCI_DEFAULT);
  6. if (msg && !outmode.emp)
  7. printf("%s\n", msg);
  8. exit(0);
  9. }
  10. //-----------------------------------------------------------------------------
  11. void __attribute__((noreturn)) errxit(const char *msg)
  12. {
  13. OCITerminate(OCI_DEFAULT);
  14. fprintf(stderr, "ERROR: %s\n", msg);
  15. exit(1);
  16. }
  17. //-----------------------------------------------------------------------------
  18. void __attribute__((noreturn)) oraxit(sword status, const con_t* con)
  19. {
  20. const char *p = NULL;
  21. char buf[512];
  22. sword ecd;
  23. switch (status) {
  24. case OCI_SUCCESS_WITH_INFO:
  25. case OCI_ERROR:
  26. ecd = 0;
  27. if(OCIErrorGet(con->errhp, 1, NULL, &ecd, (OraText*)buf,
  28. sizeof(buf), OCI_HTYPE_ERROR) != OCI_SUCCESS)
  29. {
  30. snprintf(buf, sizeof(buf), "unknown ORAERR %u", ecd);
  31. }
  32. break;
  33. default:
  34. snprintf(buf, sizeof(buf), "unknown status %u", status);
  35. break;
  36. case OCI_SUCCESS:
  37. p = "success";
  38. break;
  39. case OCI_NEED_DATA:
  40. p = "need data";
  41. break;
  42. case OCI_NO_DATA:
  43. p = "no data";
  44. break;
  45. case OCI_INVALID_HANDLE:
  46. p = "invalid handle";
  47. break;
  48. case OCI_STILL_EXECUTING: /* ORA-3123 */
  49. p = "executing";
  50. break;
  51. case OCI_CONTINUE:
  52. p = "continue";
  53. break;
  54. }
  55. if (p) {
  56. snprintf(buf, sizeof(buf), "logic error (%s)", p);
  57. }
  58. errxit(buf);
  59. }
  60. //-----------------------------------------------------------------------------
  61. //-----------------------------------------------------------------------------
  62. static void __attribute__((noreturn)) nomem(void)
  63. {
  64. errxit("no enough memory");
  65. }
  66. //-----------------------------------------------------------------------------
  67. void* safe_malloc(size_t sz)
  68. {
  69. void *p = malloc(sz);
  70. if (!p) nomem();
  71. return p;
  72. }
  73. //-----------------------------------------------------------------------------
  74. Str* str_alloc(const char *s, size_t len)
  75. {
  76. Str* ps = (Str*)safe_malloc(sizeof(Str) + len + 1);
  77. ps->len = len;
  78. memcpy(ps->s, s, len);
  79. ps->s[len] = '\0';
  80. return ps;
  81. }
  82. //-----------------------------------------------------------------------------