What can I do by calling this function in the main function? How is this function implemented? The following describes the functions and production of this function. The Code is as follows:
#001 void open_devnull_stdio (void)
#002 {
#003 int fd;
#004 static const char * name = "/dev/_ null __";
This Code specifies the name of the device node to be created. This is a null device.
#005 if (mknod (name, S_IFCHR | 0600, (1 <8) | 3) = 0 ){
This line calls the mknod function to create a device node/dev/_ null __. An empty node is regarded as the output black hole. It cannot be entered, and the characters written to it will never be full.
#006 fd = open (name, O_RDWR );
This line of code is to open an empty device for reading and writing.
#007 unlink (name );
This line of code deletes a name from the file system. If the name is the last connection to the file and no other process opens the file, the file corresponding to the name will be deleted.
#008 if (fd> = 0 ){
#009 dup2 (fd, 0 );
#010 dup2 (fd, 1 );
#011 dup2 (fd, 2 );
#012 if (fd> 2 ){
#013 close (fd );
#014}
#015 return;
#016}
This code is used to check whether the empty device is successfully turned on and whether the device is successfully redirected. If yes, the system returns the result.
#017}
#018
#019 exit (1 );
This line of code exits when an error occurs when an empty device node is created.
#020}
#021
With the above code, you can create a/dev/_ null device and test whether the device is successfully created.