If I write to terminal executing write
syscall myself on Mac, it succeeds:
int main() {
long SYS_WRITE = 0x2000004;
long STDOUT = 1;
long result;
char* str = "Hello world";
__asm__ __volatile__ (
"syscall;\n"
: "=a" (result)
: "a" (SYS_WRITE), "D" (STDOUT), "S" (str), "d" (10)
:
);
std::cout << result << endl;
return 0;
}
It prints to console "Hello world" and returns 10
as expected.
However if I try to get on purpose an error, for example, by setting 9999
as file descriptor (which does not exist):
int main() {
long SYS_WRITE = 0x2000004;
long STDOUT = 1;
long result;
char* str = "Hello world";
__asm__ __volatile__ (
"syscall;\n"
: "=a" (result)
: "a" (SYS_WRITE), "D" (9999), "S" (str), "d" (10)
:
);
std::cout << result << endl;
return 0;
}
It returns 9
, which corresponds to EBADFD
error, which is OK, but the result is not negative. I would expect the error result to be negative, on Linux it is negative.
On Mac, how do I know that syscall
returned an error?