Android development tools line_endings hacking
/********************************************************************
* Android development tools line_endings hacking
* 说明:
* 本文主要是对android源代码中的line_endings开发工具进行了解读,
* 目的是为了知道传说中的dos,unix文件之间转换的工作机制。
*
* 2016-5-3 深圳 南山平山村 曾剑锋
*******************************************************************/ #include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h> #define BUFSIZE (1024*8)
static void to_unix(char* buf);
static void unix_to_dos(char* buf2, const char* buf); // 使用方法
int usage()
{
fprintf(stderr, "usage: line_endings unix|dos FILES\n"
"\n"
"Convert FILES to either unix or dos line endings.\n");
return ;
} // 定义Node数据结构
typedef struct Node {
struct Node *next;
char buf[BUFSIZE*+];
} Node; int
main(int argc, char** argv)
{
// 枚举UNIX,DOS两种数据
enum { UNIX, DOS } ending;
int i; // 参数个数判断
if (argc < ) {
return usage();
} // 参数比较
if ( == strcmp("unix", argv[])) {
ending = UNIX;
}
else if ( == strcmp("dos", argv[])) {
ending = DOS;
}
else {
return usage();
} // 命令行传入的参数可能有多个,利用for循环进行轮流转换。
for (i=; i<argc; i++) {
int fd;
int len; // force implied
chmod(argv[i], S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); // 打开文件
fd = open(argv[i], O_RDWR);
if (fd < ) {
fprintf(stderr, "unable to open file for read/write: %s\n", argv[i]);
return ;
} // 获取文件大小
len = lseek(fd, , SEEK_END);
lseek(fd, , SEEK_SET); // 文件长度正常才有必要进行转换
if (len > ) {
// 创建根节点
Node* root = malloc(sizeof(Node));
Node* node = root;
node->buf[] = ; // root节点的buf数据位0 while (len > ) {
// 创建节点,并出示节点
node->next = malloc(sizeof(Node));
node = node->next;
node->next = NULL; // 这里还是没搞太懂为什么要+2,后面有'\0',那只需要+1就行了,为什么
// 还要+2,没搞懂。
char buf[BUFSIZE+];
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 ;
}
buf[amt2] = '\0'; // 字符串结尾
// 先转成unix文档
to_unix(buf);
if (ending == UNIX) {
strcpy(node->buf, buf);
} else {
// 这里BUFSIZE*2的主要原因应该是怕所有的都是换行符,这样转换出来
// 就是*2了,但是没搞懂为什么要+3,个人感觉最多有个+1就行了。
char buf2[(BUFSIZE*)+];
unix_to_dos(buf2, buf);
strcpy(node->buf, buf2);
}
len -= amt2;
} // 将文件长度修改为0,并重新从文件头开始
ftruncate(fd, );
lseek(fd, , SEEK_SET);
// 循环将链表中的内容写入文件,并释放链表中的内容
while (root) {
ssize_t amt2 = strlen(root->buf);
if (amt2 > ) {
ssize_t amt = write(fd, root->buf, amt2);
if (amt != amt2) {
fprintf(stderr, "unable to write file: %s\n", argv[i]);
return ;
}
}
node = root;
root = root->next;
free(node);
}
}
close(fd);
}
return ;
} // 这里相当于是字符的的不断的拷贝
void
to_unix(char* buf)
{
char* p = buf;
char* q = buf;
while (*p) {
if (p[] == '\r' && p[] == '\n') {
// dos
*q = '\n';
p += ;
q += ;
}
else if (p[] == '\r') {
// old mac
*q = '\n';
p += ;
q += ;
}
else {
*q = *p;
p += ;
q += ;
}
}
*q = '\0';
} // 这里和to_unix的动作正好相反
void
unix_to_dos(char* buf2, const char* buf)
{
const char* p = buf;
char* q = buf2;
while (*p) {
if (*p == '\n') {
q[] = '\r';
q[] = '\n';
q += ;
p += ;
} else {
*q = *p;
p += ;
q += ;
}
}
*q = '\0';
}
Android development tools line_endings hacking的更多相关文章
- ADT Android Development Tools
ADT(Android Development Tools)在Eclipse编译IDE环境中,需安装ADT(Android Developer Tools)Plug-in,这是Android在Ecli ...
- ADT-bundle(Android Development Tools)环境配置
Android开发环境有两套比较主流的:ADT-bundle和Android Studio,前者是Eclipse插件的形式进行开发,后者是Android的官方IDE. ADT环境的配置与调试:(1)安 ...
- Android Development Tools 发生checkAndLoadTargetData错误
之前使用时没有出现任何问题的,我把D:\IDE\ADT\adt-bundle-windows-x86_64-20140321\eclipse目录下面的 eclipse.exe重名名为adt.exe并设 ...
- 在eclipse里卸载已安装的插件[例如Android Development Tools ADT]
在eclipse里卸载已安装的插件 有四种方法: 1.到plugins和features目录中找到你要卸载的插件的文件夹, ...
- android 安装 出现Android Native Development Tools不能安装
Software being installed: Android Native Development Tools 20.0.0.v201206242043-391819 (com.android. ...
- Websites for more Android development information
There is a vibrant, helpful Android developer community on the Web. Here are a numberof useful websi ...
- android sdk tools 一览
ANDROID SDK ADKROID SDK的工具划分为两部分,一部分是SDK tools,与平台无关,另一部分是Platform tools支持最新的安卓平台 SDK tools有 SDK m ...
- Could not install the app on the device, read the error above for details. Make sure you have an Android emulator running or a device connected and have set up your Android development environment:
Administrator@DESKTOP-EHCTIOR MINGW64 /d/react-native-eyepetizer (master) $ react-native run-android ...
- [Android]Eclipse 安装 ADT[Android Development Tooling] 失败的两种解决办法
原因 最近想在新装的 Win7 里搭建一下 Android 的开发环境,虽然现在有 Android Studio 了,不过还是习惯 Eclipse 一点.众所周知的原因,Eclipse 直接安装 AD ...
随机推荐
- hadoop 技巧
通过--config指定不同的集群 bin/hadoop --config ./conf_time/ dfs -ls /user/rd/*/for_*/ip_table/output/ rd下是都读写 ...
- Oracle数据库表的备份和数据表的删除操作
--Oracle数据库中的表备份: --备份语句:在备份之后就可以将这张表的所有数据源删除了,但是之后有人对这张表的数据进行操作,但是在操作完成之后要记得将数据表恢复 CREATE TABLE DZH ...
- 清除HTML中的特殊字符
/// <summary> /// 清楚HTML中的特殊字符 /// </summary> /// <param name=&q ...
- Unity3D脚本中文系列教程(十七)
http://dong2008hong.blog.163.com/blog/static/469688272014032332976/ ◆ Static function PrefixLabel(to ...
- windows service 安装和卸载指令
添加服务: cd C:\Windows\Microsoft.NET\Framework\v4.0.30319InstallUtil.exe D:\OneKeyWebSiteDeployment\Ser ...
- javascript加速运动
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- VisualSVN Server的windows 2003配置和使用方法(图文并茂)
1.为什么要用VisualSVN Server,而不用Subversion? 回答: 因为如果直接使用Subversion,那么在Windows 系统上,要想让它随系统启动,就要封装SVN Serve ...
- mac下安装mysql 连接时候报错 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
http://segmentfault.com/q/1010000000094608 同样的问题,先在网上搜了下其他人解决这个问题的方法,是去修改mysql.cnf文件添加mysql.sock文件路径 ...
- 重写equals()方法时,需要同时重写hashCode()方法
package com.wangzhu.map; import java.util.HashMap; /** * hashCode方法的主要作用是为了配合基于散列的集合一起正常运行,<br/&g ...
- URL中的特殊字符处理笔记
URL中的特殊字符 有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了.编码的格式为:%加字符的ASCII码,即一个百分号%,后面跟对应字符的ASCII ...