port.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* readconf/include/port.h
  2. *
  3. * This file is part of readconf.
  4. *
  5. * readconf is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * readconf is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with readconf. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #ifndef __PORT_H
  19. #define __PORT_H
  20. /* you can port to another system that unsupports posix, or
  21. * port it to a kernel very easily, by using this h. :-) */
  22. /* and here, are the default options on the posix systems.
  23. * if you want to port, you must implement your own strncmp and strncpy */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #define readconf_puts(s) write(2, s, strlen(s))
  30. #define readconf_error(s) readconf_puts("error: " s)
  31. #define readconf_errorf(errmsg) fprintf(stderr, "libreadconf: " errmsg \
  32. ": \n\t%06d\t\t%s\n", \
  33. line_no, line_buffer)
  34. #define readconf_printf(s, ...) fprintf(stderr, s, ##__VA_ARGS__)
  35. /* this should return -1 if no data left. */
  36. extern int getbyte_fd;
  37. #define readconf_getbyte() ({ \
  38. char _c; \
  39. if (read(getbyte_fd, &_c, 1) == 0) \
  40. _c = -1; \
  41. _c; \
  42. })
  43. /* we assumes these never fail. */
  44. #define readconf_putbyte(b) write(1, &b, 1)
  45. #define readconf_malloc(s) malloc(s)
  46. #define readconf_free(s) free(s)
  47. #endif