1 /* formataddr.c - format an address field (from formatsbr) */
4 #include "../h/addrsbr.h"
5 #include "../h/formatsbr.h"
9 static char *buf; /* our current working buffer */
10 static char *bufend; /* end of working buffer */
11 static char *last_dst; /* buf ptr at end of last call */
12 static unsigned int bufsiz; /* current size of buf */
14 #define BUFINCR 512 /* how much to expand buf when if fills */
16 #define CPY(s) { cp = (s); while (*dst++ = *cp++) ; --dst; }
18 /* check if there's enough room in buf for str. add more mem if needed */
19 #define CHECKMEM(str) \
20 if ((len = strlen (str)) >= bufend - dst) {\
22 int n = last_dst - buf;\
23 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
24 buf = realloc (buf, bufsiz);\
28 adios (NULLCP, "formataddr: couldn't get buffer space");\
29 bufend = buf + bufsiz;\
33 /* fmtscan will call this routine if the user includes the function
34 * "(formataddr {component})" in a format string. "orig" is the
35 * original contents of the string register. "str" is the address
36 * string to be formatted and concatenated onto orig. This routine
37 * returns a pointer to the concatenated address string.
39 * We try to not do a lot of malloc/copy/free's (which is why we
40 * don't call "getcpy") but still place no upper limit on the
41 * length of the result string.
43 * This routine is placed in a separate library so it can be
44 * overridden by particular programs (e.g., "replsbr").
46 char *formataddr (orig, str)
55 register struct mailname *mp = NULL;
57 /* if we don't have a buffer yet, get one */
59 buf = malloc (BUFINCR);
61 adios (NULLCP, "formataddr: couldn't allocate buffer space");
62 last_dst = buf; /* XXX */
63 bufsiz = BUFINCR - 6; /* leave some slop */
64 bufend = buf + bufsiz;
67 * If "orig" points to our buffer we can just pick up where we
68 * left off. Otherwise we have to copy orig into our buffer.
72 else if (!orig || !*orig) {
76 dst = last_dst; /* XXX */
81 /* concatenate all the new addresses onto 'buf' */
82 for (isgroup = 0; cp = getname (str); ) {
83 if ((mp = getm (cp, NULLCP, 0, fmt_norm, NULLCP)) == NULL)
86 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
90 /* if we get here we're going to add an address */
96 CHECKMEM (mp->m_gname);