How to Define a Custom read() in C Without Hiding the System Call
This article explains how to create a user‑defined read() function in C that coexists with the standard library read() by using the static keyword and separate translation units, providing code examples, compilation steps, and execution results.
Problem Statement
A project already uses the standard read() system call. Developers need to provide their own function named read, have main() call the custom version, and still be able to invoke the original library function when required.
Solution Using static
Declare the custom function as static in the source file where it is defined. A static function has file‑level linkage, so it is visible only within that translation unit. Other source files can define functions with the same name without conflict, allowing the original library function to be accessed from a different file.
Simple Example (single file)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
static void read() {
printf("my read func()
");
}
int main() {
read();
return 0;
}Running this program prints my read func(), showing that the static custom read() hides the system call within the same translation unit.
Calling Both Custom and System read()
To use both versions, place the custom read() in one source file and the code that calls the real system read() in another file.
File test.c (uses the real system call)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void test() {
int fd;
char buf[128] = {0};
fd = open("123.c", O_RDWR);
if (fd < 0) {
perror("open fail
");
return;
}
read(fd, buf, 16); // calls the system read
printf("enter test(): %s
", buf);
}File 123.c (defines a static custom read() )
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
extern void test();
static void read() {
printf("my read func()
");
test(); // calls test(), which uses the system read
}
int main() {
read();
return 0;
}Compile both files together (e.g., gcc 123.c test.c -o demo) and execute ./demo. The output demonstrates:
The static custom read() prints its message.
The test() function opens 123.c and invokes the real system read() to read data.
The data read by the system call is printed by test().
Call Sequence
main()in 123.c starts execution.
The static read() defined in 123.c runs and prints my read func().
The static read() calls test() from test.c. test() opens 123.c and calls the system read() to read bytes.
The buffer content is printed by test().
Key Points
Using static gives a function internal linkage, preventing name clashes with library symbols.
Separate translation units allow a custom function to coexist with the original library function.
The original read() can still be accessed by calling it from a file that does not contain a static definition with the same name.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
