File: print.c

package info (click to toggle)
task-spooler 1.0.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 364 kB
  • sloc: ansic: 4,291; sh: 149; makefile: 105
file content (51 lines) | stat: -rw-r--r-- 1,031 bytes parent folder | download | duplicates (5)
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
/*
    Task Spooler - a task queue system for the unix user
    Copyright (C) 2007-2009  LluĂ­s Batlle i Rossell

    Please find the license in the provided COPYING file.
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/time.h>
#include "main.h"


/* maxsize: max buffer size, with '\0' */
int fd_nprintf(int fd, int maxsize, const char *fmt, ...)
{
    va_list ap;
    char *out;
    int size;
    int rest;

    va_start(ap, fmt);

    out = (char *) malloc(maxsize * sizeof(char));
    if (out == 0)
    {
        warning("Not enough memory in fd_nprintf()");
        return -1;
    }

    size = vsnprintf(out, maxsize, fmt, ap);

    rest = size; /* We don't want the last null character */
    while (rest > 0)
    {
        int res;
        res = write(fd, out, rest);
        if (res == -1)
        {
            warning("Cannot write more chars in pinfo_dump");
            break;
        }
        rest -= res;
    }

    free(out);

    return size;
}