File: module_output.c

package info (click to toggle)
cvm 0.11-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 492 kB
  • ctags: 215
  • sloc: ansic: 1,777; makefile: 48; sh: 12
file content (84 lines) | stat: -rw-r--r-- 2,029 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
81
82
83
84
/* cvm/module_output.c - Response formatting
 * Copyright (C) 2001  Bruce Guenter <bruceg@em.ca>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "module.h"

char outbuffer[BUFSIZE];
unsigned outbuflen;
static char* outbufptr;

static int fact(int number, const char* data, unsigned len)
{
  /* Always leave room for a trailing NUL */
  if (outbuflen + len + 3 > BUFSIZE) {
    outbuflen = BUFSIZE;
    return 0;
  }
  outbuflen += len + 2;
  *outbufptr++ = (char)number;
  memcpy(outbufptr, data, len);
  outbufptr += len;
  *outbufptr++ = 0;
  return 1;
}

void cvm_fact_start(void)
{
  outbuflen = 1;
  outbufptr = outbuffer + outbuflen;
}

int cvm_fact_str(int number, const char* data)
{
  if (!data) return 0;
  return fact(number, data, strlen(data));
}

void cvm_fact_end(int code)
{
  if (outbuflen >= BUFSIZE) code = CVME_BAD_MODDATA;
  if (code) {
    outbuffer[0] = code;
    outbuflen = 1;
  }
  else {
    outbuffer[0] = 0;
    *outbufptr++ = 0;
    ++outbuflen;
  }
}

int cvm_fact_uint(int number, unsigned long data)
{
  char buf[64];
  char* ptr;
  
  if (!data)
    return fact(number, "0", 1);
  ptr = buf + 63;
  *ptr-- = 0;
  while (data) {
    *ptr-- = (data % 10) + '0';
    data /= 10;
  }
  ++ptr;
  return fact(number, ptr, buf+63-ptr);
}