xref: /illumos-gate/usr/src/lib/libeti/form/common/field_buf.c (revision dd51520e127b452179a2ce4ea3bd8dee949f9afe)
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 /*	Copyright (c) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  *      Copyright (c) 1997, by Sun Microsystems, Inc.
28  *      All rights reserved.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI" /* SVr4.0 1.4 */
32 
33 /*LINTLIBRARY*/
34 
35 #include <sys/types.h>
36 #include "utility.h"
37 
38 int
39 set_field_buffer(FIELD *f, int n, char *v)
40 {
41 	char *p;
42 	char *x;
43 	size_t s;
44 	int err = 0;
45 	int	len;
46 	int	size;
47 
48 	if (!f || !v || n < 0 || n > f->nbuf)
49 		return (E_BAD_ARGUMENT);
50 
51 	len = (int)  strlen(v);
52 	size = BufSize(f);
53 
54 	if (Status(f, GROWABLE) && len > size)
55 		if (!_grow_field(f, (len - size - 1)/GrowSize(f) + 1))
56 			return (E_SYSTEM_ERROR);
57 
58 	x = Buffer(f, n);
59 	s = BufSize(f);
60 	p = memccpy(x, v, '\0', s);
61 
62 	if (p)
63 		(void) memset(p - 1, ' ', (size_t) (s - (p - x) + 1));
64 
65 	if (n == 0) {
66 		if (_sync_field(f) != E_OK)
67 			++err;
68 		if (_sync_linked(f) != E_OK)
69 			++err;
70 	}
71 	return (err ? E_SYSTEM_ERROR : E_OK);
72 }
73 
74 char *
75 field_buffer(FIELD *f, int n)
76 {
77 /*
78  * field_buffer may not be accurate on the current field unless
79  * called from within the check validation function or the
80  * form/field init/term functions.
81  * field_buffer is always accurate on validated fields.
82  */
83 
84 	if (f && n >= 0 && n <= f -> nbuf)
85 		return (Buffer(f, n));
86 	else
87 		return ((char *) 0);
88 }
89