xref: /illumos-gate/usr/src/cmd/lp/lib/forms/putform.c (revision 2a8bcb4e)
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 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
31 
32 #include "sys/types.h"
33 #include "sys/stat.h"
34 #include "stdio.h"
35 #include "string.h"
36 #include "errno.h"
37 #include "stdlib.h"
38 
39 #include "lp.h"
40 #include "form.h"
41 
42 /**
43  ** putform() - WRITE FORM STRUCTURE TO DISK FILES
44  **/
45 
46 int
putform(char * name,FORM * formp,FALERT * alertp,FILE ** p_align_fp)47 putform(char *name, FORM *formp, FALERT *alertp, FILE **p_align_fp)
48 {
49 	register char *		path;
50 
51 	int fd;
52 
53 	struct stat		statbuf;
54 
55 
56 	if (!name || !*name) {
57 		errno = EINVAL;
58 		return (-1);
59 	}
60 
61 	if (STREQU(NAME_ALL, name)) {
62 		errno = EINVAL;
63 		return (-1);
64 	}
65 
66 	/*
67 	 * Create the parent directory for this form
68 	 * if it doesn't yet exist.
69 	 */
70 	if (!(path = getformfile(name, (char *)0)))
71 		return (-1);
72 	if (Stat(path, &statbuf) == 0) {
73 		if (!S_ISDIR(statbuf.st_mode)) {
74 			Free (path);
75 			errno = ENOTDIR;
76 			return (-1);
77 		}
78 	} else if (errno != ENOENT || mkdir_lpdir(path, MODE_DIR) == -1) {
79 		Free (path);
80 		return (-1);
81 	}
82 	Free (path);
83 
84 	/*
85 	 * Open the configuration file and write out the form
86 	 * configuration (?)
87 	 */
88 	if (formp) {
89 		if (!(path = getformfile(name, DESCRIBE)))
90 			return (-1);
91 		if ((fd = open_locked(path, "w", MODE_READ)) < 0) {
92 			Free (path);
93 			return (-1);
94 		}
95 		Free (path);
96 
97 		if (wrform(name, formp, fd, 0, (int *)0) == -1) {
98 			close(fd);
99 			return (-1);
100 		}
101 		close(fd);
102 	}
103 
104 	/*
105 	 * Write out the alert condition (?)
106 	 */
107 	if (alertp) {
108 		if (
109 			alertp->shcmd
110 		     && putalert(Lp_A_Forms, name, alertp) == -1
111 		)
112 			return (-1);
113 	}
114 
115 	/*
116 	 * Write out the alignment pattern (?)
117 	 */
118 	if (p_align_fp && *p_align_fp) {
119 
120 		int			size	= 0,
121 					n;
122 
123 		char			buf[BUFSIZ];
124 
125 
126 		if (!(path = getformfile(name, ALIGN_PTRN)))
127 			return (-1);
128 		if ((fd = open_locked(path, "w", MODE_READ)) < 0) {
129 			Free (path);
130 			return (-1);
131 		}
132 
133 		while ((n = fread(buf, 1, BUFSIZ, *p_align_fp)) != 0) {
134 			size += n;
135 			write (fd, buf, n);
136 		}
137 		close(fd);
138 
139 		if (!size)
140 			Unlink(path);
141 
142 		Free(path);
143 	}
144 
145 	return (0);
146 }
147