se_stdio.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (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. */
  31. #ifndef SE_STDIO_H
  32. #define SE_STDIO_H
  33. #include <stdio.h>
  34. #include <stddef.h>
  35. #include "se_memcpy.h"
  36. #include <stdarg.h>
  37. #include <unistd.h>
  38. #include <sys/stat.h>
  39. #include <sys/types.h>
  40. #include <fcntl.h>
  41. #ifndef MAX_PATH
  42. #define MAX_PATH 260
  43. #endif
  44. static inline int se_delete_file(const char *path_name)
  45. {
  46. return unlink(path_name);
  47. }
  48. #define se_delete_tfile se_delete_file
  49. static inline int sprintf_s(char *dst_buf, size_t size_in_bytes, const char *format, ...)
  50. {
  51. va_list argptr;
  52. int cnt;
  53. va_start(argptr, format);
  54. cnt = vsnprintf(dst_buf, size_in_bytes, format, argptr);
  55. va_end(argptr);
  56. return cnt;
  57. }
  58. static inline int _snprintf_s(char *dst_buf, size_t size_in_bytes, size_t max_count, const char *format, ...)
  59. {
  60. (void) size_in_bytes;
  61. va_list argptr;
  62. int cnt;
  63. va_start(argptr, format);
  64. cnt = vsnprintf(dst_buf, max_count, format, argptr);
  65. va_end(argptr);
  66. return cnt;
  67. }
  68. static inline errno_t fopen_s(FILE **f, const char *filename, const char *mode)
  69. {
  70. errno_t err = 0;
  71. *f = fopen(filename, mode);
  72. if(*f==NULL){
  73. err = -1;
  74. }
  75. return err;
  76. }
  77. static inline int se_copy_file(const char *dst_name, const char *src_name)
  78. {
  79. int dest = -1;
  80. int source = -1;
  81. ssize_t nr_read;
  82. struct stat stat_buf;
  83. #ifndef BUF_SIZE
  84. #define BUF_SIZE 4096
  85. #endif
  86. char buf[BUF_SIZE];
  87. /* open the input file */
  88. source = open(src_name, O_RDONLY);
  89. if(source < 0)
  90. goto error;
  91. /* get size and permissions of the prebuild DB file */
  92. if (fstat(source, &stat_buf) != 0)
  93. goto error;
  94. dest = open(dst_name, O_WRONLY|O_CREAT|O_TRUNC, stat_buf.st_mode);
  95. if(dest < 0)
  96. goto error;
  97. while ((nr_read = read(source, buf, BUF_SIZE)) > 0)
  98. {
  99. if (write(dest, buf, nr_read) != nr_read)
  100. goto error;
  101. }
  102. #undef BUF_SIZE
  103. close(dest);
  104. close(source);
  105. return 0;
  106. error:
  107. if(dest>=0)close(dest);
  108. if(source>=0)close(source);
  109. return -1;
  110. }
  111. #ifdef __cplusplus
  112. template <size_t _Size>
  113. int sprintf_s(char (&dst)[_Size], const char *format, ...)
  114. {
  115. va_list argptr;
  116. int cnt;
  117. va_start(argptr, format);
  118. cnt = vsprintf(dst, format, argptr);
  119. va_end(argptr);
  120. return cnt;
  121. }
  122. template<size_t _Size>
  123. int _snprintf_s(char (&dst)[_Size], size_t max_count, const char *format, ...)
  124. {
  125. va_list argptr;
  126. int cnt;
  127. va_start(argptr, format);
  128. cnt = vsnprintf(dst, max_count, format, argptr);
  129. va_end(argptr);
  130. return cnt;
  131. }
  132. #endif
  133. #endif