File: out.c

package info (click to toggle)
sn 0.3.4a-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 784 kB
  • ctags: 826
  • sloc: ansic: 9,023; sh: 339; makefile: 208
file content (80 lines) | stat: -rw-r--r-- 1,567 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * This file is part of the sn package.
 * Distribution of sn is covered by the GNU GPL. See file COPYING.
 * Copyright  1998-2000 Harold Tay.
 * Copyright  2000- Patrik Rdman.
 */

/*
 * Simple logging function using varargs.
 */

#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "format.h"

static const char rcsid[] = "$Id$";

static char outbuf[1024];
int log_withpid = 0;
int log_withtime = 0;

int writefv (int fd, char *fmt, va_list ap)
{
   int wrote;
   int used;
   char *p;
   int len;
   char tmp[40];
   int er;

   for (used = wrote = 0; *fmt; fmt++)
      if ('%' == *fmt)
      {
         fmt++;
         if (!*fmt)
            break;
         p = vachar(*fmt, &ap, tmp, &len);
         for (; len; len--)
         {
            if (used >= sizeof (outbuf))
            {
               er = write(fd, outbuf, used);
               if (-1 == er)
                  return (-1);
               wrote += er;
               used = 0;
            }
            outbuf[used++] = *p++;
         }
      }
      else
      {
         if (used >= sizeof (outbuf))
         {
            er = write(fd, outbuf, used);
            if (-1 == er)
               return (-1);
            wrote += er;
            used = 0;
         }
         outbuf[used++] = *fmt;
      }
   er = write(fd, outbuf, used);
   if (-1 == er)
      return (-1);
   return (wrote + er);
}

int writef (int fd, char *fmt, ...)
{
   va_list ap;
   int er;

   va_start(ap, fmt);
   er = writefv(fd, fmt, ap);
   va_end(ap);
   return (er);
}