api.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #ifndef API_H
  16. #define API_H
  17. #include <stddef.h>
  18. #include <stdint.h>
  19. #include <stdarg.h>
  20. /* Macros */
  21. #ifndef likely
  22. # define likely(x) __builtin_expect((!!(x)),1)
  23. #endif
  24. #ifndef unlikely
  25. # define unlikely(x) __builtin_expect((!!(x)),0)
  26. #endif
  27. #define __alloca __builtin_alloca
  28. #define XSTRINGIFY(x) STRINGIFY(x)
  29. #define STRINGIFY(x) #x
  30. #define static_strlen(str) (sizeof(str) - 1)
  31. /* Libc functions */
  32. /* Libc String functions */
  33. int strnlen (const char *str, int maxlen);
  34. int strlen (const char *str);
  35. long strtol (const char *s, char **endptr, int base);
  36. int atoi (const char *nptr);
  37. long int atol (const char *nptr);
  38. char * strchr (const char *s, int c_in);
  39. void * memcpy (void *dstpp, const void *srcpp, int len);
  40. void * memmove (void *dstpp, void *srcpp, int len);
  41. void * memset (void *dstpp, int c, int len);
  42. int memcmp (const void *s1, const void *s2, int len);
  43. /* Some useful macro */
  44. /* force failure if str is not a static string */
  45. #define force_static(str) ("" str "")
  46. /* check if the var is exactly the same as the static string */
  47. #define strcmp_static(var, str) \
  48. (!memcmp((var), force_static(str), static_strlen(force_static(str)) + 1))
  49. /* check if the var starts with the static string */
  50. #define strpartcmp_static(var, str) \
  51. (!memcmp((var), force_static(str), static_strlen(force_static(str))))
  52. /* copy static string and return the address of the null end (null if the dest
  53. * is not large enough).*/
  54. #define strcpy_static(var, str, max) \
  55. (static_strlen(force_static(str)) + 1 > max ? NULL : \
  56. memcpy((var), force_static(str), static_strlen(force_static(str)) + 1) + \
  57. static_strlen(force_static(str)))
  58. /* Libc printf functions */
  59. void fprintfmt (int (*_fputch)(void *, int, void *), void * f, void * putdat,
  60. const char * fmt, ...);
  61. void vfprintfmt (int (*_fputch)(void *, int, void *), void * f, void * putdat,
  62. const char * fmt, va_list *ap);
  63. int snprintf (char * buf, int n, const char * fmt, ...);
  64. /* Miscelleneous */
  65. int inet_pton4 (const char *src, int len, void *dst);
  66. int inet_pton6 (const char *src, int len, void *dst);
  67. uint32_t __htonl (uint32_t x);
  68. uint32_t __ntohl (uint32_t x);
  69. uint16_t __htons (uint16_t x);
  70. uint16_t __ntohs (uint16_t x);
  71. extern const char * const * sys_errlist_internal;
  72. /* Graphene functions */
  73. int get_norm_path (const char * path, char * buf, int offset, int size);
  74. int get_base_name (const char * path, char * buf, int size);
  75. /* Loading configs / manifests */
  76. #include <linux_list.h>
  77. struct config_store {
  78. struct list_head root, entries;
  79. void * raw_data;
  80. int raw_size;
  81. void * (*malloc) (int);
  82. void (*free) (void *);
  83. };
  84. int read_config (struct config_store * store, int (*filter) (const char *, int),
  85. const char ** errstring);
  86. int free_config (struct config_store * store);
  87. int copy_config (struct config_store * store, struct config_store * new_store);
  88. int write_config (void * file, int (*write) (void *, void *, int),
  89. struct config_store * store);
  90. int get_config (struct config_store * cfg, const char * key,
  91. char * val_buf, int size);
  92. int get_config_entries (struct config_store * cfg, const char * key,
  93. char * key_buf, int size);
  94. int set_config (struct config_store * cfg, const char * key, const char * val);
  95. #define CONFIG_MAX 256
  96. #endif /* API_H */