386BSD 0.1 development
[unix-history] / usr / othersrc / public / ghostscript-2.4.1 / gp_iwatc.c
CommitLineData
c6ac39be
WJ
1/* Copyright (C) 1991, 1992 Aladdin Enterprises. All rights reserved.
2 Distributed by Free Software Foundation, Inc.
3
4This file is part of Ghostscript.
5
6Ghostscript is distributed in the hope that it will be useful, but
7WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
8to anyone for the consequences of using it or for whether it serves any
9particular purpose or works at all, unless he says so in writing. Refer
10to the Ghostscript General Public License for full details.
11
12Everyone is granted permission to copy, modify and redistribute
13Ghostscript, but only under the conditions described in the Ghostscript
14General Public License. A copy of this license is supposed to have been
15given to you along with Ghostscript so you can know your rights and
16responsibilities. It should be in a file named COPYING. Among other
17things, the copyright notice and this notice must be preserved on all
18copies. */
19
20/* gp_iwatc.c */
21/* Intel processor, Watcom C-specific routines for Ghostscript */
22#include "dos_.h"
23#include <fcntl.h>
24#include <signal.h>
25#include <stdlib.h>
26#include "stat_.h"
27#include "string_.h"
28#include "gx.h"
29#include "gp.h"
30
31/* Define the size of the C stack. */
32unsigned _stklen = 8000; /* default is 4096, we need more */
33
34/* Define a substitute for stdprn (see below). */
35private FILE *gs_stdprn;
36
37/* Forward declarations */
38private void handle_FPE(P1(int));
39
40/* Do platform-dependent initialization. */
41extern void gp_init_console(P0());
42void
43gp_init()
44{ _fmode = O_BINARY; /* Open files in 'binary' mode */
45 gs_stdprn = 0;
46 /* Set up the handler for numeric exceptions. */
47 signal(SIGFPE, handle_FPE);
48 gp_init_console();
49}
50
51/* Trap numeric exceptions. Someday we will do something */
52/* more appropriate with these. */
53private void
54handle_FPE(int sig)
55{ eprintf("Numeric exception:\n");
56 exit(1);
57}
58
59/* Do platform-dependent cleanup. */
60void
61gp_exit()
62{
63}
64
65/* ------ Printer accessing ------ */
66
67/* Open a connection to a printer. A null file name means use the */
68/* standard printer connected to the machine, if any. */
69/* Return NULL if the connection could not be opened. */
70extern void gp_set_printer_binary(P1(int));
71FILE *
72gp_open_printer(char *fname)
73{ if ( strlen(fname) == 0 || !strcmp(fname, "PRN") )
74 { if ( gs_stdprn == 0 )
75 { /* We have to effectively reopen the printer, */
76 /* because the Watcom library does \n -> \r\n */
77 /* substitution on the stdprn stream. */
78 int fno = dup(fileno(stdprn));
79 setmode(fno, O_BINARY);
80 gs_stdprn = fdopen(fno, "wb");
81 gp_set_printer_binary(fileno(gs_stdprn));
82 }
83 return gs_stdprn;
84 }
85 else
86 return fopen(fname, "wb");
87}
88
89/* Close the connection to the printer. */
90void
91gp_close_printer(FILE *pfile, const char *fname)
92{ fclose(pfile);
93 if ( pfile == gs_stdprn )
94 gs_stdprn = 0;
95}
96
97/* ------ File names ------ */
98
99/* Create and open a scratch file with a given name prefix. */
100/* Write the actual file name at fname. */
101FILE *
102gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
103{ /* Unfortunately, Watcom C doesn't provide mktemp, */
104 /* so we have to simulate it ourselves. */
105 struct stat fst;
106 char *end;
107 strcpy(fname, prefix);
108 strcat(fname, "AA.AAA");
109 end = fname + strlen(fname) - 1;
110 while ( stat(fname, &fst) == 0 )
111 { char *inc = end;
112 while ( *inc == 'Z' || *inc == '.' )
113 { if ( *inc == 'Z' ) *inc = 'A';
114 inc--;
115 if ( end - inc == 6 ) return 0;
116 }
117 ++*inc;
118 }
119 return fopen(fname, mode);
120}
121
122/* ------ File operations ------ */
123
124/* If the file given by fname exists, fill in its status and return 1; */
125/* otherwise return 0. */
126int
127gp_file_status(const char *fname, file_status *pstatus)
128{ struct stat fst;
129 if ( stat(fname, &fst) < 0 ) return 0;
130 pstatus->size_pages = (fst.st_size + 1023) >> 10;
131 pstatus->size_bytes = fst.st_size;
132 /****** CONVERSION PROBABLY REQUIRED HERE ******/
133 pstatus->time_referenced = fst.st_atime;
134 pstatus->time_created = fst.st_mtime;
135 return 1;
136}