APUE习题[实现dup2函数功能,不使用fcntl]

简介:

学习,记录。

int dup2(int oldhandle, int newhandle);

函数功能:

    复制文件句柄,newhandle指定的dup2和dup的区别就是可以用newfd参数指定新描述符的数值,如果newfd已经打开,则先将其关闭。如果newfd等于oldfd,则dup2返回newfd, 而不关闭它。dup2函数返回的新文件描述符同样与参数oldfd共享同一文件表项。

关键部分实现思路:   
    先close关闭需要复制到的文件描述符newdup。
    连续dup,每dup一次产生的新的fd记录下来。
    当新产生的fd等于需要产生的fd的时候,跳出循环,并把前面产生的fd全都close掉,
返回该描述符。

注释挺详细,看注释吧

执行结果:


 
 
  1. //Code by Pnig0s1992 
  2. //Date:2012,3,28 
  3. #include <unistd.h> 
  4. #include <fcntl.h> 
  5. #include <string.h> 
  6. #include <sys/types.h> 
  7. #include <sys/stat.h> 
  8.  
  9.  
  10. int my_dup(int olddup,int newdup); 
  11.  
  12. int main(int argc,char ** argv) 
  13.     int newdup = 3; 
  14.     const char * filename = "newfile.txt"
  15.     int fd = open(filename,O_RDWR); 
  16.     int newfd = my_dup(fd,newdup); 
  17.     if(write(newfd,"Test new fd.",strlen("Test new fd.")) < 0) 
  18.     { 
  19.         printf("Use new fd write file failed."); 
  20.         exit(2); 
  21.     }else 
  22.     { 
  23.         printf("Write successfully."); 
  24.     } 
  25.     exit(0); 
  26.  
  27. int my_dup(int olddup,int newdup) 
  28.     int tempdup; 
  29.     int icount = 0; 
  30.     int filedesarr[newdup]; 
  31.     if((tempdup = dup(olddup)) == -1) //判断原文件描述服是否有效 
  32.     { 
  33.         printf("the file desp is invalid."); 
  34.         exit(1); 
  35.     }else 
  36.     { 
  37.         close(tempdup); 
  38.     } 
  39.  
  40.     if(newdup == olddup) //若新旧文件描述符相等则直接返回 
  41.     { 
  42.         return olddup; 
  43.     } 
  44.     close(newdup);//关闭要复制的文件描述符 
  45.     for(icount = 0;icount<newdup+1;icount++) //循环复制文件描述符 
  46.     { 
  47.         filedesarr[icount] = 0; 
  48.         tempdup = dup(newdup); 
  49.         if(tempdup < 0) 
  50.         { 
  51.             return -1; 
  52.         }else 
  53.         { 
  54.             if(tempdup == newdup) 
  55.             { //若复制后的文件描述符于指定的相等则跳出 
  56.                 break
  57.             }else
  58.                 filedesarr[icount] = 1; //否则将对应下标的数组元素置为1 
  59.             } 
  60.         } 
  61.     } 
  62.     for(icount = 0;icount<newdup+1;icount++) //关闭之前打开的非指定描述符 
  63.     { 
  64.         if(filedesarr[icount] == 1) 
  65.         { 
  66.             close(icount); 
  67.         } 
  68.     } 
  69.     return tempdup; 

 













本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/819830,如需转载请自行联系原作者

相关文章
|
4月前
|
程序员 编译器 C语言
C中的 malloc 和C++中的 new 有什么区别
在C语言中,`malloc`函数用于在运行时分配内存,返回指向所分配内存的指针,需显式包含头文件 `&lt;stdlib.h&gt;`。而在C++中,`new`不仅分配内存,还对其进行构造初始化,且直接使用类型声明即可,无需额外包含头文件。`new`还支持数组初始化,能更好地融入C++的面向对象特性,而`malloc`仅作为内存分配工具。使用完毕后,`free`和`delete`分别用于释放`malloc`和`new`分配的内存。
82 21
|
8月前
|
程序员 C语言
C语言memcpy()函数用法
C语言memcpy()函数用法
|
8月前
|
C语言
C语言——oj刷题——模拟实现库函数strlen
C语言——oj刷题——模拟实现库函数strlen
54 0
|
9月前
|
C语言
C语言进阶21收尾(编程练习)(atoi,strncpy,strncat,offsetof模拟实现+找单身狗+宏交换二进制奇偶位)(下)
C语言进阶21收尾(编程练习)(atoi,strncpy,strncat,offsetof模拟实现+找单身狗+宏交换二进制奇偶位)
53 0
|
9月前
|
C语言
C语言进阶21收尾(编程练习)(atoi,strncpy,strncat,offsetof模拟实现+找单身狗+宏交换二进制奇偶位)(上)
C语言进阶21收尾(编程练习)(atoi,strncpy,strncat,offsetof模拟实现+找单身狗+宏交换二进制奇偶位)
124 0
|
存储 程序员
库函数(strcpy+memset函数精讲)
库函数(strcpy+memset函数精讲)
|
存储 C语言
【C语言】 --- getopt()函数的使用简析
【C语言】 --- getopt()函数的使用简析
180 0
|
存储 C语言 C++
【C语言】模拟实现memcpy库函数
【C语言】模拟实现memcpy库函数
【C语言】模拟实现memcpy库函数
|
C语言
C语言中typedef和define对比分析
C语言中typedef和define对比分析
116 0
C语言:sizeof和strlen的区别
1.指针在32位平台和64位平台的大小是不同的,32位下是4字节,64位下是8字节