home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CASM.ARJ / GETCWD.ASM < prev    next >
Encoding:
Assembly Source File  |  1988-07-03  |  2.4 KB  |  158 lines

  1. ;_ getcwd.asm   Sat Jan 23 1988   Modified by: Walter Bright */
  2. ; Copyright (C) 1985-1988 by Northwest
  3. ; All Rights Reserved
  4. ; Written by Walter Bright
  5.  
  6.     include    macros.asm
  7.  
  8.     begdata
  9.     c_extrn        errno,word
  10.  
  11. tmpbuf    db    64 dup (?)
  12.     enddata
  13.  
  14.     if LCODE
  15.     c_extrn        strcpy,far
  16.     c_extrn        strlen,far
  17.     c_extrn        malloc,far
  18.     else
  19.     c_extrn        strcpy,near
  20.     c_extrn        strlen,near
  21.     c_extrn        malloc,near
  22.     endif
  23.  
  24.     begcode    getcwd
  25.  
  26. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  27. ; Get current working directory.
  28. ;    char *getcwd(char *pbuf,int n);
  29. ; Input:
  30. ;    pbuf    where to store the pathname
  31. ;    n    maximum length of pathname including terminating 0
  32. ; Returns:
  33. ;    if successful
  34. ;        pbuf
  35. ;    else
  36. ;        NULL
  37. ;        errno = ENOMEM (out of memory), ERANGE (pathname longer
  38. ;            than n-1 characters)
  39.  
  40.     c_public    getcwd
  41. func    getcwd
  42.     push    BP
  43.     mov    BP,SP
  44.     .save    SI
  45.     ;if (pbuf == NULL)
  46.     if SPTR
  47.     .if    <word ptr P[BP]> ne 0, L33
  48.     else
  49.     mov    AX,P+2[BP]
  50.     or    AX,P[BP]
  51.     jne    L33
  52.     endif
  53.  
  54.     ;pbuf = malloc(n);
  55.     push    P+SIZEPTR[BP]
  56.     callm    malloc
  57.     add    SP,2
  58.     if SPTR
  59.     mov    P[BP],AX
  60.     else
  61.       ifdef MSC
  62.     mov    P+2[BP],DX
  63.     mov    P[BP],AX
  64.       else
  65.     mov    P+2[BP],AX
  66.     mov    P[BP],BX
  67.       endif
  68.     endif
  69.  
  70.     ;if (pbuf == NULL)
  71.     if SPTR
  72.     or    AX,AX
  73.     else
  74.       ifdef MSC
  75.     or    AX,DX
  76.       else
  77.     or    AX,BX
  78.       endif
  79.     endif
  80.     jne    L33
  81.     ;errno = ENOMEM;
  82.     mov    errno,8
  83.     jmps    retnull
  84.  
  85.     ;  if (n < 4)
  86. L33:    .if    <word ptr P+SIZEPTR[BP]> ge 4, L3B
  87.     jmps    erange
  88.  
  89. L3B:    ; get current drive letter
  90.     bdos    019h
  91.     mov    DL,AL
  92.     add    AL,'A'
  93.     if SPTR
  94.     mov    BX,P[BP]
  95.     mov    [BX],AL
  96.     mov    byte ptr 1[BX],':'
  97.     mov    byte ptr 2[BX],'\'
  98.     else
  99.     les    BX,P[BP]
  100.     mov    ES:[BX],AL
  101.     mov    byte ptr ES:1[BX],':'
  102.     mov    byte ptr ES:2[BX],'\'
  103.     endif
  104.  
  105.     inc    DL        ;DL = drive number
  106.     mov    SI,offset DGROUP:tmpbuf
  107.     bdos    47h        ;get current directory
  108.     jnc    L1
  109.     mov    errno,AX
  110.     jmps    retnull
  111.  
  112. L1:    ;if (strlen(tmpbuf) + 4 > n)    /* too large            */
  113.     if LPTR
  114.     push    DS
  115.     endif
  116.     push    SI
  117.     callm    strlen
  118.     add    SP,SIZEPTR
  119.     add    AX,4
  120.     .if    AX g P+SIZEPTR[BP], erange
  121.  
  122.     if SPTR
  123.     push    SI
  124.     mov    BX,P[BP]
  125.     add    BX,3
  126.     else
  127.     push    DS
  128.     push    SI
  129.     les    BX,P[BP]
  130.     add    BX,3
  131.     push    ES
  132.     endif
  133.     push    BX
  134.     callm    strcpy        ;strcpy(pbuf + 3,tmpbuf);
  135.     add    SP,2*SIZEPTR
  136.     sub    BX,3        ;return pbuf
  137. L2:    .restore SI
  138.     pop    BP
  139.     ret
  140.  
  141. erange:    mov    errno,067h    ;ERANGE
  142. retnull:
  143.     ;return (char *) NULL;
  144.     xor    AX,AX
  145.     ifdef MSC
  146.     cwd
  147.     else
  148.     mov    BX,AX
  149.     endif
  150.     jmp    L2
  151.  
  152. c_endp    getcwd
  153.  
  154.     endcode    getcwd
  155.  
  156.     end
  157.  
  158.