utils.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #ifndef __UTILS_H__
  4. #define __UTILS_H__
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <stdarg.h>
  8. #include "api.h"
  9. #define PAL_LOADER XSTRINGIFY(PAL_LOADER_PATH)
  10. #ifdef __x86_64__
  11. # include "sysdep-x86_64.h"
  12. #endif
  13. #undef IS_ERR
  14. #undef IS_ERR_P
  15. #undef ERRNO
  16. #undef ERRNO_P
  17. #define IS_ERR INTERNAL_SYSCALL_ERROR
  18. #define IS_ERR_P INTERNAL_SYSCALL_ERROR_P
  19. #define ERRNO INTERNAL_SYSCALL_ERRNO
  20. #define ERRNO_P INTERNAL_SYSCALL_ERRNO_P
  21. int printf (const char * fmt, ...);
  22. int snprintf (char * buf, size_t n, const char * fmt, ...);
  23. void * malloc (int size);
  24. void free (void * mem);
  25. extern unsigned long pagesize;
  26. extern unsigned long pageshift;
  27. extern unsigned long pagemask;
  28. static inline int is_file_uri (const char * uri)
  29. {
  30. if (uri[0] == 'f' && uri[1] == 'i' && uri[2] == 'l' && uri[3] == 'e' &&
  31. uri[4] == ':')
  32. return 1;
  33. return 0;
  34. }
  35. static inline void fast_strcpy (char * d, const char * s, int len)
  36. {
  37. while (len) {
  38. *d = *s;
  39. d++;
  40. s++;
  41. len--;
  42. }
  43. *d = 0;
  44. }
  45. static inline const char * file_uri_to_path (const char * uri, int len)
  46. {
  47. char * path;
  48. if (len == 5) {
  49. path = malloc(2);
  50. if (!path)
  51. return NULL;
  52. path[0] = '.';
  53. path[1] = 0;
  54. return path;
  55. }
  56. path = malloc(len - 4);
  57. if (!path)
  58. return NULL;
  59. fast_strcpy(path, uri + 5, len - 5);
  60. return path;
  61. }
  62. #endif /* __UTILS_H__ */