Does anyone have an idea how to get the current working directory in OSX with NASM? The syscall getcwd isn't available on osx and dtruss pwd return lots of stat sys calls. However in the manual I can't find which structure variable of stat returns the current working directory. Thanks.
Asked
Active
Viewed 616 times
2
-
2There is no `syscall` for `getcwd`. It is usually implemented by retrieving `$PWD` environment variable and doing checks to ensure it matches with stat(".") inode and device. You can get an idea of how Apple does it in their [libc code](http://www.opensource.apple.com/source/Libc/Libc-167/gen.subproj/getcwd.c) . You could link your assembly language program with `libc` and make the call to `getcwd`. – Michael Petch Sep 23 '15 at 01:07
-
so I think for osx it's better to always use the libc library instead like on Linux rely on syscalls only... – Agguro Oct 01 '15 at 22:55
1 Answers
2
That's a bit late answer, but nonetheless this can be achieved using 2 syscalls.
open_nocancel
0x2000018e (oropen
0x2000005) opening a file descriptor for current dirfcntl_nocancel
0x20000196 (orfcntl
0x2000005c) for reading the actual path
example:
#define F_GETPATH 50 ; from <sys/fcntl.h>
currentDirConstant:
db ".",0 ; needs segment read permission
outputPath:
resb 1000 ; needs segment write permission
mov rdi,currentDirConstant; input path 1st argument
xor esi, esi ; int flags 2nd argument, just use 0
xor edx, edx ; int mode 3rd argument, just use 0
mov eax, 0x2000018e ; open_nocancel syscall number
syscall
mov edi,eax ; file descriptor 1st argument of current dir from previous syscall
mov esi,F_GETPATH ; fcntl cmd 2nd argument
mov rdx, outputPath ; output buffer 3rd argument
mov eax, 0x20000196 ; fcntl_nocancel syscall number
syscall

Kamil.S
- 5,205
- 2
- 22
- 51