mach-detours/sample/main.c

41 lines
833 B
C
Raw Normal View History

2025-09-30 00:09:10 +02:00
// Copyright (c) Lysann Tranvouez. All rights reserved.
2025-09-30 23:51:28 +02:00
#include <assert.h>
#include <string.h>
2025-09-30 00:09:10 +02:00
#include "mach_detours.h"
char* (*real_strerror)(int errno) = strerror;
static int counter = 0;
char* my_strerror(const int errno)
{
counter++;
return real_strerror(errno);
}
int main(int argc, const char* argv[])
{
counter = 0;
strerror(0);
assert(counter == 0);
detour_transaction_begin();
2025-09-30 23:51:28 +02:00
detour_attach((detour_func_t*)&real_strerror, (detour_func_t)my_strerror);
2025-09-30 00:09:10 +02:00
detour_transaction_commit();
assert(counter == 0);
strerror(0);
assert(counter == 1);
2025-09-30 00:11:00 +02:00
detour_transaction_begin();
2025-09-30 23:51:28 +02:00
detour_detach((detour_func_t*)&real_strerror, (detour_func_t)my_strerror);
2025-09-30 00:11:00 +02:00
detour_transaction_commit();
assert(counter == 1);
strerror(0);
assert(counter == 1);
2025-09-30 00:09:10 +02:00
return 0;
}