/* Copyright (C) 1989, 1992, 1993, 1994 Aladdin Enterprises. All rights reserved. This file is part of Aladdin Ghostscript. Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or distributor accepts any responsibility for the consequences of using it, or for whether it serves any particular purpose or works at all, unless he or she says so in writing. Refer to the Aladdin Ghostscript Free Public License (the "License") for full details. Every copy of Aladdin Ghostscript must include a copy of the License, normally in a plain ASCII text file named PUBLIC. The License grants you the right to copy, modify and redistribute Aladdin Ghostscript, but only under certain conditions described in the License. Among other things, the License requires that the copyright notice and this notice be preserved on all copies. */ /* zfile.c */ /* Non-I/O file operators */ #include "memory_.h" #include "string_.h" #include "ghost.h" #include "gp.h" #include "gsstruct.h" /* for registering root */ #include "errors.h" #include "oper.h" #include "estack.h" /* for filenameforall */ #include "ialloc.h" #include "ilevel.h" /* %names only work in Level 2 */ #include "interp.h" /* gs_errorinfo_put_string prototype */ #include "isave.h" /* for restore */ #include "iutil.h" #include "stream.h" #include "strimpl.h" #include "sfilter.h" #include "gxiodev.h" /* must come after stream.h */ /* and before files.h */ #include "files.h" /* ditto */ #include "fname.h" /* ditto */ #include "store.h" /* Import the file_open routine for %os%, which is the default. */ extern iodev_proc_open_file(iodev_os_open_file); /* Forward references: file opening. */ int file_open(P6(const byte *, uint, const char *, uint, ref *, stream **)); /* Forward references: other. */ private int file_close_file(P1(stream *)); private stream_proc_report_error(filter_report_error); /* Imported from gs.c */ extern const char **gs_lib_paths; /* search path list, */ /* terminated by a null pointer */ /* * Since there can be many file objects referring to the same file/stream, * we can't simply free a stream when we close it. On the other hand, * we don't want freed streams to clutter up memory needlessly. * Our solution is to retain the freed streams, and reuse them. * To prevent an old file object from being able to access a reused stream, * we keep a serial number in each stream, and check it against a serial * number stored in the file object (as the "size"); when we close a file, * we increment its serial number. If the serial number ever overflows, * we leave it at zero, and do not reuse the stream. * (This will never happen.) * * Storage management for this scheme is a little tricky. * We maintain an invariant that says that a stream opened at a given * save level always uses a stream structure allocated at that level. * By doing this, we don't need to keep track separately of streams open * at a level vs. streams allocated at a level; this simplifies things. */ /* * In order to avoid complex interactions with save and restore, we maintain * a list of all streams currently allocated, both open and closed. * The save_count member of a stream indicates the number of unmatched saves * at which the given stream was the head of the list. Thus the streams * allocated at the current level are precisely those from the head of the * list up to and not including the first stream with non-zero save_count. */ private stream *file_list; private gs_gc_root_t file_list_root; /* File buffer sizes. For real files, this is arbitrary, */ /* since the C library does its own buffering in addition. */ /* stdout and stderr use smaller buffers, */ /* on the assumption that they are usually not real files. */ /* The buffer size for type 1 encrypted files is NOT arbitrary: */ /* it must be at most 512. */ #define default_buffer_size 512 const uint file_default_buffer_size = default_buffer_size; /* An invalid file object */ stream *invalid_file_entry; /* exported for zfileio.c */ private gs_gc_root_t invalid_file_root; /* Initialize the file table */ private void zfile_init(void) { /* Create and initialize an invalid (closed) stream. */ /* Initialize the stream for the sake of the GC, */ /* and so it can act as an empty input stream. */ stream *s = s_alloc(imemory, "zfile_init"); sread_string(s, NULL, 0); s->next = s->prev = 0; s->save_count = 0; s_init_no_id(s); invalid_file_entry = s; gs_register_struct_root(imemory, &invalid_file_root, (void **)&invalid_file_entry, "invalid_file_entry"); /* Initialize the bookkeeping list. */ file_list = 0; gs_register_struct_root(imemory, &file_list_root, (void **)&file_list, "file_list"); } /* Make an invalid file object. */ void make_invalid_file(ref *fp) { make_file(fp, imemory_space((gs_ref_memory_t *)invalid_file_entry->memory), ~0, invalid_file_entry); } /* file */ int zfile(register os_ptr op) { char file_access[3]; parsed_file_name pname; const byte *astr; int code; stream *s; check_read_type(*op, t_string); astr = op->value.const_bytes; switch ( r_size(op) ) { case 2: if ( astr[1] != '+' ) return_error(e_invalidfileaccess); file_access[1] = '+'; file_access[2] = 0; break; case 1: file_access[1] = 0; break; default: return_error(e_invalidfileaccess); } switch ( astr[0] ) { case 'r': case 'w': case 'a': break; default: return_error(e_invalidfileaccess); } file_access[0] = astr[0]; code = parse_file_name(op - 1, &pname); if ( code < 0 ) return code; if ( pname.iodev == NULL ) pname.iodev = iodev_default; if ( pname.fname == NULL ) /* just a device */ code = (*pname.iodev->procs.open_device)(pname.iodev, file_access, &s, imemory); else /* file */ { iodev_proc_open_file((*open_file)) = pname.iodev->procs.open_file; if ( open_file == 0 ) open_file = iodev_os_open_file; code = (*open_file)(pname.iodev, pname.fname, pname.len, file_access, &s, imemory); } if ( code < 0 ) return code; make_stream_file(op - 1, s, file_access); pop(1); return code; } /* ------ Level 2 extensions ------ */ /* deletefile - */ int zdeletefile(register os_ptr op) { parsed_file_name pname; int code = parse_real_file_name(op, &pname, "deletefile"); if ( code < 0 ) return code; code = (*pname.iodev->procs.delete_file)(pname.iodev, pname.fname); free_file_name(&pname, "deletefile"); if ( code < 0 ) return code; pop(1); return 0; } /*