bigfile2.c (2543B)
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 6 #include "nspr.h" 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #ifdef _WIN32 12 # include <windows.h> 13 #endif 14 15 #define TEST_FILE_NAME "bigfile2.txt" 16 #ifdef WINCE 17 # define TEST_FILE_NAME_FOR_CREATEFILE L"bigfile2.txt" 18 #else 19 # define TEST_FILE_NAME_FOR_CREATEFILE TEST_FILE_NAME 20 #endif 21 22 #define MESSAGE "Hello world!" 23 #define MESSAGE_SIZE 13 24 25 int main(int argc, char** argv) { 26 PRFileDesc* fd; 27 PRInt64 offset, position; 28 PRInt32 nbytes; 29 char buf[MESSAGE_SIZE]; 30 #ifdef _WIN32 31 HANDLE hFile; 32 LARGE_INTEGER li; 33 #endif /* _WIN32 */ 34 35 LL_I2L(offset, 1); 36 LL_SHL(offset, offset, 32); 37 38 fd = PR_Open(TEST_FILE_NAME, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0666); 39 if (fd == NULL) { 40 fprintf(stderr, "PR_Open failed\n"); 41 exit(1); 42 } 43 position = PR_Seek64(fd, offset, PR_SEEK_SET); 44 if (!LL_GE_ZERO(position)) { 45 fprintf(stderr, "PR_Seek64 failed\n"); 46 exit(1); 47 } 48 PR_ASSERT(LL_EQ(position, offset)); 49 strcpy(buf, MESSAGE); 50 nbytes = PR_Write(fd, buf, sizeof(buf)); 51 if (nbytes != sizeof(buf)) { 52 fprintf(stderr, "PR_Write failed\n"); 53 exit(1); 54 } 55 if (PR_Close(fd) == PR_FAILURE) { 56 fprintf(stderr, "PR_Close failed\n"); 57 exit(1); 58 } 59 60 memset(buf, 0, sizeof(buf)); 61 62 #ifdef _WIN32 63 hFile = CreateFile(TEST_FILE_NAME_FOR_CREATEFILE, GENERIC_READ, 0, NULL, 64 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 65 if (hFile == INVALID_HANDLE_VALUE) { 66 fprintf(stderr, "CreateFile failed\n"); 67 exit(1); 68 } 69 li.QuadPart = offset; 70 li.LowPart = SetFilePointer(hFile, li.LowPart, &li.HighPart, FILE_BEGIN); 71 if (li.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) { 72 fprintf(stderr, "SetFilePointer failed\n"); 73 exit(1); 74 } 75 PR_ASSERT(li.QuadPart == offset); 76 if (ReadFile(hFile, buf, sizeof(buf), &nbytes, NULL) == 0) { 77 fprintf(stderr, "ReadFile failed\n"); 78 exit(1); 79 } 80 PR_ASSERT(nbytes == sizeof(buf)); 81 if (strcmp(buf, MESSAGE)) { 82 fprintf(stderr, "corrupt data:$%s$\n", buf); 83 exit(1); 84 } 85 if (CloseHandle(hFile) == 0) { 86 fprintf(stderr, "CloseHandle failed\n"); 87 exit(1); 88 } 89 #endif /* _WIN32 */ 90 91 if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) { 92 fprintf(stderr, "PR_Delete failed\n"); 93 exit(1); 94 } 95 96 printf("PASS\n"); 97 return 0; 98 }