print.c
author viric@mandarina
Fri, 02 Oct 2009 19:20:41 +0200
changeset 274 62048132f95f
parent 267 11631dd11ff8
permissions -rw-r--r--
Making changes so it looks like the code in ts-0.6.4, which I can't find
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
165
7b914d4003c2 Fixing licenses in some files, and updating some documents.
viric@mandarina
parents: 147
diff changeset
     1
/*
7b914d4003c2 Fixing licenses in some files, and updating some documents.
viric@mandarina
parents: 147
diff changeset
     2
    Task Spooler - a task queue system for the unix user
267
11631dd11ff8 Updating copyright years in the source.
viric@mandarina
parents: 231
diff changeset
     3
    Copyright (C) 2007-2009  LluĂ­s Batlle i Rossell
165
7b914d4003c2 Fixing licenses in some files, and updating some documents.
viric@mandarina
parents: 147
diff changeset
     4
7b914d4003c2 Fixing licenses in some files, and updating some documents.
viric@mandarina
parents: 147
diff changeset
     5
    Please find the license in the provided COPYING file.
7b914d4003c2 Fixing licenses in some files, and updating some documents.
viric@mandarina
parents: 147
diff changeset
     6
*/
146
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
     7
#include <stdlib.h>
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
     8
#include <string.h>
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
     9
#include <unistd.h>
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    10
#include <stdio.h>
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    11
#include <stdarg.h>
147
e173645f5221 Added environment info through TS_ENV
viric@llimona
parents: 146
diff changeset
    12
#include <sys/time.h>
146
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    13
#include "main.h"
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    14
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    15
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    16
/* maxsize: max buffer size, with '\0' */
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    17
int fd_nprintf(int fd, int maxsize, const char *fmt, ...)
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    18
{
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    19
    va_list ap;
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    20
    char *out;
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    21
    int size;
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    22
    int rest;
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    23
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    24
    va_start(ap, fmt);
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    25
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    26
    out = (char *) malloc(maxsize * sizeof(char));
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    27
    if (out == 0)
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    28
    {
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    29
        warning("Not enough memory in fd_nprintf()");
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    30
        return -1;
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    31
    }
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    32
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    33
    size = vsnprintf(out, maxsize, fmt, ap);
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    34
147
e173645f5221 Added environment info through TS_ENV
viric@llimona
parents: 146
diff changeset
    35
    rest = size; /* We don't want the last null character */
146
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    36
    while (rest > 0)
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    37
    {
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    38
        int res;
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    39
        res = write(fd, out, rest);
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    40
        if (res == -1)
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    41
        {
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    42
            warning("Cannot write more chars in pinfo_dump");
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    43
            break;
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    44
        }
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    45
        rest -= res;
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    46
    }
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    47
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    48
    free(out);
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    49
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    50
    return size;
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents:
diff changeset
    51
}