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