2
* files.c -- $Id: files.c,v 1.1 2003/03/08 15:26:48 travo Exp $
3
* UNIX version of play file operations
5
* Copyright (c) 1999. See accompanying LEGAL file for details.
8
/* to get fileno declared */
9
#define _POSIX_SOURCE 1
12
/* to get popen declared */
13
#define _XOPEN_SOURCE 1
24
#include <sys/types.h>
28
FILE *fp; /* use FILE* for text file operations */
29
int fd; /* use file descriptor for binary file operations */
34
p_fopen(const char *unix_name, const char *mode)
36
FILE *fp = fopen(u_pathname(unix_name), mode);
37
p_file *f = fp? p_malloc(sizeof(p_file)) : 0;
41
for (; mode[0] ; mode++) if (mode[0]=='b') break;
42
f->binary = (mode[0]=='b');
48
p_popen(const char *command, const char *mode)
50
FILE *fp = popen(command, mode[0]=='w'? "w" : "r");
51
p_file *f = fp? p_malloc(sizeof(p_file)) : 0;
61
p_fclose(p_file *file)
63
int flag = (file->binary&2)? pclose(file->fp) : fclose(file->fp);
69
p_fgets(p_file *file, char *buf, int buflen)
71
return fgets(buf, buflen, file->fp);
75
p_fputs(p_file *file, const char *buf)
77
return fputs(buf, file->fp);
81
p_fread(p_file *file, void *buf, unsigned long nbytes)
84
return read(file->fd, buf, nbytes);
86
return fread(buf, 1, nbytes, file->fp);
90
p_fwrite(p_file *file, const void *buf, unsigned long nbytes)
93
return write(file->fd, buf, nbytes);
95
return fwrite(buf, 1, nbytes, file->fp);
101
if (file->binary & 1)
102
return lseek(file->fd, 0L, SEEK_CUR);
103
else if (!(file->binary & 2))
104
return ftell(file->fp);
110
p_fseek(p_file *file, unsigned long addr)
112
if (file->binary & 1)
113
return -(lseek(file->fd, addr, SEEK_SET)==-1L);
114
else if (!(file->binary & 2))
115
return fseek(file->fp, addr, SEEK_SET);
121
p_fflush(p_file *file)
123
return (file->binary & 1)? 0 : fflush(file->fp);
129
return feof(file->fp); /* does not work for file->binary */
133
p_ferror(p_file *file)
135
int flag = ferror(file->fp);
141
p_fsize(p_file *file)
144
if (fstat(file->fd, &buf)) return 0;
149
p_remove(const char *unix_name)
151
return remove(u_pathname(unix_name));
155
p_rename(const char *unix_old, const char *unix_new)
159
strncat(old, u_pathname(unix_old), P_WKSIZ);
160
return rename(old, u_pathname(unix_new));