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