home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / undel2 / part01 / stack.c < prev    next >
C/C++ Source or Header  |  1990-06-07  |  2KB  |  98 lines

  1. /*
  2.  * $Source: /afs/athena.mit.edu/user/j/jik/src/delete/RCS/stack.c,v $
  3.  * $Author: jik $
  4.  *
  5.  * This program is part of a package including delete, undelete,
  6.  * lsdel, expunge and purge.  The software suite is meant as a
  7.  * replacement for rm which allows for file recovery.
  8.  * 
  9.  * Copyright (c) 1989 by the Massachusetts Institute of Technology.
  10.  * For copying and distribution information, see the file "mit-copyright.h."
  11.  */
  12.  
  13. #if (!defined(lint) && !defined(SABER))
  14.      static char rcsid_stack_c[] = "$Header: /afs/athena.mit.edu/user/j/jik/src/delete/RCS/stack.c,v 1.5 89/12/11 03:32:37 jik Exp $";
  15. #endif
  16.  
  17. #include <sys/types.h>
  18. #include <stdio.h>
  19. #include <errno.h>
  20. #include "stack.h"
  21. #include "delete_errs.h"
  22. #include "errors.h"
  23. #include "mit-copyright.h"
  24. #include "util.h"
  25.  
  26. extern char *realloc();
  27. extern int errno;
  28.  
  29. #define STACK_INC     25
  30.  
  31.  
  32.  
  33. int dostack(data, op, bytes)
  34. caddr_t data;
  35. int op, bytes;
  36. {
  37.      static caddr_t stack = (caddr_t) NULL;
  38.      static int size = 0, count = 0;
  39.  
  40.      switch (op) {
  41.      case EMPTY_STACK:
  42.       if (size) {
  43.            free(stack);
  44.            stack = (caddr_t) NULL;
  45.            size = count = 0;
  46.       }
  47.       return 0;
  48.      case STACK_PUSH:
  49.       if (bytes == 0)
  50.            return 0;
  51.       if (size - count < bytes) {
  52.            do
  53.             size += STACK_INC;
  54.            while (size - count < bytes);
  55.            stack = (caddr_t) (stack ? realloc((char *) stack,
  56.                           (unsigned) size) :
  57.                   Malloc((unsigned) size));
  58.            if (! stack) {
  59.             size = count = 0;
  60.             set_error(errno);
  61.             error("Malloc");
  62.             return error_code;
  63.            }
  64.       }
  65.       bcopy(data, stack + count, bytes);
  66.       count += bytes;
  67.       return 0;
  68.      case STACK_POP:
  69.       if (bytes == 0)
  70.            return 0;
  71.       if (count == 0) {
  72.            set_status(STACK_EMPTY);
  73.            return error_code;
  74.       }
  75.       else {
  76.            int newblocks, newsize;
  77.  
  78.            count -= bytes;
  79.            bcopy(stack + count, data, bytes);
  80.            newblocks = count / STACK_INC + ((count % STACK_INC) ? 1 : 0);
  81.            newsize = newblocks * STACK_INC;
  82.            if (newsize < size) {
  83.             size = newsize;
  84.             stack = (caddr_t) realloc((char *) stack, (unsigned) size);
  85.             if (! stack) {
  86.              set_error(errno);
  87.              error("realloc");
  88.              return error_code;
  89.             }
  90.            }
  91.            return 0;
  92.       }
  93.      default:
  94.       set_error(STACK_BAD_OP);
  95.       return error_code;
  96.      }
  97. }
  98.