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