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