Problems with getcwd syscall on OSX

坚强是说给别人听的谎言 提交于 2020-06-28 02:56:07

问题


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.


回答1:


That's a bit late answer, but nonetheless this can be achieved using 2 syscalls.

  1. open_nocancel 0x2000018e (or open 0x2000005) opening a file descriptor for current dir
  2. fcntl_nocancel 0x20000196 (or fcntl 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


来源:https://stackoverflow.com/questions/32715311/problems-with-getcwd-syscall-on-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!