1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/* $Xorg: Util.c,v 1.3 2000/08/17 19:48:06 cpqbld Exp $ */
/*
(c) Copyright 1996 Hewlett-Packard Company
(c) Copyright 1996 International Business Machines Corp.
(c) Copyright 1996 Sun Microsystems, Inc.
(c) Copyright 1996 Novell, Inc.
(c) Copyright 1996 Digital Equipment Corp.
(c) Copyright 1996 Fujitsu Limited
(c) Copyright 1996 Hitachi, Ltd.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the names of the copyright holders shall
not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization from said
copyright holders.
*/
/* $XFree86: xc/programs/Xserver/Xprint/Util.c,v 1.12 2001/08/01 00:44:45 tsi Exp $ */
/* To get the tempnam() prototype in <stdio.h> */
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif
#if defined(linux) && defined(__STRICT_ANSI__)
#undef __STRICT_ANSI__
#endif
#include <X11/Xos.h> /* for unistd.h and string.h */
#include <stdio.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include "misc.h"
#include "dixstruct.h"
#include <X11/extensions/Print.h>
#include "attributes.h"
#define IN_FILE_STRING "%(InFile)%"
#define OUT_FILE_STRING "%(OutFile)%"
/*
* ReplaceAnyString returns a string combining the input strings.
* It replaces all occurances of 'target' with the supplied
* 'replacement'.
* The original input string will generally be freed,
* and the caller is responsible for freeing whatever string is returned.
*/
char *
ReplaceAnyString(
char *string,
char *target,
char *replacement)
{
char *pKeyString;
if(replacement != (char *)NULL)
{
while((pKeyString = strstr(string, target)) != (char *)NULL)
{
char *newString;
newString = (char *)xalloc(strlen(string) + strlen(replacement) -
strlen(target) + 1);
strncpy(newString, string, pKeyString - string);
newString[pKeyString - string] = '\0';
strcat(newString, replacement);
strcat(newString, pKeyString + strlen(target));
xfree(string);
string = newString;
}
}
return string;
}
/*
* ReplaceFileString returns a string combining the input strings.
* It replaces all occurances of IN_FILE_STRING with the supplied
* inFileName, and all occurances of OUT_FILE_STRING with the
* supplied outFileName. The original input string will generally be freed,
* and the caller is responsible for freeing whatever string is returned.
*/
char *
ReplaceFileString(
char *string,
char *inFileName,
char *outFileName)
{
char *pKeyString,
*pInFileString = IN_FILE_STRING,
*pOutFileString = OUT_FILE_STRING;
if(inFileName != (char *)NULL)
{
while((pKeyString = strstr(string, pInFileString)) !=
(char *)NULL)
{
char *newString;
newString = (char *)xalloc(strlen(string) +
strlen(inFileName) + 1);
strncpy(newString, string, pKeyString - string);
newString[pKeyString - string] = '\0';
strcat(newString, inFileName);
strcat(newString, pKeyString + strlen(pInFileString));
xfree(string);
string = newString;
}
}
if(outFileName != (char *)NULL)
{
while((pKeyString = strstr(string, pOutFileString)) !=
(char *)NULL)
{
char *newString;
newString = (char *)xalloc(strlen(string) +
strlen(outFileName) + 1);
strncpy(newString, string, pKeyString - string);
newString[pKeyString - string] = '\0';
strcat(newString, outFileName);
strcat(newString, pKeyString + strlen(pOutFileString));
xfree(string);
string = newString;
}
}
return string;
}
/*
* TransferBytes reads numBytes of data from pSrcFile and writes them
* to pDstFile. It returns the number of bytes actually transfered,
* which will be numBytes if it's successful. Neither pSrcFile nor
* pDstFile are rewound or their pointers otherwise modified prior to
* beginning the transfer.
*/
int
TransferBytes(
FILE *pSrcFile,
FILE *pDstFile,
int numBytes)
{
char buf[10240];
#define BUF_SIZE (sizeof(buf)*sizeof(char))
int bytesWritten = 0;
unsigned bytesToXfer;
for(bytesToXfer = min(BUF_SIZE, (unsigned)numBytes);
bytesToXfer > 0;
bytesToXfer = min(BUF_SIZE, (unsigned)(numBytes - bytesWritten)))
{
if(fread((void *)buf, (size_t) 1, bytesToXfer, pSrcFile) < bytesToXfer)
return bytesWritten;
if(fwrite((void *)buf, (size_t) 1, bytesToXfer, pDstFile) < bytesToXfer)
return bytesWritten;
bytesWritten += bytesToXfer;
}
return bytesWritten;
}
/*
* CopyContentsAndDelete - does the work of copying and deleting the
* pre, no, and post raster files as well as the raster file itself.
*/
Bool
CopyContentsAndDelete(
FILE **ppSrcFile,
char **pSrcFileName,
FILE *pDstFile)
{
struct stat statBuf;
if(stat(*pSrcFileName, &statBuf) < 0)
return FALSE;
rewind(*ppSrcFile);
if(TransferBytes(*ppSrcFile, pDstFile,
(int)statBuf.st_size) != (int)statBuf.st_size)
return FALSE;
fclose(*ppSrcFile);
*ppSrcFile = (FILE *)NULL;
unlink(*pSrcFileName);
xfree(*pSrcFileName);
*pSrcFileName = (char *)NULL;
return TRUE;
}
#define QUADPAD(x) ((((x)+3)>>2)<<2)
int
XpSendDocumentData(
ClientPtr client,
FILE *fp,
int fileLen,
int maxBufSize)
{
xPrintGetDocumentDataReply *pRep;
int bytesWritten;
unsigned bytesToWrite;
int result = Success;
if(client->clientGone)
return Success;
pRep = (xPrintGetDocumentDataReply *)xalloc(sz_xPrintGetDocumentDataReply+
QUADPAD(maxBufSize));
for(bytesToWrite = min(maxBufSize, fileLen),
bytesWritten = 0;
bytesToWrite > 0;
bytesToWrite = min(maxBufSize, fileLen - bytesWritten))
{
pRep->type = X_Reply;
pRep->sequenceNumber = client->sequence;
pRep->length = (QUADPAD(bytesToWrite)) >> 2;
pRep->dataLen = bytesToWrite;
if(fread((void *)(pRep + 1), 1, bytesToWrite, fp) < bytesToWrite)
{
result = BadAlloc; /* XXX poor error choice? */
pRep->statusCode = 2; /* XXX Is this the right value??? */
}
else
pRep->statusCode = 0; /* XXX Ignored??? */
pRep->finishedFlag = FALSE;
if (client->swapped) {
int n;
long l;
swaps(&pRep->sequenceNumber, n);
swapl(&pRep->length, l);
swapl(&pRep->statusCode, l); /* XXX Why are these longs??? */
swapl(&pRep->finishedFlag, l); /* XXX Why are these longs??? */
swapl(&pRep->dataLen, l);
}
(void)WriteToClient(client,
sz_xPrintGetDocumentDataReply + bytesToWrite,
(char *)pRep);
bytesWritten += bytesToWrite;
}
xfree(pRep);
return result;
}
/*
* XpFinishDocData - send a DocumentData reply with the "finishedFlag"
* field set to TRUE. This routine should be called from the EndJob
* function of a driver after the driver has sent all required
* document data (presumably via XpSendDocumentData).
*/
int
XpFinishDocData(
ClientPtr client)
{
xPrintGetDocumentDataReply rep;
if(client->clientGone)
return Success;
rep.type = X_Reply;
rep.sequenceNumber = client->sequence;
rep.length = 0;
rep.dataLen = 0;
rep.finishedFlag = TRUE;
rep.statusCode = 0;
if (client->swapped) {
int n;
long l;
swaps(&rep.sequenceNumber, n);
swapl(&rep.length, l);
swapl(&rep.statusCode, l); /* XXX Why are these longs??? */
swapl(&rep.finishedFlag, l); /* XXX Why are these longs??? */
swapl(&rep.dataLen, l);
}
(void)WriteToClient(client, sz_xPrintGetDocumentDataReply, (char *)&rep);
return Success;
}
#ifndef HAS_MKSTEMP
static
char *XpDirName(char *fname)
{
char *fn, *ptr;
fn = (char *)xalloc(strlen(fname) + 1);
if (fn) {
strcpy(fn, fname);
ptr = strrchr(fn, '/');
if (!ptr) {
ptr = fn;
*ptr++ = '.';
} else if (ptr == fn)
ptr++;
*ptr = '\0';
}
return fn;
}
#endif
Bool
XpOpenTmpFile(
char *mode,
char **fname,
FILE **stream)
{
#ifndef HAS_MKSTEMP
char *fn = NULL;
/* note that there is a small race condition here... */
if (!(*fname = tempnam(NULL, NULL)) ||
!(fn = XpDirName(*fname)) ||
access(fn, W_OK) ||
!(*stream = fopen(*fname, mode)))
{
xfree(fn);
xfree(*fname);
*fname = NULL;
*stream = NULL;
return FALSE;
}
xfree(fn);
#else
int fd;
*stream = NULL;
*fname = (char *)xalloc(14);
if (*fname == NULL)
return FALSE;
strcpy(*fname, "/tmp/xpXXXXXX");
fd = mkstemp(*fname);
if (fd < 0) {
xfree(*fname);
*fname = NULL;
return FALSE;
}
*stream = fdopen(fd, mode);
if (stream == NULL) {
xfree(*fname);
*fname = NULL;
return FALSE;
}
#endif
return TRUE;
}
|