PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/RdFToBuf.c

#
C | 124 lines | 66 code | 8 blank | 50 comment | 10 complexity | cf076b12eac6629f74e941d954995b54 MD5 | raw file
  1. /*
  2. * Copyright (C) 1989-95 GROUPE BULL
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  18. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * Except as contained in this notice, the name of GROUPE BULL shall not be
  22. * used in advertising or otherwise to promote the sale, use or other dealings
  23. * in this Software without prior written authorization from GROUPE BULL.
  24. */
  25. /*****************************************************************************\
  26. * RdFToBuf.c: *
  27. * *
  28. * XPM library *
  29. * Copy a file to a malloc'ed buffer, provided as a convenience. *
  30. * *
  31. * Developed by Arnaud Le Hors *
  32. \*****************************************************************************/
  33. /*
  34. * The code related to FOR_MSW has been added by
  35. * HeDu (hedu@cul-ipn.uni-kiel.de) 4/94
  36. */
  37. /* October 2004, source code review by Thomas Biege <thomas@suse.de> */
  38. #ifdef HAVE_CONFIG_H
  39. #include <config.h>
  40. #endif
  41. #include "XpmI.h"
  42. #include <sys/stat.h>
  43. #if !defined(FOR_MSW) && !defined(WIN32)
  44. #include <unistd.h>
  45. #endif
  46. #ifndef VAX11C
  47. #include <fcntl.h>
  48. #endif
  49. #if defined(FOR_MSW) || defined(WIN32)
  50. #include <io.h>
  51. #define stat _stat
  52. #define fstat _fstat
  53. #define fdopen _fdopen
  54. #define O_RDONLY _O_RDONLY
  55. #endif
  56. int
  57. XpmReadFileToBuffer(
  58. char *filename,
  59. char **buffer_return)
  60. {
  61. int fd, fcheck;
  62. off_t len;
  63. char *ptr;
  64. struct stat stats;
  65. FILE *fp;
  66. *buffer_return = NULL;
  67. #ifndef VAX11C
  68. fd = open(filename, O_RDONLY);
  69. #else
  70. fd = open(filename, O_RDONLY, NULL);
  71. #endif
  72. if (fd < 0)
  73. return XpmOpenFailed;
  74. if (fstat(fd, &stats)) {
  75. close(fd);
  76. return XpmOpenFailed;
  77. }
  78. fp = fdopen(fd, "r");
  79. if (!fp) {
  80. close(fd);
  81. return XpmOpenFailed;
  82. }
  83. len = stats.st_size;
  84. ptr = (char *) XpmMalloc(len + 1);
  85. if (!ptr) {
  86. fclose(fp);
  87. return XpmNoMemory;
  88. }
  89. fcheck = fread(ptr, 1, len, fp);
  90. fclose(fp);
  91. #ifdef VMS
  92. /* VMS often stores text files in a variable-length record format,
  93. where there are two bytes of size followed by the record. fread
  94. converts this so it looks like a record followed by a newline.
  95. Unfortunately, the size reported by fstat() (and fseek/ftell)
  96. counts the two bytes for the record terminator, while fread()
  97. counts only one. So, fread() sees fewer bytes in the file (size
  98. minus # of records) and thus when asked to read the amount
  99. returned by stat(), it fails.
  100. The best solution, suggested by DEC, seems to consider the length
  101. returned from fstat() as an upper bound and call fread() with
  102. a record length of 1. Then don't check the return value.
  103. We'll check for 0 for gross error that's all.
  104. */
  105. len = fcheck;
  106. if (fcheck == 0) {
  107. #else
  108. if (fcheck != len) {
  109. #endif
  110. XpmFree(ptr);
  111. return XpmOpenFailed;
  112. }
  113. ptr[len] = '\0';
  114. *buffer_return = ptr;
  115. return XpmSuccess;
  116. }