1 /* -----------------------------------------------------------------------------
4 * This file implements a file-like object that can be built around an
5 * ordinary FILE * or integer file descriptor.
7 * Author(s) : David Beazley (beazley@cs.uchicago.edu)
9 * Copyright (C) 1999-2000. The University of Chicago
10 * See the file LICENSE for information on usage and redistribution.
11 * ----------------------------------------------------------------------------- */
13 char cvsroot_file_c[] = "$Id: file.c 10898 2008-11-03 12:51:45Z wsfulton $";
28 /* -----------------------------------------------------------------------------
30 * ----------------------------------------------------------------------------- */
32 static void DelFile(DOH *fo) {
33 DohFile *f = (DohFile *) ObjData(fo);
47 /* -----------------------------------------------------------------------------
49 * ----------------------------------------------------------------------------- */
51 static int File_read(DOH *fo, void *buffer, int len) {
52 DohFile *f = (DohFile *) ObjData(fo);
55 return fread(buffer, 1, len, f->filep);
58 return read(f->fd, buffer, len);
64 /* -----------------------------------------------------------------------------
66 * ----------------------------------------------------------------------------- */
68 static int File_write(DOH *fo, void *buffer, int len) {
69 DohFile *f = (DohFile *) ObjData(fo);
71 int ret = (int) fwrite(buffer, 1, len, f->filep);
72 int err = (ret != len) ? ferror(f->filep) : 0;
73 return err ? -1 : ret;
76 return write(f->fd, buffer, len);
82 /* -----------------------------------------------------------------------------
84 * ----------------------------------------------------------------------------- */
86 static int File_seek(DOH *fo, long offset, int whence) {
87 DohFile *f = (DohFile *) ObjData(fo);
89 return fseek(f->filep, offset, whence);
92 return lseek(f->fd, offset, whence);
98 /* -----------------------------------------------------------------------------
100 * ----------------------------------------------------------------------------- */
102 static long File_tell(DOH *fo) {
103 DohFile *f = (DohFile *) ObjData(fo);
105 return ftell(f->filep);
108 return lseek(f->fd, 0, SEEK_CUR);
114 /* -----------------------------------------------------------------------------
116 * ----------------------------------------------------------------------------- */
118 static int File_putc(DOH *fo, int ch) {
119 DohFile *f = (DohFile *) ObjData(fo);
121 return fputc(ch, f->filep);
126 return write(f->fd, &c, 1);
132 /* -----------------------------------------------------------------------------
134 * ----------------------------------------------------------------------------- */
136 static int File_getc(DOH *fo) {
137 DohFile *f = (DohFile *) ObjData(fo);
139 return fgetc(f->filep);
143 if (read(f->fd, &c, 1) < 0)
151 /* -----------------------------------------------------------------------------
154 * Put a character back onto the input
155 * ----------------------------------------------------------------------------- */
157 static int File_ungetc(DOH *fo, int ch) {
158 DohFile *f = (DohFile *) ObjData(fo);
160 return ungetc(ch, f->filep);
163 /* Not implemented yet */
169 /* -----------------------------------------------------------------------------
173 * ----------------------------------------------------------------------------- */
175 static int File_close(DOH *fo) {
177 DohFile *f = (DohFile *) ObjData(fo);
179 ret = fclose(f->filep);
190 static DohFileMethods FileFileMethods = {
198 File_close, /* close */
201 static DohObjInfo DohFileType = {
202 "DohFile", /* objname */
203 DelFile, /* doh_del */
220 0, /* doh_sequence */
221 &FileFileMethods, /* doh_file */
223 0, /* doh_callable */
224 0, /* doh_position */
227 /* -----------------------------------------------------------------------------
230 * Create a new file from a given filename and mode.
231 * If newfiles is non-zero, the filename is added to the list of new files.
232 * ----------------------------------------------------------------------------- */
234 DOH *DohNewFile(DOH *filename, const char *mode, DOHList *newfiles) {
239 filen = Char(filename);
240 file = fopen(filen, mode);
244 f = (DohFile *) DohMalloc(sizeof(DohFile));
250 Append(newfiles, filename);
254 return DohObjMalloc(&DohFileType, f);
257 /* -----------------------------------------------------------------------------
260 * Create a file object from an already open FILE *.
261 * ----------------------------------------------------------------------------- */
263 DOH *DohNewFileFromFile(FILE *file) {
265 f = (DohFile *) DohMalloc(sizeof(DohFile));
271 return DohObjMalloc(&DohFileType, f);
274 /* -----------------------------------------------------------------------------
277 * Create a file object from an already open FILE *.
278 * ----------------------------------------------------------------------------- */
280 DOH *DohNewFileFromFd(int fd) {
282 f = (DohFile *) DohMalloc(sizeof(DohFile));
288 return DohObjMalloc(&DohFileType, f);
291 /* -----------------------------------------------------------------------------
294 * Display cause of one of the NewFile functions failing.
295 * ----------------------------------------------------------------------------- */
297 void DohFileErrorDisplay(DOHString * filename) {
298 Printf(stderr, "Unable to open file %s: %s\n", filename, strerror(errno));