Linux DD Source Program

Source: Internet
Author: User



#include   "stdio.h" #include   "stdlib.h" #include   "sys/stat.h" #include   "Sys/types.h" #include   "Fcntl.h" #define  block_size 512#define buf_size   1024#define total_ buf   (buf_size<<1) typedef struct {char data[buf_size*2+1];int base,top;int  length;} BUF_QUEUE;VOID&NBSP;DO_DD (int infilefd ,int outfilefd ,int iperbytes ,int  Operbytes ,int cperbytes ,int totalblocks); Static inline int buf_empty (buf_ QUEUE&NBSP;*BUF); Static inline int buf_length (BUF_QUEUE&NBSP;*BUF); Int read2buf (int  fd,buf_queue *buf,int size); Int writefrombuf (int fd,buf_queue *buf,int size); int  main (INT&NBSP;ARGC,CHAR&NBSP;**ARGV) {int i;int iperbytes=block_size,operbytes=block_size, cperbytes=0;int totalblocks = -1,seekblocks=0,skipblocks=0;clock_t begin,end;off_t  Fbegin,fend;int noxfer=0;int infilefd,outfilefd;char *infile=null,*outfile=null,*iflags=null,*oflags= Null;for (i=1;i<argc ;i++) {if (!strncmp (Argv[i], "BS", Strlen ("BS"))) {Iperbytes = operbytes  = atoi (Argv[i]+strlen ("BS") +1);} else if  (!STRNCMP (Argv[i], "CBS", Strlen ("CBS"))) {Cperbytes = atoi ("Argv[i]+strlen (" CBS ") +1);} else if  (!STRNCMP (Argv[i], "conv")) {}else if  (!strncmp (Argv[i], "Count", strlen ("Count"))) { Totalblocks = atoi (Argv[i]+strlen ("Count") +1);} else if  (!STRNCMP (Argv[i], "IBS", strlen ("IBS"))) {Iperbytes = atoi (Argv[i]+strlen ("IBS") +1);} else if  (!STRNCMP (Argv[i], "Obs", strlen ("Obs"))) {Operbytes = atoi (Argv[i]+strlen ("Obs") +1);} else if  (!STRNCMP (Argv[i], "If", strlen ("if")) {Infile = argv[i]+strlen ("if") +1;} else if  (!STRNCMP (Argv[i], "of", strlen ("of"))) {Outfile = argv[i]+strlen ("of") +1;} else if  (!STRNCMP (Argv[i], "Iflag", strLen ("Iflag"))) {Iflags = argv[i]+strlen ("Iflag") +1;} else if  (!STRNCMP (Argv[i], "Oflag", strlen ("Oflag"))) {Oflags = argv[i]+strlen ("Oflag") +1;} else if  (!STRNCMP (Argv[i], "seek", strlen ("seek")) {Seekblocks = atoi (argv[i] +  Strlen ("seek") +1);} else if  (!STRNCMP (Argv[i], "Skip", strlen ("Skip"))) {Skipblocks = atoi (argv[i] +  Strlen ("Skip") +1);} else if  (!STRNCMP (Argv[i], "status", strlen ("status")) {noxfer =!strncmp (Argv[i]+strlen ("status") +1, "Noxfer", strlen ("Noxfer"));}} if (infile) {if ((Infilefd = open (infile,o_rdonly)) == -1) {perror ("Open infile error  :  "); return -1;}} Else{infilefd = fileno (stdin);} Lseek (Infilefd,skipblocks*block_size,seek_set), if (outfile) {if (seekblocks!=0) {Outfilefd = open ( outfile, o_append | o_wronly);} Else{outfilefd = open (outfile, o_creat| o_wronly| o_trunc,00660);} if (outfilefd == -1) {perror ("open outfile error : "); Close (infilefd); return -1;}} Else{outfilefd = fileno (stdout);} Lseek (Outfilefd,seekblocks*block_size,seek_set);d o_dd (Infilefd,outfilefd,iperbytes,operbytes,cperbytes, Totalblocks);} VOID&NBSP;DO_DD (int infilefd ,int outfilefd ,int iperbytes ,int operbytes  ,int cperbytes ,int totalblocks) {buf_queue *buf;int readbytes = 0, Writebytes =0;if (  (iperbytes >  (buf_size*2))  &&  (operbytes >   (BUF_SIZE&NBSP;*&NBSP;2)) iperbytes = operbytes = buf_size*2;else{if (iperbytes  > buf_size) iperbytes = buf_size;if (operbytes > buf_size) operbytes =  Buf_size;} if (buf =  (buf_queue* ) malloc (sizeof (buf_queue)) ==null) perror ("malloc memory failed  : "); buf->base=buf->top=0;buf->length =0;readbytes = iperbyteS;writebytes = operbytes;while (readbytes == iperbytes ) {while (Buf_empty (BUF)   >= iperbytes)  &&  (readbytes == iperbytes)  ) {readbytes =  Read2buf (infilefd,buf,iperbytes);} while ( buf_length (BUF)  >= operbytes &&  (writebytes == operbytes) )  {writefrombuf (outfilefd,buf,operbytes);}} Writefrombuf (Outfilefd,buf,buf_length (BUF));} Inline int buf_empty (buf_queue *buf) {return total_buf-buf->length;} Inline int buf_length (buf_queue *buf) {return buf->length;} read data into top and need the stuation is size <  buf_empty;int read2buf (int fd,buf_queue *buf,int size) {Int readbytes,totalread  = 0,toread;if (size >  (total_buf-buf->top)) toread = total_buf -  Buf->top;elsetoread = size;while (Toread){if ((Readbytes = read (Fd,buf->data+buf->top,toread))  == -1) {perror ("Read file  error :  "); return -1;} totalread += readbytes;buf->length +=readbytes;buf->top =  (buf->top +  readbytes)  % total_buf;if (readbytes != toread) return totalread;toread =  size - toread;size = toread;} Return totalread;} read data into top and need the stuation is size <  buf_empty;int writefrombuf (int fd,buf_queue *buf,int size) {int writebytes, Totalwrite = 0,towrite;if (size >  (total_buf-buf->base)) Towrite = TOTAL_BUF  - buf->base;elsetowrite = size;while (Towrite) {if (Writebytes = write (fd,buf- >data+buf->base,towrite)  == -1) {perror ("write error "); return -1;} Totalwrite += writebytes;buf->length-=writebytes;buf->base =  (buf->base + writebytes)  %  Total_buf;if (Writebytes != towrite) return totalwrite;towrite = size -  Towrite;size = towrite;} Return totalwrite;} /*//read data into top and need the stuation is size <  buf_empty;int read2buf (int fd,char *buf,char *base,char *top,int size) {int  toread = 0,readbytes = 0;if (top > base) {toread = buf +  buf_size - topif (Size <= toread) {toread = size;readbytes =  Read (Fd,top,toread);top = top +  ((top - buf + readbytes)  %  buf_size) Return readbytes;} Else{readbytes = read (Fd,top,toread);top = top +  ((top - buf +  readbytes)  % buf_size) if (readbyTes != toread) Return readbytes;else{toread = size-toread;readbytes = read ( Fd,top,toread);top = top +  ((top - buf + readbytes)  % BUF_ SIZE) return size-(toread - size)}}}else{readbytes = read (fd,top,size); top =  top +  ((top - buf + readbytes)  % buf_size) return readbytes;}} */


Linux DD Source Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.