home *** CD-ROM | disk | FTP | other *** search
/ The UNIX CD Bookshelf / OREILLY_TUCB_UNIX_CD.iso / upt / examples / SOURCES / DELETE / PART01.Z / PART01 / stack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-24  |  3.6 KB  |  144 lines

  1. /*
  2.  * $Source: /afs/athena.mit.edu/user/j/jik/delete/src/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/delete/src/RCS/stack.c,v 1.8 90/09/26 03:45: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. #ifdef STACK_DEBUG
  48.       fprintf(stderr, "dostack: return 1 (EMPTY_STACK).\n");
  49. #endif
  50.       return 0;
  51.      case STACK_PUSH:
  52.       if (bytes == 0) {
  53. #ifdef STACK_DEBUG
  54.            fprintf(stderr, "Pushing 0 bytes at %d offset.\n", count);
  55.            fprintf(stderr, "dostack: return 2 (STACK_PUSH).\n");
  56. #endif
  57.            return 0;
  58.       }
  59.       if (size - count < bytes) {
  60.            do
  61.             size += STACK_INC;
  62.            while (size - count < bytes);
  63.            stack = (caddr_t) (stack ? realloc((char *) stack,
  64.                           (unsigned) size) :
  65.                   Malloc((unsigned) size));
  66.            if (! stack) {
  67.             size = count = 0;
  68.             set_error(errno);
  69.             error("Malloc");
  70. #ifdef STACK_DEBUG
  71.             fprintf(stderr, "dostack: return 3 (STACK_PUSH).\n");
  72. #endif
  73.             return error_code;
  74.            }
  75.       }
  76. #ifdef STACK_DEBUG
  77.       fprintf(stderr, "Pushing %d bytes at %d offset.\n", bytes, count);
  78. #endif
  79. #if defined(SYSV) || (defined(__STDC__) && !defined(__HIGHC__))
  80.       memcpy(stack + count, data, bytes);
  81. #else
  82.       bcopy(data, stack + count, bytes);
  83. #endif
  84.       count += bytes;
  85. #ifdef STACK_DEBUG
  86.       fprintf(stderr, "dostack: return 4 (STACK_PUSH).\n");
  87. #endif
  88.       return 0;
  89.      case STACK_POP:
  90.       if (bytes == 0) {
  91. #ifdef STACK_DEBUG
  92.            fprintf(stderr, "Popping 0 bytes at %d offset.\n", count);
  93.            fprintf(stderr, "dostack: return 5 (STACK_POP).\n");
  94. #endif
  95.            return 0;
  96.       }
  97.       if (count == 0) {
  98.            set_status(STACK_EMPTY);
  99. #ifdef STACK_DEBUG
  100.            fprintf(stderr, "dostack: return 6 (STACK_POP).\n");
  101. #endif
  102.            return error_code;
  103.       }
  104.       else {
  105.            int newblocks, newsize;
  106.  
  107.            count -= bytes;
  108. #ifdef STACK_DEBUG
  109.            fprintf(stderr, "Popping %d bytes at %d offset.\n", bytes,
  110.                count);
  111. #endif
  112. #if defined(SYSV) || (defined(__STDC__) && !defined(__HIGHC__))
  113.            memcpy(data, stack + count, bytes);
  114. #else
  115.            bcopy(stack + count, data, bytes);
  116. #endif
  117.            newblocks = count / STACK_INC + ((count % STACK_INC) ? 1 : 0);
  118.            newsize = newblocks * STACK_INC;
  119.            if (newsize < size) {
  120.             size = newsize;
  121.             stack = (caddr_t) realloc((char *) stack, (unsigned) size);
  122.             if ((size != 0) && (! stack)) {
  123.              set_error(errno);
  124.              error("realloc");
  125. #ifdef STACK_DEBUG
  126.              fprintf(stderr, "dostack: return 7 (STACK_POP).\n");
  127. #endif
  128.              return error_code;
  129.             }
  130.            }
  131. #ifdef STACK_DEBUG
  132.            fprintf(stderr, "dostack: return 8 (STACK_POP).\n");
  133. #endif
  134.            return 0;
  135.       }
  136.      default:
  137.       set_error(STACK_BAD_OP);
  138. #ifdef STACK_DEBUG
  139.       fprintf(stderr, "dostack: return 9.\n");
  140. #endif
  141.       return error_code;
  142.      }
  143. }
  144.