Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / global / msg_stats_scan.c
blob2c3f3db0e02516c160341a80338268e5797e909c
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* msg_stats_scan
6 /* SUMMARY
7 /* read MSG_STATS from stream
8 /* SYNOPSIS
9 /* #include <msg_stats.h>
11 /* int msg_stats_scan(scan_fn, stream, flags, ptr)
12 /* ATTR_SCAN_MASTER_FN scan_fn;
13 /* VSTREAM *stream;
14 /* int flags;
15 /* void *ptr;
16 /* DESCRIPTION
17 /* msg_stats_scan() reads an MSG_STATS from the named stream
18 /* using the specified attribute scan routine. msg_stats_scan()
19 /* is meant to be passed as a call-back to attr_scan(), thusly:
21 /* ... ATTR_SCAN_FUNC, msg_stats_scan, (void *) &stats, ...
22 /* DIAGNOSTICS
23 /* Fatal: out of memory.
24 /* LICENSE
25 /* .ad
26 /* .fi
27 /* The Secure Mailer license must be distributed with this software.
28 /* AUTHOR(S)
29 /* Wietse Venema
30 /* IBM T.J. Watson Research
31 /* P.O. Box 704
32 /* Yorktown Heights, NY 10598, USA
33 /*--*/
35 /* System library. */
37 #include <sys_defs.h>
39 /* Utility library. */
41 #include <attr.h>
42 #include <vstring.h>
43 #include <msg.h>
45 /* Global library. */
47 #include <mail_proto.h>
48 #include <msg_stats.h>
51 * SLMs.
53 #define STR(x) vstring_str(x)
54 #define LEN(x) VSTRING_LEN(x)
56 /* msg_stats_scan - read MSG_STATS from stream */
58 int msg_stats_scan(ATTR_SCAN_MASTER_FN scan_fn, VSTREAM *fp,
59 int flags, void *ptr)
61 MSG_STATS *stats = (MSG_STATS *) ptr;
62 VSTRING *buf = vstring_alloc(sizeof(MSG_STATS) * 2);
63 int ret;
66 * Receive the entire structure. This is not only simpler but also likely
67 * to be quicker than having the sender figure out what fields need to be
68 * sent, converting those numbers to string and back, and having the
69 * receiver initialize the unused fields by hand.
71 * XXX Would be nice if VSTRINGs could import a fixed-size buffer and
72 * gracefully reject attempts to extend it.
74 ret = scan_fn(fp, flags | ATTR_FLAG_MORE,
75 ATTR_TYPE_DATA, MAIL_ATTR_TIME, buf,
76 ATTR_TYPE_END);
77 if (ret == 1) {
78 if (LEN(buf) == sizeof(*stats)) {
79 memcpy((char *) stats, STR(buf), sizeof(*stats));
80 } else {
81 msg_warn("msg_stats_scan: size mis-match: %u != %u",
82 (unsigned) LEN(buf), (unsigned) sizeof(*stats));
83 ret = (-1);
86 vstring_free(buf);
87 return (ret);