summaryrefslogtreecommitdiff
path: root/apps/JAWS/clients/WebSTONE/src/nsapi-includes/base/file.h
blob: fc4d26b32ecc271e21210127129c686a4ecbc770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * Copyright (c) 1994, 1995.  Netscape Communications Corporation.  All
 * rights reserved.
 *
 * Use of this software is governed by the terms of the license agreement for
 * the Netscape Communications or Netscape Comemrce Server between the
 * parties.
 */


/* ------------------------------------------------------------------------ */


/*
 * file.h: system specific functions for reading/writing files
 *
 * Rob McCool
 */


#ifndef FILE_H
#define FILE_H

#ifdef XP_WIN32
#include <nt/ntfile.h>
#else


#include "netsite.h"
#include "systems.h"


/*
 * I cheat: These are set up such that system_read can be a macro for read
 * under UNIX. IO_OKAY is anything positive.
 */

#define IO_OKAY 1
#define IO_ERROR -1
#define IO_EOF 0


#ifdef FILE_STDIO
#include <stdio.h>

#elif defined(FILE_UNIX)
#include <sys/types.h>
#include <sys/file.h>
#include <fcntl.h>
#include <unistd.h>
#endif


/* -------------------------- File related defs --------------------------- */


/* The disk page size on this machine. */
#define FILE_BUFFERSIZE 4096


/*
 * The fd data type for this system.
 */

#if defined(FILE_STDIO)
typedef FILE* SYS_FILE;
#define SYS_ERROR_FD NULL
#define SYS_STDERR stderr

#elif defined(FILE_UNIX)
typedef int SYS_FILE;
#define SYS_ERROR_FD -1
#define SYS_STDERR STDERR_FILENO

#else
#error "undefined file typing for current system"
#endif

#ifdef XP_UNIX
#define FILE_PATHSEP '/'
#define FILE_PARENT "../"

#define system_chdir chdir
#endif


/*
 * system_fread reads sz bytes from fd into to buf, return number of bytes
 * read, or IO_EOF if EOF, or IO_ERROR if error.
 */

#if defined(FILE_STDIO)
int system_fread(SYS_FILE fd, char *buf, int sz);

#elif defined(FILE_UNIX)
#define system_fread(fd,buf,sz) read(fd,buf,sz)

#endif

/*
 * system_fopenRO opens a given file for reading only
 * system_fopenWA opens a given file for writing, appending new output
 */

#if defined(FILE_STDIO)
#define system_fopenRO(path) fopen(path,"r")
#define system_fopenWA(path) fopen(path,"a")
#define system_fopenRW(path) fopen(path,"w")

#elif defined(FILE_UNIX)
#define system_fopenRO(path) open(path, O_RDONLY)
#define system_fopenWA(path) \
               open(path, O_RDWR | O_CREAT | O_APPEND, 0644)
#define system_fopenRW(path) \
               open(path, O_RDWR | O_CREAT, 0644)

#endif


/*
 * system_fclose closes the file fd
 */

#if defined(FILE_STDIO)
#define system_fclose(fd) fclose(fd)

#elif defined(FILE_UNIX)
#define system_fclose(fd) close(fd)
#endif

/*
 * This call stops core dumps in a portable way. Returns -1 on error.
 */

int system_nocoredumps(void);


#if defined(FILE_STDIO)
#define system_lseek fseek

#elif defined(FILE_UNIX)
#define system_lseek lseek

#endif

/*
 * system_write writes sz bytes from buf to fd. The handler function should
 * handle partial writes and anything else like that. Returns IO_*
 */

int system_fwrite(SYS_FILE fd,char *buf,int sz);

/*
 * system_fwrite_atomic locks the given fd before writing to it. This avoids
 * interference between simultaneous writes. Returns IO_*
 */

int system_fwrite_atomic(SYS_FILE fd, char *buf, int sz);

/*
 * system_errmsg returns the last error that occured while processing file
 * descriptor fd. fd does not have to be specified (if the error is a global
 * such as in UNIX systems). PPS: Rob is a halfwit. This parameter is useless.
 */

#ifndef FILE_WIN32
#include <errno.h>

extern char *sys_errlist[];
#define file_notfound() (errno == ENOENT)
#define system_errmsg(fd) (sys_errlist[errno])
#endif


/*
 * flock locks a file against interference from other processes
 * ulock unlocks it.
 */
#ifdef BSD_FLOCK
#include <sys/file.h>
#define system_initlock(fd) (0)
#define system_flock(fd) flock(fd, LOCK_EX)
#define system_ulock(fd) flock(fd, LOCK_UN)

#elif defined(FILE_UNIX)
#include <unistd.h>
#define system_initlock(fd) (0)
#define system_flock(fd) lockf(fd, F_LOCK, 0)
#define system_ulock(fd) lockf(fd, F_ULOCK, 0)

#endif


/*
 * unix2local converts a unix-style pathname to a local one
 */

#ifdef XP_UNIX
#define file_unix2local(path,p2) strcpy(p2,path)
#endif

/* -------------------------- Dir related defs ---------------------------- */


#ifdef XP_UNIX
#include <dirent.h>
typedef DIR* SYS_DIR;
typedef struct dirent SYS_DIRENT;
#define dir_open opendir
#define dir_read readdir
#define dir_close closedir

#endif
#endif
#endif