-
Notifications
You must be signed in to change notification settings - Fork 24
/
print.c
executable file
·64 lines (55 loc) · 1.37 KB
/
print.c
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
/*
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);
va_end(ap);
return size;
}
char *ints_to_chars(int *array, int n, const char *delim) {
char *tmp = malloc(n * sizeof(char) + n * strlen(delim) * sizeof(char) + 1);
int j = 0;
for (int i = 0; i < n; i++) {
j += sprintf(tmp + j, "%d", array[i]);
if (i < n - 1) {
strcat(tmp, delim);
j += strlen(delim);
}
}
return tmp;
}