xref: /openbsd-src/lib/libform/fld_dup.c (revision c7ef0cfc17afcba97172c25e1e3a943e893bc632)
1*c7ef0cfcSnicm /*	$OpenBSD: fld_dup.c,v 1.7 2023/10/17 09:52:10 nicm Exp $	*/
202f2426aSmillert /****************************************************************************
3*c7ef0cfcSnicm  * Copyright 2020 Thomas E. Dickey                                          *
4*c7ef0cfcSnicm  * Copyright 1998-2010,2012 Free Software Foundation, Inc.                  *
502f2426aSmillert  *                                                                          *
602f2426aSmillert  * Permission is hereby granted, free of charge, to any person obtaining a  *
702f2426aSmillert  * copy of this software and associated documentation files (the            *
802f2426aSmillert  * "Software"), to deal in the Software without restriction, including      *
902f2426aSmillert  * without limitation the rights to use, copy, modify, merge, publish,      *
1002f2426aSmillert  * distribute, distribute with modifications, sublicense, and/or sell       *
1102f2426aSmillert  * copies of the Software, and to permit persons to whom the Software is    *
1202f2426aSmillert  * furnished to do so, subject to the following conditions:                 *
1302f2426aSmillert  *                                                                          *
1402f2426aSmillert  * The above copyright notice and this permission notice shall be included  *
1502f2426aSmillert  * in all copies or substantial portions of the Software.                   *
1602f2426aSmillert  *                                                                          *
1702f2426aSmillert  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1802f2426aSmillert  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1902f2426aSmillert  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
2002f2426aSmillert  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
2102f2426aSmillert  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2202f2426aSmillert  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2302f2426aSmillert  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2402f2426aSmillert  *                                                                          *
2502f2426aSmillert  * Except as contained in this notice, the name(s) of the above copyright   *
2602f2426aSmillert  * holders shall not be used in advertising or otherwise to promote the     *
2702f2426aSmillert  * sale, use or other dealings in this Software without prior written       *
2802f2426aSmillert  * authorization.                                                           *
2902f2426aSmillert  ****************************************************************************/
3002f2426aSmillert 
3102f2426aSmillert /****************************************************************************
3281d8c4e1Snicm  *   Author:  Juergen Pfeifer, 1995,1997                                    *
3302f2426aSmillert  ****************************************************************************/
348d0fca71Smillert 
358d0fca71Smillert #include "form.priv.h"
368d0fca71Smillert 
37*c7ef0cfcSnicm MODULE_ID("$Id: fld_dup.c,v 1.7 2023/10/17 09:52:10 nicm Exp $")
388d0fca71Smillert 
398d0fca71Smillert /*---------------------------------------------------------------------------
408d0fca71Smillert |   Facility      :  libnform
418d0fca71Smillert |   Function      :  FIELD *dup_field(FIELD *field, int frow, int fcol)
428d0fca71Smillert |
438d0fca71Smillert |   Description   :  Duplicates the field at the specified position. All
448d0fca71Smillert |                    field attributes and the buffers are copied.
458d0fca71Smillert |                    If an error occurs, errno is set to
468d0fca71Smillert |
478d0fca71Smillert |                    E_BAD_ARGUMENT - invalid argument
488d0fca71Smillert |                    E_SYSTEM_ERROR - system error
498d0fca71Smillert |
508d0fca71Smillert |   Return Values :  Pointer to the new field or NULL if failure
518d0fca71Smillert +--------------------------------------------------------------------------*/
FORM_EXPORT(FIELD *)52*c7ef0cfcSnicm FORM_EXPORT(FIELD *)
5384af20ceSmillert dup_field(FIELD *field, int frow, int fcol)
548d0fca71Smillert {
558d0fca71Smillert   FIELD *New_Field = (FIELD *)0;
568d0fca71Smillert   int err = E_BAD_ARGUMENT;
578d0fca71Smillert 
58*c7ef0cfcSnicm   T((T_CALLED("dup_field(%p,%d,%d)"), (void *)field, frow, fcol));
598d0fca71Smillert   if (field && (frow >= 0) && (fcol >= 0) &&
608d0fca71Smillert       ((err = E_SYSTEM_ERROR) != 0) &&	/* trick : this resets the default error */
6181d8c4e1Snicm       (New_Field = typeMalloc(FIELD, 1)))
628d0fca71Smillert     {
63*c7ef0cfcSnicm       T((T_CREATE("field %p"), (void *)New_Field));
648d0fca71Smillert       *New_Field = *_nc_Default_Field;
65*c7ef0cfcSnicm       New_Field->frow = (short)frow;
66*c7ef0cfcSnicm       New_Field->fcol = (short)fcol;
678d0fca71Smillert       New_Field->link = New_Field;
688d0fca71Smillert       New_Field->rows = field->rows;
698d0fca71Smillert       New_Field->cols = field->cols;
708d0fca71Smillert       New_Field->nrow = field->nrow;
718d0fca71Smillert       New_Field->drows = field->drows;
728d0fca71Smillert       New_Field->dcols = field->dcols;
738d0fca71Smillert       New_Field->maxgrow = field->maxgrow;
748d0fca71Smillert       New_Field->nbuf = field->nbuf;
758d0fca71Smillert       New_Field->just = field->just;
768d0fca71Smillert       New_Field->fore = field->fore;
778d0fca71Smillert       New_Field->back = field->back;
788d0fca71Smillert       New_Field->pad = field->pad;
798d0fca71Smillert       New_Field->opts = field->opts;
808d0fca71Smillert       New_Field->usrptr = field->usrptr;
818d0fca71Smillert 
828d0fca71Smillert       if (_nc_Copy_Type(New_Field, field))
838d0fca71Smillert 	{
84*c7ef0cfcSnicm 	  size_t len;
858d0fca71Smillert 
868d0fca71Smillert 	  len = Total_Buffer_Size(New_Field);
87*c7ef0cfcSnicm 	  if ((New_Field->buf = (FIELD_CELL *)malloc(len * 20)))
888d0fca71Smillert 	    {
89*c7ef0cfcSnicm 	      memcpy(New_Field->buf, field->buf, len);
9081d8c4e1Snicm 	      returnField(New_Field);
918d0fca71Smillert 	    }
928d0fca71Smillert 	}
938d0fca71Smillert     }
948d0fca71Smillert 
958d0fca71Smillert   if (New_Field)
968d0fca71Smillert     free_field(New_Field);
978d0fca71Smillert 
988d0fca71Smillert   SET_ERROR(err);
9981d8c4e1Snicm   returnField((FIELD *)0);
1008d0fca71Smillert }
1018d0fca71Smillert 
1028d0fca71Smillert /* fld_dup.c ends here */
103