home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / FCOPY / TRUENAME.ASM < prev    next >
Assembly Source File  |  1993-12-01  |  9KB  |  155 lines

  1. ;-----------------------------------------------------------------------;
  2. ; TRUENAME.ASM                                                          ;
  3. ;                                                                       ;
  4. ; This module contains a C-callable function to determine the           ;
  5. ; canonical (fully qualified) name of a given path specification.       ;
  6. ; It uses a direct call to undocumented DOS function 60h.  This         ;
  7. ; function did not become available until DOS version 3.0, so this      ;
  8. ; procedure first checks for a DOS major version number that must       ;
  9. ; be 3 or above.  If DOS 2.X or earlier is detected, truename returns   ;
  10. ; an error.                                                             ;
  11. ;                                                                       ;
  12. ; DOS function 60h expects the path string to have no leading or        ;
  13. ; trailing white space.  The function strtrim (in module STRTRIM.ASM)   ;
  14. ; has been provided for this purpose, and should be called ahead of     ;
  15. ; truename unless the caller is certain that the string is already      ;
  16. ; properly formed.                                                      ;
  17. ;                                                                       ;
  18. ; DOS function 60h will return a qualified file name even if the        ;
  19. ; final subdirectory\filename.ext does not exist.  It therefore         ;
  20. ; cannot be used to test for the actual existence of the file.          ;
  21. ; Function 60h is more useful than similar routines that predated       ;
  22. ; DOS 3.0, in that it can "see through" SUBST and JOINed drives and     ;
  23. ; directories.                                                          ;
  24. ;                                                                       ;
  25. ; This module recognizes a _model symbol, and can therefore             ;
  26. ; generate object code for any model desired.  If _model is not         ;
  27. ; defined, the SMALL model is used by default.                          ;
  28. ;-----------------------------------------------------------------------;
  29. ; Usage:                                                                ;
  30. ;                                                                       ;
  31. ; #include "fcopy.h"                                                    ;
  32. ;                                                                       ;
  33. ; Function prototype:                                                   ;
  34. ;                                                                       ;
  35. ; int truename (char *pathname, char *qualname)                         ;
  36. ;                                                                       ;
  37. ; Returns:  If successful, returns 0.                                   ;
  38. ;                                                                       ;
  39. ;           If function fails, returns -1 and sets errno                ;
  40. ;           and _doserrno to one of the following:                      ;
  41. ;                                                                       ;
  42. ;               EINVFNC    Invalid function number (1)                     ;
  43. ;               ENOENT  Invalid source name (2)                         ;
  44. ;               ENOPATH Invalid drive or malformed path (3)             ;
  45. ;-----------------------------------------------------------------------;
  46. ; Revision history:                                                     ;
  47. ;                                                                       ;
  48. ;       1.0     19 APR 92       Original.                               ;
  49. ;                                                                       ;
  50. ;                               Uses conditional assembly for all       ;
  51. ;                               memory models by defining _model.       ;
  52. ;                               Defaults to the small model if          ;
  53. ;                               _model is not defined.                  ;
  54. ;                                                                       ;
  55. ;       1.1     12 MAY 92       Fixed bug in large data models when     ;
  56. ;                               running under a DOS version earlier     ;
  57. ;                               than 3.0.  Data segment was being       ;
  58. ;                               popped on exit, but was not pushed      ;
  59. ;                               on entry.                               ;
  60. ;                                                                       ;
  61. ;                               Now makes no assumption about setting   ;
  62. ;                               of DS on entry.  For large data models  ;
  63. ;                               (compact and large), sets DS to the     ;
  64. ;                               default data segment.  For huge model,  ;
  65. ;                               sets DS to segment containing errno     ;
  66. ;                               so that errno and _doserrno can be set  ;
  67. ;                               on error.                               ;
  68. ;-----------------------------------------------------------------------;
  69. ;       Copyright (c) 1992 Ray Waters                                   ;
  70. ;       All Rights Reserved                                             ;
  71. ;-----------------------------------------------------------------------;
  72.  
  73. EINVFNC EQU     1               ; equate for invalid function number
  74. ENOENT  EQU     2               ; equate for invalid source name
  75. ENOPATH EQU     3               ; equate for invalid drive or malformed path
  76.  
  77. IFDEF   _model                          ; if a _model was defined,
  78.         .MODEL  _model, C               ;  use it
  79. ELSE                                    ; else, default to
  80.         .MODEL  SMALL, C                ;  SMALL model
  81. ENDIF
  82.  
  83. IFDEF   ??version                       ; if using TASM,
  84.         LOCALS                          ;  enable local labels
  85. ENDIF
  86.  
  87.         EXTRN   C errno:WORD            ; global variables for errno
  88.         EXTRN   C _doserrno:WORD        ;  and _doserrno
  89.  
  90.         .CODE                           ; open code segment
  91.  
  92.         PUBLIC  truename                ; make visible to Linker
  93. ;-----------------------------------------------------------------------;
  94. ; int truename (char *pathname, char *qualname)                         ;
  95. ;                                                                       ;
  96. ; Returns:      If successful, returns 0.                               ;
  97. ;                                                                       ;
  98. ;               If unsuccessful, returns -1 and sets errno              ;
  99. ;               and _doserrno to one of the following:                  ;
  100. ;                                                                       ;
  101. ;                   EINVFNC Invalid function number (1)                 ;
  102. ;                   ENOENT  Invalid source name (2)                     ;
  103. ;                   ENOPATH Invalid drive or malformed path (3)         ;
  104. ;-----------------------------------------------------------------------;
  105. truename        PROC    C USES si di, pathname:PTR BYTE, qualname:PTR BYTE
  106.  
  107. IF @DataSize                            ; if large data model,
  108.         push    ds                      ;  save original data segment
  109. ENDIF
  110.         mov     ax,3000h                ; check DOS version number
  111.         int     21h
  112.         cmp     al,3                    ; is it version 3.0 or later?
  113.         mov     ax,EINVFNC              ; assume not, set error code
  114.         jb      @@error                 ; abort if not version 3.0 or above
  115.  
  116. IF @DataSize                            ; if large data model,
  117.         lds     si,[pathname]           ;  load pathname address in DS:SI
  118.         les     di,[qualname]           ;  load ES:DI with qualname buffer
  119. ELSE                                    ; for small data model,
  120.         mov     ax,ds                   ;  set ES = DS
  121.         mov     es,ax
  122.         mov     si,[pathname]           ;  load pathname address in DS:SI
  123.         mov     di,[qualname]           ;  load ES:DI with qualname buffer
  124. ENDIF
  125.  
  126.         mov     ah,60h                  ; DOS function to qualify pathname
  127.         int     21h
  128.         jc      @@error                 ; jump on error
  129.         xor     ax,ax                   ; AX = 0 for success
  130.  
  131. IF @DataSize                            ; if large data model,
  132.         pop     ds                      ;  restore original data segment
  133. ENDIF
  134.         ret                             ; return to caller
  135.  
  136. @@error:
  137. IF @DataSize EQ 2                       ; if huge data model,
  138.         mov     bx,SEG errno            ; get segment of errno
  139.         mov     ds,bx                   ;  into DS
  140. ELSEIF @DataSize EQ 1                   ; if large data model,
  141.         mov     bx,@data                ;  get default data segment
  142.         mov     ds,bx                   ;  into DS
  143. ENDIF
  144.         mov     [errno],ax              ; set errno
  145.         mov     [_doserrno],ax          ; set _doserrno
  146. IF @DataSize                            ; if large data model,
  147.         pop     ds                      ;  restore original data segment
  148. ENDIF
  149.         mov     ax,-1                   ; return int value of -1
  150.         ret                             ; return to caller
  151.  
  152. truename        ENDP
  153.  
  154.         END
  155.