Understanding Unix/Linux Programming-cp指令练习
2016-08-26 18:12
567 查看
1 #include <stdio.h> 2 3 #include <unistd.h> 4 5 #include <fcntl.h> 6 7 #include <stdlib.h> 8 9 10 11 #define BUFFERSIZE 4096 12 13 #define COPYMODE 0644 14 15 16 17 void oops(char * , char * ) ; 18 19 20 21 main(int ac , char * av[]){ 22 23 int in_fd , out_fd , n_chars ; 24 25 char buf[BUFFERSIZE]; 26 27 28 29 if( ac != 3 ){ 30 31 fprintf(stderr, "usage:%s source destination\n", *av ); 32 33 exit(1); 34 35 } 36 37 38 39 if( (in_fd = open(av[1] , O_RDONLY)) == -1 ){ 40 41 oops("Can not open " , av[1] ); 42 43 } 44 45 46 47 if(( out_fd = creat(av[2] , COPYMODE)) == -1 ){ 48 49 oops("Can not open " , av[2] ); 50 51 } 52 53 54 55 while( (n_chars = read(in_fd , buf , BUFFERSIZE ) ) > 0 ){ 56 57 58 59 if( write(out_fd , buf , n_chars) != n_chars ) 60 61 { 62 63 oops("Write error to " , av[2]); 64 65 } 66 67 } 68 69 70 71 if(n_chars == -1) 72 73 { 74 75 oops("Read error from " , av[1]); 76 77 } 78 79 80 81 if( close(in_fd ) == -1 || close( out_fd ) == -1 ){ 82 83 oops("Error closing files." , ""); 84 85 } 86 87 88 89 } 90 91 92 93 void oops(char * s1 , char * s2 ){ 94 95 fprintf(stderr, "Error:%s\n", s1 ); 96 97 perror(s2); 98 99 exit(1); 100 101 }
来自为知笔记(Wiz)
相关文章推荐
- Understanding Unix/Linux Programming-pwd指令练习
- Understanding Unix/Linux Programming-ls指令练习二
- Understanding Unix/Linux Programming-ls指令练习一
- Understanding Unix/Linux Programming 笔记:chapter 12:连接和协议:编写Web服务器
- Understanding Unix/Linux Programming 笔记:chapter 8:进程和程序:编写命令解释器sh
- Understanding Unix/Linux Programming 笔记:chapter 13:基于数据报(Datagram)的编程:编写许可证服务器
- Understanding Unix/Linux Programming-stty指令练习
- Understanding Unix/Linux Programming note:chapter 2, chapter 3, chapter 4
- Understanding Unix/Linux Programming-文件、设备和流
- Understanding Unix/Linux Programming 笔记:chapter 11:连接到近端或远端的进程:服务器与Socket(套接字)
- Understanding Unix/Linux Programming-用户程序:play_again0
- Understanding Unix/Linux Programming-设备文件与磁盘连接的概念
- Understanding Unix/Linux Programming-信号与play_again4.c的准备知识
- Understanding Unix/Linux Programming-用户程序:play_again2
- Understanding Unix/Linux Programming-时钟编程:Alarms
- Understanding Unix/Linux Programming 笔记:chapter 14:线程机制:并发函数的使用
- Understanding Unix/Linux Programming-用户程序:play_again1
- Understanding Unix/Linux Programming-终端控制和信号
- Understanding Unix/Linux Programming note:chapter 6:为用户编程:终端控制和信号
- Understanding Unix/Linux Programming-用户程序play_again4.c