Back to index

lightning-sunbird  0.9+nobinonly
bigfile3.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nspr.h"
00039 
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include <string.h>
00043 #ifdef _WIN32
00044 #include <windows.h>
00045 #endif
00046 
00047 #define TEST_FILE_NAME "bigfile3.txt"
00048 
00049 #define MESSAGE "Hello world!"
00050 #define MESSAGE_SIZE 13
00051 
00052 int main(int argc, char **argv)
00053 {
00054     PRFileDesc *fd;
00055     PRInt64 offset, position;
00056     PRInt32 nbytes;
00057     char buf[MESSAGE_SIZE];
00058 #ifdef _WIN32
00059     HANDLE hFile;
00060     LARGE_INTEGER li;
00061 #endif /* _WIN32 */
00062 
00063     LL_I2L(offset, 1);
00064     LL_SHL(offset, offset, 32);
00065 
00066 #ifdef _WIN32
00067     hFile = CreateFile(TEST_FILE_NAME, GENERIC_WRITE, 0, NULL,
00068             CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
00069     if (hFile == INVALID_HANDLE_VALUE) {
00070         fprintf(stderr, "CreateFile failed\n");
00071         exit(1);
00072     }
00073     li.QuadPart = offset;
00074     li.LowPart = SetFilePointer(hFile, li.LowPart, &li.HighPart, FILE_BEGIN);
00075     if (li.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
00076         fprintf(stderr, "SetFilePointer failed\n");
00077         exit(1);
00078     }
00079     PR_ASSERT(li.QuadPart == offset);
00080     strcpy(buf, MESSAGE);
00081     if (WriteFile(hFile, buf, sizeof(buf), &nbytes, NULL) == 0) {
00082         fprintf(stderr, "WriteFile failed\n");
00083         exit(1);
00084     }
00085     PR_ASSERT(nbytes == sizeof(buf));
00086     if (CloseHandle(hFile) == 0) {
00087         fprintf(stderr, "CloseHandle failed\n");
00088         exit(1);
00089     }
00090 #endif /* _WIN32 */
00091 
00092     memset(buf, 0, sizeof(buf));
00093 
00094     fd = PR_Open(TEST_FILE_NAME, PR_RDONLY, 0666);
00095     if (fd == NULL) {
00096         fprintf(stderr, "PR_Open failed\n");
00097         exit(1);
00098     }
00099     position = PR_Seek64(fd, offset, PR_SEEK_SET);
00100     if (!LL_GE_ZERO(position)) {
00101         fprintf(stderr, "PR_Seek64 failed\n");
00102         exit(1);
00103     }
00104     PR_ASSERT(LL_EQ(position, offset));
00105     nbytes = PR_Read(fd, buf, sizeof(buf));
00106     if (nbytes != sizeof(buf)) {
00107         fprintf(stderr, "PR_Read failed\n");
00108         exit(1);
00109     }
00110     if (strcmp(buf, MESSAGE)) {
00111         fprintf(stderr, "corrupt data:$%s$\n", buf);
00112         exit(1);
00113     }
00114     if (PR_Close(fd) == PR_FAILURE) {
00115         fprintf(stderr, "PR_Close failed\n");
00116         exit(1);
00117     }
00118 
00119     if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) {
00120         fprintf(stderr, "PR_Delete failed\n");
00121         exit(1);
00122     }
00123 
00124     printf("PASS\n");
00125     return 0;
00126 }