bigfile3.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nspr.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #ifdef _WIN32
  10. #include <windows.h>
  11. #endif
  12. #define TEST_FILE_NAME "bigfile3.txt"
  13. #ifdef WINCE
  14. #define TEST_FILE_NAME_FOR_CREATEFILE L"bigfile3.txt"
  15. #else
  16. #define TEST_FILE_NAME_FOR_CREATEFILE TEST_FILE_NAME
  17. #endif
  18. #define MESSAGE "Hello world!"
  19. #define MESSAGE_SIZE 13
  20. int main(int argc, char **argv)
  21. {
  22. PRFileDesc *fd;
  23. PRInt64 offset, position;
  24. PRInt32 nbytes;
  25. char buf[MESSAGE_SIZE];
  26. #ifdef _WIN32
  27. HANDLE hFile;
  28. LARGE_INTEGER li;
  29. #endif /* _WIN32 */
  30. LL_I2L(offset, 1);
  31. LL_SHL(offset, offset, 32);
  32. #ifdef _WIN32
  33. hFile = CreateFile(TEST_FILE_NAME_FOR_CREATEFILE, GENERIC_WRITE, 0, NULL,
  34. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  35. if (hFile == INVALID_HANDLE_VALUE) {
  36. fprintf(stderr, "CreateFile failed\n");
  37. exit(1);
  38. }
  39. li.QuadPart = offset;
  40. li.LowPart = SetFilePointer(hFile, li.LowPart, &li.HighPart, FILE_BEGIN);
  41. if (li.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
  42. fprintf(stderr, "SetFilePointer failed\n");
  43. exit(1);
  44. }
  45. PR_ASSERT(li.QuadPart == offset);
  46. strcpy(buf, MESSAGE);
  47. if (WriteFile(hFile, buf, sizeof(buf), &nbytes, NULL) == 0) {
  48. fprintf(stderr, "WriteFile failed\n");
  49. exit(1);
  50. }
  51. PR_ASSERT(nbytes == sizeof(buf));
  52. if (CloseHandle(hFile) == 0) {
  53. fprintf(stderr, "CloseHandle failed\n");
  54. exit(1);
  55. }
  56. #endif /* _WIN32 */
  57. memset(buf, 0, sizeof(buf));
  58. fd = PR_Open(TEST_FILE_NAME, PR_RDONLY, 0666);
  59. if (fd == NULL) {
  60. fprintf(stderr, "PR_Open failed\n");
  61. exit(1);
  62. }
  63. position = PR_Seek64(fd, offset, PR_SEEK_SET);
  64. if (!LL_GE_ZERO(position)) {
  65. fprintf(stderr, "PR_Seek64 failed\n");
  66. exit(1);
  67. }
  68. PR_ASSERT(LL_EQ(position, offset));
  69. nbytes = PR_Read(fd, buf, sizeof(buf));
  70. if (nbytes != sizeof(buf)) {
  71. fprintf(stderr, "PR_Read failed\n");
  72. exit(1);
  73. }
  74. if (strcmp(buf, MESSAGE)) {
  75. fprintf(stderr, "corrupt data:$%s$\n", buf);
  76. exit(1);
  77. }
  78. if (PR_Close(fd) == PR_FAILURE) {
  79. fprintf(stderr, "PR_Close failed\n");
  80. exit(1);
  81. }
  82. if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) {
  83. fprintf(stderr, "PR_Delete failed\n");
  84. exit(1);
  85. }
  86. printf("PASS\n");
  87. return 0;
  88. }