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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/*++
/* NAME
/* intv 3
/* SUMMARY
/* integer array utilities
/* SYNOPSIS
/* #include <intv.h>
/*
/* INTV *intv_alloc(len)
/* int len;
/*
/* INTV *intv_free(intvp)
/* INTV *intvp;
/*
/* void intv_add(intvp, count, arg, ...)
/* INTV *intvp;
/* int count;
/* int *arg;
/* DESCRIPTION
/* The functions in this module manipulate arrays of integers.
/* An INTV structure contains the following members:
/* .IP len
/* The actual length of the \fIintv\fR array.
/* .IP intc
/* The number of \fIintv\fR elements used.
/* .IP intv
/* An array of integer values.
/* .PP
/* intv_alloc() returns an empty integer array of the requested
/* length. The result is ready for use by intv_add().
/*
/* intv_add() copies zero or more integers and adds them to the
/* specified integer array.
/*
/* intv_free() releases storage for an integer array, and conveniently
/* returns a null pointer.
/* SEE ALSO
/* msg(3) diagnostics interface
/* DIAGNOSTICS
/* Fatal errors: memory allocation problem.
/* LICENSE
/* .ad
/* .fi
/* The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/* Wietse Venema
/* IBM T.J. Watson Research
/* P.O. Box 704
/* Yorktown Heights, NY 10598, USA
/*--*/
/* System libraries. */
#include <sys_defs.h>
#include <stdlib.h> /* 44BSD stdarg.h uses abort() */
#include <stdarg.h>
/* Application-specific. */
#include "mymalloc.h"
#include "msg.h"
#include "intv.h"
/* intv_free - destroy integer array */
INTV *intv_free(INTV *intvp)
{
myfree((char *) intvp->intv);
myfree((char *) intvp);
return (0);
}
/* intv_alloc - initialize integer array */
INTV *intv_alloc(int len)
{
INTV *intvp;
/*
* Sanity check.
*/
if (len < 1)
msg_panic("intv_alloc: bad array length %d", len);
/*
* Initialize.
*/
intvp = (INTV *) mymalloc(sizeof(*intvp));
intvp->len = len;
intvp->intv = (int *) mymalloc(intvp->len * sizeof(intvp->intv[0]));
intvp->intc = 0;
return (intvp);
}
/* intv_add - add integer to vector */
void intv_add(INTV *intvp, int count,...)
{
va_list ap;
/*
* Make sure that always intvp->intc < intvp->len.
*/
va_start(ap, count);
while (count-- > 0) {
if (intvp->intc >= intvp->len) {
intvp->len *= 2;
intvp->intv = (int *) myrealloc((char *) intvp->intv,
intvp->len * sizeof(int));
}
intvp->intv[intvp->intc++] = va_arg(ap, int);
}
va_end(ap);
}
|