xref: /onnv-gate/usr/src/cmd/lp/lib/forms/putform.c (revision 871:f917b2ed7d21)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*871Scasper  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.10	*/
320Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "sys/types.h"
350Sstevel@tonic-gate #include "sys/stat.h"
360Sstevel@tonic-gate #include "stdio.h"
370Sstevel@tonic-gate #include "string.h"
380Sstevel@tonic-gate #include "errno.h"
390Sstevel@tonic-gate #include "stdlib.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include "lp.h"
420Sstevel@tonic-gate #include "form.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /**
450Sstevel@tonic-gate  ** putform() - WRITE FORM STRUCTURE TO DISK FILES
460Sstevel@tonic-gate  **/
470Sstevel@tonic-gate 
480Sstevel@tonic-gate int
putform(char * name,FORM * formp,FALERT * alertp,FILE ** p_align_fp)490Sstevel@tonic-gate putform(char *name, FORM *formp, FALERT *alertp, FILE **p_align_fp)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	register char *		path;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	int fd;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	struct stat		statbuf;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	if (!name || !*name) {
590Sstevel@tonic-gate 		errno = EINVAL;
600Sstevel@tonic-gate 		return (-1);
610Sstevel@tonic-gate 	}
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	if (STREQU(NAME_ALL, name)) {
640Sstevel@tonic-gate 		errno = EINVAL;
650Sstevel@tonic-gate 		return (-1);
660Sstevel@tonic-gate 	}
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	/*
690Sstevel@tonic-gate 	 * Create the parent directory for this form
700Sstevel@tonic-gate 	 * if it doesn't yet exist.
710Sstevel@tonic-gate 	 */
720Sstevel@tonic-gate 	if (!(path = getformfile(name, (char *)0)))
730Sstevel@tonic-gate 		return (-1);
740Sstevel@tonic-gate 	if (Stat(path, &statbuf) == 0) {
75*871Scasper 		if (!S_ISDIR(statbuf.st_mode)) {
760Sstevel@tonic-gate 			Free (path);
770Sstevel@tonic-gate 			errno = ENOTDIR;
780Sstevel@tonic-gate 			return (-1);
790Sstevel@tonic-gate 		}
800Sstevel@tonic-gate 	} else if (errno != ENOENT || mkdir_lpdir(path, MODE_DIR) == -1) {
810Sstevel@tonic-gate 		Free (path);
820Sstevel@tonic-gate 		return (-1);
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 	Free (path);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/*
870Sstevel@tonic-gate 	 * Open the configuration file and write out the form
880Sstevel@tonic-gate 	 * configuration (?)
890Sstevel@tonic-gate 	 */
900Sstevel@tonic-gate 	if (formp) {
910Sstevel@tonic-gate 		if (!(path = getformfile(name, DESCRIBE)))
920Sstevel@tonic-gate 			return (-1);
930Sstevel@tonic-gate 		if ((fd = open_locked(path, "w", MODE_READ)) < 0) {
940Sstevel@tonic-gate 			Free (path);
950Sstevel@tonic-gate 			return (-1);
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 		Free (path);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 		if (wrform(name, formp, fd, 0, (int *)0) == -1) {
1000Sstevel@tonic-gate 			close(fd);
1010Sstevel@tonic-gate 			return (-1);
1020Sstevel@tonic-gate 		}
1030Sstevel@tonic-gate 		close(fd);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	/*
1070Sstevel@tonic-gate 	 * Write out the alert condition (?)
1080Sstevel@tonic-gate 	 */
1090Sstevel@tonic-gate 	if (alertp) {
1100Sstevel@tonic-gate 		if (
1110Sstevel@tonic-gate 			alertp->shcmd
1120Sstevel@tonic-gate 		     && putalert(Lp_A_Forms, name, alertp) == -1
1130Sstevel@tonic-gate 		)
1140Sstevel@tonic-gate 			return (-1);
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	/*
1180Sstevel@tonic-gate 	 * Write out the alignment pattern (?)
1190Sstevel@tonic-gate 	 */
1200Sstevel@tonic-gate 	if (p_align_fp && *p_align_fp) {
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 		int			size	= 0,
1230Sstevel@tonic-gate 					n;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 		char			buf[BUFSIZ];
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 		if (!(path = getformfile(name, ALIGN_PTRN)))
1290Sstevel@tonic-gate 			return (-1);
1300Sstevel@tonic-gate 		if ((fd = open_locked(path, "w", MODE_READ)) < 0) {
1310Sstevel@tonic-gate 			Free (path);
1320Sstevel@tonic-gate 			return (-1);
1330Sstevel@tonic-gate 		}
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 		while ((n = fread(buf, 1, BUFSIZ, *p_align_fp)) != 0) {
1360Sstevel@tonic-gate 			size += n;
1370Sstevel@tonic-gate 			write (fd, buf, n);
1380Sstevel@tonic-gate 		}
1390Sstevel@tonic-gate 		close(fd);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 		if (!size)
1420Sstevel@tonic-gate 			Unlink(path);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 		Free(path);
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	return (0);
1480Sstevel@tonic-gate }
149