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月前
|
存储 Linux
Linux文件编程(lseek函数和stat函数)
Linux文件编程(lseek函数和stat函数)
37 0
Linux文件编程(lseek函数和stat函数)
|
4月前
|
Unix Linux
fcntl()函数的作用及用法
fcntl()函数的作用及用法
63 0
|
11月前
|
监控 NoSQL C语言
研发中学习C(file函数、宏定义、gdb调试、strstr函数)
研发中学习C(file函数、宏定义、gdb调试、strstr函数)
条件编译 #if #endif #if #elif #else #endif #ifdef #if define 的使用方法 引头文件#include<> 和#include““的区别
条件编译 #if #endif #if #elif #else #endif #ifdef #if define 的使用方法 引头文件#include<> 和#include““的区别
97 0
【APUE】文件 I/O 操作(二)
【APUE】文件 I/O 操作(二)
91 0
|
存储
【APUE】文件 I/O 操作(一)
【APUE】文件 I/O 操作(一)
188 0
fcntl函数
fcntl函数
133 0
《UNIXLinux程序设计教程》一3.4 dup()和dup2()函数
本节书摘来自华章出版社《UNIXLinux程序设计教程》一 书中的第3章,第3.4节,作者:赵克佳 沈志宇,更多章节内容可以访问云栖社区“华章计算机”公众号查看。
1277 0