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