home *** CD-ROM | disk | FTP | other *** search
- ;_ getcwd.asm Sat Jan 23 1988 Modified by: Walter Bright */
- ; Copyright (C) 1985-1988 by Northwest
- ; All Rights Reserved
- ; Written by Walter Bright
-
- include macros.asm
-
- begdata
- c_extrn errno,word
-
- tmpbuf db 64 dup (?)
- enddata
-
- if LCODE
- c_extrn strcpy,far
- c_extrn strlen,far
- c_extrn malloc,far
- else
- c_extrn strcpy,near
- c_extrn strlen,near
- c_extrn malloc,near
- endif
-
- begcode getcwd
-
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Get current working directory.
- ; char *getcwd(char *pbuf,int n);
- ; Input:
- ; pbuf where to store the pathname
- ; n maximum length of pathname including terminating 0
- ; Returns:
- ; if successful
- ; pbuf
- ; else
- ; NULL
- ; errno = ENOMEM (out of memory), ERANGE (pathname longer
- ; than n-1 characters)
-
- c_public getcwd
- func getcwd
- push BP
- mov BP,SP
- .save SI
- ;if (pbuf == NULL)
- if SPTR
- .if <word ptr P[BP]> ne 0, L33
- else
- mov AX,P+2[BP]
- or AX,P[BP]
- jne L33
- endif
-
- ;pbuf = malloc(n);
- push P+SIZEPTR[BP]
- callm malloc
- add SP,2
- if SPTR
- mov P[BP],AX
- else
- ifdef MSC
- mov P+2[BP],DX
- mov P[BP],AX
- else
- mov P+2[BP],AX
- mov P[BP],BX
- endif
- endif
-
- ;if (pbuf == NULL)
- if SPTR
- or AX,AX
- else
- ifdef MSC
- or AX,DX
- else
- or AX,BX
- endif
- endif
- jne L33
- ;errno = ENOMEM;
- mov errno,8
- jmps retnull
-
- ; if (n < 4)
- L33: .if <word ptr P+SIZEPTR[BP]> ge 4, L3B
- jmps erange
-
- L3B: ; get current drive letter
- bdos 019h
- mov DL,AL
- add AL,'A'
- if SPTR
- mov BX,P[BP]
- mov [BX],AL
- mov byte ptr 1[BX],':'
- mov byte ptr 2[BX],'\'
- else
- les BX,P[BP]
- mov ES:[BX],AL
- mov byte ptr ES:1[BX],':'
- mov byte ptr ES:2[BX],'\'
- endif
-
- inc DL ;DL = drive number
- mov SI,offset DGROUP:tmpbuf
- bdos 47h ;get current directory
- jnc L1
- mov errno,AX
- jmps retnull
-
- L1: ;if (strlen(tmpbuf) + 4 > n) /* too large */
- if LPTR
- push DS
- endif
- push SI
- callm strlen
- add SP,SIZEPTR
- add AX,4
- .if AX g P+SIZEPTR[BP], erange
-
- if SPTR
- push SI
- mov BX,P[BP]
- add BX,3
- else
- push DS
- push SI
- les BX,P[BP]
- add BX,3
- push ES
- endif
- push BX
- callm strcpy ;strcpy(pbuf + 3,tmpbuf);
- add SP,2*SIZEPTR
- sub BX,3 ;return pbuf
- L2: .restore SI
- pop BP
- ret
-
- erange: mov errno,067h ;ERANGE
- retnull:
- ;return (char *) NULL;
- xor AX,AX
- ifdef MSC
- cwd
- else
- mov BX,AX
- endif
- jmp L2
-
- c_endp getcwd
-
- endcode getcwd
-
- end
-
-