/* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises. All rights reserved. This file is part of Ghostscript. Ghostscript is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the consequences of using it or for whether it serves any particular purpose or works at all, unless he says so in writing. Refer to the Ghostscript General Public License for full details. Everyone is granted permission to copy, modify and redistribute Ghostscript, but only under the conditions described in the Ghostscript General Public License. A copy of this license is supposed to have been given to you along with Ghostscript so you can know your rights and responsibilities. It should be in a file named COPYING. Among other things, the copyright notice and this notice must be preserved on all copies. */ /* zfile.c */ /* Non-I/O file operators for Ghostscript */ #include "memory_.h" #include "string_.h" #include "ghost.h" #include "gp.h" #include "errors.h" #include "oper.h" #include "alloc.h" #include "estack.h" /* for filenameforall, file_close */ #include "ilevel.h" /* %names only work in Level 2 */ #include "iutil.h" #include "save.h" /* for restore */ #include "stream.h" #include "filedev.h" /* must come after stream.h */ #include "files.h" /* ditto */ #include "store.h" /* Import the file device table from zfiledev.c. */ extern file_device *file_device_table[]; #define fdev_default file_device_table[0] /* Forward references: file name parsing. */ /* Parsed file name type. Note that the file name may be either a */ /* Ghostscript string (no terminator) or a C string (null terminator). */ typedef struct parsed_file_name_s { file_device *fdev; const char *fname; uint len; } parsed_file_name; private int parse_file_name(P2(os_ptr, parsed_file_name *)); private int parse_real_file_name(P3(os_ptr, parsed_file_name *, const char *)); private void free_real_file_name(P2(parsed_file_name *, const char *)); /* Forward references: file opening. */ int file_open(P6(const byte *, uint, const char *, uint, ref *, stream **)); stream *file_alloc_stream(P0()); int file_open_stream(P6(const byte *, uint, const char *, uint, stream **, fdev_proc_fopen_t)); private void make_stream_file(P3(ref *, stream *, const char *)); /* Forward references: other. */ private int file_close_file(P1(stream *)); /* Imported from gs.c */ extern 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. */ /* * The chain of all streams allocated at the current level. * We need this so that we can do the right thing for restore. * Note that this chain includes both open and closed files. */ private ref file_list_ref; /* t_file */ #define file_list file_list_ref.value.pfile /* 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; /* Initialize the file table */ private void zfile_init(void) { invalid_file_entry = (stream *)gs_malloc(1, sizeof(stream), "zfile_init"); s_disable(invalid_file_entry); s_init_no_id(invalid_file_entry); /* Initialize the bookkeeping lists. */ make_file(&file_list_ref, 0, 0, 0); } /* 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.fdev == NULL ) pname.fdev = fdev_default; if ( pname.fname == NULL ) /* just a device */ code = (*pname.fdev->procs.open_device)(pname.fdev, file_access, &s); else /* file */ code = (*pname.fdev->procs.open_file)(pname.fdev, pname.fname, pname.len, file_access, &s); if ( code < 0 ) return code; make_stream_file(op - 1, s, file_access); pop(1); return code; } /* closefile - */ int zclosefile(register os_ptr op) { stream *s; check_file(s, op); switch ( sclose(s) ) { case 0: pop(1); return 0; default: return_error(e_ioerror); } } /* ------ 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.fdev->procs.delete_file)(pname.fdev, pname.fname); free_real_file_name(&pname, "deletefile"); if ( code < 0 ) return code; pop(1); return 0; } /*