Back to index

lightning-sunbird  0.9+nobinonly
append.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 /*
00039 ** File:        append.c
00040 ** Description: Testing File writes where PR_APPEND was used on open
00041 **
00042 ** append attempts to verify that a file opened with PR_APPEND
00043 ** will always append to the end of file, regardless where the
00044 ** current file pointer is positioned. To do this, PR_Seek() is
00045 ** called before each write with the position set to beginning of
00046 ** file. Subsequent writes should always append.
00047 ** The file is read back, summing the integer data written to the
00048 ** file. If the expected result is equal, the test passes.
00049 **
00050 ** See BugSplat: 4090
00051 */
00052 #include "plgetopt.h"
00053 #include "nspr.h"
00054 
00055 #include <stdio.h>
00056 #include <stdlib.h>
00057 
00058 PRIntn  debug = 0;
00059 PRIntn  verbose = 0;
00060 PRBool  failedAlready = PR_FALSE;
00061 const PRInt32 addedBytes = 1000;
00062 const PRInt32   buf = 1; /* constant written to fd, addedBytes times */
00063 PRInt32         inBuf;   /* read it back into here */
00064 
00065 PRIntn main(PRIntn argc, char *argv[])
00066 {
00067     PRStatus    rc;
00068     PRInt32     rv;
00069     PRFileDesc  *fd;
00070     PRIntn      i;
00071     PRInt32     sum = 0;
00072 
00073     {   /* Get command line options */
00074         PLOptStatus os;
00075         PLOptState *opt = PL_CreateOptState(argc, argv, "vd");
00076 
00077            while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00078         {
00079                   if (PL_OPT_BAD == os) continue;
00080             switch (opt->option)
00081             {
00082             case 'd':  /* debug */
00083                 debug = 1;
00084                 break;
00085             case 'v':  /* verbose */
00086                 verbose = 1;
00087                 break;
00088              default:
00089                 break;
00090             }
00091         }
00092            PL_DestroyOptState(opt);
00093     } /* end block "Get command line options" */
00094 /* ---------------------------------------------------------------------- */
00095     fd = PR_Open( "/tmp/nsprAppend", (PR_APPEND | PR_CREATE_FILE | PR_TRUNCATE | PR_WRONLY), 0666 );
00096     if ( NULL == fd )  {
00097         if (debug) printf("PR_Open() failed for writing: %d\n", PR_GetError());
00098         failedAlready = PR_TRUE;
00099         goto Finished;
00100     }
00101 
00102     for ( i = 0; i < addedBytes ; i++ ) {
00103         rv = PR_Write( fd, &buf, sizeof(buf));
00104         if ( sizeof(buf) != rv )  {
00105             if (debug) printf("PR_Write() failed: %d\n", PR_GetError());
00106             failedAlready = PR_TRUE;
00107             goto Finished;
00108         }
00109         rv = PR_Seek( fd, 0 , PR_SEEK_SET );
00110         if ( -1 == rv )  {
00111             if (debug) printf("PR_Seek() failed: %d\n", PR_GetError());
00112             failedAlready = PR_TRUE;
00113             goto Finished;
00114         }
00115     }
00116     rc = PR_Close( fd );
00117     if ( PR_FAILURE == rc ) {
00118         if (debug) printf("PR_Close() failed after writing: %d\n", PR_GetError());
00119         failedAlready = PR_TRUE;
00120         goto Finished;
00121     }
00122 /* ---------------------------------------------------------------------- */
00123     fd = PR_Open( "/tmp/nsprAppend", PR_RDONLY, 0 );
00124     if ( NULL == fd )  {
00125         if (debug) printf("PR_Open() failed for reading: %d\n", PR_GetError());
00126         failedAlready = PR_TRUE;
00127         goto Finished;
00128     }
00129 
00130     for ( i = 0; i < addedBytes ; i++ ) {
00131         rv = PR_Read( fd, &inBuf, sizeof(inBuf));
00132         if ( sizeof(inBuf) != rv)  {
00133             if (debug) printf("PR_Write() failed: %d\n", PR_GetError());
00134             failedAlready = PR_TRUE;
00135             goto Finished;
00136         }
00137         sum += inBuf;
00138     }
00139 
00140     rc = PR_Close( fd );
00141     if ( PR_FAILURE == rc ) {
00142         if (debug) printf("PR_Close() failed after reading: %d\n", PR_GetError());
00143         failedAlready = PR_TRUE;
00144         goto Finished;
00145     }
00146     if ( sum != addedBytes )  {
00147         if (debug) printf("Uh Oh! addedBytes: %d. Sum: %d\n", addedBytes, sum);
00148         failedAlready = PR_TRUE;
00149         goto Finished;
00150     }
00151 
00152 /* ---------------------------------------------------------------------- */
00153 Finished:
00154     if (debug || verbose) printf("%s\n", (failedAlready)? "FAILED" : "PASSED" );
00155     return( (failedAlready)? 1 : 0 );
00156 }  /* main() */
00157 
00158 /* append.c */