xref: /illumos-gate/usr/src/cmd/lp/lib/forms/putform.c (revision 0173c38a73f34277e0c97a19fedfd25d81ba8380)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.10	*/
32 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33 
34 #include "sys/types.h"
35 #include "sys/stat.h"
36 #include "stdio.h"
37 #include "string.h"
38 #include "errno.h"
39 #include "stdlib.h"
40 
41 #include "lp.h"
42 #include "form.h"
43 
44 /**
45  ** putform() - WRITE FORM STRUCTURE TO DISK FILES
46  **/
47 
48 int
49 putform(char *name, FORM *formp, FALERT *alertp, FILE **p_align_fp)
50 {
51 	register char *		path;
52 
53 	int fd;
54 
55 	struct stat		statbuf;
56 
57 
58 	if (!name || !*name) {
59 		errno = EINVAL;
60 		return (-1);
61 	}
62 
63 	if (STREQU(NAME_ALL, name)) {
64 		errno = EINVAL;
65 		return (-1);
66 	}
67 
68 	/*
69 	 * Create the parent directory for this form
70 	 * if it doesn't yet exist.
71 	 */
72 	if (!(path = getformfile(name, (char *)0)))
73 		return (-1);
74 	if (Stat(path, &statbuf) == 0) {
75 		if (!S_ISDIR(statbuf.st_mode)) {
76 			Free (path);
77 			errno = ENOTDIR;
78 			return (-1);
79 		}
80 	} else if (errno != ENOENT || mkdir_lpdir(path, MODE_DIR) == -1) {
81 		Free (path);
82 		return (-1);
83 	}
84 	Free (path);
85 
86 	/*
87 	 * Open the configuration file and write out the form
88 	 * configuration (?)
89 	 */
90 	if (formp) {
91 		if (!(path = getformfile(name, DESCRIBE)))
92 			return (-1);
93 		if ((fd = open_locked(path, "w", MODE_READ)) < 0) {
94 			Free (path);
95 			return (-1);
96 		}
97 		Free (path);
98 
99 		if (wrform(name, formp, fd, 0, (int *)0) == -1) {
100 			close(fd);
101 			return (-1);
102 		}
103 		close(fd);
104 	}
105 
106 	/*
107 	 * Write out the alert condition (?)
108 	 */
109 	if (alertp) {
110 		if (
111 			alertp->shcmd
112 		     && putalert(Lp_A_Forms, name, alertp) == -1
113 		)
114 			return (-1);
115 	}
116 
117 	/*
118 	 * Write out the alignment pattern (?)
119 	 */
120 	if (p_align_fp && *p_align_fp) {
121 
122 		int			size	= 0,
123 					n;
124 
125 		char			buf[BUFSIZ];
126 
127 
128 		if (!(path = getformfile(name, ALIGN_PTRN)))
129 			return (-1);
130 		if ((fd = open_locked(path, "w", MODE_READ)) < 0) {
131 			Free (path);
132 			return (-1);
133 		}
134 
135 		while ((n = fread(buf, 1, BUFSIZ, *p_align_fp)) != 0) {
136 			size += n;
137 			write (fd, buf, n);
138 		}
139 		close(fd);
140 
141 		if (!size)
142 			Unlink(path);
143 
144 		Free(path);
145 	}
146 
147 	return (0);
148 }
149