Back to index

lightning-sunbird  0.9+nobinonly
openfile.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) 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  * This test calls PR_OpenFile to create a bunch of files
00040  * with various file modes.
00041  */
00042 
00043 #include "prio.h"
00044 #include "prerror.h"
00045 #include "prinit.h"
00046 
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 
00050 #define TEMPLATE_FILE_NAME "template.txt"
00051 
00052 int main()
00053 {
00054     FILE *template;
00055     char buf[32];
00056     PRInt32 nbytes;
00057     PRFileDesc *fd;
00058 
00059     
00060     /* Write in text mode.  Let stdio deal with line endings. */
00061     template = fopen(TEMPLATE_FILE_NAME, "w");
00062     fputs("line 1\nline 2\n", template);
00063     fclose(template);
00064 
00065     /* Read in binary mode */
00066     fd = PR_OpenFile(TEMPLATE_FILE_NAME, PR_RDONLY, 0666);
00067     nbytes = PR_Read(fd, buf, sizeof(buf));
00068     PR_Close(fd);
00069     PR_Delete(TEMPLATE_FILE_NAME);
00070 
00071     fd = PR_OpenFile("tfil0700.txt", PR_RDWR | PR_CREATE_FILE, 0700);
00072     if (NULL == fd) {
00073         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
00074                 PR_GetError(), PR_GetOSError());
00075         exit(1);
00076     }
00077     PR_Write(fd, buf, nbytes);
00078     PR_Close(fd);
00079 
00080     fd = PR_OpenFile("tfil0500.txt", PR_RDWR | PR_CREATE_FILE, 0500);
00081     if (NULL == fd) {
00082         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
00083                 PR_GetError(), PR_GetOSError());
00084         exit(1);
00085     }
00086     PR_Write(fd, buf, nbytes);
00087     PR_Close(fd);
00088 
00089     fd = PR_OpenFile("tfil0400.txt", PR_RDWR | PR_CREATE_FILE, 0400);
00090     if (NULL == fd) {
00091         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
00092                 PR_GetError(), PR_GetOSError());
00093         exit(1);
00094     }
00095     PR_Write(fd, buf, nbytes);
00096     PR_Close(fd);
00097 
00098     fd = PR_OpenFile("tfil0644.txt", PR_RDWR | PR_CREATE_FILE, 0644);
00099     if (NULL == fd) {
00100         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
00101                 PR_GetError(), PR_GetOSError());
00102         exit(1);
00103     }
00104     PR_Write(fd, buf, nbytes);
00105     PR_Close(fd);
00106 
00107     fd = PR_OpenFile("tfil0664.txt", PR_RDWR | PR_CREATE_FILE, 0664);
00108     if (NULL == fd) {
00109         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
00110                 PR_GetError(), PR_GetOSError());
00111         exit(1);
00112     }
00113     PR_Write(fd, buf, nbytes);
00114     PR_Close(fd);
00115 
00116     fd = PR_OpenFile("tfil0660.txt", PR_RDWR | PR_CREATE_FILE, 0660);
00117     if (NULL == fd) {
00118         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
00119                 PR_GetError(), PR_GetOSError());
00120         exit(1);
00121     }
00122     PR_Write(fd, buf, nbytes);
00123     PR_Close(fd);
00124 
00125     fd = PR_OpenFile("tfil0666.txt", PR_RDWR | PR_CREATE_FILE, 0666);
00126     if (NULL == fd) {
00127         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
00128                 PR_GetError(), PR_GetOSError());
00129         exit(1);
00130     }
00131     PR_Write(fd, buf, nbytes);
00132     PR_Close(fd);
00133 
00134     fd = PR_OpenFile("tfil0640.txt", PR_RDWR | PR_CREATE_FILE, 0640);
00135     if (NULL == fd) {
00136         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
00137                 PR_GetError(), PR_GetOSError());
00138         exit(1);
00139     }
00140     PR_Write(fd, buf, nbytes);
00141     PR_Close(fd);
00142 
00143     PR_Cleanup();
00144     return 0;
00145 }