The following code is taken from Linux 0.11. Although the code is outdated, its implementation is very intuitive and simple. It is of great value and help for understanding dup implementation. Through this code, you can easily understand the thoughts behind dup.
//// Copy the file handle (descriptor ). -- Fs/fcntl. c
// The fd parameter is the file handle to be copied. arg specifies the minimum value of the new file handle.
// Return the new file handle or error code.
Static int
Dupfd (unsigned int fd, unsigned int arg)
{
// If the file handle value is greater than the maximum number of files opened by a program, or the file structure of the handle does not exist, an error occurs,
// Return the error code and exit.
If (fd> = NR_OPEN |! Current-> filp [fd])
Return-EBADF;
// If the specified new handle value arg is greater than the maximum number of files opened, an error is returned and an error code is exited.
If (arg> = NR_OPEN)
Return-EINVAL;
// Search for the items whose index number is greater than or equal to arg but not used in the file structure pointer array of the current process.
While (arg <NR_OPEN)
If (current-> filp [arg])
Arg ++;
Else
Break;
// If the new handle value arg is greater than the maximum number of files opened, an error occurs. The system returns an error code and exits.
If (arg> = NR_OPEN)
Return-EMFILE;
// Close the flag bitmap to reset the handle during execution. That is, the handle is not closed when running the exec () class function.
Current-> close_on_exec & = ~ (1 <arg );
// Make the file structure pointer equal to the pointer of the original handle fd, and increase the file reference count by 1.
(Current-> filp [arg] = current-> filp [fd])-> f_count ++;
Return arg; // return the new file handle.
}
//// Copy the file handle. The system calls the function.
// Copy the specified file handle oldfd. The new handle value is newfd. If newfd is enabled, disable it first.
Int
Sys_dup2 (unsigned int oldfd, unsigned int newfd)
{
Sys_close (newfd); // If the handle newfd has been opened, close it first.
Return dupfd (oldfd, newfd); // copy and return the new handle.
}
//// Copy the file handle. The system calls the function.
// Copy the specified file handle oldfd. The value of the new handle is the minimum unused handle currently.
Int
Sys_dup (unsigned int fildes)
{
Return dupfd (fildes, 0 );
}