My build of nnn with minor changes
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.
 
 
 
 
 
 

92 lines
2.7 KiB

  1. /*
  2. * BSD 2-Clause License
  3. *
  4. * Copyright (C) 2014-2016, Lazaros Koromilas <lostd@2f30.org>
  5. * Copyright (C) 2014-2016, Dimitris Papastamos <sin@2f30.org>
  6. * Copyright (C) 2016-2020, Arun Prakash Jana <engineerarun@gmail.com>
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright notice, this
  13. * list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #pragma once
  31. #ifdef DBGMODE
  32. static int DEBUG_FD;
  33. static int xprintf(int fd, const char *fmt, ...)
  34. {
  35. char buf[BUFSIZ];
  36. int r;
  37. va_list ap;
  38. va_start(ap, fmt);
  39. r = vsnprintf(buf, sizeof(buf), fmt, ap);
  40. if (r > 0 && (unsigned int)r < sizeof(buf))
  41. r = write(fd, buf, r);
  42. va_end(ap);
  43. return r;
  44. }
  45. static int enabledbg(void)
  46. {
  47. FILE *fp = fopen("/tmp/nnndbg", "w");
  48. if (!fp) {
  49. perror("dbg(1)");
  50. fp = fopen("./nnndbg", "w");
  51. if (!fp) {
  52. perror("dbg(2)");
  53. return -1;
  54. }
  55. }
  56. DEBUG_FD = dup(fileno(fp));
  57. fclose(fp);
  58. if (DEBUG_FD == -1) {
  59. perror("dbg(3)");
  60. return -1;
  61. }
  62. return 0;
  63. }
  64. static void disabledbg(void)
  65. {
  66. close(DEBUG_FD);
  67. }
  68. #define STRINGIFY(x) #x
  69. #define TOSTRING(x) STRINGIFY(x)
  70. #define DPRINTF_D(x) xprintf(DEBUG_FD, "ln " TOSTRING(__LINE__) ": " #x "=%d\n", x)
  71. #define DPRINTF_U(x) xprintf(DEBUG_FD, "ln " TOSTRING(__LINE__) ": " #x "=%u\n", x)
  72. #define DPRINTF_S(x) xprintf(DEBUG_FD, "ln " TOSTRING(__LINE__) ": " #x "=%s\n", x)
  73. #define DPRINTF_P(x) xprintf(DEBUG_FD, "ln " TOSTRING(__LINE__) ": " #x "=%p\n", x)
  74. #else
  75. #define DPRINTF_D(x)
  76. #define DPRINTF_U(x)
  77. #define DPRINTF_S(x)
  78. #define DPRINTF_P(x)
  79. #endif /* DBGMODE */