Back to index

lightning-sunbird  0.9+nobinonly
op_excl.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 **
00040 ** Name: op_excl.c
00041 **
00042 ** Description: Test Program to verify function of PR_EXCL open flag
00043 **
00044 ** Modification History:
00045 ** 27-Oct-1999 lth. Initial version
00046 ***********************************************************************/
00047 
00048 #include <plgetopt.h> 
00049 #include <nspr.h> 
00050 #include <stdio.h>
00051 #include <stdlib.h>
00052 
00053 /*
00054 ** Test harness infrastructure
00055 */
00056 PRLogModuleInfo *lm;
00057 PRLogModuleLevel msgLevel = PR_LOG_NONE;
00058 PRIntn  debug = 0;
00059 PRUint32  failed_already = 0;
00060 /* end Test harness infrastructure */
00061 /*
00062 ** Emit help text for this test
00063 */
00064 static void Help( void )
00065 {
00066     printf("op_excl: Help");
00067     printf("op_excl [-d]");
00068     printf("-d enables debug messages");
00069     exit(1);
00070 } /* end Help() */
00071 
00072 
00073 
00074 PRIntn main(PRIntn argc, char *argv[])
00075 {
00076     PRFileDesc  *fd;
00077     PRStatus    rv;
00078     PRInt32     written;
00079     char        outBuf[] = "op_excl.c test file";
00080 #define OUT_SIZE sizeof(outBuf)
00081 #define NEW_FILENAME "xxxExclNewFile"
00082 
00083     {
00084         /*
00085         ** Get command line options
00086         */
00087         PLOptStatus os;
00088         PLOptState *opt = PL_CreateOptState(argc, argv, "hd");
00089 
00090            while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00091         {
00092                   if (PL_OPT_BAD == os) continue;
00093             switch (opt->option)
00094             {
00095             case 'd':  /* debug */
00096                 debug = 1;
00097                          msgLevel = PR_LOG_ERROR;
00098                 break;
00099             case 'h':  /* help message */
00100                          Help();
00101                 break;
00102              default:
00103                 break;
00104             }
00105         }
00106            PL_DestroyOptState(opt);
00107     }
00108 
00109     lm = PR_NewLogModule("Test");       /* Initialize logging */
00110 
00111     /*
00112     ** First, open a file, PR_EXCL, we believe not to exist
00113     */
00114     fd = PR_Open( NEW_FILENAME, PR_CREATE_FILE | PR_EXCL | PR_WRONLY, 0666 );
00115     if ( NULL == fd )  {
00116         if (debug) fprintf( stderr, "Open exclusive. Expected success, got failure\n");
00117         failed_already = 1;
00118         goto Finished;
00119     }
00120 
00121     written = PR_Write( fd, outBuf, OUT_SIZE );
00122     if ( OUT_SIZE != written )  {
00123         if (debug) fprintf( stderr, "Write after open exclusive failed\n");
00124         failed_already = 1;
00125         goto Finished;
00126     }
00127 
00128     rv = PR_Close(fd);
00129     if ( PR_FAILURE == rv )  {
00130         if (debug) fprintf( stderr, "Close after open exclusive failed\n");
00131         failed_already = 1;
00132         goto Finished;
00133     }
00134 
00135     /*
00136     ** Second, open the same file, PR_EXCL, expect it to fail
00137     */
00138     fd = PR_Open( NEW_FILENAME, PR_CREATE_FILE | PR_EXCL | PR_WRONLY, 0666 );
00139     if ( NULL != fd )  {
00140         if (debug) fprintf( stderr, "Open exclusive. Expected failure, got success\n");
00141         failed_already = 1;
00142         PR_Close(fd);
00143     }
00144 
00145     rv = PR_Delete( NEW_FILENAME );
00146     if ( PR_FAILURE == rv ) {
00147         if (debug) fprintf( stderr, "PR_Delete() failed\n");
00148         failed_already = 1;
00149     }
00150 
00151 Finished:
00152     if (debug) printf("%s\n", (failed_already)? "FAIL" : "PASS");
00153     return( (failed_already == PR_TRUE )? 1 : 0 );
00154 }  /* main() */
00155 /* end op_excl.c */
00156