#include "sys/types.h"
#include "sys/stat.h"
#include "stdio.h"
#include "string.h"
#include "errno.h"
#include "stdlib.h"
#include "lp.h"
#include "form.h"
int
putform(char *name, FORM *formp, FALERT *alertp, FILE **p_align_fp)
{
register char * path;
int fd;
struct stat statbuf;
if (!name || !*name) {
errno = EINVAL;
return (-1);
}
if (STREQU(NAME_ALL, name)) {
errno = EINVAL;
return (-1);
}
if (!(path = getformfile(name, (char *)0)))
return (-1);
if (Stat(path, &statbuf) == 0) {
if (!S_ISDIR(statbuf.st_mode)) {
Free (path);
errno = ENOTDIR;
return (-1);
}
} else if (errno != ENOENT || mkdir_lpdir(path, MODE_DIR) == -1) {
Free (path);
return (-1);
}
Free (path);
if (formp) {
if (!(path = getformfile(name, DESCRIBE)))
return (-1);
if ((fd = open_locked(path, "w", MODE_READ)) < 0) {
Free (path);
return (-1);
}
Free (path);
if (wrform(name, formp, fd, 0, (int *)0) == -1) {
close(fd);
return (-1);
}
close(fd);
}
if (alertp) {
if (
alertp->shcmd
&& putalert(Lp_A_Forms, name, alertp) == -1
)
return (-1);
}
if (p_align_fp && *p_align_fp) {
int size = 0,
n;
char buf[BUFSIZ];
if (!(path = getformfile(name, ALIGN_PTRN)))
return (-1);
if ((fd = open_locked(path, "w", MODE_READ)) < 0) {
Free (path);
return (-1);
}
while ((n = fread(buf, 1, BUFSIZ, *p_align_fp)) != 0) {
size += n;
write (fd, buf, n);
}
close(fd);
if (!size)
Unlink(path);
Free(path);
}
return (0);
}