1*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gate /*
4*0Sstevel@tonic-gate * This file is part of libdyn.a, the C Dynamic Object library. It
5*0Sstevel@tonic-gate * contains the source code for the function DynDelete().
6*0Sstevel@tonic-gate *
7*0Sstevel@tonic-gate * There are no restrictions on this code; however, if you make any
8*0Sstevel@tonic-gate * changes, I request that you document them so that I do not get
9*0Sstevel@tonic-gate * credit or blame for your modifications.
10*0Sstevel@tonic-gate *
11*0Sstevel@tonic-gate * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
12*0Sstevel@tonic-gate * and MIT-Project Athena, 1989.
13*0Sstevel@tonic-gate */
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gate #include <stdio.h>
16*0Sstevel@tonic-gate #include <strings.h>
17*0Sstevel@tonic-gate #include <string.h>
18*0Sstevel@tonic-gate
19*0Sstevel@tonic-gate #include "dynP.h"
20*0Sstevel@tonic-gate
21*0Sstevel@tonic-gate /*
22*0Sstevel@tonic-gate * Checkers! Get away from that "hard disk erase" button!
23*0Sstevel@tonic-gate * (Stupid dog. He almost did it to me again ...)
24*0Sstevel@tonic-gate */
DynDelete(obj,idx)25*0Sstevel@tonic-gate int DynDelete(obj, idx)
26*0Sstevel@tonic-gate DynObjectP obj;
27*0Sstevel@tonic-gate int idx;
28*0Sstevel@tonic-gate {
29*0Sstevel@tonic-gate if (idx < 0) {
30*0Sstevel@tonic-gate if (obj->debug)
31*0Sstevel@tonic-gate fprintf(stderr, "dyn: delete: bad index %d\n", idx);
32*0Sstevel@tonic-gate return DYN_BADINDEX;
33*0Sstevel@tonic-gate }
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate if (idx >= obj->num_el) {
36*0Sstevel@tonic-gate if (obj->debug)
37*0Sstevel@tonic-gate fprintf(stderr, "dyn: delete: Highest index is %d.\n",
38*0Sstevel@tonic-gate obj->num_el);
39*0Sstevel@tonic-gate return DYN_BADINDEX;
40*0Sstevel@tonic-gate }
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate if (idx == obj->num_el-1) {
43*0Sstevel@tonic-gate if (obj->paranoid) {
44*0Sstevel@tonic-gate if (obj->debug)
45*0Sstevel@tonic-gate fprintf(stderr, "dyn: delete: last element, zeroing.\n");
46*0Sstevel@tonic-gate memset(obj->array + idx*obj->el_size, 0, obj->el_size);
47*0Sstevel@tonic-gate }
48*0Sstevel@tonic-gate else {
49*0Sstevel@tonic-gate if (obj->debug)
50*0Sstevel@tonic-gate fprintf(stderr, "dyn: delete: last element, punting.\n");
51*0Sstevel@tonic-gate }
52*0Sstevel@tonic-gate }
53*0Sstevel@tonic-gate else {
54*0Sstevel@tonic-gate if (obj->debug)
55*0Sstevel@tonic-gate fprintf(stderr,
56*0Sstevel@tonic-gate "dyn: delete: copying %d bytes from %d + %d to + %d.\n",
57*0Sstevel@tonic-gate obj->el_size*(obj->num_el - idx), obj->array,
58*0Sstevel@tonic-gate (idx+1)*obj->el_size, idx*obj->el_size);
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate #ifdef HAVE_MEMMOVE
61*0Sstevel@tonic-gate memmove(obj->array + idx*obj->el_size,
62*0Sstevel@tonic-gate obj->array + (idx+1)*obj->el_size,
63*0Sstevel@tonic-gate obj->el_size*(obj->num_el - idx));
64*0Sstevel@tonic-gate #else
65*0Sstevel@tonic-gate bcopy(obj->array + (idx+1)*obj->el_size,
66*0Sstevel@tonic-gate obj->array + idx*obj->el_size,
67*0Sstevel@tonic-gate obj->el_size*(obj->num_el - idx));
68*0Sstevel@tonic-gate #endif
69*0Sstevel@tonic-gate if (obj->paranoid) {
70*0Sstevel@tonic-gate if (obj->debug)
71*0Sstevel@tonic-gate fprintf(stderr,
72*0Sstevel@tonic-gate "dyn: delete: zeroing %d bytes from %d + %d\n",
73*0Sstevel@tonic-gate obj->el_size, obj->array,
74*0Sstevel@tonic-gate obj->el_size*(obj->num_el - 1));
75*0Sstevel@tonic-gate memset(obj->array + obj->el_size*(obj->num_el - 1), 0,
76*0Sstevel@tonic-gate obj->el_size);
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate --obj->num_el;
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate if (obj->debug)
83*0Sstevel@tonic-gate fprintf(stderr, "dyn: delete: done.\n");
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate return DYN_OK;
86*0Sstevel@tonic-gate }
87