summaryrefslogtreecommitdiff
path: root/Build/source/texk/xdvik/my-vsnprintf.c
diff options
context:
space:
mode:
authorPeter Breitenlohner <peb@mppmu.mpg.de>2008-07-01 15:42:36 +0000
committerPeter Breitenlohner <peb@mppmu.mpg.de>2008-07-01 15:42:36 +0000
commit6552be7f745c5a8d506148c8af9d367c9d996d24 (patch)
treeb9d673871a1490b64273a265d4d961316a2174db /Build/source/texk/xdvik/my-vsnprintf.c
parent48e2b240973977c208bc840b589b75b91a6f4c30 (diff)
xdvik 22.84.14
git-svn-id: svn://tug.org/texlive/trunk@9142 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build/source/texk/xdvik/my-vsnprintf.c')
-rw-r--r--Build/source/texk/xdvik/my-vsnprintf.c26
1 files changed, 17 insertions, 9 deletions
diff --git a/Build/source/texk/xdvik/my-vsnprintf.c b/Build/source/texk/xdvik/my-vsnprintf.c
index dc2b9b7603c..b5db0dfb83e 100644
--- a/Build/source/texk/xdvik/my-vsnprintf.c
+++ b/Build/source/texk/xdvik/my-vsnprintf.c
@@ -51,7 +51,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*------------------------------------------------------------*/
int
-my_vsnprintf(char *buf, int len, const char *format, va_list argp)
+my_vsnprintf(char *buf, size_t len, const char *format, va_list argp)
{
int pipe_fd[2];
FILE *fd;
@@ -59,7 +59,9 @@ my_vsnprintf(char *buf, int len, const char *format, va_list argp)
#ifdef DEBUG
printf("=============my_vsnprintf called!\n");
#endif
- len--; /* for the trailing '\0' */
+ if (len > 0) {
+ len--; /* for the trailing '\0' */
+ }
/* create a pipe for reading/writing */
if (xpipe(pipe_fd) == -1) {
@@ -70,17 +72,23 @@ my_vsnprintf(char *buf, int len, const char *format, va_list argp)
perror("my_vsnprintf: fdopen");
xdvi_exit(EXIT_FAILURE);
}
- /* instead of setting fd to non-buffering, flush it explicitly: */
- /* setvbuf(fd, NULL, _IONBF, BUFSIZ); */
+ /* instead of setting fd to non-buffering:
+ setvbuf(fd, NULL, _IONBF, BUFSIZ);
+ flush it explicitly: */
size = vfprintf(fd, format, argp);
fflush(fd);
- /* wrote less characters than len -> adjust len for the subsequent read */
- if (size < len) {
- len = size;
+ /* according to C99, buf may be NULL, in which case
+ just return correct number of chars. Also, don't
+ write anything if there had been an output error. */
+ if (buf != NULL && size >= 0) {
+ /* wrote less characters than len -> adjust len for the subsequent read */
+ if ((size_t)size < len) {
+ len = size;
+ }
+ read(pipe_fd[0], buf, len);
+ buf[len] = '\0'; /* always null-terminate */
}
- read(pipe_fd[0], buf, len);
- buf[len] = '\0'; /* always null-terminate */
fclose(fd);
close(pipe_fd[0]);