xref: /plan9/sys/src/cmd/gs/src/gp_unix.c (revision 8ccd4a6360d974db7bd7bbd4f37e7018419ea908)
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.5 2001/04/08 08:43:24 ghostgum 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 }
61 
62 /* ------ Miscellaneous ------ */
63 
64 /* Get the string corresponding to an OS error number. */
65 /* Unix systems support this so inconsistently that we don't attempt */
66 /* to figure out whether it's available. */
67 const char *
68 gp_strerror(int errnum)
69 {
70     return NULL;
71 }
72 
73 /* ------ Date and time ------ */
74 
75 /* Read the current time (in seconds since Jan. 1, 1970) */
76 /* and fraction (in nanoseconds). */
77 void
78 gp_get_realtime(long *pdt)
79 {
80     struct timeval tp;
81 
82 #if gettimeofday_no_timezone	/* older versions of SVR4 */
83     {
84 	if (gettimeofday(&tp) == -1) {
85 	    lprintf("Ghostscript: gettimeofday failed!\n");
86 	    tp.tv_sec = tp.tv_usec = 0;
87 	}
88     }
89 #else /* All other systems */
90     {
91 	struct timezone tzp;
92 
93 	if (gettimeofday(&tp, &tzp) == -1) {
94 	    lprintf("Ghostscript: gettimeofday failed!\n");
95 	    tp.tv_sec = tp.tv_usec = 0;
96 	}
97     }
98 #endif
99 
100     /* tp.tv_sec is #secs since Jan 1, 1970 */
101     pdt[0] = tp.tv_sec;
102 
103     /* Some Unix systems (e.g., Interactive 3.2 r3.0) return garbage */
104     /* in tp.tv_usec.  Try to filter out the worst of it here. */
105     pdt[1] = tp.tv_usec >= 0 && tp.tv_usec < 1000000 ? tp.tv_usec * 1000 : 0;
106 
107 #ifdef DEBUG_CLOCK
108     printf("tp.tv_sec = %d  tp.tv_usec = %d  pdt[0] = %ld  pdt[1] = %ld\n",
109 	   tp.tv_sec, tp.tv_usec, pdt[0], pdt[1]);
110 #endif
111 }
112 
113 /* Read the current user CPU time (in seconds) */
114 /* and fraction (in nanoseconds).  */
115 void
116 gp_get_usertime(long *pdt)
117 {
118 #if use_times_for_usertime
119     struct tms tms;
120     long ticks;
121     const long ticks_per_sec = CLK_TCK;
122 
123     times(&tms);
124     ticks = tms.tms_utime + tms.tms_stime + tms.tms_cutime + tms.tms_cstime;
125     pdt[0] = ticks / ticks_per_sec;
126     pdt[1] = (ticks % ticks_per_sec) * (1000000000 / ticks_per_sec);
127 #else
128     gp_get_realtime(pdt);	/* Use an approximation on other hosts.  */
129 #endif
130 }
131 
132 /* ------ Screen management ------ */
133 
134 /* Get the environment variable that specifies the display to use. */
135 const char *
136 gp_getenv_display(void)
137 {
138     return getenv("DISPLAY");
139 }
140 
141 /* ------ Printer accessing ------ */
142 
143 /* Open a connection to a printer.  See gp.h for details. */
144 FILE *
145 gp_open_printer(char fname[gp_file_name_sizeof], int binary_mode)
146 {
147     const char *fmode = (binary_mode ? "wb" : "w");
148 
149     return (strlen(fname) == 0 ? 0 : fopen(fname, fmode));
150 }
151 
152 /* Close the connection to the printer. */
153 void
154 gp_close_printer(FILE * pfile, const char *fname)
155 {
156     if (fname[0] == '|')
157 	pclose(pfile);
158     else
159 	fclose(pfile);
160 }
161