xref: /illumos-gate/usr/src/lib/krb5/dyn/dyn_insert.c (revision 7c478bd9)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 
3 /*
4  * This file is part of libdyn.a, the C Dynamic Object library.  It
5  * contains the source code for the function DynInsert().
6  *
7  * There are no restrictions on this code; however, if you make any
8  * changes, I request that you document them so that I do not get
9  * credit or blame for your modifications.
10  *
11  * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
12  * and MIT-Project Athena, 1989.
13  */
14 
15 #include <stdio.h>
16 #include <strings.h>
17 #include "dynP.h"
18 
DynInsert(obj,idx,els_in,num)19 int DynInsert(obj, idx, els_in, num)
20    DynObjectP obj;
21    void *els_in;
22    int idx, num;
23 {
24      DynPtr els = (DynPtr) els_in;
25      int ret;
26 
27      if (idx < 0 || idx > obj->num_el) {
28 	  if (obj->debug)
29 	       fprintf(stderr, "dyn: insert: index %d is not in [0,%d]\n",
30 		       idx, obj->num_el);
31 	  return DYN_BADINDEX;
32      }
33 
34      if (num < 1) {
35 	  if (obj->debug)
36 	       fprintf(stderr, "dyn: insert: cannot insert %d elements\n",
37 		       num);
38 	  return DYN_BADVALUE;
39      }
40 
41      if (obj->debug)
42 	  fprintf(stderr,"dyn: insert: Moving %d bytes from %d + %d to + %d\n",
43 		  (obj->num_el-idx)*obj->el_size, obj->array,
44 		  obj->el_size*idx, obj->el_size*(idx+num));
45 
46      if ((ret = _DynResize(obj, obj->num_el + num)) != DYN_OK)
47 	  return ret;
48 #ifdef HAVE_MEMMOVE
49      memmove(obj->array + obj->el_size*(idx + num),
50 	     obj->array + obj->el_size*idx,
51 	     (obj->num_el-idx)*obj->el_size);
52 #else
53      bcopy(obj->array + obj->el_size*idx,
54 	   obj->array + obj->el_size*(idx + num),
55 	   (obj->num_el-idx)*obj->el_size);
56 #endif
57 
58      if (obj->debug)
59 	  fprintf(stderr, "dyn: insert: Copying %d bytes from %d to %d + %d\n",
60 		  obj->el_size*num, els, obj->array, obj->el_size*idx);
61 
62 #ifdef HAVE_MEMMOVE
63      memmove(obj->array + obj->el_size*idx, els, obj->el_size*num);
64 #else
65      bcopy(els, obj->array + obj->el_size*idx, obj->el_size*num);
66 #endif
67      obj->num_el += num;
68 
69      if (obj->debug)
70 	  fprintf(stderr, "dyn: insert: done.\n");
71 
72      return DYN_OK;
73 }
74