NESe (pronounced "Nessie") is a NES emulator based on the e6502 emulator, also written in C with a focus on speed and portability for use on embedded platforms, especially ARM.
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.

57 lines
1.2KB

  1. #include <stdio.h>
  2. #include "compat_impl.h"
  3. #if !defined (__unix__) && !(defined (__APPLE__) && defined (__MACH__))
  4. #include <stdlib.h>
  5. #include <string.h>
  6. /*
  7. static inline size_t strnlen(const char* str, size_t size) {
  8. int len = 0, remain = size;
  9. while (remain > 0 && *str++) --remain;
  10. return (size - remain);
  11. }
  12. */
  13. char* strndup(const char *str, size_t size) {
  14. size_t len = strnlen(str, size);
  15. char* dup = malloc(len + 1);
  16. dup[len] = '\0';
  17. return memcpy(dup, str, len);
  18. }
  19. ssize_t getline(char **restrict lineptr, size_t *restrict n,
  20. FILE *restrict stream) {
  21. if (NULL == *lineptr || 0 == *n) {
  22. *n = 120;
  23. *lineptr = (char*)malloc(*n);
  24. if (NULL == *lineptr) return -1;
  25. }
  26. int chr = 0;
  27. ssize_t len = 0;
  28. char* ptr = *lineptr;
  29. while ((chr = fgetc(stream)) >= 0) {
  30. ++len;
  31. *ptr++ = chr;
  32. if (*n < len + 1) {
  33. char* more = realloc(*lineptr, *n * 2);
  34. if (NULL == more) return -1;
  35. *lineptr = more;
  36. *n *= 2;
  37. ptr = *lineptr + len;
  38. }
  39. if ('\n' == chr) break;
  40. }
  41. *ptr = '\0';
  42. return (len == 0 ? -1 : len);
  43. }
  44. #endif