* file named AUTHORS, in the root directory of this distribution.
*/
/*
- * $Id: packet.c,v 1.6 2004/02/13 14:00:35 martinea Exp $
+ * $Id: packet.c,v 1.8 2006/05/25 01:47:12 johnfranks Exp $
*
* Routines for modifying the amanda protocol packet type
*/
{ "ACK", P_ACK },
{ "NAK", P_NAK }
};
-#define NPKTYPES (sizeof(pktypes) / sizeof(pktypes[0]))
+#define NPKTYPES (int)(SIZEOF(pktypes) / SIZEOF(pktypes[0]))
/*
* Initialize a packet
*/
+void pkt_init_empty(
+ pkt_t *pkt,
+ pktype_t type)
+{
+ assert(pkt != NULL);
+ assert(strcmp(pkt_type2str(type), "BOGUS") != 0);
+
+ pkt->type = type;
+ pkt->packet_size = 1000;
+ pkt->body = alloc(pkt->packet_size);
+ pkt->body[0] = '\0';
+ pkt->size = strlen(pkt->body);
+}
+
printf_arglist_function2(void pkt_init, pkt_t *, pkt, pktype_t, type,
const char *, fmt)
{
- va_list argp;
+ va_list argp;
+ int len;
assert(pkt != NULL);
assert(strcmp(pkt_type2str(type), "BOGUS") != 0);
- assert(fmt != NULL);
+ if(fmt == NULL)
+ fmt = "";
pkt->type = type;
-
- arglist_start(argp, fmt);
- vsnprintf(pkt->body, sizeof(pkt->body), fmt, argp);
- arglist_end(argp);
+ pkt->packet_size = 1000;
+ pkt->body = alloc(pkt->packet_size);
+ while(1) {
+ arglist_start(argp, fmt);
+ len = g_vsnprintf(pkt->body, pkt->packet_size, fmt, argp);
+ arglist_end(argp);
+ if (len > -1 && len < (int)(pkt->packet_size - 1))
+ break;
+ pkt->packet_size *= 2;
+ amfree(pkt->body);
+ pkt->body = alloc(pkt->packet_size);
+ }
+ pkt->size = strlen(pkt->body);
}
/*
*/
printf_arglist_function1(void pkt_cat, pkt_t *, pkt, const char *, fmt)
{
- size_t len, bufsize;
- va_list argp;
+ size_t len;
+ int lenX;
+ va_list argp;
+ char * pktbody;
assert(pkt != NULL);
assert(fmt != NULL);
len = strlen(pkt->body);
- assert(len < sizeof(pkt->body));
- bufsize = sizeof(pkt->body) - len;
- if (bufsize <= 0)
- return;
-
- arglist_start(argp, fmt);
- vsnprintf(pkt->body + len, bufsize, fmt, argp);
- arglist_end(argp);
+ while(1) {
+ arglist_start(argp, fmt);
+ lenX = g_vsnprintf(pkt->body + len, pkt->packet_size - len, fmt,argp);
+ arglist_end(argp);
+ if (lenX > -1 && lenX < (int)(pkt->packet_size - len - 1))
+ break;
+ pkt->packet_size *= 2;
+ pktbody = alloc(pkt->packet_size);
+ strncpy(pktbody, pkt->body, len);
+ pktbody[len] = '\0';
+ amfree(pkt->body);
+ pkt->body = pktbody;
+ }
+ pkt->size = strlen(pkt->body);
}
/*
* Converts a string into a packet type
*/
pktype_t
-pkt_str2type(typestr)
- const char *typestr;
+pkt_str2type(
+ const char *typestr)
{
int i;
assert(typestr != NULL);
- for (i = 0; i < NPKTYPES; i++)
+ for (i = 0; i < (int)NPKTYPES; i++)
if (strcmp(typestr, pktypes[i].name) == 0)
return (pktypes[i].type);
return ((pktype_t)-1);
* Converts a packet type into a string
*/
const char *
-pkt_type2str(type)
- pktype_t type;
+pkt_type2str(
+ pktype_t type)
{
int i;
- for (i = 0; i < NPKTYPES; i++)
+ for (i = 0; i < (int)NPKTYPES; i++)
if (pktypes[i].type == type)
return (pktypes[i].name);
return ("BOGUS");