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