xref: /illumos-gate/usr/src/lib/krb5/dyn/dyn_delete.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 DynDelete().
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 <string.h>
18 
19 #include "dynP.h"
20 
21 /*
22  * Checkers!  Get away from that "hard disk erase" button!
23  *    (Stupid dog.  He almost did it to me again ...)
24  */
25 int DynDelete(obj, idx)
26    DynObjectP obj;
27    int idx;
28 {
29      if (idx < 0) {
30 	  if (obj->debug)
31 	       fprintf(stderr, "dyn: delete: bad index %d\n", idx);
32 	  return DYN_BADINDEX;
33      }
34 
35      if (idx >= obj->num_el) {
36 	  if (obj->debug)
37 	       fprintf(stderr, "dyn: delete: Highest index is %d.\n",
38 		       obj->num_el);
39 	  return DYN_BADINDEX;
40      }
41 
42      if (idx == obj->num_el-1) {
43 	  if (obj->paranoid) {
44 	       if (obj->debug)
45 		    fprintf(stderr, "dyn: delete: last element, zeroing.\n");
46 	       memset(obj->array + idx*obj->el_size, 0, obj->el_size);
47 	  }
48 	  else {
49 	       if (obj->debug)
50 		    fprintf(stderr, "dyn: delete: last element, punting.\n");
51 	  }
52      }
53      else {
54 	  if (obj->debug)
55 	       fprintf(stderr,
56 		       "dyn: delete: copying %d bytes from %d + %d to + %d.\n",
57 		       obj->el_size*(obj->num_el - idx), obj->array,
58 		       (idx+1)*obj->el_size, idx*obj->el_size);
59 
60 #ifdef HAVE_MEMMOVE
61 	  memmove(obj->array + idx*obj->el_size,
62 		  obj->array + (idx+1)*obj->el_size,
63 		  obj->el_size*(obj->num_el - idx));
64 #else
65 	  bcopy(obj->array + (idx+1)*obj->el_size,
66 		  obj->array + idx*obj->el_size,
67 		  obj->el_size*(obj->num_el - idx));
68 #endif
69 	  if (obj->paranoid) {
70 	       if (obj->debug)
71 		    fprintf(stderr,
72 			    "dyn: delete: zeroing %d bytes from %d + %d\n",
73 			    obj->el_size, obj->array,
74 			    obj->el_size*(obj->num_el - 1));
75 	       memset(obj->array + obj->el_size*(obj->num_el - 1), 0,
76 		     obj->el_size);
77 	  }
78      }
79 
80      --obj->num_el;
81 
82      if (obj->debug)
83 	  fprintf(stderr, "dyn: delete: done.\n");
84 
85      return DYN_OK;
86 }
87