home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.ee.lbl.gov
/
2014.05.ftp.ee.lbl.gov.tar
/
ftp.ee.lbl.gov
/
acld-1.11.tar.gz
/
acld-1.11.tar
/
acld-1.11
/
io.c
< prev
next >
Wrap
C/C++ Source or Header
|
2010-09-16
|
5KB
|
187 lines
/*
* Copyright (c) 2004, 2006, 2008, 2009, 2010
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the University of California,
* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
* the University nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef lint
static const char rcsid[] =
"@(#) $Id: io.c 692 2010-09-16 05:17:44Z leres $ (LBL)";
#endif
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "acld.h"
/* Locals */
static const char crnl[] = "\r\n";
/* Appends formatted and cr/nl to iobuf; assures EOS termination */
void
ioappendfmt(struct iobuf *ip, const char *fmt, ...)
{
va_list ap;
/* Make sure we have room for current, new and EOS */
DYNARRAY(ip->buf, ip->len + strlen(fmt) + sizeof(crnl),
&ip->size, sizeof(*ip->buf), 128, 1024, "ioappendfmt");
va_start(ap, fmt);
ip->len += vsnprintf(ip->buf + ip->len, ip->size - ip->len, fmt, ap);
va_end(ap);
strcpy(ip->buf + ip->len, crnl);
ip->len += sizeof(crnl) - 1;
}
/* Appends string and cr/nl to iobuf; assures EOS termination */
void
ioappendline(struct iobuf *ip, const char *buf, size_t len)
{
/* Make sure we have room for current, new and EOS */
DYNARRAY(ip->buf, ip->len + len + sizeof(crnl),
&ip->size, sizeof(*ip->buf), 128, 1024, "ioappendline");
strcpy(ip->buf + ip->len, buf);
ip->len += len;
strcpy(ip->buf + ip->len, crnl);
ip->len += sizeof(crnl) - 1;
}
/* Appends formatted and cr/nl to iobuf; assures EOS termination */
void
ioappendvfmt(struct iobuf *ip, const char *fmt, va_list ap)
{
/* Make sure we have room for current, new and EOS */
DYNARRAY(ip->buf, ip->len + strlen(fmt) + sizeof(crnl),
&ip->size, sizeof(*ip->buf), 128, 1024, "ioappendfmt");
ip->len += vsnprintf(ip->buf + ip->len, ip->size - ip->len, fmt, ap);
strcpy(ip->buf + ip->len, crnl);
ip->len += sizeof(crnl) - 1;
}
void
iofree(struct iobuf *ip)
{
/* If size is zero, buf wasn't dynamically allocated */
if (ip->buf != NULL && ip->size > 0) {
free(ip->buf);
ip->buf = NULL;
ip->size = 0;
}
ip->len = 0;
}
/* Return a line (newline terminated) or partial line (zero terminated) */
char *
iogetstr(struct iobuf *ip)
{
char *cp;
size_t size;
static char *buf = NULL;
if (buf != NULL) {
free(buf);
buf = NULL;
}
if (ip->len == 0)
return (NULL);
for (cp = ip->buf; *cp != '\n' && *cp != '\0'; ++cp)
continue;
size = cp - ip->buf + 1;
buf = new(size + 1, sizeof(*buf), "iogetstr buf");
strncpy(buf, ip->buf, size);
buf[size] = '\0';
if (*cp == '\0')
ip->len = 0;
else {
memmove(ip->buf, ip->buf + size, ip->len - size + 1);
ip->len -= size;
}
ip->buf[ip->len] = '\0';
return (buf);
}
/* Return true if there's a newline in the buffer */
int
iohaveline(struct iobuf *ip)
{
int n;
char *cp;
for (n = ip->len, cp = ip->buf; n > 0; --n, ++cp)
if (*cp == '\n')
return (1);
return (0);
}
/* Read from a socket and to an iobuf */
int
ioread(int fd, struct iobuf *ip)
{
ssize_t cc;
DYNARRAY(ip->buf, ip->len, &ip->size,
sizeof(*ip->buf), 8192, 8192, "ioread buf");
cc = read(fd, ip->buf + ip->len, ip->size - ip->len - 1);
if (cc < 0)
return (cc);
ip->len += cc;
ip->buf[ip->len] = '\0';
return (ip->len);
}
/* Write from an iobuf to a socket */
int
iowrite(int fd, struct iobuf *ip)
{
ssize_t cc;
cc = write(fd, ip->buf, ip->len);
if (cc < 0) {
/* Bummer */
ip->len = 0;
return (cc);
}
if (cc > ip->len) {
lg(LOG_ERR, "iowrite: cc > ip-len, %d > %d (Can't happen?)\n",
(int)cc, (int)ip->len);
cc = ip->len;
}
if (cc == ip->len) {
ip->len = 0;
return (cc);
}
/* Move leftover chars on over */
memmove(ip->buf, ip->buf + cc, ip->len - cc);
ip->len -= cc;
return (cc);
}