/* reads a line from the input into lnbuf
* if there is no error, it returns
* the number of characters in the buffer
* if there is an error and there where characters
* read, it returns the negative value of the
* number of characters read
* if there is an error and no characters were read,
* it returns the negative value of 1 greater than
* the size of the line buffer
*/
int
readline(int inpfd)
{
unsigned char *ap;
int i, rv;
ap = lnbuf;
lnbuf[0] = '\0';
i = 0;
alarm(60);
do {
rv = read(inpfd, ap, 1);
} while (rv==1 && ++i && *ap != '\n' && ap++ && (i < LNBFSZ - 2));
alarm(0);
if (i != 0 && *ap != '\n') {
*++ap = '\n';
i++;
}
*++ap = '\0';
if (rv < 0) {
error("read error; lost connection\n");
if (i==0) i = -(LNBFSZ+1);
else i = -i;
}
return(i);
}
int
getfiles(void)
{
unsigned char *ap;
int filecnt, bsize, rv;
filecnt = 0;
/* get a line, hopefully containing a ctrl char, size, and name */
for(;;) {
ap = lnbuf;
if ((rv=readline(0)) < 0) NAK();
if (rv <= 0) {
return(filecnt);
}
switch(*ap++) {
case '\1': /* cleanup - data sent was bad (whatever that means) */
break;
case '\2': /* read control file */
bsize = atoi((const char *)ap);
cntrlfd = tempfile();
if (readfile(cntrlfd, bsize) < 0) {
close(cntrlfd);
NAK();
return(0);
}
break;
case '\3': /* read data file */
bsize = atoi((const char *)ap);
datafd[filecnt] = tempfile();
if (readfile(datafd[filecnt], bsize) < 0) {
close(datafd[filecnt]);
NAK();
return(0);
}
filecnt++;
break;
default:
error("protocol error <%d>\n", *(ap-1));
NAK();
}
}
return(filecnt);
}
if (fd < 0) error("getjobinfo: bad file descriptor\n");
if (lseek(fd, 0L, 0) < 0) {
error("error seeking in temp file\n");
exit(7);
}
/* the following strings should be < NAMELEN or else they will not
* be null terminated.
*/
strncpy(info.user, "daemon", NAMELEN);
strncpy(info.host, "nowhere", NAMELEN);
/* there may be a space after the name and host. It will be filtered out
* by CPYFIELD.
*/
while ((rv=readline(fd)) > 0) {
ap = lnbuf;
ap[rv-1] = '\0'; /* remove newline from string */
switch (*ap) {
case 'H':
if (ap[1] == '\0')
strncpy(info.host, "unknown", NAMELEN);
else
strncpy(info.host, (const char *)&ap[1], NAMELEN);
info.host[NAMELEN] = '\0';
break;
case 'P':
if (ap[1] == '\0')
strncpy(info.user, "unknown", NAMELEN);
else
strncpy(info.user, (const char *)&ap[1], NAMELEN);
info.user[NAMELEN] = '\0';
break;
}
}
return(&info);
}