VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/openfile.c@ 46155

Last change on this file since 46155 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/*
39 * This test calls PR_OpenFile to create a bunch of files
40 * with various file modes.
41 */
42
43#include "prio.h"
44#include "prerror.h"
45#include "prinit.h"
46
47#include <stdio.h>
48#include <stdlib.h>
49
50#define TEMPLATE_FILE_NAME "template.txt"
51
52int main()
53{
54 FILE *template;
55 char buf[32];
56 PRInt32 nbytes;
57 PRFileDesc *fd;
58
59
60 /* Write in text mode. Let stdio deal with line endings. */
61 template = fopen(TEMPLATE_FILE_NAME, "w");
62 fputs("line 1\nline 2\n", template);
63 fclose(template);
64
65 /* Read in binary mode */
66 fd = PR_OpenFile(TEMPLATE_FILE_NAME, PR_RDONLY, 0666);
67 nbytes = PR_Read(fd, buf, sizeof(buf));
68 PR_Close(fd);
69 PR_Delete(TEMPLATE_FILE_NAME);
70
71 fd = PR_OpenFile("tfil0700.txt", PR_RDWR | PR_CREATE_FILE, 0700);
72 if (NULL == fd) {
73 fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
74 PR_GetError(), PR_GetOSError());
75 exit(1);
76 }
77 PR_Write(fd, buf, nbytes);
78 PR_Close(fd);
79
80 fd = PR_OpenFile("tfil0500.txt", PR_RDWR | PR_CREATE_FILE, 0500);
81 if (NULL == fd) {
82 fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
83 PR_GetError(), PR_GetOSError());
84 exit(1);
85 }
86 PR_Write(fd, buf, nbytes);
87 PR_Close(fd);
88
89 fd = PR_OpenFile("tfil0400.txt", PR_RDWR | PR_CREATE_FILE, 0400);
90 if (NULL == fd) {
91 fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
92 PR_GetError(), PR_GetOSError());
93 exit(1);
94 }
95 PR_Write(fd, buf, nbytes);
96 PR_Close(fd);
97
98 fd = PR_OpenFile("tfil0644.txt", PR_RDWR | PR_CREATE_FILE, 0644);
99 if (NULL == fd) {
100 fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
101 PR_GetError(), PR_GetOSError());
102 exit(1);
103 }
104 PR_Write(fd, buf, nbytes);
105 PR_Close(fd);
106
107 fd = PR_OpenFile("tfil0664.txt", PR_RDWR | PR_CREATE_FILE, 0664);
108 if (NULL == fd) {
109 fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
110 PR_GetError(), PR_GetOSError());
111 exit(1);
112 }
113 PR_Write(fd, buf, nbytes);
114 PR_Close(fd);
115
116 fd = PR_OpenFile("tfil0660.txt", PR_RDWR | PR_CREATE_FILE, 0660);
117 if (NULL == fd) {
118 fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
119 PR_GetError(), PR_GetOSError());
120 exit(1);
121 }
122 PR_Write(fd, buf, nbytes);
123 PR_Close(fd);
124
125 fd = PR_OpenFile("tfil0666.txt", PR_RDWR | PR_CREATE_FILE, 0666);
126 if (NULL == fd) {
127 fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
128 PR_GetError(), PR_GetOSError());
129 exit(1);
130 }
131 PR_Write(fd, buf, nbytes);
132 PR_Close(fd);
133
134 fd = PR_OpenFile("tfil0640.txt", PR_RDWR | PR_CREATE_FILE, 0640);
135 if (NULL == fd) {
136 fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
137 PR_GetError(), PR_GetOSError());
138 exit(1);
139 }
140 PR_Write(fd, buf, nbytes);
141 PR_Close(fd);
142
143 PR_Cleanup();
144 return 0;
145}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette