2
* files.c -- $Id: files.c,v 1.1 2003/03/08 15:26:49 travo Exp $
3
* MS Windows version of plib file operations
5
* Copyright (c) 1999. See accompanying LEGAL file for details.
18
FILE *fp; /* use FILE* for text file operations */
19
int fd; /* use file descriptor for binary file operations */
24
p_fopen(const char *unix_name, const char *mode)
26
FILE *fp = fopen(w_pathname(unix_name), mode);
27
p_file *f = fp? p_malloc(sizeof(p_file)) : 0;
31
for (; mode[0] ; mode++) if (mode[0]=='b') break;
32
f->binary = (mode[0]=='b');
34
* in _O_TEXT mode, ftell only works properly if the file
35
* really is a DOS file with CRLF newlines; if it is a UNIX
36
* file (like all of the distribution .i files), ftell fails
37
* on the other hand, using _O_BINARY makes fgets fail to remove
38
* the CR and fputs not insert CR
39
* (may depend on Windows flavor, this is Win2k */
40
if (!f->binary) _setmode(f->fd, _O_BINARY);
46
p_popen(const char *command, const char *mode)
48
/* WARNING- for non-console programs, returned FILE* causes
49
* program to hang forever (according to msdn documentation) */
50
FILE *fp = _popen(command, mode);
51
p_file *f = fp? p_malloc(sizeof(p_file)) : 0;
55
for (; mode[0] ; mode++) if (mode[0]=='b') break;
56
f->binary = (mode[0]=='b') | 2;
62
p_fclose(p_file *file)
64
int flag = (file->binary&2)? _pclose(file->fp) : fclose(file->fp);
70
p_fgets(p_file *file, char *buf, int buflen)
72
char *b = fgets(buf, buflen, file->fp);
74
/* remove CR if line ends with CRLF */
76
for (n=0 ; n<buflen-2 ; n++) {
78
if (buf[n]=='\015' && buf[n+1]=='\n' && buf[n+2]=='\0')
79
buf[n] = '\n', buf[n+1] = '\0';
86
p_fputs(p_file *file, const char *buf)
91
for (i=0 ; i<1024 ; i++,buf++) {
93
if (buf[0] == '\n') b[i++] = '\015';
97
dn = fputs(b, file->fp);
98
if (dn < 0) return dn;
106
p_fread(p_file *file, void *buf, unsigned long nbytes)
108
if (file->binary&1) {
109
int fd = fileno(file->fp);
110
return _read(fd, buf, nbytes);
112
return fread(buf, 1, nbytes, file->fp);
117
p_fwrite(p_file *file, const void *buf, unsigned long nbytes)
119
if (file->binary&1) {
120
int fd = fileno(file->fp);
121
return _write(fd, buf, nbytes);
123
return fwrite(buf, 1, nbytes, file->fp);
128
p_ftell(p_file *file)
131
return _tell(file->fd);
132
else /* broken in _O_TEXT mode, see p_fopen */
133
return ftell(file->fp);
137
p_fseek(p_file *file, unsigned long addr)
140
return -(_lseek(file->fd, addr, SEEK_SET)==-1L);
142
return fseek(file->fp, addr, SEEK_SET);
146
p_fflush(p_file *file)
148
return fflush(file->fp);
154
return feof(file->fp);
158
p_ferror(p_file *file)
160
int flag = ferror(file->fp);
166
p_fsize(p_file *file)
168
return _filelength(file->fd);
172
p_remove(const char *unix_name)
174
return -(!DeleteFile(w_pathname(unix_name)));
178
p_rename(const char *unix_old, const char *unix_new)
182
strncat(old, w_pathname(unix_old), P_WKSIZ);
183
return -(!MoveFile(old, w_pathname(unix_new)));