home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Aktief 1995 #6 / CDA_6.iso / shell / utils / disked29.arj / SOURCE.ZIP / FILETRCK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-19  |  6.4 KB  |  272 lines

  1. /***
  2. *filetrck.c -  disked file "tracking"
  3. *
  4. *Copyright (c) 1994-1995, Gregg Jennings.  All wrongs reserved.
  5. *   P O Box 200, Falmouth, MA 02541-0200
  6. *
  7. *Purpose:
  8. *
  9. *Notice:
  10. *   This progam may be freely used and distributed.  Any distrubution
  11. *   with modifications must retain the above copyright statement and
  12. *   modifications noted.
  13. *   No pulp-publication, in whole or in part, permitted without
  14. *   permission (magazines or books).
  15. *******************************************************************************/
  16.  
  17. /*
  18.    Versions
  19.  
  20.    0.1   14-Mar-1995
  21.    0.0   02-Sep-1994    PRELIMINARY
  22.  
  23.    Notes:   To track a file - create an array holding the cluster
  24.             numbers which are allocated to the file - that's it!
  25.             From this array, the current cluster (only those
  26.             clusters allocated to the file will be able to be
  27.             selected) is used to figure out the offset into
  28.             the file for the sector buffer display.
  29.  
  30. */
  31.  
  32. #include <stdlib.h>
  33. #include <malloc.h>
  34.  
  35. #include "disked.h"
  36. #include "diskio.h"
  37. #include "files.h"
  38. #include "keys.h"
  39. #include "alloc.h"
  40. #include "arrays.h"
  41. #include "console.h"
  42.  
  43. /* globals defined here */
  44.  
  45. extern int ft_track = 0;
  46.  
  47. /* globals referenced here
  48.  
  49.    log_sector, secs_cluster, clusters,
  50.    cluster_size, num_clusters
  51. */
  52.  
  53. static unsigned int *ftarray;
  54. static long ftsize;
  55. static size_t ftclstrs;
  56.  
  57. extern int filetrack(const char *file, const int fnum)
  58. {
  59.    if (ft_track == 0)
  60.    {
  61.       ftsize = filesize(file);
  62.       if (ftsize == -1L)
  63.          return 0;
  64.       ftclstrs = (unsigned)(ftsize / cluster_size) + 1;
  65.       if ((ftarray = alloc(ftclstrs,sizeof(int))) == NULL)
  66.          return -1;
  67.       arraysub(clusters,(int *)ftarray,num_clusters,fnum);
  68.       ft_track = 1;
  69.    }
  70.    else
  71.    {
  72.       ft_track = 0;
  73.       if (ftarray)
  74.          freep(ftarray);
  75.    }
  76.    return ft_track;
  77. }
  78.  
  79. extern void filetell(void)
  80. {
  81. size_t i;
  82.  
  83.    for (i = 0; i < ftclstrs; i++)
  84.       print("%d ",ftarray[i]);
  85. }
  86.  
  87. extern int fileoffset(unsigned int cluster)
  88. {
  89.    return arrayfind((int __huge *)ftarray,ftclstrs,cluster);
  90. }
  91.  
  92. extern long filebytes(void)
  93. {
  94. int j = clustersector(log_sector);
  95. int s = fileoffset(sectortocluster(log_sector));
  96.  
  97.    return (long)(((long)s*(long)secs_cluster) + (long)j) * (long)sec_size;
  98. }
  99.  
  100. /***
  101. *trackfile -
  102. *
  103. *
  104. ****/
  105.  
  106. extern int trackfile(unsigned int *c)
  107. {
  108. int r = 1;
  109.  
  110.    if (!ft_track)
  111.       return 0;      /* tracking off */
  112.  
  113.    switch (*c)
  114.    {
  115.       case 'q':
  116.       case '.':
  117.       case ESC:
  118.          if (Display || Verify)
  119.             print(" exit tracking");
  120.          if (*c != ESC && Verify && !getver("",0))
  121.             break;
  122.          filetrack(NULL,0);
  123.          r = -1;
  124.          break;
  125.       case 'n':                           /* next sector */
  126.       case DOWN:
  127.          if (clustersector(log_sector) == secs_cluster-1)
  128.             *c = 'j';
  129.          break;
  130.       case 'N':
  131.       case RIGHT:                         /* next cluster */
  132.          *c = 'j';
  133.          break;
  134.       case 'b':                           /* back sector */
  135.       case UP:
  136.          if (clustersector(log_sector) == 0)
  137.             *c = 'J';
  138.          break;
  139.       case 'B':
  140.       case LEFT:
  141.          *c = 'J';
  142.          break;
  143.       case 'H':
  144.       case HOME:
  145.          *c = CHOME;
  146.          break;
  147.       case 'E':
  148.       case END:
  149.          *c = CEND;
  150.          break;
  151.  
  152.       /* commands to ignore */
  153.  
  154.       case 'r':      /* */
  155.       case 'R':
  156.       case 'R'-'@':
  157.       case F0:
  158.       case 'f':
  159.       case '\\':
  160.       case 'F'-'@':
  161.       case 'G'-'@':
  162.       case '+':
  163.       case '-':      /* */
  164.       case 'N'-'@':
  165.       case PGDN:
  166.       case 'B'-'@':
  167.       case PGUP:
  168.       case 'S'-'@':
  169.       case 'S':
  170.       case 's':
  171.       case 'h':
  172.          r = -1;        /* ignore */
  173.    }
  174.    return r;            /* performed a conversion or ok */
  175. }
  176.  
  177. #if 0
  178. extern int filefind(int kase)
  179. {
  180. int i;                                    /* return value */
  181. char str[SEARCH+1];                       /* 4 times max string length */
  182. static char findstr[SEARCH+1];            /* string to search disk for */
  183. static char tmpfstr[SEARCH+1];            /* plus a bunch extra */
  184. static unsigned int findlen=0;
  185.  
  186.    i = -1;
  187.    str[0] = '\0';
  188.  
  189.    if (getstr(str,SEARCH,_TEXT) == ABORT) /* get string */
  190.       return(ABORT);                      /* return on ESC or ^C */
  191.    if (str[0] == '\0' && findlen == 0)    /* is no string to search for */
  192.       return(ABORT);                      /* then return ABORT */
  193.    if (str[0])                            /* save the string in tempstr */
  194.    {
  195.       strcpy(findstr,str);                /* to search for the same string */
  196.       strcpy(tmpfstr,str);                /* tmpfstr holds the un-converted */
  197.       findlen = convert(findstr);         /* convert if needed */
  198.    }
  199.    else
  200.       print(tmpfstr);
  201.  
  202.    i = dsk_search((byte *)findstr,findlen, (kase) ? isearch : search);
  203.  
  204.    return i;
  205. }
  206.  
  207. static int search(byte *str, int strcnt, int (func(byte*,byte*,word,word)))
  208. {
  209. unsigned long ntimes;
  210. register int io,i;
  211. byte *xbuf,*lxbuf;
  212. int bcnt,first;
  213.  
  214.    if ((xbuf = (byte *)alloc(1,sec_size+strcnt)) == NULL)
  215.       return -1;
  216.  
  217.    i = -1;
  218.    io = 0;
  219.    ntimes = 0L;
  220.    bcnt = sec_size - strcnt;
  221.    first = 1;
  222.  
  223.    for (;;)
  224.    {
  225.       if ((io = diskio(DISK_INC,1L,xbuf+strcnt)) != DISK_OK)
  226.       {
  227.          printerror(Debug);
  228.          if (!getver("continue",CLR_ARG))
  229.             break;
  230.       }
  231.       if (first)
  232.       {
  233.          i = func(lxbuf=xbuf+strcnt,str,sec_size-strcnt,strcnt);
  234.          first = 0;
  235.       }
  236.       else
  237.          i = func(lxbuf=xbuf,str,sec_size,strcnt);
  238.  
  239.       if (i != -1)                              /* found? */
  240.       {
  241.          if (i < strcnt)
  242.          {
  243.             if (lxbuf != xbuf)      /* first */
  244.                break;
  245.             movesector(-1);
  246.             i = sec_size - (strcnt - i);
  247.          }
  248.          else if (i > strcnt)
  249.          {
  250.             if (lxbuf != xbuf)      /* first */
  251.                break;
  252.             i -= strcnt;
  253.          }
  254.          break;
  255.       }
  256.       if (++ntimes >= num_sectors)
  257.       {
  258.          print(" not found");
  259.          break;
  260.       }
  261.       /* copy end of sector */
  262.       memcpy(xbuf,xbuf+bcnt+strcnt,strcnt);
  263.  
  264.       /* check for abort */
  265.       if (kbhit())
  266.          break;
  267.    }
  268.    freep(xbuf);
  269.    return i;
  270. }
  271. #endif
  272.