xref: /illumos-gate/usr/src/lib/krb5/dyn/dyn_put.c (revision 51396a8ee7fb52fe0ab33bfe7b4f495ad431904a)
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 functions DynGet() and DynAdd().
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 
18 #include "dynP.h"
19 
20 DynPtr DynArray(obj)
21    DynObjectP obj;
22 {
23      if (obj->debug)
24 	  fprintf(stderr, "dyn: array: returning array pointer %d.\n",
25 		  obj->array);
26 
27      return obj->array;
28 }
29 
30 DynPtr DynGet(obj, num)
31    DynObjectP obj;
32    int num;
33 {
34      if (num < 0) {
35 	  if (obj->debug)
36 	       fprintf(stderr, "dyn: get: bad index %d\n", num);
37 	  return NULL;
38      }
39 
40      if (num >= obj->num_el) {
41 	  if (obj->debug)
42 	       fprintf(stderr, "dyn: get: highest element is %d.\n",
43 		       obj->num_el);
44 	  return NULL;
45      }
46 
47      if (obj->debug)
48 	  fprintf(stderr, "dyn: get: Returning address %d + %d.\n",
49 		  obj->array, obj->el_size*num);
50 
51      return (DynPtr) obj->array + obj->el_size*num;
52 }
53 
54 int DynAdd(obj, el)
55    DynObjectP obj;
56    void *el;
57 {
58      int	ret;
59 
60      ret = DynPut(obj, el, obj->num_el);
61      if (ret != DYN_OK)
62 	  return ret;
63 
64      ++obj->num_el;
65      return ret;
66 }
67 
68 /*
69  * WARNING!  There is a reason this function is not documented in the
70  * man page.  If DynPut used to mutate already existing elements,
71  * everything will go fine.  If it is used to add new elements
72  * directly, however, the state within the object (such as
73  * obj->num_el) will not be updated properly and many other functions
74  * in the library will lose.  Have a nice day.
75  */
76 int DynPut(obj, el_in, idx)
77    DynObjectP obj;
78    void *el_in;
79    int idx;
80 {
81      DynPtr el = (DynPtr) el_in;
82      int ret;
83 
84      if (obj->debug)
85 	  fprintf(stderr, "dyn: put: Writing %d bytes from %d to %d + %d\n",
86 		  obj->el_size, el, obj->array, idx*obj->el_size);
87 
88      if ((ret = _DynResize(obj, idx)) != DYN_OK)
89 	  return ret;
90 
91 #ifdef HAVE_MEMMOVE
92      memmove(obj->array + idx*obj->el_size, el, obj->el_size);
93 #else
94      bcopy(el, obj->array + idx*obj->el_size, obj->el_size);
95 #endif
96 
97      if (obj->debug)
98 	  fprintf(stderr, "dyn: put: done.\n");
99 
100      return DYN_OK;
101 }
102