home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 13 / MA_Cover_13.bin / source / c / stefanb_src / dospath / src / dospath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-25  |  6.3 KB  |  243 lines

  1. /*
  2.  * dospath.c  V1.0
  3.  *
  4.  * Library routines
  5.  *
  6.  * (c) 1996 Stefan Becker
  7.  */
  8.  
  9. #include "dospath.h"
  10.  
  11. /*
  12.  * Object file dummy entry point
  13.  */
  14. static ULONG Dummy(void)
  15. {
  16.  return(0);
  17. }
  18.  
  19. /* Library name, ID string and other constant strings */
  20. #define INTTOSTR(a) #a
  21. static const char LibraryName[] = DOSPATH_NAME;
  22. static const char LibraryID[]   = "$VER: " DOSPATH_NAME " "
  23.                                    INTTOSTR(DOSPATH_VERSION) "."
  24.                                    INTTOSTR(DOSPATH_REVISION)
  25.                                    " (" __COMMODORE_DATE__ ")\r\n";
  26.  
  27. /* Standard library function prototypes */
  28. __geta4 static struct Library *LibraryInit(__A0 BPTR, __A6 struct Library *);
  29. __geta4 static struct Library *LibraryOpen(__A6 struct DOSPathBase *);
  30. __geta4 static BPTR            LibraryClose(__A6 struct DOSPathBase *);
  31. __geta4 static BPTR            LibraryExpunge(__A6 struct Library *);
  32.         static ULONG           LibraryReserved(void);
  33.  
  34. /* ROMTag structure */
  35. static const struct Resident ROMTag = { RTC_MATCHWORD, &ROMTag, &ROMTag + 1, 0,
  36.  DOSPATH_VERSION, NT_LIBRARY, 0, LibraryName, LibraryID, LibraryInit
  37. };
  38.  
  39. /* Library functions table */
  40. static const APTR LibraryVectors[] = {
  41.  /* Standard functions */
  42.  (APTR) LibraryOpen,
  43.  (APTR) LibraryClose,
  44.  (APTR) LibraryExpunge,
  45.  (APTR) LibraryReserved,
  46.  
  47.  /* Library specific functions */
  48.  (APTR) LibraryReserved, /* reserved for ARexx */
  49.  (APTR) FreePathList,
  50.  (APTR) CopyPathList,
  51.  (APTR) BuildPathListTagList,
  52.  (APTR) FindFileInPathList,
  53.  (APTR) RemoveFromPathList,
  54.  (APTR) GetProcessPathList,
  55.  (APTR) SetProcessPathList,
  56.  (APTR) CopyWorkbenchPathList,
  57.  
  58.  /* End of table */
  59.  (APTR) -1
  60. };
  61.  
  62. /* Local data */
  63. static struct DOSPathBase *DOSPathBase    = NULL; /* DCC: Don't remove! */
  64. static BPTR                DOSPathSegment;
  65.  
  66. /* Global library bases */
  67. struct Library *SysBase;
  68.  
  69. /* Generate a DOSPath library base */
  70. static struct DOSPathBase *CreateLibraryBase(void)
  71. {
  72.  struct DOSPathBase *dpb;
  73.  
  74.  if (dpb = (struct DOSPathBase *) MakeLibrary(LibraryVectors, NULL, NULL,
  75.                                               sizeof(struct DOSPathBase),
  76.                                               NULL)) {
  77.  
  78.   /* Initialize libray structure */
  79.   dpb->dpb_Library.lib_Node.ln_Type = NT_LIBRARY;
  80.   dpb->dpb_Library.lib_Node.ln_Name = LibraryName;
  81.   dpb->dpb_Library.lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  82.   dpb->dpb_Library.lib_Version      = DOSPATH_VERSION;
  83.   dpb->dpb_Library.lib_Revision     = DOSPATH_REVISION;
  84.   dpb->dpb_Library.lib_IdString     = (APTR) LibraryID;
  85.  }
  86.  
  87.  /* Return pointer to new library base */
  88.  return(dpb);
  89. }
  90.  
  91. /* Free library base */
  92. static void FreeLibraryBase(struct DOSPathBase *dpb)
  93. {
  94.  FreeMem((void *) ((ULONG) dpb - dpb->dpb_Library.lib_NegSize),
  95.          dpb->dpb_Library.lib_NegSize + dpb->dpb_Library.lib_PosSize);
  96. }
  97.  
  98. /* Initialize library */
  99. __geta4 static struct Library *LibraryInit(__A0 BPTR Segment,
  100.                                            __A6 struct Library *ExecBase)
  101. {
  102.  struct Library *lib = NULL;
  103.  
  104.  /* Check OS version, must be OS 2.x or better */
  105.  if (ExecBase->lib_Version >= 37) {
  106.  
  107.   /* Initialize SysBase */
  108.   SysBase = ExecBase;
  109.  
  110.   /* Create global library base */
  111.   if (lib = (struct Library *) CreateLibraryBase()) {
  112.  
  113.    /* Add the library to the system */
  114.    AddLibrary(lib);
  115.  
  116.    /* Set global pointers */
  117.    DOSPathBase    = (struct DOSPathBase *) lib;
  118.    DOSPathSegment = Segment;
  119.  
  120.    DEBUGLOG(kprintf("Init Lib: %08lx Seg 0x%08lx\n", lib, Segment);)
  121.   }
  122.  }
  123.  
  124.  /* Return new library pointer */
  125.  return(lib);
  126. }
  127.  
  128. /* Standard library function: Open. Called in Forbid() state */
  129. __geta4 static struct Library *LibraryOpen(__A6 struct DOSPathBase *gdb)
  130. {
  131.  struct DOSPathBase *dpb;
  132.  
  133.  /* Create new library base */
  134.  if (dpb = CreateLibraryBase()) {
  135.  
  136.   DEBUGLOG(kprintf("Open Lib: Lib 0x%08lx\n", dpb);)
  137.  
  138.   /* Open dos.library */
  139.   if (dpb->dpb_DOSBase = OpenLibrary("dos.library", 37)) {
  140.  
  141.    DEBUGLOG(kprintf("Open Lib: DOSBase 0x%08lx\n", dpb->dpb_DOSBase);)
  142.  
  143.    /* Open utility.library */
  144.    if (dpb->dpb_UtilityBase = OpenLibrary("utility.library", 37)) {
  145.  
  146.     /* Calculate library checksum */
  147.     SumLibrary((struct Library *) dpb);
  148.  
  149.     /* Oh another user :-) */
  150.     gdb->dpb_Library.lib_OpenCnt++;
  151.     dpb->dpb_Library.lib_OpenCnt = 1;
  152.  
  153.     /* Reset delayed expunge flag */
  154.     gdb->dpb_Library.lib_Flags &= ~LIBF_DELEXP;
  155.  
  156.     DEBUGLOG(kprintf("Open Lib: Open count %ld\n",
  157.              gdb->dpb_Library.lib_OpenCnt);)
  158.  
  159.    } else {
  160.  
  161.     /* Couldn't open utility.library */
  162.     CloseLibrary(dpb->dpb_DOSBase);
  163.     FreeLibraryBase(dpb);
  164.     dpb = NULL;
  165.    }
  166.   } else {
  167.  
  168.    /* Couldn't open dos.library */
  169.    FreeLibraryBase(dpb);
  170.    dpb = NULL;
  171.   }
  172.  }
  173.  
  174.  /* Return library pointer */
  175.  return((struct Library *) dpb);
  176. }
  177.  
  178. /* Standard library function: Close. Called in Forbid() state */
  179. __geta4 static BPTR LibraryClose(__A6 struct DOSPathBase *dpb)
  180. {
  181.  BPTR rc = NULL;
  182.  
  183.  DEBUGLOG(kprintf("Close Lib: Lib 0x%08lx\n", dpb);)
  184.  
  185.  /* Close Libraries */
  186.  CloseLibrary(dpb->dpb_UtilityBase);
  187.  CloseLibrary(dpb->dpb_DOSBase);
  188.  
  189.  /* Free library base */
  190.  FreeLibraryBase(dpb);
  191.  
  192.  /* Open count greater zero, only one user and delayed expunge bit set? */
  193.  if ((DOSPathBase->dpb_Library.lib_OpenCnt > 0) &&
  194.      (--DOSPathBase->dpb_Library.lib_OpenCnt == 0) &&
  195.      (DOSPathBase->dpb_Library.lib_Flags & LIBF_DELEXP))
  196.  
  197.   /* Yes, try to remove the library */
  198.   rc = LibraryExpunge((struct Library *) DOSPathBase);
  199.  
  200.  DEBUGLOG(kprintf("Close Lib: Open Count %ld Segment 0x%08lx\n",
  201.                   DOSPathBase->dpb_Library.lib_OpenCnt, rc);)
  202.  
  203.  /* Return library segment if expunge was successful */
  204.  return(rc);
  205. }
  206.  
  207. /* Standard library function: Expunge. Called in Forbid() state */
  208. __geta4 static BPTR LibraryExpunge(__A6 struct Library *lib)
  209. {
  210.  BPTR rc = NULL;
  211.  
  212.  DEBUGLOG(kprintf("Expunge Lib: %08lx Seg: 0x%08lx\n", lib, DOSPathSegment);)
  213.  
  214.  /* Does anybody use library now? */
  215.  if (lib->lib_OpenCnt > 0)
  216.  
  217.   /* Yes, library still in use -> set delayed expunge flag */
  218.   lib->lib_Flags |= LIBF_DELEXP;
  219.  
  220.  else {
  221.  
  222.   /* No, remove library */
  223.   Remove(&lib->lib_Node);
  224.  
  225.   /* Return library segment */
  226.   rc = DOSPathSegment;
  227.  
  228.   /* Free memory for library base */
  229.   FreeLibraryBase((struct DOSPathBase *) lib);
  230.  
  231.   DEBUGLOG(kprintf("Removing library...\n");)
  232.  }
  233.  
  234.  /* Return library segment if expunge was successful */
  235.  return(rc);
  236. }
  237.  
  238. /* Reserved function, returns NULL */
  239. static ULONG LibraryReserved(void)
  240. {
  241.  return(0);
  242. }
  243.