xref: /aosp_15_r20/external/mtools/plain_io.c (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1*d5c9a868SElliott Hughes /*  Copyright 1995-2007,2009,2011 Alain Knaff.
2*d5c9a868SElliott Hughes  *  This file is part of mtools.
3*d5c9a868SElliott Hughes  *
4*d5c9a868SElliott Hughes  *  Mtools is free software: you can redistribute it and/or modify
5*d5c9a868SElliott Hughes  *  it under the terms of the GNU General Public License as published by
6*d5c9a868SElliott Hughes  *  the Free Software Foundation, either version 3 of the License, or
7*d5c9a868SElliott Hughes  *  (at your option) any later version.
8*d5c9a868SElliott Hughes  *
9*d5c9a868SElliott Hughes  *  Mtools is distributed in the hope that it will be useful,
10*d5c9a868SElliott Hughes  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d5c9a868SElliott Hughes  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*d5c9a868SElliott Hughes  *  GNU General Public License for more details.
13*d5c9a868SElliott Hughes  *
14*d5c9a868SElliott Hughes  *  You should have received a copy of the GNU General Public License
15*d5c9a868SElliott Hughes  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16*d5c9a868SElliott Hughes  *
17*d5c9a868SElliott Hughes  * Io to a plain file or device
18*d5c9a868SElliott Hughes  *
19*d5c9a868SElliott Hughes  * written by:
20*d5c9a868SElliott Hughes  *
21*d5c9a868SElliott Hughes  * Alain L. Knaff
22*d5c9a868SElliott Hughes  * [email protected]
23*d5c9a868SElliott Hughes  *
24*d5c9a868SElliott Hughes  */
25*d5c9a868SElliott Hughes 
26*d5c9a868SElliott Hughes #include "sysincludes.h"
27*d5c9a868SElliott Hughes #include "stream.h"
28*d5c9a868SElliott Hughes #include "mtools.h"
29*d5c9a868SElliott Hughes #include "msdos.h"
30*d5c9a868SElliott Hughes #include "open_image.h"
31*d5c9a868SElliott Hughes #include "devices.h"
32*d5c9a868SElliott Hughes #include "plain_io.h"
33*d5c9a868SElliott Hughes #include "llong.h"
34*d5c9a868SElliott Hughes 
35*d5c9a868SElliott Hughes #ifdef HAVE_LINUX_FS_H
36*d5c9a868SElliott Hughes # include <linux/fs.h>
37*d5c9a868SElliott Hughes #endif
38*d5c9a868SElliott Hughes 
39*d5c9a868SElliott Hughes typedef struct SimpleFile_t {
40*d5c9a868SElliott Hughes     struct Stream_t head;
41*d5c9a868SElliott Hughes 
42*d5c9a868SElliott Hughes     struct MT_STAT statbuf;
43*d5c9a868SElliott Hughes     int fd;
44*d5c9a868SElliott Hughes     mt_off_t lastwhere;
45*d5c9a868SElliott Hughes     int seekable;
46*d5c9a868SElliott Hughes     int privileged;
47*d5c9a868SElliott Hughes #ifdef OS_hpux
48*d5c9a868SElliott Hughes     int size_limited;
49*d5c9a868SElliott Hughes #endif
50*d5c9a868SElliott Hughes } SimpleFile_t;
51*d5c9a868SElliott Hughes 
52*d5c9a868SElliott Hughes 
53*d5c9a868SElliott Hughes #include "lockdev.h"
54*d5c9a868SElliott Hughes 
55*d5c9a868SElliott Hughes typedef ssize_t (*iofn) (int, void *, size_t);
56*d5c9a868SElliott Hughes 
file_io(SimpleFile_t * This,char * buf,mt_off_t where,size_t len,iofn io)57*d5c9a868SElliott Hughes static ssize_t file_io(SimpleFile_t *This, char *buf,
58*d5c9a868SElliott Hughes 		       mt_off_t where, size_t len,
59*d5c9a868SElliott Hughes 		       iofn io)
60*d5c9a868SElliott Hughes {
61*d5c9a868SElliott Hughes 	ssize_t ret;
62*d5c9a868SElliott Hughes 
63*d5c9a868SElliott Hughes 	if (This->seekable && where != This->lastwhere ){
64*d5c9a868SElliott Hughes 		if(mt_lseek( This->fd, where, SEEK_SET) < 0 ){
65*d5c9a868SElliott Hughes 			perror("seek");
66*d5c9a868SElliott Hughes 			return -1; /* If seek failed, lastwhere did
67*d5c9a868SElliott Hughes 				      not change */
68*d5c9a868SElliott Hughes 		}
69*d5c9a868SElliott Hughes 		This->lastwhere = where;
70*d5c9a868SElliott Hughes 	}
71*d5c9a868SElliott Hughes 
72*d5c9a868SElliott Hughes #ifdef OS_hpux
73*d5c9a868SElliott Hughes 	/*
74*d5c9a868SElliott Hughes 	 * On HP/UX, we can not write more than MAX_LEN bytes in one go.
75*d5c9a868SElliott Hughes 	 * If more are written, the write fails with EINVAL
76*d5c9a868SElliott Hughes 	 */
77*d5c9a868SElliott Hughes 	#define MAX_SCSI_LEN (127*1024)
78*d5c9a868SElliott Hughes 	if(This->size_limited && len > MAX_SCSI_LEN)
79*d5c9a868SElliott Hughes 		len = MAX_SCSI_LEN;
80*d5c9a868SElliott Hughes #endif
81*d5c9a868SElliott Hughes 	ret = io(This->fd, buf, len);
82*d5c9a868SElliott Hughes 
83*d5c9a868SElliott Hughes #ifdef OS_hpux
84*d5c9a868SElliott Hughes 	if (ret == -1 &&
85*d5c9a868SElliott Hughes 		errno == EINVAL && /* if we got EINVAL */
86*d5c9a868SElliott Hughes 		len > MAX_SCSI_LEN) {
87*d5c9a868SElliott Hughes 		This->size_limited = 1;
88*d5c9a868SElliott Hughes 		len = MAX_SCSI_LEN;
89*d5c9a868SElliott Hughes 		ret = io(This->fd, buf, len);
90*d5c9a868SElliott Hughes 	}
91*d5c9a868SElliott Hughes #endif
92*d5c9a868SElliott Hughes 
93*d5c9a868SElliott Hughes 	if ( ret == -1 ){
94*d5c9a868SElliott Hughes 		perror("plain_io");
95*d5c9a868SElliott Hughes 		return -1;
96*d5c9a868SElliott Hughes 	}
97*d5c9a868SElliott Hughes 	This->lastwhere = where + ret;
98*d5c9a868SElliott Hughes 	return ret;
99*d5c9a868SElliott Hughes }
100*d5c9a868SElliott Hughes 
file_read(Stream_t * Stream,char * buf,size_t len)101*d5c9a868SElliott Hughes static ssize_t file_read(Stream_t *Stream, char *buf, size_t len)
102*d5c9a868SElliott Hughes {
103*d5c9a868SElliott Hughes 	DeclareThis(SimpleFile_t);
104*d5c9a868SElliott Hughes 	return file_io(This, buf, This->lastwhere, len, read);
105*d5c9a868SElliott Hughes }
106*d5c9a868SElliott Hughes 
file_write(Stream_t * Stream,char * buf,size_t len)107*d5c9a868SElliott Hughes static ssize_t file_write(Stream_t *Stream, char *buf, size_t len)
108*d5c9a868SElliott Hughes {
109*d5c9a868SElliott Hughes 	DeclareThis(SimpleFile_t);
110*d5c9a868SElliott Hughes 	return file_io(This, buf, This->lastwhere, len, (iofn) write);
111*d5c9a868SElliott Hughes }
112*d5c9a868SElliott Hughes 
file_pread(Stream_t * Stream,char * buf,mt_off_t where,size_t len)113*d5c9a868SElliott Hughes static ssize_t file_pread(Stream_t *Stream, char *buf,
114*d5c9a868SElliott Hughes 			  mt_off_t where, size_t len)
115*d5c9a868SElliott Hughes {
116*d5c9a868SElliott Hughes 	DeclareThis(SimpleFile_t);
117*d5c9a868SElliott Hughes 	return file_io(This, buf, where, len, read);
118*d5c9a868SElliott Hughes }
119*d5c9a868SElliott Hughes 
file_pwrite(Stream_t * Stream,char * buf,mt_off_t where,size_t len)120*d5c9a868SElliott Hughes static ssize_t file_pwrite(Stream_t *Stream, char *buf,
121*d5c9a868SElliott Hughes 			   mt_off_t where, size_t len)
122*d5c9a868SElliott Hughes {
123*d5c9a868SElliott Hughes 	DeclareThis(SimpleFile_t);
124*d5c9a868SElliott Hughes 	return file_io(This, buf, where, len, (iofn) write);
125*d5c9a868SElliott Hughes }
126*d5c9a868SElliott Hughes 
file_flush(Stream_t * Stream UNUSEDP)127*d5c9a868SElliott Hughes static int file_flush(Stream_t *Stream UNUSEDP)
128*d5c9a868SElliott Hughes {
129*d5c9a868SElliott Hughes #if 0
130*d5c9a868SElliott Hughes 	DeclareThis(SimpleFile_t);
131*d5c9a868SElliott Hughes 
132*d5c9a868SElliott Hughes 	return fsync(This->fd);
133*d5c9a868SElliott Hughes #endif
134*d5c9a868SElliott Hughes 	return 0;
135*d5c9a868SElliott Hughes }
136*d5c9a868SElliott Hughes 
file_free(Stream_t * Stream)137*d5c9a868SElliott Hughes static int file_free(Stream_t *Stream)
138*d5c9a868SElliott Hughes {
139*d5c9a868SElliott Hughes 	DeclareThis(SimpleFile_t);
140*d5c9a868SElliott Hughes 
141*d5c9a868SElliott Hughes 	if (This->fd > 2)
142*d5c9a868SElliott Hughes 		return close(This->fd);
143*d5c9a868SElliott Hughes 	else
144*d5c9a868SElliott Hughes 		return 0;
145*d5c9a868SElliott Hughes }
146*d5c9a868SElliott Hughes 
init_geom_with_reg(int fd,struct device * dev,struct device * orig_dev,struct MT_STAT * statbuf)147*d5c9a868SElliott Hughes static int init_geom_with_reg(int fd, struct device *dev,
148*d5c9a868SElliott Hughes 			      struct device *orig_dev,
149*d5c9a868SElliott Hughes 			      struct MT_STAT *statbuf) {
150*d5c9a868SElliott Hughes 	if(S_ISREG(statbuf->st_mode)) {
151*d5c9a868SElliott Hughes 		/* Regular file (image file) */
152*d5c9a868SElliott Hughes 		mt_off_t sectors;
153*d5c9a868SElliott Hughes 		if(statbuf->st_size == 0) {
154*d5c9a868SElliott Hughes 			/* zero sized image => newly created.
155*d5c9a868SElliott Hughes 			   Size not actually known...
156*d5c9a868SElliott Hughes 			*/
157*d5c9a868SElliott Hughes 			return 0;
158*d5c9a868SElliott Hughes 		}
159*d5c9a868SElliott Hughes 		sectors = statbuf->st_size /
160*d5c9a868SElliott Hughes 			(mt_off_t)(dev->sector_size ? dev->sector_size : 512);
161*d5c9a868SElliott Hughes 		dev->tot_sectors =
162*d5c9a868SElliott Hughes 			((smt_off_t) sectors > UINT32_MAX)
163*d5c9a868SElliott Hughes 			? UINT32_MAX
164*d5c9a868SElliott Hughes 			: (uint32_t) sectors;
165*d5c9a868SElliott Hughes 		return 0;
166*d5c9a868SElliott Hughes 	} else {
167*d5c9a868SElliott Hughes 		/* All the rest (devices, etc.) */
168*d5c9a868SElliott Hughes 		return init_geom(fd, dev, orig_dev, statbuf);
169*d5c9a868SElliott Hughes 	}
170*d5c9a868SElliott Hughes }
171*d5c9a868SElliott Hughes 
file_geom(Stream_t * Stream,struct device * dev,struct device * orig_dev)172*d5c9a868SElliott Hughes static int file_geom(Stream_t *Stream, struct device *dev,
173*d5c9a868SElliott Hughes 		     struct device *orig_dev)
174*d5c9a868SElliott Hughes {
175*d5c9a868SElliott Hughes 	int ret;
176*d5c9a868SElliott Hughes 	DeclareThis(SimpleFile_t);
177*d5c9a868SElliott Hughes 
178*d5c9a868SElliott Hughes 	if(dev->sector_size && dev->sector_size != 512) {
179*d5c9a868SElliott Hughes 		dev->sectors =
180*d5c9a868SElliott Hughes 			(uint16_t) (dev->sectors * dev->sector_size / 512);
181*d5c9a868SElliott Hughes 	}
182*d5c9a868SElliott Hughes 
183*d5c9a868SElliott Hughes #ifdef JPD
184*d5c9a868SElliott Hughes 	printf("file_geom:media=%0X=>cyl=%d,heads=%d,sects=%d,ssize=%d,use2m=%X\n",
185*d5c9a868SElliott Hughes 	       media, dev->tracks, dev->heads, dev->sectors, dev->ssize,
186*d5c9a868SElliott Hughes 	       dev->use_2m);
187*d5c9a868SElliott Hughes #endif
188*d5c9a868SElliott Hughes 	ret = init_geom_with_reg(This->fd,dev, orig_dev, &This->statbuf);
189*d5c9a868SElliott Hughes 	if(dev->sector_size && dev->sector_size != 512) {
190*d5c9a868SElliott Hughes 		dev->sectors =
191*d5c9a868SElliott Hughes 			(uint16_t) (dev->sectors * 512 / dev->sector_size);
192*d5c9a868SElliott Hughes 	}
193*d5c9a868SElliott Hughes #ifdef JPD
194*d5c9a868SElliott Hughes 	printf("f_geom: after init_geom(), sects=%d\n", dev->sectors);
195*d5c9a868SElliott Hughes #endif
196*d5c9a868SElliott Hughes 	return ret;
197*d5c9a868SElliott Hughes }
198*d5c9a868SElliott Hughes 
199*d5c9a868SElliott Hughes 
file_data(Stream_t * Stream,time_t * date,mt_off_t * size,int * type,uint32_t * address)200*d5c9a868SElliott Hughes static int file_data(Stream_t *Stream, time_t *date, mt_off_t *size,
201*d5c9a868SElliott Hughes 		     int *type, uint32_t *address)
202*d5c9a868SElliott Hughes {
203*d5c9a868SElliott Hughes 	DeclareThis(SimpleFile_t);
204*d5c9a868SElliott Hughes 
205*d5c9a868SElliott Hughes 	if(date)
206*d5c9a868SElliott Hughes 		*date = This->statbuf.st_mtime;
207*d5c9a868SElliott Hughes 	if(size)
208*d5c9a868SElliott Hughes 		*size = This->statbuf.st_size;
209*d5c9a868SElliott Hughes 	if(type)
210*d5c9a868SElliott Hughes 		*type = S_ISDIR(This->statbuf.st_mode);
211*d5c9a868SElliott Hughes 	if(address)
212*d5c9a868SElliott Hughes 		*address = 0;
213*d5c9a868SElliott Hughes 	return 0;
214*d5c9a868SElliott Hughes }
215*d5c9a868SElliott Hughes 
file_discard(Stream_t * Stream UNUSEDP)216*d5c9a868SElliott Hughes static int file_discard(Stream_t *Stream UNUSEDP)
217*d5c9a868SElliott Hughes {
218*d5c9a868SElliott Hughes #ifdef BLKFLSBUF
219*d5c9a868SElliott Hughes 	int ret;
220*d5c9a868SElliott Hughes 	DeclareThis(SimpleFile_t);
221*d5c9a868SElliott Hughes 	ret= ioctl(This->fd, BLKFLSBUF);
222*d5c9a868SElliott Hughes 	if(ret < 0)
223*d5c9a868SElliott Hughes 		perror("BLKFLSBUF");
224*d5c9a868SElliott Hughes 	return ret;
225*d5c9a868SElliott Hughes #else
226*d5c9a868SElliott Hughes 	return 0;
227*d5c9a868SElliott Hughes #endif
228*d5c9a868SElliott Hughes }
229*d5c9a868SElliott Hughes 
230*d5c9a868SElliott Hughes static Class_t SimpleFileClass = {
231*d5c9a868SElliott Hughes 	file_read,
232*d5c9a868SElliott Hughes 	file_write,
233*d5c9a868SElliott Hughes 	file_pread,
234*d5c9a868SElliott Hughes 	file_pwrite,
235*d5c9a868SElliott Hughes 	file_flush,
236*d5c9a868SElliott Hughes 	file_free,
237*d5c9a868SElliott Hughes 	file_geom,
238*d5c9a868SElliott Hughes 	file_data,
239*d5c9a868SElliott Hughes 	0, /* pre_allocate */
240*d5c9a868SElliott Hughes 	0, /* dos-convert */
241*d5c9a868SElliott Hughes 	file_discard
242*d5c9a868SElliott Hughes };
243*d5c9a868SElliott Hughes 
244*d5c9a868SElliott Hughes 
LockDevice(int fd,struct device * dev,int locked,int lockMode,char * errmsg)245*d5c9a868SElliott Hughes int LockDevice(int fd, struct device *dev,
246*d5c9a868SElliott Hughes 	       int locked, int lockMode,
247*d5c9a868SElliott Hughes 	       char *errmsg)
248*d5c9a868SElliott Hughes {
249*d5c9a868SElliott Hughes #ifndef __EMX__
250*d5c9a868SElliott Hughes #ifndef __CYGWIN__
251*d5c9a868SElliott Hughes #ifndef OS_mingw32msvc
252*d5c9a868SElliott Hughes 	/* lock the device on writes */
253*d5c9a868SElliott Hughes 	if (locked && lock_dev(fd, (lockMode&O_ACCMODE) == O_RDWR, dev)) {
254*d5c9a868SElliott Hughes 		if(errmsg)
255*d5c9a868SElliott Hughes #ifdef HAVE_SNPRINTF
256*d5c9a868SElliott Hughes 			snprintf(errmsg,199,
257*d5c9a868SElliott Hughes 				"plain floppy: device \"%s\" busy (%s):",
258*d5c9a868SElliott Hughes 				dev ? dev->name : "unknown", strerror(errno));
259*d5c9a868SElliott Hughes #else
260*d5c9a868SElliott Hughes 			sprintf(errmsg,
261*d5c9a868SElliott Hughes 				"plain floppy: device \"%s\" busy (%s):",
262*d5c9a868SElliott Hughes 				(dev && strlen(dev->name) < 50) ?
263*d5c9a868SElliott Hughes 				 dev->name : "unknown", strerror(errno));
264*d5c9a868SElliott Hughes #endif
265*d5c9a868SElliott Hughes 
266*d5c9a868SElliott Hughes 		if(errno != EOPNOTSUPP || (lockMode&O_ACCMODE) == O_RDWR) {
267*d5c9a868SElliott Hughes 			/* If error is "not supported", and we're only
268*d5c9a868SElliott Hughes 			 * reading from the device anyways, then ignore. Some
269*d5c9a868SElliott Hughes 			 * OS'es don't support locks on read-only devices, even
270*d5c9a868SElliott Hughes 			 * if they are shared (read-only) locks */
271*d5c9a868SElliott Hughes 			return -1;
272*d5c9a868SElliott Hughes 		}
273*d5c9a868SElliott Hughes 	}
274*d5c9a868SElliott Hughes #endif
275*d5c9a868SElliott Hughes #endif
276*d5c9a868SElliott Hughes #endif
277*d5c9a868SElliott Hughes 	return 0;
278*d5c9a868SElliott Hughes }
279*d5c9a868SElliott Hughes 
SimpleFileOpen(struct device * dev,struct device * orig_dev,const char * name,int mode,char * errmsg,int mode2,int locked,mt_off_t * maxSize)280*d5c9a868SElliott Hughes Stream_t *SimpleFileOpen(struct device *dev, struct device *orig_dev,
281*d5c9a868SElliott Hughes 			 const char *name, int mode, char *errmsg,
282*d5c9a868SElliott Hughes 			 int mode2, int locked, mt_off_t *maxSize) {
283*d5c9a868SElliott Hughes 	return SimpleFileOpenWithLm(dev, orig_dev, name, mode,
284*d5c9a868SElliott Hughes 				    errmsg, mode2, locked, mode, maxSize,
285*d5c9a868SElliott Hughes 				    NULL);
286*d5c9a868SElliott Hughes }
287*d5c9a868SElliott Hughes 
SimpleFileOpenWithLm(struct device * dev,struct device * orig_dev,const char * name,int mode,char * errmsg,int mode2,int locked,int lockMode,mt_off_t * maxSize,int * geomFailure)288*d5c9a868SElliott Hughes Stream_t *SimpleFileOpenWithLm(struct device *dev, struct device *orig_dev,
289*d5c9a868SElliott Hughes 			       const char *name, int mode, char *errmsg,
290*d5c9a868SElliott Hughes 			       int mode2, int locked, int lockMode,
291*d5c9a868SElliott Hughes 			       mt_off_t *maxSize, int *geomFailure)
292*d5c9a868SElliott Hughes {
293*d5c9a868SElliott Hughes 	SimpleFile_t *This;
294*d5c9a868SElliott Hughes #ifdef __EMX__
295*d5c9a868SElliott Hughes HFILE FileHandle;
296*d5c9a868SElliott Hughes ULONG Action;
297*d5c9a868SElliott Hughes APIRET rc;
298*d5c9a868SElliott Hughes #endif
299*d5c9a868SElliott Hughes 	if (IS_SCSI(dev))
300*d5c9a868SElliott Hughes 		return NULL;
301*d5c9a868SElliott Hughes 	This = New(SimpleFile_t);
302*d5c9a868SElliott Hughes 	if (!This){
303*d5c9a868SElliott Hughes 		printOom();
304*d5c9a868SElliott Hughes 		return 0;
305*d5c9a868SElliott Hughes 	}
306*d5c9a868SElliott Hughes 	memset((void*)This, 0, sizeof(SimpleFile_t));
307*d5c9a868SElliott Hughes 	This->seekable = 1;
308*d5c9a868SElliott Hughes #ifdef OS_hpux
309*d5c9a868SElliott Hughes 	This->size_limited = 0;
310*d5c9a868SElliott Hughes #endif
311*d5c9a868SElliott Hughes 	init_head(&This->head, &SimpleFileClass, NULL);
312*d5c9a868SElliott Hughes 	if (!name || strcmp(name,"-") == 0 ){
313*d5c9a868SElliott Hughes 		if (mode == O_RDONLY)
314*d5c9a868SElliott Hughes 			This->fd = 0;
315*d5c9a868SElliott Hughes 		else
316*d5c9a868SElliott Hughes 			This->fd = 1;
317*d5c9a868SElliott Hughes 		This->seekable = 0;
318*d5c9a868SElliott Hughes 		if (MT_FSTAT(This->fd, &This->statbuf) < 0) {
319*d5c9a868SElliott Hughes 		    Free(This);
320*d5c9a868SElliott Hughes 		    if(errmsg)
321*d5c9a868SElliott Hughes #ifdef HAVE_SNPRINTF
322*d5c9a868SElliott Hughes 			snprintf(errmsg,199,"Can't stat -: %s",
323*d5c9a868SElliott Hughes 				strerror(errno));
324*d5c9a868SElliott Hughes #else
325*d5c9a868SElliott Hughes 			sprintf(errmsg,"Can't stat -: %s",
326*d5c9a868SElliott Hughes 				strerror(errno));
327*d5c9a868SElliott Hughes #endif
328*d5c9a868SElliott Hughes 		    return NULL;
329*d5c9a868SElliott Hughes 		}
330*d5c9a868SElliott Hughes 
331*d5c9a868SElliott Hughes 		return &This->head;
332*d5c9a868SElliott Hughes 	}
333*d5c9a868SElliott Hughes 
334*d5c9a868SElliott Hughes 
335*d5c9a868SElliott Hughes 	if(dev) {
336*d5c9a868SElliott Hughes 		if(!(mode2 & NO_PRIV))
337*d5c9a868SElliott Hughes 			This->privileged = IS_PRIVILEGED(dev);
338*d5c9a868SElliott Hughes 		mode |= dev->mode;
339*d5c9a868SElliott Hughes 	}
340*d5c9a868SElliott Hughes 
341*d5c9a868SElliott Hughes 	precmd(dev);
342*d5c9a868SElliott Hughes 	if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
343*d5c9a868SElliott Hughes 		reclaim_privs();
344*d5c9a868SElliott Hughes 
345*d5c9a868SElliott Hughes #ifdef __EMX__
346*d5c9a868SElliott Hughes #define DOSOPEN_FLAGS	(OPEN_FLAGS_DASD | OPEN_FLAGS_WRITE_THROUGH | \
347*d5c9a868SElliott Hughes 			OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_RANDOM | \
348*d5c9a868SElliott Hughes 			OPEN_FLAGS_NO_CACHE)
349*d5c9a868SElliott Hughes #define DOSOPEN_FD_ACCESS (OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE)
350*d5c9a868SElliott Hughes #define DOSOPEN_HD_ACCESS (OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY)
351*d5c9a868SElliott Hughes 
352*d5c9a868SElliott Hughes 	if (isalpha(*name) && (*(name+1) == ':')) {
353*d5c9a868SElliott Hughes 		rc = DosOpen(
354*d5c9a868SElliott Hughes 			name, &FileHandle, &Action, 0L, FILE_NORMAL,
355*d5c9a868SElliott Hughes 			OPEN_ACTION_OPEN_IF_EXISTS, DOSOPEN_FLAGS |
356*d5c9a868SElliott Hughes 			(IS_NOLOCK(dev)?DOSOPEN_HD_ACCESS:DOSOPEN_FD_ACCESS),
357*d5c9a868SElliott Hughes 			0L);
358*d5c9a868SElliott Hughes #if DEBUG
359*d5c9a868SElliott Hughes 		if (rc != NO_ERROR) fprintf (stderr, "DosOpen() returned %d\n", rc);
360*d5c9a868SElliott Hughes #endif
361*d5c9a868SElliott Hughes 		if (!IS_NOLOCK(dev)) {
362*d5c9a868SElliott Hughes 			rc = DosDevIOCtl(
363*d5c9a868SElliott Hughes 			FileHandle, 0x08L, DSK_LOCKDRIVE, 0, 0, 0, 0, 0, 0);
364*d5c9a868SElliott Hughes #if DEBUG
365*d5c9a868SElliott Hughes 			if (rc != NO_ERROR) fprintf (stderr, "DosDevIOCtl() returned %d\n", rc);
366*d5c9a868SElliott Hughes #endif
367*d5c9a868SElliott Hughes 		}
368*d5c9a868SElliott Hughes 		if (rc == NO_ERROR)
369*d5c9a868SElliott Hughes 			This->fd = _imphandle(FileHandle); else This->fd = -1;
370*d5c9a868SElliott Hughes 	} else
371*d5c9a868SElliott Hughes #endif
372*d5c9a868SElliott Hughes 	    {
373*d5c9a868SElliott Hughes 		    This->fd = open(name, mode | O_LARGEFILE | O_BINARY,
374*d5c9a868SElliott Hughes 				    IS_NOLOCK(dev)?0444:0666);
375*d5c9a868SElliott Hughes 	    }
376*d5c9a868SElliott Hughes 
377*d5c9a868SElliott Hughes 	if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
378*d5c9a868SElliott Hughes 		drop_privs();
379*d5c9a868SElliott Hughes 
380*d5c9a868SElliott Hughes 	if (This->fd < 0) {
381*d5c9a868SElliott Hughes 		if(errmsg) {
382*d5c9a868SElliott Hughes #ifdef HAVE_SNPRINTF
383*d5c9a868SElliott Hughes 			snprintf(errmsg, 199, "Can't open %s: %s",
384*d5c9a868SElliott Hughes 				name, strerror(errno));
385*d5c9a868SElliott Hughes #else
386*d5c9a868SElliott Hughes 			sprintf(errmsg, "Can't open %s: %s",
387*d5c9a868SElliott Hughes 				name, strerror(errno));
388*d5c9a868SElliott Hughes #endif
389*d5c9a868SElliott Hughes 		}
390*d5c9a868SElliott Hughes 		goto exit_1;
391*d5c9a868SElliott Hughes 	}
392*d5c9a868SElliott Hughes 
393*d5c9a868SElliott Hughes 	if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
394*d5c9a868SElliott Hughes 		closeExec(This->fd);
395*d5c9a868SElliott Hughes 
396*d5c9a868SElliott Hughes #ifdef __EMX__
397*d5c9a868SElliott Hughes 	if (*(name+1) != ':')
398*d5c9a868SElliott Hughes #endif
399*d5c9a868SElliott Hughes 	if (MT_FSTAT(This->fd, &This->statbuf) < 0
400*d5c9a868SElliott Hughes #ifdef OS_mingw32msvc
401*d5c9a868SElliott Hughes 	    && strncmp(name, "\\\\.\\", 4) != 0
402*d5c9a868SElliott Hughes #endif
403*d5c9a868SElliott Hughes 	   ) {
404*d5c9a868SElliott Hughes 		if(errmsg) {
405*d5c9a868SElliott Hughes #ifdef HAVE_SNPRINTF
406*d5c9a868SElliott Hughes 			snprintf(errmsg,199,"Can't stat %s: %s",
407*d5c9a868SElliott Hughes 				name, strerror(errno));
408*d5c9a868SElliott Hughes #else
409*d5c9a868SElliott Hughes 			if(strlen(name) > 50) {
410*d5c9a868SElliott Hughes 			    sprintf(errmsg,"Can't stat file: %s",
411*d5c9a868SElliott Hughes 				    strerror(errno));
412*d5c9a868SElliott Hughes 			} else {
413*d5c9a868SElliott Hughes 			    sprintf(errmsg,"Can't stat %s: %s",
414*d5c9a868SElliott Hughes 				name, strerror(errno));
415*d5c9a868SElliott Hughes 			}
416*d5c9a868SElliott Hughes #endif
417*d5c9a868SElliott Hughes 		}
418*d5c9a868SElliott Hughes 		goto exit_0;
419*d5c9a868SElliott Hughes 	}
420*d5c9a868SElliott Hughes 
421*d5c9a868SElliott Hughes 	if(LockDevice(This->fd, dev, locked, lockMode, errmsg) < 0)
422*d5c9a868SElliott Hughes 		goto exit_0;
423*d5c9a868SElliott Hughes 
424*d5c9a868SElliott Hughes 	/* set default parameters, if needed */
425*d5c9a868SElliott Hughes 	if (dev){
426*d5c9a868SElliott Hughes 		errno=0;
427*d5c9a868SElliott Hughes 		if (((!IS_MFORMAT_ONLY(dev) && dev->tracks) ||
428*d5c9a868SElliott Hughes 		     mode2 & ALWAYS_GET_GEOMETRY) &&
429*d5c9a868SElliott Hughes 		    init_geom_with_reg(This->fd, dev, orig_dev,
430*d5c9a868SElliott Hughes 				       &This->statbuf)){
431*d5c9a868SElliott Hughes 			if(geomFailure && (errno==EBADF || errno==EPERM)) {
432*d5c9a868SElliott Hughes 				*geomFailure=1;
433*d5c9a868SElliott Hughes 				return NULL;
434*d5c9a868SElliott Hughes 			} else if(errmsg)
435*d5c9a868SElliott Hughes 				sprintf(errmsg,"init: set default params");
436*d5c9a868SElliott Hughes 			goto exit_0;
437*d5c9a868SElliott Hughes 		}
438*d5c9a868SElliott Hughes 	}
439*d5c9a868SElliott Hughes 
440*d5c9a868SElliott Hughes 	if(maxSize)
441*d5c9a868SElliott Hughes 		*maxSize = max_off_t_seek;
442*d5c9a868SElliott Hughes 
443*d5c9a868SElliott Hughes 	This->lastwhere = 0;
444*d5c9a868SElliott Hughes 
445*d5c9a868SElliott Hughes 	return &This->head;
446*d5c9a868SElliott Hughes  exit_0:
447*d5c9a868SElliott Hughes 	close(This->fd);
448*d5c9a868SElliott Hughes  exit_1:
449*d5c9a868SElliott Hughes 	Free(This);
450*d5c9a868SElliott Hughes 	return NULL;
451*d5c9a868SElliott Hughes }
452*d5c9a868SElliott Hughes 
get_fd(Stream_t * Stream)453*d5c9a868SElliott Hughes int get_fd(Stream_t *Stream)
454*d5c9a868SElliott Hughes {
455*d5c9a868SElliott Hughes 	Class_t *clazz;
456*d5c9a868SElliott Hughes 	DeclareThis(SimpleFile_t);
457*d5c9a868SElliott Hughes 	clazz = This->head.Class;
458*d5c9a868SElliott Hughes 	if(clazz != &SimpleFileClass)
459*d5c9a868SElliott Hughes 	  return -1;
460*d5c9a868SElliott Hughes 	else
461*d5c9a868SElliott Hughes 	  return This->fd;
462*d5c9a868SElliott Hughes }
463