| 1 |
/*------------------------------------------------------------ |
| 2 |
|
| 3 |
written by Stefan Ulrich <[email protected]> 2001/02/25 |
| 4 |
|
| 5 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
of this software and associated documentation files (the "Software"), to |
| 7 |
deal in the Software without restriction, including without limitation the |
| 8 |
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 9 |
sell copies of the Software, and to permit persons to whom the Software is |
| 10 |
furnished to do so, subject to the following conditions: |
| 11 |
|
| 12 |
The above copyright notice and this permission notice shall be included in |
| 13 |
all copies or substantial portions of the Software. |
| 14 |
|
| 15 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 |
IN NO EVENT SHALL PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE |
| 19 |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 20 |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 21 |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 |
------------------------------------------------------------*/ |
| 23 |
|
| 24 |
#include "xdvi-config.h" |
| 25 |
|
| 26 |
#include <string.h> |
| 27 |
#include <stdio.h> |
| 28 |
|
| 29 |
#include "xdvi.h" |
| 30 |
#include "my-vsnprintf.h" |
| 31 |
|
| 32 |
#if !HAVE_VSNPRINTF || !HAVE_GOOD_VSNPRINTF |
| 33 |
/*------------------------------------------------------------ |
| 34 |
* my_vsnprintf - emulation of glibc2.1 vsnprintf, use if |
| 35 |
* vsnprintf is not available |
| 36 |
* |
| 37 |
* Arguments: |
| 38 |
* char *buf - buffer to print <format> string into |
| 39 |
* int len - print only len characters of <format> |
| 40 |
* char *format - format string |
| 41 |
* va_list argp - variable argument list |
| 42 |
* |
| 43 |
* Returns: |
| 44 |
* int size - number of characters that would have been |
| 45 |
* written if enough space had been available |
| 46 |
* |
| 47 |
* Purpose: |
| 48 |
* Implementation of GNU's vsnprintf using POSIX pipes (less |
| 49 |
* portable and much slower than the original, but also much |
| 50 |
* easier to implement ;-): |
| 51 |
* Print the formatted string to a pipe and read back |
| 52 |
* at most len-1 characters, appending a '\0' at the end. |
| 53 |
*------------------------------------------------------------*/ |
| 54 |
|
| 55 |
int |
| 56 |
my_vsnprintf(char *buf, size_t len, const char *format, va_list argp) |
| 57 |
{ |
| 58 |
int pipe_fd[2]; |
| 59 |
FILE *fd; |
| 60 |
int size; |
| 61 |
#ifdef DEBUG |
| 62 |
printf("=============my_vsnprintf called!\n"); |
| 63 |
#endif |
| 64 |
if (len > 0) { |
| 65 |
len--; /* for the trailing '\0' */ |
| 66 |
} |
| 67 |
|
| 68 |
/* create a pipe for reading/writing */ |
| 69 |
if (xpipe(pipe_fd) == -1) { |
| 70 |
perror("my_vsnprintf: pipe"); |
| 71 |
xdvi_exit(EXIT_FAILURE); |
| 72 |
} |
| 73 |
if ((fd = try_fdopen(pipe_fd[1], "w")) == NULL) { |
| 74 |
perror("my_vsnprintf: fdopen"); |
| 75 |
xdvi_exit(EXIT_FAILURE); |
| 76 |
} |
| 77 |
/* instead of setting fd to non-buffering: |
| 78 |
setvbuf(fd, NULL, _IONBF, BUFSIZ); |
| 79 |
flush it explicitly: */ |
| 80 |
size = vfprintf(fd, format, argp); |
| 81 |
fflush(fd); |
| 82 |
|
| 83 |
/* according to C99, buf may be NULL, in which case |
| 84 |
just return correct number of chars. Also, don't |
| 85 |
write anything if there had been an output error. */ |
| 86 |
if (buf != NULL && size >= 0) { |
| 87 |
/* wrote less characters than len -> adjust len for the subsequent read */ |
| 88 |
if ((size_t)size < len) { |
| 89 |
len = size; |
| 90 |
} |
| 91 |
read(pipe_fd[0], buf, len); |
| 92 |
buf[len] = '\0'; /* always null-terminate */ |
| 93 |
} |
| 94 |
|
| 95 |
fclose(fd); |
| 96 |
close(pipe_fd[0]); |
| 97 |
close(pipe_fd[1]); |
| 98 |
|
| 99 |
return size; |
| 100 |
} |
| 101 |
|
| 102 |
#endif /* !HAVE_VSNPRINTF || !HAVE_GOOD_VSNPRINTF */ |