home *** CD-ROM | disk | FTP | other *** search
/ Shareware 1 2 the Maxx / sw_1.zip / sw_1 / PROGRAM / SPAWNO40.ZIP / SOURCE.ZIP / SPAWNPTH.ASM < prev    next >
Assembly Source File  |  1991-11-17  |  4KB  |  165 lines

  1.     NAME    spawnpth
  2.     TITLE    Path search for disk-swapping spawn() by Ralf Brown
  3.     PAGE    60,132
  4. ;-----------------------------------
  5. ; (c) Copyright 1990, 1991 Ralf Brown  All Rights Reserved
  6. ; This code may be redistributed as a part of the complete SPAWNO package,
  7. ; under its distribution conditions.
  8. ;-----------------------------------
  9. ;  SPAWNO v4.00
  10. ;
  11. ;  assembler defines:
  12. ;   __TINY__, __SMALL__, __COMPACT__, __MEDIUM__, __LARGE__, __HUGE__
  13. ;    exactly one of the above must be defined to specify the memory
  14. ;    model in use
  15. ;-----------------------------------
  16.  
  17. __BSS__ equ 1                ; yes, we are using uninitialized storage
  18.     INCLUDE RULES.ASI
  19.  
  20.     Header@                ; set up segment and group definitions
  21.  
  22. ; bit flags for the extensions which were found
  23. COM_EXT equ 1
  24. EXE_EXT equ 2
  25. USER_EXT equ 4
  26.  
  27. ;-----------------------------------------------------------
  28. ; initialized data
  29.  
  30. DSeg@
  31. IFNDEF __TP    ; defined in BSeg@ if __TP
  32. ExtSym@ _psp,WORD,__CDECL__
  33. ENDIF
  34.  
  35. PATH_EQUALS db 'PATH='
  36. PATH_EQUALS_LEN equ $-PATH_EQUALS
  37. DSegEnd@
  38.  
  39. ;-----------------------------------------------------------
  40. ; uninitialized data
  41.  
  42. BSeg@
  43. IFDEF __TP
  44. ExtSym@ _psp, WORD, __CDECL__
  45. ENDIF
  46.  
  47. env        dd ?
  48.  
  49. extrn ___SPAWN_PNAME:byte
  50. executable_path equ ___SPAWN_PNAME
  51. executable_pad    equ ___SPAWN_PNAME + 80
  52.  
  53. BSegEnd@
  54.  
  55. CSeg@
  56.     ASSUME    DS:DGROUP
  57.  
  58. extrn __SPAWN_ERRNO:near
  59. extrn __SPAWN_DDSEP:near
  60. extrn __SPAWN_CHECK_DIR:near
  61.  
  62. ;----------------------------------------------------------------
  63. ; char * pascal __spawn_search(const char *name) ;
  64. ;    given a program name, search the PATH for it, returning the full pathname
  65. ;    or NULL if not found
  66. ;----------------------------------------------------------------
  67. PubProc@ __SPAWN_SEARCH,__PASCAL__
  68. ; parameters
  69.  @name = DPTR_ [bp+2+cPtrSize]
  70. ; start of subroutine
  71.     ASSUME    DS:DGROUP,ES:NOTHING
  72.     push    bp
  73.     mov    bp,sp
  74. IFDEF __HUGE__
  75.     push    ds            ; save caller's DS
  76.     mov    ax,DGROUP        ;   and establish addressing to our
  77.     mov    ds,ax            ;   data segment
  78. ENDIF ;__HUGE__
  79.     push    es
  80.     push    di
  81.     push    si
  82. ; set up pointer to environment
  83. IFDEF __TINY__
  84.     mov    es,ds:[002Ch]
  85. ELSE
  86.     mov    es,_psp@
  87.     mov    es,es:[002Ch]
  88. ENDIF ;__TINY__
  89.     xor    di,di
  90.     jmp short find_path
  91. find_path_loop:
  92.     mov    al,0
  93.     cmp    byte ptr es:[di],al
  94.     je    found_path
  95.     cld
  96. find_path_loop2:
  97.     scasb                ; find terminating zero
  98.     jne    find_path_loop2
  99. find_path:
  100.     mov    cx,PATH_EQUALS_LEN
  101.     mov    si,offset DGROUP:PATH_EQUALS
  102.     repe    cmpsb
  103.     jne    find_path_loop
  104. found_path:
  105.     mov    word ptr env+2,es
  106.     mov    word ptr env,di
  107.     mov    di,offset DGROUP:executable_path
  108. search_loop:
  109.     call    __SPAWN_CHECK_DIR
  110.     jnz    search_successful
  111.     or    al,al            ; was there a pathspec in the name?
  112.     jne    search_failed         ; don't look at PATH unless simple filename
  113.     les    bx,env
  114.     ASSUME    ES:NOTHING
  115.     cmp    byte ptr es:[bx],0
  116.     je    search_failed
  117.     mov    di,offset DGROUP:executable_path
  118. dir_copy_loop:
  119.     mov    al,es:[bx]
  120.     or    al,al
  121.     je    dir_copy_done
  122.     inc    bx
  123.     cmp    al,';'            ; separator for path entries
  124.     je    dir_copy_done
  125.     mov    [di],al
  126.     inc    di
  127.     cmp    di,offset DGROUP:executable_pad      ; make sure we don't overrun buf
  128.     jb    dir_copy_loop
  129. dir_copy_done:
  130.     mov    al,[di-1]
  131.     call    __spawn_ddsep        ; was last char a slash/backslash/colon?
  132.     je    dir_has_slash
  133.     mov    byte ptr [di],'\'
  134.     inc    di
  135. dir_has_slash:
  136.     mov    word ptr env,bx
  137.     jmp    search_loop
  138.  
  139. search_successful:
  140.     mov    ax,bx
  141. IF LDATA
  142.     mov    dx,ds
  143. ENDIF
  144.     jmp short search_done
  145. search_failed:
  146.     mov    ax,2            ; file not found
  147.     call    __SPAWN_ERRNO
  148.     xor    ax,ax
  149. IF LDATA
  150.     xor    dx,dx
  151. ENDIF
  152. search_done:
  153.     pop    si
  154.     pop    di
  155.     pop    es
  156. IFDEF __HUGE__
  157.     pop    ds
  158. ENDIF ;__HUGE__
  159.     pop    bp
  160.     ret    dPtrSize
  161. EndProc@ __SPAWN_SEARCH,__PASCAL__
  162.  
  163. CSegEnd@
  164.     END
  165.