/******************************************************************** * Android Development tools Line_endings HAC King * Description: * This article is mainly to the Android source code in the Line_endings development tool to understand, * The purpose is to know the legendary amount of Dos,unix files between the working mechanism of the conversion. * 2016-5-3 Shenzhen Nanshan Ping Shan village Zengjianfeng *************************************************************** ****/#include<unistd.h>#include<fcntl.h>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<sys/stat.h>#defineBUFSIZE (1024*8)Static voidTo_unix (Char*buf);Static voidUnix_to_dos (Char* Buf2,Const Char*buf);//How to useintusage () {fprintf (stderr,"usage:line_endings Unix|dos files\n" "\ n" "Convert FILES to either UNIX or DOS line endings.\n"); return 1;}//defining node Data StructurestypedefstructNode {structNode *Next; Charbuf[bufsize*2+3];} Node;intMain (intargcChar**argv) { //Enumerate Unix,dos Two kinds of data enum{UNIX, DOS} ending; inti; //determine the number of parameters if(ARGC <2) { returnusage (); } //parameter comparison if(0= = strcmp ("Unix", argv[1]) {ending=UNIX; } Else if(0= = strcmp ("dos", argv[1]) {ending=DOS; } Else { returnusage (); } //There may be multiple arguments passed into the command line, with the For loop taking turns. for(i=2; i<argc; i++) { intFD; intLen; //Force impliedchmod (Argv[i], s_irusr| s_iwusr| s_irgrp|s_iwgrp); //Open FileFD =Open (Argv[i], O_RDWR); if(FD <0) {fprintf (stderr,"Unable to open file for Read/write:%s\n", Argv[i]); return 1; } //Get File SizeLen = Lseek (FD,0, Seek_end); Lseek (FD,0, Seek_set); //the file length is normal before conversion is necessary if(Len >0) { //Creating the root nodenode* root =malloc(sizeof(Node)); Node* node =Root; Node->buf[0] =0;//root node buf data bit 0 while(Len >0) { //Create the node and show the nodeNode->next =malloc(sizeof(Node)); Node= node->Next; Node->next =NULL; //here still do not understand why to +2, the back of the ' s ', that only need +1 on the line, why//+2 More, I don't understand. CharBuf[bufsize+2]; Ssize_t Amt; ssize_t amt2= Len < BUFSIZE?len:bufsize; AMT=Read (FD, buf, AMT2); if(Amt! =amt2) {fprintf (stderr,"Unable to read file:%s\n", Argv[i]); return 1; } Buf[amt2]=' /';//End of String//go to UNIX documents firstTo_unix (BUF); if(Ending = =UNIX) {strcpy (node-buf, BUF); } Else { //The main reason for bufsize*2 here is to be afraid that all of them are newline characters, so convert them.//is * 2, but do not understand why to +3, personal feeling up to a +1 on the line. Charbuf2[(bufsize*2)+3]; Unix_to_dos (Buf2, buf); strcpy (Node-buf, BUF2); } Len-=amt2; } //change the file length to 0 and start again from the file headerFtruncate (FD,0); Lseek (FD,0, Seek_set); //Loop writes the contents of the linked list to a file and releases the contents of the linked list while(Root) {ssize_t amt2= strlen (root->buf); if(Amt2 >0) {ssize_t amt= Write (FD, root->buf, AMT2); if(Amt! =amt2) {fprintf (stderr,"Unable to write file:%s\n", Argv[i]); return 1; }} node=Root; Root= root->Next; Free(node); }} close (FD); } return 0;}//This is the constant copy of the word Fu.voidTo_unix (Char*buf) { Char* p =buf; Char* Q =buf; while(*p) {if(p[0] =='\ r'&& p[1] =='\ n') { //dos*q ='\ n'; P+=2; Q+=1; } Else if(p[0] =='\ r') { //Old mac*q ='\ n'; P+=1; Q+=1; } Else { *q = *p; P+=1; Q+=1; } } *q =' /';}//This is just the opposite of To_unix's action.voidUnix_to_dos (Char* Buf2,Const Char*buf) { Const Char* p =buf; Char* Q =Buf2; while(*p) {if(*p = ='\ n') {q[0] ='\ r'; q[1] ='\ n'; Q+=2; P+=1; } Else { *q = *p; P+=1; Q+=1; } } *q =' /';}
Android Development tools Line_endings hacking