xref: /plan9/sys/src/cmd/gs/src/gp_unix.c (revision 80ee5cbfe36716af62da8896207e9763b8e3d760)
1 /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
2 
3   This file is part of AFPL Ghostscript.
4 
5   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
6   distributor accepts any responsibility for the consequences of using it, or
7   for whether it serves any particular purpose or works at all, unless he or
8   she says so in writing.  Refer to the Aladdin Free Public License (the
9   "License") for full details.
10 
11   Every copy of AFPL Ghostscript must include a copy of the License, normally
12   in a plain ASCII text file named PUBLIC.  The License grants you the right
13   to copy, modify and redistribute AFPL Ghostscript, but only under certain
14   conditions described in the License.  Among other things, the License
15   requires that the copyright notice and this notice be preserved on all
16   copies.
17 */
18 
19 /*$Id: gp_unix.c,v 1.3 2000/09/19 19:00:25 lpd Exp $ */
20 /* Unix-specific routines for Ghostscript */
21 #include "pipe_.h"
22 #include "string_.h"
23 #include "time_.h"
24 #include "gx.h"
25 #include "gsexit.h"
26 #include "gp.h"
27 
28 /*
29  * This is the only place in Ghostscript that calls 'exit'.  Including
30  * <stdlib.h> is overkill, but that's where it's declared on ANSI systems.
31  * We don't have any way of detecting whether we have a standard library
32  * (some GNU compilers perversely define __STDC__ but don't provide
33  * an ANSI-compliant library), so we check __PROTOTYPES__ and
34  * hope for the best.  We pick up getenv at the same time.
35  */
36 #ifdef __PROTOTYPES__
37 #  include <stdlib.h>		/* for exit and getenv */
38 #else
39 extern void exit(P1(int));
40 extern char *getenv(P1(const char *));
41 
42 #endif
43 
44 /* Do platform-dependent initialization. */
45 void
46 gp_init(void)
47 {
48 }
49 
50 /* Do platform-dependent cleanup. */
51 void
52 gp_exit(int exit_status, int code)
53 {
54 }
55 
56 /* Exit the program. */
57 void
58 gp_do_exit(int exit_status)
59 {
60     exit(exit_status);
61 }
62 
63 /* ------ Miscellaneous ------ */
64 
65 /* Get the string corresponding to an OS error number. */
66 /* Unix systems support this so inconsistently that we don't attempt */
67 /* to figure out whether it's available. */
68 const char *
69 gp_strerror(int errnum)
70 {
71     return NULL;
72 }
73 
74 /* ------ Date and time ------ */
75 
76 /* Read the current time (in seconds since Jan. 1, 1970) */
77 /* and fraction (in nanoseconds). */
78 void
79 gp_get_realtime(long *pdt)
80 {
81     struct timeval tp;
82 
83 #if gettimeofday_no_timezone	/* older versions of SVR4 */
84     {
85 	if (gettimeofday(&tp) == -1) {
86 	    lprintf("Ghostscript: gettimeofday failed!\n");
87 	    gs_exit(1);
88 	}
89     }
90 #else /* All other systems */
91     {
92 	struct timezone tzp;
93 
94 	if (gettimeofday(&tp, &tzp) == -1) {
95 	    lprintf("Ghostscript: gettimeofday failed!\n");
96 	    gs_exit(1);
97 	}
98     }
99 #endif
100 
101     /* tp.tv_sec is #secs since Jan 1, 1970 */
102     pdt[0] = tp.tv_sec;
103 
104     /* Some Unix systems (e.g., Interactive 3.2 r3.0) return garbage */
105     /* in tp.tv_usec.  Try to filter out the worst of it here. */
106     pdt[1] = tp.tv_usec >= 0 && tp.tv_usec < 1000000 ? tp.tv_usec * 1000 : 0;
107 
108 #ifdef DEBUG_CLOCK
109     printf("tp.tv_sec = %d  tp.tv_usec = %d  pdt[0] = %ld  pdt[1] = %ld\n",
110 	   tp.tv_sec, tp.tv_usec, pdt[0], pdt[1]);
111 #endif
112 }
113 
114 /* Read the current user CPU time (in seconds) */
115 /* and fraction (in nanoseconds).  */
116 void
117 gp_get_usertime(long *pdt)
118 {
119 #if use_times_for_usertime
120     struct tms tms;
121     long ticks;
122     const long ticks_per_sec = CLK_TCK;
123 
124     times(&tms);
125     ticks = tms.tms_utime + tms.tms_stime + tms.tms_cutime + tms.tms_cstime;
126     pdt[0] = ticks / ticks_per_sec;
127     pdt[1] = (ticks % ticks_per_sec) * (1000000000 / ticks_per_sec);
128 #else
129     gp_get_realtime(pdt);	/* Use an approximation on other hosts.  */
130 #endif
131 }
132 
133 /* ------ Screen management ------ */
134 
135 /* Get the environment variable that specifies the display to use. */
136 const char *
137 gp_getenv_display(void)
138 {
139     return getenv("DISPLAY");
140 }
141 
142 /* ------ Printer accessing ------ */
143 
144 /* Open a connection to a printer.  See gp.h for details. */
145 FILE *
146 gp_open_printer(char fname[gp_file_name_sizeof], int binary_mode)
147 {
148     const char *fmode = (binary_mode ? "wb" : "w");
149 
150     return (strlen(fname) == 0 ? 0 : fopen(fname, fmode));
151 }
152 
153 /* Close the connection to the printer. */
154 void
155 gp_close_printer(FILE * pfile, const char *fname)
156 {
157     if (fname[0] == '|')
158 	pclose(pfile);
159     else
160 	fclose(pfile);
161 }
162