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