xref: /illumos-gate/usr/src/lib/krb5/dyn/dyn_delete.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * This file is part of libdyn.a, the C Dynamic Object library.  It
37c478bd9Sstevel@tonic-gate  * contains the source code for the function DynDelete().
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * There are no restrictions on this code; however, if you make any
67c478bd9Sstevel@tonic-gate  * changes, I request that you document them so that I do not get
77c478bd9Sstevel@tonic-gate  * credit or blame for your modifications.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
107c478bd9Sstevel@tonic-gate  * and MIT-Project Athena, 1989.
11*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #include <stdio.h>
157c478bd9Sstevel@tonic-gate #include <strings.h>
167c478bd9Sstevel@tonic-gate #include <string.h>
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include "dynP.h"
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate /*
217c478bd9Sstevel@tonic-gate  * Checkers!  Get away from that "hard disk erase" button!
227c478bd9Sstevel@tonic-gate  *    (Stupid dog.  He almost did it to me again ...)
23*48bbca81SDaniel Hoffman  */
DynDelete(obj,idx)247c478bd9Sstevel@tonic-gate int DynDelete(obj, idx)
257c478bd9Sstevel@tonic-gate    DynObjectP obj;
267c478bd9Sstevel@tonic-gate    int idx;
277c478bd9Sstevel@tonic-gate {
287c478bd9Sstevel@tonic-gate      if (idx < 0) {
297c478bd9Sstevel@tonic-gate 	  if (obj->debug)
307c478bd9Sstevel@tonic-gate 	       fprintf(stderr, "dyn: delete: bad index %d\n", idx);
317c478bd9Sstevel@tonic-gate 	  return DYN_BADINDEX;
327c478bd9Sstevel@tonic-gate      }
33*48bbca81SDaniel Hoffman 
347c478bd9Sstevel@tonic-gate      if (idx >= obj->num_el) {
357c478bd9Sstevel@tonic-gate 	  if (obj->debug)
367c478bd9Sstevel@tonic-gate 	       fprintf(stderr, "dyn: delete: Highest index is %d.\n",
377c478bd9Sstevel@tonic-gate 		       obj->num_el);
387c478bd9Sstevel@tonic-gate 	  return DYN_BADINDEX;
397c478bd9Sstevel@tonic-gate      }
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate      if (idx == obj->num_el-1) {
427c478bd9Sstevel@tonic-gate 	  if (obj->paranoid) {
437c478bd9Sstevel@tonic-gate 	       if (obj->debug)
447c478bd9Sstevel@tonic-gate 		    fprintf(stderr, "dyn: delete: last element, zeroing.\n");
457c478bd9Sstevel@tonic-gate 	       memset(obj->array + idx*obj->el_size, 0, obj->el_size);
467c478bd9Sstevel@tonic-gate 	  }
477c478bd9Sstevel@tonic-gate 	  else {
487c478bd9Sstevel@tonic-gate 	       if (obj->debug)
497c478bd9Sstevel@tonic-gate 		    fprintf(stderr, "dyn: delete: last element, punting.\n");
507c478bd9Sstevel@tonic-gate 	  }
51*48bbca81SDaniel Hoffman      }
527c478bd9Sstevel@tonic-gate      else {
537c478bd9Sstevel@tonic-gate 	  if (obj->debug)
547c478bd9Sstevel@tonic-gate 	       fprintf(stderr,
557c478bd9Sstevel@tonic-gate 		       "dyn: delete: copying %d bytes from %d + %d to + %d.\n",
567c478bd9Sstevel@tonic-gate 		       obj->el_size*(obj->num_el - idx), obj->array,
577c478bd9Sstevel@tonic-gate 		       (idx+1)*obj->el_size, idx*obj->el_size);
58*48bbca81SDaniel Hoffman 
597c478bd9Sstevel@tonic-gate #ifdef HAVE_MEMMOVE
607c478bd9Sstevel@tonic-gate 	  memmove(obj->array + idx*obj->el_size,
617c478bd9Sstevel@tonic-gate 		  obj->array + (idx+1)*obj->el_size,
627c478bd9Sstevel@tonic-gate 		  obj->el_size*(obj->num_el - idx));
637c478bd9Sstevel@tonic-gate #else
647c478bd9Sstevel@tonic-gate 	  bcopy(obj->array + (idx+1)*obj->el_size,
657c478bd9Sstevel@tonic-gate 		  obj->array + idx*obj->el_size,
667c478bd9Sstevel@tonic-gate 		  obj->el_size*(obj->num_el - idx));
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate 	  if (obj->paranoid) {
697c478bd9Sstevel@tonic-gate 	       if (obj->debug)
707c478bd9Sstevel@tonic-gate 		    fprintf(stderr,
717c478bd9Sstevel@tonic-gate 			    "dyn: delete: zeroing %d bytes from %d + %d\n",
727c478bd9Sstevel@tonic-gate 			    obj->el_size, obj->array,
737c478bd9Sstevel@tonic-gate 			    obj->el_size*(obj->num_el - 1));
747c478bd9Sstevel@tonic-gate 	       memset(obj->array + obj->el_size*(obj->num_el - 1), 0,
757c478bd9Sstevel@tonic-gate 		     obj->el_size);
767c478bd9Sstevel@tonic-gate 	  }
777c478bd9Sstevel@tonic-gate      }
78*48bbca81SDaniel Hoffman 
797c478bd9Sstevel@tonic-gate      --obj->num_el;
80*48bbca81SDaniel Hoffman 
817c478bd9Sstevel@tonic-gate      if (obj->debug)
827c478bd9Sstevel@tonic-gate 	  fprintf(stderr, "dyn: delete: done.\n");
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate      return DYN_OK;
857c478bd9Sstevel@tonic-gate }
86