home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff236.lzh / DiskHandler / buff.c < prev    next >
C/C++ Source or Header  |  1989-08-09  |  5KB  |  143 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /* |_o_o|\\ Copyright (c) 1987 The Software Distillery.  All Rights Reserved */
  3. /* |. o.| || This program may not be distributed without the permission of   */
  4. /* | .  | || the authors:                                          BBS:      */
  5. /* | o  | ||   John Toebes     Dave Baker                                    */
  6. /* |  . |//                                                                  */
  7. /* ======                                                                    */
  8. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9. /* File buffer manipulation */
  10.  
  11. #include "handler.h"
  12.  
  13. /*---------------------------------------------------------------------------*/
  14. /*                                                                           */
  15. /*              initbuf( global, mode, flags )                               */
  16. /*                                                                           */
  17. /*---------------------------------------------------------------------------*/
  18.  
  19. int initbuf(global, mode, flags)
  20. struct global *global;
  21. int mode;
  22. int flags;
  23. {
  24.    long key;
  25.    struct EFileHandle *efh;
  26.    struct FileHandle *fh;
  27.    struct FileLock *lock;
  28.  
  29.    fh = (struct FileHandle *)global->pkt->dp_Arg1;
  30.    lock = (struct FileLock *)global->pkt->dp_Arg2;
  31.  
  32.    BUG(("initbuf:enter flags = %ld\n", flags));
  33.    BUG(("Initbuf: fh = %08lx  lock = %08lx  mode = %ld\n", fh, lock, mode));
  34.    BUGBSTR("Name to open is :", global->pkt->dp_Arg3);
  35.  
  36.    /* allocate a buffer */
  37.    efh = fh->fh_Arg1 = (struct EFileHandle *)
  38.                        DosAllocMem(global, sizeof(struct EFileHandle));
  39.  
  40.    BUG(("initbuf: EFileHandle is at %08lx\n", efh));
  41.  
  42.    if (efh == NULL) return( DOS_FALSE );
  43.  
  44.    /* Put it on the chain */
  45.    efh->efh_Next = global->AllHandles;
  46.    global->AllHandles = efh;
  47.  
  48.    fh->fh_Type = global->port;
  49.    fh->fh_Port = DOS_FALSE;  /* not an interactive file */
  50.  
  51.    if (flags & NEWFILE)
  52.       {
  53.       /* Make sure we have the rights to write to this disk */
  54.       if (unsafe(global, efh, 1)) goto error;
  55.  
  56.       BUG(("Calling CreateEntry\n"));
  57.       key = CreateEntry(global, GetKey(global,lock),
  58.                                 (char *)global->pkt->dp_Arg3,
  59.                                 OLDFILE, ST_FILE);
  60.       }
  61.    else
  62.       {
  63.       /* Make sure the disk is in a safe state to be doing this stuff */
  64.       if (unsafe(global, efh, 0)) goto error;
  65.  
  66.       BUG(("Calling LocateEntry\n"));
  67.       key = LocateEntry(global, GetKey(global,lock),
  68.                                 (char *)global->pkt->dp_Arg3);
  69.       }
  70.  
  71.    efh->efh_Lock = CreateLock(global, key, mode);
  72.  
  73.    BUG(("initbuf: Key = %ld  lock = %08lx\n", key, efh->efh_Lock));
  74.  
  75.    if (efh->efh_Lock == NULL)
  76.       goto error;
  77.  
  78.    if (GetType(global,key) != ST_FILE)
  79.       {
  80.       BUG(("Gee, It's not the right type %ld\n", GetType(global, key)));
  81.       global->pkt->dp_Res2 = ERROR_OBJECT_WRONG_TYPE;
  82.       goto error;
  83.       }
  84.  
  85.    /* We maintain these fields so we don't have to go seeking for them. */
  86.    efh->efh_CurPos = 0;
  87.    efh->efh_CurKey = 0;
  88.    efh->efh_CurBlock = 1;
  89.    efh->efh_CurExtBlock = 0;
  90.    efh->efh_ExtBlockKey = key;
  91.    efh->efh_Protect = GetProtect(global, key);
  92.  
  93.    BUG(("initbuf: done\n"));
  94.    return(DOS_TRUE);
  95.  
  96. error:
  97.    termbuf( global, efh );
  98.    return(DOS_FALSE);
  99. }
  100.  
  101.  
  102. /*---------------------------------------------------------------------------*/
  103. /*                                                                           */
  104. /*                      termbuf( global, efh )                               */
  105. /*                                                                           */
  106. /*---------------------------------------------------------------------------*/
  107.  
  108. void termbuf(global, efh)
  109. GLOBAL           global;
  110. EFH              efh;
  111. {
  112.    struct EFileHandle   *prev;
  113.  
  114.    /* Unlink this efh from our global chain */
  115.    if (global->AllHandles == efh)
  116.       global->AllHandles = efh->efh_Next;
  117.    else
  118.    {
  119.       for (prev = global->AllHandles;
  120.            prev && (prev->efh_Next != efh);
  121.            prev = prev->efh_Next);
  122.  
  123.       /* Just in case.... but this should never happen!!! */
  124.       if (!prev)
  125.       {
  126.          BUG(("termbuf: oh shit, cant find efh %08lx in chain!!!!!\n",efh));
  127.          /* dont free it because its probably garbage */
  128.          return;
  129.       }
  130.  
  131.       /* remove efh from chain */
  132.       prev->efh_Next = efh->efh_Next;
  133.    }
  134.  
  135.    if (efh->efh_Lock)
  136.       freelock(global, efh->efh_Lock);
  137.  
  138.    /* free up the memory for the buffer */
  139.    DosFreeMem( (char *)efh );
  140.    global->pkt->dp_Arg1 = NULL;
  141. }
  142.  
  143.