<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hacklog &#187; c/c++</title>
	<atom:link href="http://ihacklog.com/cat/programming/c/feed" rel="self" type="application/rss+xml" />
	<link>http://ihacklog.com</link>
	<description>荒野无灯weblog</description>
	<lastBuildDate>Sat, 04 Feb 2012 12:30:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>BlowFish 加密算法Bcrypt</title>
		<link>http://ihacklog.com/programming/c/blowfish-cipher.html</link>
		<comments>http://ihacklog.com/programming/c/blowfish-cipher.html#comments</comments>
		<pubDate>Fri, 14 Oct 2011 14:56:57 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=4939</guid>
		<description><![CDATA[Blowfish是1993年布鲁斯·施奈尔(Bruce Schneier)开发的对称密钥区块加密算法，区块长为64位，密钥为1至448位的可变长度。与DES等算法相比，其处理速度较快。因为其无须授权即可使用，作为一种自由授权的加密方式在SSH、文件加密软件等被广泛地使用。 关于此算法的发明者： 布鲁斯·施奈尔 （Bruce Schneier，1963年1月15日－）是一位美国的密码学学者、信息安全专家与作家。他撰写了数本信息安全与密码学相关的书籍，并且创办了BT公司并担任其首席技术官(CTO)。 分组密码...]]></description>
			<content:encoded><![CDATA[<blockquote><p>Blowfish是1993年布鲁斯·施奈尔(Bruce Schneier)开发的对称密钥区块加密算法，区块长为64位，密钥为1至448位的可变长度。与DES等算法相比，其处理速度较快。因为其无须授权即可使用，作为一种自由授权的加密方式在SSH、文件加密软件等被广泛地使用。</p></blockquote>
<p>关于此算法的发明者：</p>
<blockquote><p><a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fzh.wikipedia.org%2Fwiki%2FBruce_Schneier" target="_blank">布鲁斯·施奈尔</a> （Bruce Schneier，1963年1月15日－）是一位美国的密码学学者、信息安全专家与作家。他撰写了数本信息安全与密码学相关的书籍，并且创办了BT公司并担任其首席技术官(CTO)。</p></blockquote>
<h3>分组密码(Block cypher)</h3>
<p>分组密码（Block cypher，又称<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fzh.wikipedia.org%2Fwiki%2F%25E5%25A1%258A%25E5%25AF%2586%25E7%25A2%25BC" target="_blank">分块密码</a>），是一种对称密钥密码。它的特点是将明文分成多个等长的组，并用相同的密码算法和密钥对每组分别进行加密和解密。其中典型的如DES和AES作为美国政府核定的标准加密算法，应用领域从电子邮件加密到银行交易转帐，非常广泛。<br />
blowfish属于分组密码中的一种。</p>
<p>要了解blowfish加密算法，也许我们应该先了解一下<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FFeistel_cipher" target="_blank">Feistel 密码</a>,因为blowfish加密算法也是feistel密码算法中的一种：<br />
假设F是轮函数，然后K<sub>0</sub>到K<sub>n</sub>作为0到n轮的sub-keys,<br />
基本的操作如下：</p>
<div style="font-size:16px;">
把明文分为相等的两块：L<sub>0</sub>和R<sub>0</sub>,<br />
对于每一轮i=0,1,2,&#8230;,n 进行如下运算：<br />
L<sub>i+1</sub> = R<sub>i</sub><br />
R<sub>i+1</sub> = L<sub>i</sub> ^ F(R<sub>i</sub>,K<sub>i</sub>)<br />
最后得到密文(R<sub>n+1</sub>,L<sub>n+1</sub>)<br />
解密(R<sub>n+1</sub>,L<sub>n+1</sub>)是通过如下步骤来完成的：<br />
令i=n,n-1,&#8230;,0<br />
R<sub>i</sub> = L<sub>i+1</sub><br />
L<sub>i</sub> = R<sub>i+1</sub> ^ F(L<sub>i+1</sub>,K<sub>i</sub>)<br />
最后又得到原来的(L<sub>0</sub>,R<sub>0</sub>)了
</div>
<p>这么说可能不好理解，看下图（来自wikipedia)就清楚了：<br />
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Feistel_cipher_diagram_en.svg/511px-Feistel_cipher_diagram_en.svg.png" alt="feistel cypher" style="background-color:#cdcdcd;"/><br />
简单地说：<br />
就是一种分组密码，一般分为64位一组，一组分左右部分，进行一般为16轮的迭代运算，每次迭代完后交换左右位置，可以自己进行设计的有:<br />
分组大小<br />
密钥长度<br />
轮次数<br />
子密钥生成<br />
轮函数</p>
<p>然后应该了解一下<strong><a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fzh.wikipedia.org%2Fwiki%2F%E5%9D%97%E5%AF%86%E7%A0%81%E7%9A%84%E5%B7%A5%E4%BD%9C%E6%A8%A1%E5%BC%8F" target="_blank">块密码的工作模式</a></strong>：<br />
from wikipedia:</p>
<blockquote><p>密码学中，块密码的工作模式允许使用同一个块密码密钥对多于一块的数据进行加密，并保证其安全性。[1][2] 块密码自身只能加密长度等于密码块长度的单块数据，若要加密变长数据，则数据必须先被划分为一些单独的密码块。通常而言，最后一块数据也需要使用合适填充方式将数据扩展到符合密码块大小的长度。一种工作模式描述了加密每一数据块的过程，并常常使用基于一个通常称为初始化向量的附加输入值以进行随机化，以保证安全[1]。</p>
<p>工作模式主要用来进行加密和认证。[1][3] 对加密模式的研究曾经包含数据的完整性保护，即在某些数据被修改后的情况下密码的误差传播特性。后来的研究则将完整性保护作为另一个完全不同的，与加密无关的密码学目标。部分现代的工作模式用有效的方法将加密和认证结合起来，称为认证加密模式。[2]</p>
<p>虽然工作模式通常应用于对称加密[2]，它亦可以应用于公钥加密，例如在原理上对RSA进行处理，但在实用中，公钥密码学通常不用于加密较长的信息，而是使用混合加密方案</p></blockquote>
<p>一般来说常用的加密模式有这么几种：</p>
<blockquote><p> 电子密码本（ECB）<br />
 密码块链接（CBC）：在CBC模式中，每个平文块先与前一个密文块进行异或后，再进行加密。在这种方法中，每个密文块都依赖于它前面的所有平文块。同时，为了保证每条消息的唯一性，在第一个块中需要使用初始化向量。<br />
 填充密码块链接（PCBC）<br />
 密文反馈（CFB）<br />
 输出反馈（OFB）<br />
 计数器模式（CTR）</p></blockquote>
<h3>初始化向量（IV）</h3>
<blockquote><p>初始化向量（IV，Initialization Vector）是许多工作模式中用于随机化加密的一块数据，因此可以由相同的明文，相同的密钥产生不同的密文，而无需重新产生密钥，避免了通常相当复杂的这一过程。</p>
<p>初始化向量与密钥相比有不同的安全性需求，因此IV通常无须保密，然而在大多数情况中，不应当在使用同一密钥的情况下两次使用同一个IV。对于CBC和CFB，重用IV会导致泄露平文首个块的某些信息，亦包括两个不同消息中相同的前缀。对于OFB和CTR而言，重用IV会导致完全失去安全性。另外，在CBC模式中，IV在加密时必须是无法预测的；特别的，在许多实现中使用的产生IV的方法，例如SSL2.0使用的，即采用上一个消息的最后一块密文作为下一个消息的IV，是不安全的[12]。</p></blockquote>
<h3>填充</h3>
<blockquote><p>块密码只能对确定长度的数据块进行处理，而消息的长度通常是可变的。因此部分模式（即ECB和CBC）需要最后一块在加密前进行填充。有数种填充方法，其中最简单的一种是在平文的最后填充空字符以使其长度为块长度的整数倍，但必须保证可以恢复平文的原始长度；例如，若平文是C语言风格的字符串，则只有串尾会有空字符。稍微复杂一点的方法则是原始的DES使用的方法，即在数据后添加一个1位，再添加足够的0位直到满足块长度的要求；若消息长度刚好符合块长度，则添加一个填充块。最复杂的则是针对CBC的方法，例如密文窃取，残块终结等，不会产生额外的密文，但会增加一些复杂度。布鲁斯·施奈尔和尼尔斯·弗格森提出了两种简单的可能性：添加一个值为128的字节（十六进制的80），再以0字节填满最后一个块；或向最后一个块填充n个值均为n的字节[13]。</p>
<p>CFB，OFB和CTR模式不需要对长度不为密码块大小整数倍的消息进行特别的处理。因为这些模式是通过对块密码的输出与平文进行异或工作的。最后一个平文块（可能是不完整的）与密钥流块的前几个字节异或后，产生了与该平文块大小相同的密文块。流密码的这个特性使得它们可以应用在需要密文和平文数据长度严格相等的场合，也可以应用在以流形式传输数据而不便于进行填充的场合。</p></blockquote>
<p>另外在mcrypt的man文档里也找到了加密模式的介绍：</p>
<h3>加密模式</h3>
<blockquote><p>modes of encryption:<br />
<strong>ECB</strong>:  The Electronic CodeBook mode. It is the simplest mode to use with a block cipher. Encrypts each block independently. </p>
<p><strong>CBC</strong>: The Cipher Block Chaining mode. It is better than ECB since the plaintext is XOR&#8217;ed with the previous ciphertext. A random block is placed as the first block so the same block or messages always encrypt to something different. (This is the default mode) </p>
<p><strong>CFB</strong>: The Cipher-Feedback Mode (in 8bit). This is a self-synchronizing stream cipher implemented from a block cipher. </p>
<p><strong>OFB</strong>: The Output-Feedback Mode (in 8bit). This is a synchronous stream cipher implemented from a block cipher. It is intended for use in noisy lines, because corrupted ciphertext blocks do not corrupt the plaintext blocks that follow. Insecure (because used in 8bit mode) so I recommend against using it. Added just for completeness. </p>
<p><strong>nOFB</strong>: The Output-Feedback Mode (in nbit). n Is the size of the block of the algorithm. This is a synchronous stream cipher implemented from a block cipher. It is intended for use in noisy lines, because corrupted ciphertext blocks do not corrupt the plaintext blocks that follow.</p></blockquote>
<p>via http://mcrypt.hellug.gr/mcrypt/mcrypt.1.html<br />
BTW,The author of mcrypt and libmcrypt is <strong>Nikos Mavroyanopoulos</strong>.</p>
<h3>分块大小</h3>
<p><a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FBlock_size_(cryptography)" target="_blank">Block size</a></p>
<blockquote><p>现代加密算法中，对称密钥算法通常被分为stream ciphers和block ciphers。分块密码处理固定长度的字符串位数(bit)。这个比特字符串的长度必须和分块大小一样长。输入（明文）和输出（密文）都是同样长度的。输出不能比输入短——这是遵循<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPigeonhole_principle" target="_blank">Pigeonhole principle</a>（鸽巢原理，又名狄利克雷抽屉原理、鸽笼原理）和密码必须是可逆的事实——然而，输出比输入更长又是不可取的。</p></blockquote>
<p>鸽巢原理：</p>
<blockquote><p>其中一种简单的表述法为：<br />
    若有n个笼子和n+1只鸽子，所有的鸽子都被关在鸽笼里，那么至少有一个笼子有至少2只鸽子。<br />
另一种为：<br />
    若有n个笼子和kn+1只鸽子，所有的鸽子都被关在鸽笼里，那么至少有一个笼子有至少k+1只鸽子。<br />
拉姆齐定理是此原理的推广。</p></blockquote>
<p>通俗点说：<br />
10只鸽子放进9个鸽笼，那么一定有一个鸽笼放进了至少两只鸽子。</p>
<p>S-box (Substitution-box,替换盒)<br />
在分块密码中，S-box通常被用来掩盖密钥和密文之间的关系。</p>
<blockquote><p>In general, an S-Box takes some number of input bits, m, and transforms them into some number of output bits, n: an m×n S-Box can be implemented as a lookup table with 2m words of n bits each. Fixed tables are normally used, as in the Data Encryption Standard (DES), but in some ciphers the tables are generated dynamically from the key; e.g. the Blowfish and the Twofish encryption algorithms. Bruce Schneier describes IDEA&#8217;s modular multiplication step as a key-dependent S-Box.</p></blockquote>
<p><a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FInternational_Data_Encryption_Algorithm" target="_blank">IDEA(International Data Encryption Algorithm)</a></p>
<p><a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FRijndael_S-box" target="_blank">Rijndael S-box</a></p>
<p>算法说明：<br />
BlowFish 使用了两个box,除了著名的S-box，还有一个p-box<br />
pbox有18个unsigned long元素<br />
sbox有4&#215;256个unsigned long元素<br />
  BlowFish算法中，有一个核心加密函数，该函数输入64位信息，运算后， 以64位密文的形式输出。 用BlowFish算法加密信息，需要两个过程：<br />
这里我看的源码是<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPaul_Kocher" target="_blank">Paul Kocher</a>的C语言版本。<br />
源码下载地址：<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.schneier.com%2Fblowfish-download.html" target="_blank">http://www.schneier.com/blowfish-download.html</a></p>
<p>1.密钥预处理<br />
2.信息加密</p>
<p>结构体定义：</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> P<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">16</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> S<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">256</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> BLOWFISH_CTX<span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>1.密钥预处理<br />
  BlowFish算法的源密钥——pbox和sbox是固定的。此pbox和sbox的值来自<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.super-computing.org%2Fpi-hexa_current.html" target="_blank">PI的十六进制数字值</a>，使用这些数的原因是这些数看不出有什么明显的规律。(<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPi" target="_blank">PI</a>)<br />
这里摘取1-500位：</p>
<pre class='smooth'>
3.243F6A8885 A308D31319 8A2E037073 44A4093822 299F31D008
  2EFA98EC4E 6C89452821 E638D01377 BE5466CF34 E90C6CC0AC
  29B7C97C50 DD3F84D5B5 B547091792 16D5D98979 FB1BD1310B
  A698DFB5AC 2FFD72DBD0 1ADFB7B8E1 AFED6A267E 96BA7C9045
  F12C7F9924 A19947B391 6CF70801F2 E2858EFC16 636920D871
  574E69A458 FEA3F4933D 7E0D95748F 728EB65871 8BCD588215
  4AEE7B54A4 1DC25A59B5 9C30D5392A F26013C5D1 B023286085
  F0CA417918 B8DB38EF8E 79DCB0603A 180E6C9E0E 8BB01E8A3E
  D71577C1BD 314B2778AF 2FDA55605C 60E65525F3 AA55AB9457
  48986263E8 144055CA39 6A2AAB10B6 B4CC5C3411 41E8CEA154
</pre>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">void</span> Blowfish_Init<span style="color: #009900;">&#40;</span>BLOWFISH_CTX <span style="color: #339933;">*</span>ctx<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>key<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> keyLen<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">int</span> i<span style="color: #339933;">,</span> j<span style="color: #339933;">,</span> k<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> data<span style="color: #339933;">,</span> datal<span style="color: #339933;">,</span> datar<span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">256</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> ORIG_S<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N <span style="color: #339933;">+</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; data <span style="color: #339933;">=</span> <span style="color: #208080;">0x00000000</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>k <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> k <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>k<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; data <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>data <span style="color: #339933;">&lt;&lt;</span> <span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> key<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; j <span style="color: #339933;">=</span> j <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">&gt;=</span> keyLen<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> ORIG_P<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">^</span> data<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; datal <span style="color: #339933;">=</span> <span style="color: #208080;">0x00000000</span><span style="color: #339933;">;</span><br />
&nbsp; datar <span style="color: #339933;">=</span> <span style="color: #208080;">0x00000000</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N <span style="color: #339933;">+</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Blowfish_Encrypt<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>datal<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>datar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> datal<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>i <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> datar<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">256</span><span style="color: #339933;">;</span> j <span style="color: #339933;">+=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Blowfish_Encrypt<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>datal<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>datar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> datal<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> datar<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>我们要加密一个信息，<br />
  需要自己选择一个key，用这个key对pbox和sbox进行变换，得到下一步信息加密<br />
  所要用的pbox和sbox。<br />
  具体的变化算法如下：<br />
  用原sbox: ORIG_S 填充 sbox</p>
<p>  然后，每次取key与data进行运算，运算后的结果送给pbox<br />
  运算过程是这样的：<br />
  进行N+2次运算（N=16),<br />
  令 32位无符号data为0,由于Key是unsigned char类型的，每次对data左移8位(一个字节)之后与<br />
  相应的key相或（即相加），当key长度小于4时，循环使用Key。</p>
<p>接下来，用bf_encypt加密一个全0的64位信息（分为datal和datar,各32位）<br />
用输出的结果datal和datar分别替换pbox[0]和pbox[1]<br />
然后，继续加密datal和datar,用输出结果替换pbox[2]和pbox[3]<br />
&#8230;&#8230;<br />
这样循环18次，把pbox全部替换完成。</p>
<p>接下来是对sbox的替换了。<br />
这次总共是循环4&#215;256次，每次循环的过程与上面的一样。<br />
不过这里用的datal和datar就是上面运算过后的datal和datar.</p>
<p>2.信息加密<br />
接下来看这个加密函数，上面的初始化过程已经用到它了。</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">void</span> Blowfish_Encrypt<span style="color: #009900;">&#40;</span>BLOWFISH_CTX <span style="color: #339933;">*</span>ctx<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>xl<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>xr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;Xl<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;Xr<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;temp<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">short</span> &nbsp; &nbsp; &nbsp; i<span style="color: #339933;">;</span><br />
<br />
&nbsp; Xl <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>xl<span style="color: #339933;">;</span><br />
&nbsp; Xr <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>xr<span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Xl <span style="color: #339933;">=</span> Xl <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Xr <span style="color: #339933;">=</span> F<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> Xl<span style="color: #009900;">&#41;</span> <span style="color: #339933;">^</span> Xr<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; temp <span style="color: #339933;">=</span> Xl<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Xl <span style="color: #339933;">=</span> Xr<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Xr <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; temp <span style="color: #339933;">=</span> Xl<span style="color: #339933;">;</span><br />
&nbsp; Xl <span style="color: #339933;">=</span> Xr<span style="color: #339933;">;</span><br />
&nbsp; Xr <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span><br />
<br />
&nbsp; Xr <span style="color: #339933;">=</span> Xr <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; Xl <span style="color: #339933;">=</span> Xl <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>N <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #339933;">*</span>xl <span style="color: #339933;">=</span> Xl<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #339933;">*</span>xr <span style="color: #339933;">=</span> Xr<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>与上面说的Feistel 密码的运算过程是一样的：<br />
把64位的明文分为l和r<br />
进行16轮运算：<br />
令i=0,1,2,3,&#8230;,N-1<br />
 Xl = Xl ^ ctx->P[i];<br />
 Xr = F(ctx, Xl) ^ Xr;<br />
若i&lt;N,交换Xl和Xr的值继续做上述运算。<br />
16轮运算完了之后，再做一次Xl与Xr的交换，因为第16次运算完了之后，实际上<br />
没有再做运算了，但是Xl与Xr还是交换了，因此，得换回来。</p>
<p>然后，Xr = Xr ^ ctx->P[N];<br />
  Xl = Xl ^ ctx->P[N + 1];<br />
现在的Xl和Xr就是加密后的数据了。<br />
加密过程图解：<br />
<img src="http://upload.wikimedia.org/wikipedia/en/3/34/BlowfishDiagram.png" alt="blowfish encryption" /></p>
<p>这加密过程中用到了F变换：</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">static</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> F<span style="color: #009900;">&#40;</span>BLOWFISH_CTX <span style="color: #339933;">*</span>ctx<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #993333;">unsigned</span> <span style="color: #993333;">short</span> a<span style="color: #339933;">,</span> b<span style="color: #339933;">,</span> c<span style="color: #339933;">,</span> d<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;y<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;d <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">short</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;x <span style="color: #339933;">&gt;&gt;=</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;c <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">short</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;x <span style="color: #339933;">&gt;&gt;=</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;b <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">short</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;x <span style="color: #339933;">&gt;&gt;=</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;a <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">short</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;y <span style="color: #339933;">=</span> ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>b<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;y <span style="color: #339933;">=</span> y <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>c<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;y <span style="color: #339933;">=</span> y <span style="color: #339933;">+</span> ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>d<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">return</span> y<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>这个变换是这样的：<br />
它接收一个32位的数据，然后从高位到低位分为四段，每段8位，依次给<br />
a,b,c,d<br />
取sbox[0][a]+sbox[1][b]，相加后的结果再与sbox[2][c]做异或运算，<br />
得出的结果再加上sbox[3][d]<br />
由于sbox的元素都是32位的，因此，F变换后的输出也是32位的。<br />
轮函数图解：<br />
<img src="http://upload.wikimedia.org/wikipedia/en/thumb/2/22/BlowfishFFunction.svg/250px-BlowfishFFunction.svg.png" alt="blowfish F function" style="background-color:#ddd;"/></p>
<p>解密函数：<br />
可以看到解密函数与加密函数非常的相似，只是把p0,p1,&#8230;,p17 逆序使用：</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">void</span> Blowfish_Decrypt<span style="color: #009900;">&#40;</span>BLOWFISH_CTX <span style="color: #339933;">*</span>ctx<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>xl<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>xr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;Xl<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;Xr<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;temp<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">short</span> &nbsp; &nbsp; &nbsp; i<span style="color: #339933;">;</span><br />
<br />
&nbsp; Xl <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>xl<span style="color: #339933;">;</span><br />
&nbsp; Xr <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>xr<span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> N <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">--</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Xl <span style="color: #339933;">=</span> Xl <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Xr <span style="color: #339933;">=</span> F<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> Xl<span style="color: #009900;">&#41;</span> <span style="color: #339933;">^</span> Xr<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Exchange Xl and Xr */</span><br />
&nbsp; &nbsp; temp <span style="color: #339933;">=</span> Xl<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Xl <span style="color: #339933;">=</span> Xr<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Xr <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #808080; font-style: italic;">/* Exchange Xl and Xr */</span><br />
&nbsp; temp <span style="color: #339933;">=</span> Xl<span style="color: #339933;">;</span><br />
&nbsp; Xl <span style="color: #339933;">=</span> Xr<span style="color: #339933;">;</span><br />
&nbsp; Xr <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span><br />
<br />
&nbsp; Xr <span style="color: #339933;">=</span> Xr <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; Xl <span style="color: #339933;">=</span> Xl <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #339933;">*</span>xl <span style="color: #339933;">=</span> Xl<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #339933;">*</span>xr <span style="color: #339933;">=</span> Xr<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>关于BlowFish 密码，个人认为可以把它理解为一种复杂的<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fzh.wikipedia.org%2Fwiki%2F%E5%BC%82%E6%88%96%E5%AF%86%E7%A0%81" target="_blank">异或密码</a>,异或运算的规律：</p>
<pre class='smooth'>
A ^ 0 = A
A ^ A = 0
(A ^ B) ^ C = A ^ (B ^ C)
(B ^ A) ^ A = B ^ 0 = B
</pre>
<p>按这种逻辑，文本序列的每个字符可以通过与给定的密钥进行按位异或运算来加密。如果要解密，只需要将加密後的结果与密钥再次进行按位异或运算即可。</p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4939";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4939" title="Permanent Link to BlowFish 加密算法Bcrypt" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4939</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2012年01月3日  //  <a href="http://ihacklog.com/security_optimization/js-malicious-code.html" title="Louis Han童鞋应该是中招了">Louis Han童鞋应该是中招了</a> (0)</li><li>2008年12月24日  //  <a href="http://ihacklog.com/memory/%e4%b8%80%e4%b8%aa%e4%ba%ba%e7%9a%84%e5%a4%a9%e7%a9%ba%e5%be%88%e8%93%9d%ef%bc%8c%e8%93%9d%e5%be%97%e6%9c%89%e7%82%b9%e5%bf%a7%e9%83%81%ef%bc%81.html" title="一个人的天空很蓝，蓝得有点忧郁！">一个人的天空很蓝，蓝得有点忧郁！</a> (0)</li><li>2011年07月6日  //  <a href="http://ihacklog.com/php/php-truth-table.html" title="PHP 真值表  ">PHP 真值表  </a> (2)</li><li>2009年06月17日  //  <a href="http://ihacklog.com/software/cool_software_share/pichuliquanzidongpeizhiipdizhiwangguandns.html" title="批处理全自动配置IP地址,网关,dns">批处理全自动配置IP地址,网关,dns</a> (3)</li><li>2010年07月17日  //  <a href="http://ihacklog.com/software/cool_software_share/currports.html" title="CurrPorts：windows下查看网络连接端口的利器">CurrPorts：windows下查看网络连接端口的利器</a> (0)</li><li>2010年12月12日  //  <a href="http://ihacklog.com/php/eclipse-svn-version-control-plug-in-installation-and-usage.html" title="Eclipse中SVN版本控制插件的安装和使用">Eclipse中SVN版本控制插件的安装和使用</a> (4)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/blowfish-cipher.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>关于C语言的char类型是有符号还是无符号</title>
		<link>http://ihacklog.com/programming/c/char-is-signed-or-unsigned-by-default.html</link>
		<comments>http://ihacklog.com/programming/c/char-is-signed-or-unsigned-by-default.html#comments</comments>
		<pubDate>Fri, 24 Jun 2011 07:53:04 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>
		<category><![CDATA[char]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[signed]]></category>
		<category><![CDATA[unsigned]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=4579</guid>
		<description><![CDATA[这是C语言里面比较纠结的一个问题。 有时候这个问题和平台或者编译器有关。 貌似gcc和 Ms cl 都默认是signed 123456789#include&#160; &#160; &#60;stdio.h&#62; #include &#160; &#160;&#60;limits.h&#62; int main&#40;&#41; &#123; &#160; &#160; &#160; &#160; printf&#40;&#34;%d\n&#34;,CHAR_MIN&#41;; &#160; &#...]]></description>
			<content:encoded><![CDATA[<p>这是C语言里面比较纠结的一个问题。<br />
有时候这个问题和平台或者编译器有关。<br />
貌似gcc和 Ms cl 都默认是signed</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include&nbsp; &nbsp; &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &nbsp; &nbsp;&lt;limits.h&gt;</span><br />
<br />
<span style="color: #993333;">int</span><br />
main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>CHAR_MIN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>输出是 -128 .</p>
<p>其实C标准并没有规定char 应该是unsigned 还是 signed<br />
事实上标准定义了三种类型：char, signed char 和unsigned char<br />
#include
<limits.h>然后检查CHAR_MIN 的值就知道默认的是有符号还是无符号。<br />
若等于0 ，则说明是无符号的，否则，它就是等于SCHAR_MIN (一般是-128）。<br />
我们查看下/usr/include/limits.h<br />
可以发现如下定义：</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/* Number of bits in a `char'.&nbsp; */</span><br />
<span style="color: #339933;"># &nbsp;define CHAR_BIT&nbsp; 8</span><br />
<br />
<span style="color: #808080; font-style: italic;">/* Minimum and maximum values a `signed char' can hold. &nbsp;*/</span><br />
<span style="color: #339933;"># &nbsp;define SCHAR_MIN (-128)</span><br />
<span style="color: #339933;"># &nbsp;define SCHAR_MAX 127</span><br />
<br />
<span style="color: #808080; font-style: italic;">/* Maximum value an `unsigned char' can hold. &nbsp;(Minimum is 0.) &nbsp;*/</span><br />
<span style="color: #339933;"># &nbsp;define UCHAR_MAX 255</span><br />
<br />
<span style="color: #808080; font-style: italic;">/* Minimum and maximum values a `char' can hold. &nbsp;*/</span><br />
<span style="color: #339933;"># &nbsp;ifdef __CHAR_UNSIGNED__</span><br />
<span style="color: #339933;"># &nbsp; define CHAR_MIN 0</span><br />
<span style="color: #339933;"># &nbsp; define CHAR_MAX UCHAR_MAX</span><br />
<span style="color: #339933;"># &nbsp;else</span><br />
<span style="color: #339933;"># &nbsp; define CHAR_MIN SCHAR_MIN</span><br />
<span style="color: #339933;"># &nbsp; define CHAR_MAX SCHAR_MAX</span><br />
<span style="color: #339933;"># &nbsp;endif</span></div></td></tr></tbody></table></div>
<p>当然，这个默认行为也是可以通过编译器来改变的，如gcc可通过加<strong>-funsigned-char</strong><br />
来指定其默认为无符号型。<br />
MSVC 可以用<strong>/J</strong>来指定char 默认为无符号型。</p>
<p>本文参考：<br />
<a href="http://stackoverflow.com/questions/2054939/char-is-signed-or-unsigned-by-default">c &#8211; char is signed or unsigned by default &#8211; Stack Overflow</a></p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4579";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4579" title="Permanent Link to 关于C语言的char类型是有符号还是无符号" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4579</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2011年07月21日  //  <a href="http://ihacklog.com/software/cool_software_share/patch-deadbeef-to-support-gbk-id3-tags-and-add-lyrics-support.html" title="让Deadbeef支持GBK标签,彻底解决中文乱码问题，增加歌词显示">让Deadbeef支持GBK标签,彻底解决中文乱码问题，增加歌词显示</a> (4)</li><li>2011年03月18日  //  <a href="http://ihacklog.com/programming/c/filecombine.html" title="文本文件批量合并小工具">文本文件批量合并小工具</a> (15)</li><li>2010年06月1日  //  <a href="http://ihacklog.com/programming/c/tchar-and-char-in-vc.html" title="关于VC中的TCHAR和char">关于VC中的TCHAR和char</a> (0)</li><li>2010年05月26日  //  <a href="http://ihacklog.com/programming/c/cgl-a-cgi-library-in-ansi-c.html" title="CGL库——cgi网页开发好帮手">CGL库——cgi网页开发好帮手</a> (4)</li><li>2010年05月10日  //  <a href="http://ihacklog.com/programming/c/calculate-the-week-of-a-particular-day-a-certain-period.html" title="C语言学习——计算某年某月某日是星期几的函数">C语言学习——计算某年某月某日是星期几的函数</a> (4)</li><li>2010年05月3日  //  <a href="http://ihacklog.com/programming/c/rgb-and-hex-color-convertion.html" title="RGB和十六进制颜色值互转">RGB和十六进制颜色值互转</a> (2)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/char-is-signed-or-unsigned-by-default.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux中gotoxy函数的一个实现</title>
		<link>http://ihacklog.com/programming/c/gotoxy-in-linux.html</link>
		<comments>http://ihacklog.com/programming/c/gotoxy-in-linux.html#comments</comments>
		<pubDate>Thu, 02 Jun 2011 16:19:08 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>
		<category><![CDATA[gotoxy]]></category>
		<category><![CDATA[implement]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/programming/c/gotoxy-in-linux.html</guid>
		<description><![CDATA[摘自：Linux: gotoxy() &#8211; C Linux中要移动光标一般要用到ncurses库中的move函数，这里找到一个纯C实现的gotoxy函数： 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465#include &#60;stdio.h&#62; #include &#60;string.h&#...]]></description>
			<content:encoded><![CDATA[<p>摘自：<a href="http://www.daniweb.com/software-development/c/code/216326">Linux: gotoxy() &#8211; C</a></p>
<p>Linux中要移动光标一般要用到ncurses库中的move函数，这里找到一个纯C实现的gotoxy函数：</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;string.h&gt;</span><br />
<br />
<span style="color: #993333;">void</span> clrscr<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">100</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// A bunch of new lines for now. It's blank, hey!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/putchar.html"><span style="color: #000066;">putchar</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">int</span> gotoxy<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> essq<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// String variable to hold the escape sequence</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> xstr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Strings to hold the x and y coordinates</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> ystr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Escape sequences must be built with characters</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/*<br />
&nbsp; &nbsp; ** Convert the screen coordinates to strings<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>xstr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>ystr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/*<br />
&nbsp; &nbsp; ** Build the escape sequence (vertical move)<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; essq<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>essq<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #006699; font-weight: bold;">\033</span>[&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>essq<span style="color: #339933;">,</span> ystr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/*<br />
&nbsp; &nbsp; ** Described in man terminfo as vpa=\E[%p1%dd<br />
&nbsp; &nbsp; ** Vertical position absolute<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>essq<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/*<br />
&nbsp; &nbsp; ** Horizontal move<br />
&nbsp; &nbsp; ** Horizontal position absolute<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>essq<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #006699; font-weight: bold;">\033</span>[&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>essq<span style="color: #339933;">,</span> xstr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Described in man terminfo as hpa=\E[%p1%dG</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>essq<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;G&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/*<br />
&nbsp; &nbsp; ** Execute the escape sequence<br />
&nbsp; &nbsp; ** This will move the cursor to x, y<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span> essq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #808080; font-style: italic;">/*<br />
** Example<br />
*/</span><br />
<span style="color: #993333;">int</span> main <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; clrscr<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gotoxy<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Coordinates: x = 2; y = 0;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gotoxy<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Coordinates: x = 5; y = 5;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4552";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4552" title="Permanent Link to Linux中gotoxy函数的一个实现" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4552</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2011年10月18日  //  <a href="http://ihacklog.com/linux/myget-the-multi-thread-download-tools-under-linux.html" title="Linux下多线程下载工具Myget原是国人所写">Linux下多线程下载工具Myget原是国人所写</a> (3)</li><li>2011年10月7日  //  <a href="http://ihacklog.com/linux/archlinux/pdnsd-cache-server-setup.html" title="Linux下用pdnsd做dns缓存服务器">Linux下用pdnsd做dns缓存服务器</a> (5)</li><li>2011年07月22日  //  <a href="http://ihacklog.com/linux/archlinux/multi-route-cause-slow-speed.html" title="最近上网很卡——原来是multi-route的缘故">最近上网很卡——原来是multi-route的缘故</a> (2)</li><li>2011年06月6日  //  <a href="http://ihacklog.com/software/font-manager-for-linux.html" title="font manager for linux">font manager for linux</a> (0)</li><li>2011年06月6日  //  <a href="http://ihacklog.com/linux/chinese-chess-for-linux.html" title="Linux下的中国象棋">Linux下的中国象棋</a> (0)</li><li>2011年06月6日  //  <a href="http://ihacklog.com/linux/linux-game-alienarena.html" title="Linux下一款好玩的游戏：alienarena">Linux下一款好玩的游戏：alienarena</a> (4)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/gotoxy-in-linux.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>文本文件批量合并小工具</title>
		<link>http://ihacklog.com/programming/c/filecombine.html</link>
		<comments>http://ihacklog.com/programming/c/filecombine.html#comments</comments>
		<pubDate>Fri, 18 Mar 2011 08:52:55 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>
		<category><![CDATA[txt]]></category>
		<category><![CDATA[合并]]></category>
		<category><![CDATA[文件文件]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/?p=4317</guid>
		<description><![CDATA[在深圳时，奔爷有一小说要合并，win下用批处理貌似不好办，Linux下的shell 命令我也不是很熟悉，还是用C语言写一个吧，于是操刀(VIM)开工。因此有了这篇文章。程序很简单，没啥技术含量，拿出来与大家分享。有兴趣的童鞋可以一起来修改它。 Usage:./filecombine prefix start end [combine_name] [fileext] Description:此程序用来合并命名有规律的txt文件为一个文件, 如foo1.txt , foo2.txt ,foo3.txt &#8...]]></description>
			<content:encoded><![CDATA[<p>在深圳时，奔爷有一小说要合并，win下用批处理貌似不好办，Linux下的shell 命令我也不是很熟悉，还是用C语言写一个吧，于是操刀(VIM)开工。因此有了这篇文章。程序很简单，没啥技术含量，拿出来与大家分享。有兴趣的童鞋可以一起来修改它。</p>
<blockquote><p>Usage:./filecombine prefix start end [combine_name] [fileext]</p>
<p>Description:此程序用来合并命名有规律的txt文件为一个文件,<br />
如foo1.txt , foo2.txt ,foo3.txt &#8230;&#8230; foo199.txt , foo200.txt<br />
参数解释：<br />
prefix:        文件共同的前缀(比如上面的 foo)<br />
start:         要合并的文件的开始数字(如1)<br />
end:           要合并的文件的结束数字(如200)<br />
combine_name:  合并后的文件名称(可选)<br />
fileext:       文件后缀(默认为.txt,可选)</p></blockquote>
<p><a href="http://static.ihacklog.com/wp-files/2011/03/filecombine-1.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/03/filecombine-1-550x330.png" alt="" title="filecombine-1" width="550" height="330" class="alignnone size-medium wp-image-4318" /> </a></p>
<p><a href="http://static.ihacklog.com/wp-files/2011/03/filecombine-2.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/03/filecombine-2-550x392.png" alt="" title="filecombine-2" width="550" height="392" class="alignnone size-medium wp-image-4319" /> </a></p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">gcc</span> <span style="color: #660033;">-Wall</span> <span style="color: #660033;">-o</span> filecombine <span style="color: #660033;">-O3</span> filecombine.c</div></td></tr></tbody></table></div>
<p>源码如下，可在WIN32或者Linux 32下编译运行。win下access函数是用头文件io.h，linux下没有这个文件，包含于unistd.h中。<br />
附上源码包，里面包含WIN32和LINUX下的可执行文件：<br />
注意：该页面嵌入了下载文件，请访问 <a href="http://ihacklog.com/programming/c/filecombine.html">该页面</a>下载该文件。</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/*<br />
&nbsp;* =====================================================================================<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp; &nbsp; Filename: &nbsp;filecombine.c<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp;Description: 此程序用来合并命名有规律的txt文件为一个文件，如foo1.txt , foo2.txt ,foo3.txt ...... foo199.txt , foo200.txt<br />
&nbsp;*&nbsp; &nbsp; &nbsp; 用法：filecombine.exe 文件共同的前缀(比如上面的 foo) 要合并的文件的开始数字(如1) 要合并的文件的结束数字(如200) [合并后的文件名称]<br />
&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (可选) [文件后缀](默认为.txt,可选)<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp;Version: &nbsp;1.0<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp;Created: &nbsp;2011年03月01日 21时52分28秒<br />
&nbsp;* &nbsp; &nbsp; &nbsp; Revision: &nbsp;none<br />
&nbsp;* &nbsp; &nbsp; &nbsp; Compiler: &nbsp;gcc<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; Author: &nbsp;荒野无灯 (HuangYeWuDeng), admin#ihacklog.com<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp;Company: &nbsp;hhtc edu<br />
&nbsp;*<br />
&nbsp;* =====================================================================================<br />
&nbsp;*/</span><br />
<span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span><br />
<span style="color: #339933;">#include &lt;string.h&gt;</span><br />
<span style="color: #339933;">#include &lt;time.h&gt;</span><br />
<span style="color: #339933;">#include &lt;sys/types.h&gt;</span><br />
<span style="color: #339933;">#include &lt;sys/stat.h&gt;</span><br />
<br />
<span style="color: #339933;">#if defined(WIN32)</span><br />
<span style="color: #339933;">#include &lt;io.h&gt;</span><br />
<span style="color: #339933;">#elif defined(__linux__)</span><br />
<span style="color: #339933;">#include &lt;unistd.h&gt;</span><br />
<span style="color: #339933;">#endif</span><br />
<br />
<span style="color: #339933;">#define CHUNK_SIZE 512</span><br />
<br />
<span style="color: #993333;">off_t</span> filesize<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">int</span><br />
main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ************文本文件批量合并小工具************<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>argc<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Error parameter!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Usage:%s prefix start end [combine_name] [fileext]<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,*</span>argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Description:此程序用来合并命名有规律的txt文件为一个文件,<span style="color: #000099; font-weight: bold;">\n</span>如foo1.txt , foo2.txt ,foo3.txt ...... foo199.txt , foo200.txt<span style="color: #000099; font-weight: bold;">\n</span>参数解释：<span style="color: #000099; font-weight: bold;">\n</span>prefix: &nbsp; &nbsp; &nbsp; &nbsp;文件共同的前缀(比如上面的 foo) <span style="color: #000099; font-weight: bold;">\n</span>start: &nbsp; &nbsp; &nbsp; &nbsp; 要合并的文件的开始数字(如1)<span style="color: #000099; font-weight: bold;">\n</span>end: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 要合并的文件的结束数字(如200) <span style="color: #000099; font-weight: bold;">\n</span>combine_name: &nbsp;合并后的文件名称(可选) <span style="color: #000099; font-weight: bold;">\n</span>fileext: &nbsp; &nbsp; &nbsp; 文件后缀(默认为.txt,可选)<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;--------------------------------------------------a small tool by 荒野无灯<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;--------------------------------------------------http://www.ihacklog.com<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; FILE <span style="color: #339933;">*</span>fp<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; FILE <span style="color: #339933;">*</span>fpw<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> fileext<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">10</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> combine_filename<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">200</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> renamed_combine_filename<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">300</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>argc<span style="color: #339933;">==</span><span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>fileext<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcpy.html"><span style="color: #000066;">strcpy</span></a><span style="color: #009900;">&#40;</span>fileext<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>NULL <span style="color: #339933;">!=</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>combine_filename<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%s_combined%s&quot;</span><span style="color: #339933;">,</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>fileext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>combine_filename<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%s_combined%s&quot;</span><span style="color: #339933;">,</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>fileext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>access<span style="color: #009900;">&#40;</span>combine_filename<span style="color: #339933;">,</span><span style="color: #208080;">0x00</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span> timebuf<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; time_t t<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/time.html"><span style="color: #000066;">time</span></a> <span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strftime.html"><span style="color: #000066;">strftime</span></a><span style="color: #009900;">&#40;</span>timebuf<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>timebuf<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%Y-%m-%d-%H%M%S&quot;</span><span style="color: #339933;">,</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/localtime.html"><span style="color: #000066;">localtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>renamed_combine_filename<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%s_%s&quot;</span><span style="color: #339933;">,</span>timebuf<span style="color: #339933;">,</span>combine_filename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;注意：文件%s已经存在，合并后的文件名为：%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>combine_filename<span style="color: #339933;">,</span>renamed_combine_filename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcpy.html"><span style="color: #000066;">strcpy</span></a><span style="color: #009900;">&#40;</span>renamed_combine_filename<span style="color: #339933;">,</span>combine_filename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; fpw<span style="color: #339933;">=</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/fopen.html"><span style="color: #000066;">fopen</span></a><span style="color: #009900;">&#40;</span>renamed_combine_filename<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;ab&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>fpw<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;create file %s failed!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>renamed_combine_filename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> start_num<span style="color: #339933;">=</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/atoi.html"><span style="color: #000066;">atoi</span></a><span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> end_num<span style="color: #339933;">=</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/atoi.html"><span style="color: #000066;">atoi</span></a><span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span>start_num<span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>end_num<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span> fname<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">200</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcpy.html"><span style="color: #000066;">strcpy</span></a><span style="color: #009900;">&#40;</span>fname<span style="color: #339933;">,</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span> tmp<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">10</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>fname<span style="color: #339933;">,</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>fname<span style="color: #339933;">,</span>fileext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fp<span style="color: #339933;">=</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/fopen.html"><span style="color: #000066;">fopen</span></a><span style="color: #009900;">&#40;</span>fname<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;rb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>fp<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Error:<span style="color: #000099; font-weight: bold;">\n</span>open file %s failed!perhaps file does not exists.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>fname<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;正在添加文件: &nbsp;%s ......<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>fname<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span> data<span style="color: #009900;">&#91;</span>CHUNK_SIZE<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> file_size<span style="color: #339933;">=</span>filesize<span style="color: #009900;">&#40;</span>fname<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> toread<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>file_size<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toread<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span> file_size<span style="color: #339933;">&gt;</span>CHUNK_SIZE <span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span>CHUNK_SIZE<span style="color: #339933;">:</span>file_size<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fread.html"><span style="color: #000066;">fread</span></a><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span>toread<span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>fp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fwrite.html"><span style="color: #000066;">fwrite</span></a><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span>toread<span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>fpw<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file_size<span style="color: #339933;">-=</span>toread<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fclose.html"><span style="color: #000066;">fclose</span></a><span style="color: #009900;">&#40;</span>fp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fclose.html"><span style="color: #000066;">fclose</span></a><span style="color: #009900;">&#40;</span>fpw<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;文件合并成功！合并后的文件名为：%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>renamed_combine_filename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><br />
<br />
<br />
<span style="color: #993333;">off_t</span><br />
filesize<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>filename<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">struct</span> stat s<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">!=</span> stat<span style="color: #009900;">&#40;</span>filename<span style="color: #339933;">,&amp;</span>s<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> s.<span style="color: #202020;">st_size</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4317";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4317" title="Permanent Link to 文本文件批量合并小工具" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4317</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2011年07月21日  //  <a href="http://ihacklog.com/software/cool_software_share/patch-deadbeef-to-support-gbk-id3-tags-and-add-lyrics-support.html" title="让Deadbeef支持GBK标签,彻底解决中文乱码问题，增加歌词显示">让Deadbeef支持GBK标签,彻底解决中文乱码问题，增加歌词显示</a> (4)</li><li>2011年06月24日  //  <a href="http://ihacklog.com/programming/c/char-is-signed-or-unsigned-by-default.html" title="关于C语言的char类型是有符号还是无符号">关于C语言的char类型是有符号还是无符号</a> (0)</li><li>2010年05月26日  //  <a href="http://ihacklog.com/programming/c/cgl-a-cgi-library-in-ansi-c.html" title="CGL库——cgi网页开发好帮手">CGL库——cgi网页开发好帮手</a> (4)</li><li>2010年05月10日  //  <a href="http://ihacklog.com/programming/c/calculate-the-week-of-a-particular-day-a-certain-period.html" title="C语言学习——计算某年某月某日是星期几的函数">C语言学习——计算某年某月某日是星期几的函数</a> (4)</li><li>2010年05月3日  //  <a href="http://ihacklog.com/programming/c/rgb-and-hex-color-convertion.html" title="RGB和十六进制颜色值互转">RGB和十六进制颜色值互转</a> (2)</li><li>2010年04月25日  //  <a href="http://ihacklog.com/programming/c/differences-between-c-and-cpp.html" title="读书笔记：C与C++的区别">读书笔记：C与C++的区别</a> (7)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/filecombine.html/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>avast病毒库更新程序的改进</title>
		<link>http://ihacklog.com/programming/c/avast-update-custom-written-in-c.html</link>
		<comments>http://ihacklog.com/programming/c/avast-update-custom-written-in-c.html#comments</comments>
		<pubDate>Fri, 26 Nov 2010 01:12:26 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/?p=4250</guid>
		<description><![CDATA[avast病毒库更新程序为avast-update ，这个东东实际上是调用了wget 来下载病毒库的，但是运行以后没有任何提示，病毒库到底有没有在下载呢？不清楚。于是写了个东东来以便可以直观的看到是否在下载以及下载速度的快慢（根据旋转速度）。 /opt/avast4workstation-1.3.0/bin/avast-update 是 avast-update 的路径。 这个小程序主要用到了LINUX内核的 inotify 来监控文件变化。 TODO： 增加超时检测（超时自动退出） 增加更新完成后自动退...]]></description>
			<content:encoded><![CDATA[<p>avast病毒库更新程序为avast-update ，这个东东实际上是调用了wget 来下载病毒库的，但是运行以后没有任何提示，病毒库到底有没有在下载呢？不清楚。于是写了个东东来以便可以直观的看到是否在下载以及下载速度的快慢（根据旋转速度）。<br />
<strong>/opt/avast4workstation-1.3.0/bin/avast-update</strong> 是 avast-update 的路径。</p>
<p>这个小程序主要用到了LINUX内核的 inotify 来监控文件变化。</p>
<p>TODO：<br />
增加超时检测（超时自动退出）<br />
增加更新完成后自动退出（这一点应该简单）</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/*<br />
&nbsp;* =====================================================================================<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp; &nbsp; Filename: &nbsp;avast-watch.c<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp;Description: &nbsp;监视avast病毒库更新<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp;Version: &nbsp;1.0<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp;Created: &nbsp;2010年10月13日 13时35分28秒<br />
&nbsp;* &nbsp; &nbsp; &nbsp; Revision: &nbsp;none<br />
&nbsp;* &nbsp; &nbsp; &nbsp; Compiler: &nbsp;gcc<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; Author: &nbsp;荒野无灯 (HuangYeWuDeng), admin@ihacklog.com<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp;Company: &nbsp;hhtc edu<br />
&nbsp;*<br />
&nbsp;* =====================================================================================<br />
&nbsp;*/</span><br />
<br />
<span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;unistd.h&gt;</span><br />
<span style="color: #339933;">#include &lt;time.h&gt;</span><br />
<span style="color: #339933;">#include &lt;sys/inotify.h&gt;</span><br />
<span style="color: #339933;">#define MAX_BUF_SIZE 4096</span><br />
<br />
<br />
<br />
<span style="color: #808080; font-style: italic;">/* &nbsp;<br />
struct inotify_event {<br />
int wd; // watch descriptor<br />
uint32_t mask; // mask of events<br />
uint32_t cookie; // unique cookie associating related events ( form rename(2) )<br />
uint32_t len; // size of name field<br />
char name[]; // optional null-terminated name<br />
}<br />
*/</span><br />
<br />
<br />
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// fd : file discriptor</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// wp &nbsp;watch discriptor</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> fd<span style="color: #339933;">,</span> wd<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> len<span style="color: #339933;">,</span> index<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> buffer<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4096</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">struct</span> inotify_event <span style="color: #339933;">*</span>event<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>path <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;/home/hacklog/.avast&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>prog<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;/opt/avast4workstation-1.3.0/bin/avast-update&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> progress <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> indicator<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;-<span style="color: #000099; font-weight: bold;">\\</span>|/&quot;</span><span style="color: #339933;">;</span><br />
<br />
<br />
&nbsp; &nbsp; FILE <span style="color: #339933;">*</span>fp<span style="color: #339933;">=</span>popen<span style="color: #009900;">&#40;</span>prog<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; fd <span style="color: #339933;">=</span> inotify_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>fd <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Failed to initialize inotify.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; wd <span style="color: #339933;">=</span> inotify_add_watch<span style="color: #009900;">&#40;</span>fd<span style="color: #339933;">,</span> path<span style="color: #339933;">,</span> IN_CLOSE_WRITE <span style="color: #339933;">|</span> IN_CREATE <span style="color: #339933;">|</span> IN_MODIFY<span style="color: #339933;">|</span> IN_DELETE <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>wd <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Can't add watch for %s&quot;</span><span style="color: #339933;">,</span> path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 当没有事件发生时，fd 处于阻塞状态，read阻塞直到有一个事件发生.</span><br />
&nbsp; &nbsp; time_t start_time<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; time_t end_time<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>len <span style="color: #339933;">=</span> read<span style="color: #009900;">&#40;</span>fd<span style="color: #339933;">,</span> buffer<span style="color: #339933;">,</span> MAX_BUF_SIZE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; index <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>index <span style="color: #339933;">&lt;</span> len<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> inotify_event <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>buffer <span style="color: #339933;">+</span> index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 如果不是我们添加的对.avast目录的监视，就继续 （因为我们可能添加了多个监视的，每个监视都有一个 wd (watch discriptor )</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>event<span style="color: #339933;">-&gt;</span>wd <span style="color: #339933;">!=</span> wd<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #666666; font-style: italic;">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (event-&gt;mask &amp; IN_OPEN)</span><br />
<span style="color: #666666; font-style: italic;">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;file %s is opened.\n&quot;, event-&gt;name);</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>event<span style="color: #339933;">-&gt;</span>mask <span style="color: #339933;">&amp;</span> IN_CREATE<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcmp.html"><span style="color: #000066;">strcmp</span></a><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">-&gt;</span>name<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;400.vps.new.md5&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/time.html"><span style="color: #000066;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>start_time<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;file %s is created.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> event<span style="color: #339933;">-&gt;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>event<span style="color: #339933;">-&gt;</span>mask <span style="color: #339933;">&amp;</span> IN_MODIFY<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;file %s is modified.\n&quot;, event-&gt;name);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; %c&quot;</span><span style="color: #339933;">,</span>indicator<span style="color: #009900;">&#91;</span>progress<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fflush.html"><span style="color: #000066;">fflush</span></a><span style="color: #009900;">&#40;</span>stdout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\b</span><span style="color: #000099; font-weight: bold;">\b</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; progress<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span> progress <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #339933;">%</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>event<span style="color: #339933;">-&gt;</span>mask <span style="color: #339933;">&amp;</span> IN_CLOSE_WRITE<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;file %s is closed for write.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> event<span style="color: #339933;">-&gt;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>event<span style="color: #339933;">-&gt;</span>mask <span style="color: #339933;">&amp;</span> IN_DELETE<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;file %s is deleted.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> event<span style="color: #339933;">-&gt;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// sizeof(struct inotify_event)+len 为每个inotify_event 的长度</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index <span style="color: #339933;">+=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> inotify_event<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> event<span style="color: #339933;">-&gt;</span>len<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>IBM DW：<a href="http://static.ihacklog.com/wp-files/2010/11/avast-update-watch.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2010/11/avast-update-watch-550x384.png" alt="" title="avast-update-watch" width="550" height="384" class="alignnone size-medium wp-image-4251" /> </a></p>
<p>参考文档：<br />
<a target="_blank" href="http://www.ihacklog.com/l.php?url=www.ibm.com/developerworks/cn/linux/l-inotifynew/">inotify &#8212; Linux 2.6 内核中的文件系统变化通知机制</a><br />
<a target="_blank" href="http://www.ihacklog.com/l.php?url=blog.csdn.net/soulxu/archive/2008/10/19/3087292.aspx">linux开发 &#8212; 文件监控 inotify</a><br />
扩展阅读：<br />
<a target="_blank" href="http://www.ihacklog.com/l.php?url=www.bitscn.com/plus/view.phporz__aid=158600">linux inotify+rsync+ssh数据时时同步</a></p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4250";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4250" title="Permanent Link to avast病毒库更新程序的改进" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4250</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2010年04月24日  //  <a href="http://ihacklog.com/php/wordpress/plugins/wp-embeded-ajax-comments.html" title="折腾WordPress內置嵌套评论专用Ajax comments">折腾WordPress內置嵌套评论专用Ajax comments</a> (32)</li><li>2011年06月6日  //  <a href="http://ihacklog.com/software/font-manager-for-linux.html" title="font manager for linux">font manager for linux</a> (0)</li><li>2009年04月17日  //  <a href="http://ihacklog.com/php/xiangyaojingtongphpmaxianxuehaolinuxunixhecba.html" title="想要精通PHP吗？先学好Linux/Unix和C吧">想要精通PHP吗？先学好Linux/Unix和C吧</a> (0)</li><li>2009年04月5日  //  <a href="http://ihacklog.com/php/sql%e4%b8%adlike%e4%b8%ad%e7%9a%84%e7%9a%84%e5%a6%99%e7%94%a8.html" title="SQL中like中的%的妙用">SQL中like中的%的妙用</a> (0)</li><li>2011年04月28日  //  <a href="http://ihacklog.com/php/drupal/show-taxonomy-as-menus-in-drupal.html" title="drupal分类显示在菜单中的方法">drupal分类显示在菜单中的方法</a> (2)</li><li>2011年11月25日  //  <a href="http://ihacklog.com/php/wordpress/upgrade/wordpress-multisite-setup.html" title="wordpress多用户站点配置">wordpress多用户站点配置</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/avast-update-custom-written-in-c.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>关于递归与迭代</title>
		<link>http://ihacklog.com/programming/c/recursive-and-iterative.html</link>
		<comments>http://ihacklog.com/programming/c/recursive-and-iterative.html#comments</comments>
		<pubDate>Wed, 24 Nov 2010 06:08:52 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/?p=4240</guid>
		<description><![CDATA[这篇文章是由一道省二计算机等级考试的题目引发的。 那天寝室一同学的同学考省二，发短信过来求救，要我给他算一个题目。 题目简单描述如下： f（0）=f（1）=1 f（2）=0 f(n)=f(n-1)-2*f(n-2)+f(n-3) 求当n2) f(n)的最大值是多少。 由于时间非常紧，那同学快下考了，首先我便用了简单的递归实现f(n)函数。 1234567891011121314151617181920212223242526272829303132333435363738#include &#60;std...]]></description>
			<content:encoded><![CDATA[<p>这篇文章是由一道省二计算机等级考试的题目引发的。<br />
那天寝室一同学的同学考省二，发短信过来求救，要我给他算一个题目。<br />
题目简单描述如下：</p>
<blockquote><p>f（0）=f（1）=1<br />
f（2）=0<br />
f(n)=f(n-1)-2*f(n-2)+f(n-3)<br />
求当n<= 50 时 (n>2) f(n)的最大值是多少。</p></blockquote>
<p>由于时间非常紧，那同学快下考了，首先我便用了简单的<strong>递归</strong>实现f(n)函数。</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #993333;">int</span> f<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<br />
<br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> max<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">51</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//printf(&quot;now :%d\n&quot;,i);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> max <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max<span style="color: #339933;">=</span>f<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;max=%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>max <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">int</span> f<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> f<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>f<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>f<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>一运行，发现老半天不出结果，调试一下发现，算到n等于30多的时候就变得非常慢了，CPU占用达50%以上。很明显，这个函数不能用递归来实现。因为递归实际上<strong>效率是非常低</strong>的，而且，就这个题目而言，运算量非常大。<br />
于是赶紧修改了一下f(n) 函数，转而用<strong>迭代</strong>来实现。</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> f<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> a<span style="color: #339933;">,</span>b<span style="color: #339933;">,</span>c<span style="color: #339933;">,</span>d<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; a<span style="color: #339933;">=</span>b<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; c<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> t<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// a b c d</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d<span style="color: #339933;">=</span>c<span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>b <span style="color: #339933;">+</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t<span style="color: #339933;">=</span>d<span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>c<span style="color: #339933;">+</span>b<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a<span style="color: #339933;">=</span>b<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b<span style="color: #339933;">=</span>c<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c<span style="color: #339933;">=</span>d<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d<span style="color: #339933;">=</span>t<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n<span style="color: #339933;">--;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> d<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>这样再运行，结果马上出来了： 598325<br />
速给那同学把答案发去了。</p>
<p>后来，又再网上找到另一种更简便的方法，用一个数组来存储每次运算的结果，这样，后面的运行就可以直接利用前面运算得出的结果，而不要重新计算。从效率来说，这个更高一些。</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> arr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">51</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>i<span style="color: #339933;">,</span>num_max<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; arr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>arr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; arr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;=</span><span style="color: #0000dd;">50</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>num_max<span style="color: #339933;">&lt;</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_max<span style="color: #339933;">=</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;max=%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>num_max<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>看来我还是把问题想复杂了，有时候，最简单的方法就是最好的方法。简单就是美。</p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4240";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4240" title="Permanent Link to 关于递归与迭代" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4240</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2008年02月21日  //  <a href="http://ihacklog.com/xml_html_css_js_ajax/%e9%a1%b5%e9%9d%a2%e8%bd%ac%e5%90%91%e4%bb%a3%e7%a0%81.html" title="页面转向代码">页面转向代码</a> (0)</li><li>2009年04月9日  //  <a href="http://ihacklog.com/php/original/yigehaoyongdefenyeleiwozaidiscuz61shouyebeijingyinlechajianzhongyongdao.html" title="一个好用的分页类【我在discuz6.1首页背景音乐插件中用到】">一个好用的分页类【我在discuz6.1首页背景音乐插件中用到】</a> (1)</li><li>2010年02月28日  //  <a href="http://ihacklog.com/php/wordpress/wpskills/disable-wp_post_revisions.html" title="禁用并删除wordpress修订记录">禁用并删除wordpress修订记录</a> (3)</li><li>2011年12月1日  //  <a href="http://ihacklog.com/software/cool_software_share/prefer-skydrive.html" title="以后分享东西我还是用skydrive吧">以后分享东西我还是用skydrive吧</a> (14)</li><li>2011年08月20日  //  <a href="http://ihacklog.com/php/habari/hb-highslide.html" title="原创插件：hb-Highslide">原创插件：hb-Highslide</a> (2)</li><li>2011年10月4日  //  <a href="http://ihacklog.com/php/wordpress/wpskills/how-to-check-a-script-has-been-registerd-or-not-in-wordpress.html" title="WP中检测脚本是否已经注册的方法">WP中检测脚本是否已经注册的方法</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/recursive-and-iterative.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>常量指针和指针常量——令人蛋疼的两个词</title>
		<link>http://ihacklog.com/programming/c/a-constant-pointer-to-a-character-or-a-pointer-to-a-constant-character.html</link>
		<comments>http://ihacklog.com/programming/c/a-constant-pointer-to-a-character-or-a-pointer-to-a-constant-character.html#comments</comments>
		<pubDate>Sat, 23 Oct 2010 15:49:28 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/?p=4176</guid>
		<description><![CDATA[在《C专家编程》（徐波 译）第64页： 最后 ，把” char * const ” 解释为指向字符的常量指针。 不知道原书中是怎样的。我们看一下原书： Finally, take the “char * const”, as a constant pointer to a character. 可见，在本书中，”常量指针“ 代表的是”constant pointer” ,即这个指针本身是一个常量。也就是说指针本身是只读的，是不可修改的。 因为C语言中的const 关键字实际上是代表被它声明的对象是只读的，...]]></description>
			<content:encoded><![CDATA[<p>在《C专家编程》（徐波 译）第64页：</p>
<blockquote><p>
最后 ，把”  char * const ” 解释为指向字符的常量指针。
</p></blockquote>
<p>不知道原书中是怎样的。我们看一下原书：</p>
<blockquote><p>Finally, take the “char * const”, as a constant pointer to a character. </p></blockquote>
<p>可见，在本书中，”常量指针“ 代表的是”constant pointer” ,即这个指针本身是一个常量。也就是说指针本身是只读的，是不可修改的。<br />
因为C语言中的const 关键字实际上是代表被它声明的对象是只读的，不可修改的。<br />
char str[]=”Hello C!\n”;<br />
char * const pchar=str;<br />
这里pchar 是不可修改的，也就是说pchar 这个指针，它是个常量。<br />
徐波把它翻译为常量指针，也是可以理解的。 constant  在这里是个形容词，用来修饰 pointer的。</p>
<p>http://www.codeguru.com/cpp/cpp/cpp_mfc/general/article.php/c6967</p>
<p>那 a pointer to a constant character ， 应该把它认为是一个指针常量吧。它首先是一个指针，指向一个常量 （字符常量）。</p>
<p>这样看来，<br />
char * const pchar; //常量指针<br />
char const * pchar; //指针常量 ，或指向常量的指针<br />
似乎是合理的。<br />
GOOGLE了一下，发现很多文章中的说法与上面的正好相反。<br />
也有部分相同的，如<a target="_blank" href="http://www.ihacklog.com/l.php?url=faq.csdn.net/read/217386.html">http://faq.csdn.net/read/217386.html</a> 就把第一种形式的解释为“常量指针” 。此文中说：</p>
<blockquote><p>“常量指针”即指针本身的值是常量，但“能靠解引用改变它指向的对象的值”</p></blockquote>
<blockquote><p>
5）关于记忆技巧：<br />
对于区分const int *pa和int *const pa这两者，<br />
前者中，const直接修饰*（不考虑int，因为类型在这里是没影响的），说明*（解引用）这种行为具有常量性，即“不能靠解引用改变它指向的对象的值”，即指向常量的指针。<br />
后者中，const直接修饰pa，说明pa本身的值具有常量性，即常量指针。</p>
<p>或者也可以这样来记忆：<br />
const int a;       // const常量<br />
const int *a; //指向常量的指针<br />
int *const a=&#038;n; //常量指针<br />
你在纸上按顺序写下以上三行，记住它们的注释长度：短——长——短，<br />
分别对应着：const常量、指向常量的指针、常量指针这三种，应该就不会混淆了。</p>
<p>个人认为以上记忆法比《Effective ++》条款21中推荐的划线分左右法更好记。</p>
<p>另1：也有教材反过来把上述第二者称呼为“常量指针”，第三者称呼为：“指针常量”，相对来说更容易让人混淆，大家还是只记住上面的那种称呼法就好。</p>
<p>另2：const和typedef在一起时容易让人陷入陷阱，具体见《typedef的用途和陷阱》。
</p></blockquote>
<p>相反的记法，可参见：</p>
<p>http://www.cppblog.com/cc/archive/2006/03/12/4045.html</p>
<p>http://bfzero.blog.hexun.com/8479899_d.html</p>
<p>不推荐这种叫法，就像上面说的，这种称呼方法更容易让人混淆。</p>
<p>因此，还是把声明类似于：<strong>char * const p;</strong> 之类的指针，称呼之为“常量指针”比较好。毕竟，这个叫起来更习惯一些。（《C专家编程》，传说中的鱼书，是一本相当权威的C语言学习参考书，我还是相信权威，还有就是依自己的习惯。）</p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4176";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4176" title="Permanent Link to 常量指针和指针常量——令人蛋疼的两个词" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4176</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2011年03月16日  //  <a href="http://ihacklog.com/linux/ubuntu-desktop/gpg-error-no_pubkey-solution.html" title="GPG 校验签名出错  NO_PUBKEY的解决办法">GPG 校验签名出错  NO_PUBKEY的解决办法</a> (5)</li><li>2010年11月26日  //  <a href="http://ihacklog.com/xml_html_css_js_ajax/jquery-grid-plugin.html" title="jQ表格插件推荐：jQgrid">jQ表格插件推荐：jQgrid</a> (6)</li><li>2011年12月20日  //  <a href="http://ihacklog.com/php/wordpress/plugins/hacklog-remote-attachment-upaiyun-version.html" title="Hacklog Remote Attachment Upyun(又拍云版) v1.2.6">Hacklog Remote Attachment Upyun(又拍云版) v1.2.6</a> (39)</li><li>2009年11月28日  //  <a href="http://ihacklog.com/php/discuz/ucenter-application-developing-note.html" title="ucenter应用开发笔记之通信篇">ucenter应用开发笔记之通信篇</a> (2)</li><li>2008年12月19日  //  <a href="http://ihacklog.com/memory/huangyewudeng.html" title="荒野无灯——叶延滨">荒野无灯——叶延滨</a> (0)</li><li>2011年11月20日  //  <a href="http://ihacklog.com/php/wordpress/plugins/migrated-all-wordpress-attachments-to-a-remote-server-successfully.html" title="成功将博客所有附件转移到远程服务器~~感谢蓝冰提供空间">成功将博客所有附件转移到远程服务器~~感谢蓝冰提供空间</a> (40)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/a-constant-pointer-to-a-character-or-a-pointer-to-a-constant-character.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>硬盘整数分区计算工具</title>
		<link>http://ihacklog.com/programming/c/int-partion-size.html</link>
		<comments>http://ihacklog.com/programming/c/int-partion-size.html#comments</comments>
		<pubDate>Fri, 15 Oct 2010 20:40:49 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/?p=4158</guid>
		<description><![CDATA[前天帮寝室小则装系统，把他的全盘格了，重新分区，他说要整数分区，总不是每次都拿计算器来算吧？ 于是写了这么个小工具来计算整数分区的。用C语言编写，为了避免有童鞋打开时出现找不到msvcr100.dll文件的情况，采用的静态编译，因此体积较大。 工具下载： 有图有真相 ，看下分区效果： 共四个分区，前三个为整数分区，分别是 50GB , 100 GB , 100 GB 的： 关于算法，我这里引用下 slyar童鞋的说明： 对于FAT32格式： 应分配MB数=(分区GB数－1)×4+1024×分区GB数 也就...]]></description>
			<content:encoded><![CDATA[<p>前天帮寝室小则装系统，把他的全盘格了，重新分区，他说要整数分区，总不是每次都拿计算器来算吧？<br />
于是写了这么个小工具来计算整数分区的。用C语言编写，为了避免有童鞋打开时出现找不到msvcr100.dll文件的情况，采用的静态编译，因此体积较大。</p>
<p>工具下载：注意：该页面嵌入了下载文件，请访问 <a href="http://ihacklog.com/programming/c/int-partion-size.html">该页面</a>下载该文件。</p>
<p>有图有真相 <img src='http://ihacklog.com/wordpress/wp-includes/images/smilies/icon_biggrin.gif' alt=':grin:' class='wp-smiley' />  ，看下分区效果：<br />
共四个分区，前三个为整数分区，分别是 50GB , 100 GB , 100 GB 的：<br />
<a href="http://static.ihacklog.com/wp-files/2010/10/int_partition_mycomputer.jpg" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2010/10/int_partition_mycomputer-550x74.jpg" alt="" title="int_partition_mycomputer" width="550" height="74" class="alignnone size-medium wp-image-4159" /> </a></p>
<p><a href="http://static.ihacklog.com/wp-files/2010/10/int_partition.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2010/10/int_partition.png" alt="" title="int_partition" width="508" height="304" class="alignnone size-full wp-image-4160" /> </a></p>
<p>关于算法，我这里引用下 <a target="_blank" href="http://www.ihacklog.com/l.php?url=www.slyar.com/blog/disk-int-ntfs-fat32.html">slyar</a>童鞋的说明：</p>
<blockquote>
<p>对于FAT32格式：</p>
<p>应分配MB数=(分区GB数－1)×4+1024×分区GB数</p>
<p>也就是说，如果你要在FAT32格式的硬盘上分出一个10 GB的硬盘，那么：</p>
<p>你应该分配的MB数=(10-1)×4+1024×10=10276 MB</p>
<p>这样你就可以得到整数的10 GB分区了。</p>
<p>对于NTFS格式：</p>
<p>硬盘一般有255磁头，63扇区，故每柱面大小为：</p>
<p>512byte x 255 x 63＝8225280bytes ＝7.84423828125 MB</p>
<p>如果要分10 GB,那么要10&#215;1024 MB=10240 MB</p>
<p>需要柱面数为10240÷7.84423828125=1305.416744475568004979769685652</p>
<p>取整数既为1306个柱面</p>
<p>应分MB数为1306&#215;7.84423828125=10244.5751953125 MB</p>
<p>不管小数点后面几位都进1，也就是10245 MB，系统也就认为是10.00 GB了。</p></blockquote>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4158";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4158" title="Permanent Link to 硬盘整数分区计算工具" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4158</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2011年07月11日  //  <a href="http://ihacklog.com/software/development-tool/netbeans-ide-7-font-config-for-chinese.html" title="NetBeans IDE 7 中使用英文字体中文变方块的解决办法">NetBeans IDE 7 中使用英文字体中文变方块的解决办法</a> (4)</li><li>2010年11月12日  //  <a href="http://ihacklog.com/software/tips/install-win7-via-harddisk.html" title="硬盘安装Win7[转]">硬盘安装Win7[转]</a> (8)</li><li>2008年12月24日  //  <a href="http://ihacklog.com/memory/%e4%b8%80%e5%80%8b%e4%ba%ba%e7%9a%84%e5%a4%a9%e7%a9%ba%e5%be%88%e8%97%8d-%e8%93%9d%e7%9a%84%e6%9c%89%e7%82%b9%e5%bf%a7%e9%83%81-by1987102500.html" title="一個人的天空很藍 蓝的有点忧郁&#8212;&#8211;by1987102500">一個人的天空很藍 蓝的有点忧郁&#8212;&#8211;by1987102500</a> (0)</li><li>2010年06月20日  //  <a href="http://ihacklog.com/memory/music/xinyuan.html" title="心愿——分享一首好听的校园歌曲">心愿——分享一首好听的校园歌曲</a> (8)</li><li>2008年12月25日  //  <a href="http://ihacklog.com/php/skills/discuz%e4%b8%ad%e7%9a%84%e5%88%86%e9%a1%b5%e5%87%bd%e6%95%b0.html" title="Discuz中的分页函数">Discuz中的分页函数</a> (0)</li><li>2008年12月31日  //  <a href="http://ihacklog.com/php/skills/%e5%9f%ba%e7%a1%80%e8%a1%a5%e4%b9%a0%ef%bc%9aphp%e6%98%93%e6%b7%b7%e6%b7%86%e5%87%bd%e6%95%b0%e7%9a%84%e5%8c%ba%e5%88%86.html" title="基础补习：PHP易混淆函数的区分">基础补习：PHP易混淆函数的区分</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/int-partion-size.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>vsftpd.log 中文显示为问号?的解决办法</title>
		<link>http://ihacklog.com/programming/c/vsftpd-log-chinese-replacement-problem-solution.html</link>
		<comments>http://ihacklog.com/programming/c/vsftpd-log-chinese-replacement-problem-solution.html#comments</comments>
		<pubDate>Thu, 23 Sep 2010 10:41:48 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/?p=4116</guid>
		<description><![CDATA[查看了下vsftpd.log的源码，发现是其代码本身没有考虑到多字节字符问题。 为了确保安全，vsftpd把所有不可打印字符全部替换为了? .在替换时没有考虑到多字节字符，于是导致杯具的发生。见： logging.c 153 vsf_log_do_log_to_file str.c 671 str_replace_unprintable sysutil.c 922 vsf_sysutil_isprint 经在ubuntu 10.04 server 和desktop下测试，按以下方法修改不能登录成功，但是，...]]></description>
			<content:encoded><![CDATA[<p>查看了下vsftpd.log的源码，发现是其代码本身没有考虑到多字节字符问题。<br />
为了确保安全，vsftpd把所有不可打印字符全部替换为了? .在替换时没有考虑到多字节字符，于是导致杯具的发生。见：<br />
logging.c 153  vsf_log_do_log_to_file<br />
str.c  671   str_replace_unprintable<br />
sysutil.c 922 vsf_sysutil_isprint</p>
<blockquote><p>
经在ubuntu 10.04 server 和desktop下测试，按以下方法修改不能登录成功，但是，如果手动安装pam 再编译，则能够登录成功。但是，手动编译安装pam后，无论是server还是desktop版都不能登录系统了。 <img src='http://ihacklog.com/wordpress/wp-includes/images/smilies/icon_sad.gif' alt=':sad:' class='wp-smiley' /><br />
在ubuntu 10.04 server 和desktop下，<br />
如果直接make &#038;&#038; make install ,这时，ldd vsftpd 会发现没有加载pam.<br />
如果果手动安装pam 再编译,再直接make &#038;&#038; make install ,ldd vsftpd 会发现pam已经加载。不知道这是什么原因。
</p></blockquote>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">static</span> <span style="color: #993333;">void</span><br />
vsf_log_do_log_to_file<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> fd<span style="color: #339933;">,</span> <span style="color: #993333;">struct</span> mystr<span style="color: #339933;">*</span> p_str<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>tunable_no_log_lock<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> retval <span style="color: #339933;">=</span> vsf_sysutil_lock_file_write<span style="color: #009900;">&#40;</span>fd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>vsf_sysutil_retval_is_error<span style="color: #009900;">&#40;</span>retval<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; str_replace_unprintable<span style="color: #009900;">&#40;</span>p_str<span style="color: #339933;">,</span> <span style="color: #ff0000;">'?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//这里便是导致不能显示中文的替换啦！</span><br />
&nbsp; str_append_char<span style="color: #009900;">&#40;</span>p_str<span style="color: #339933;">,</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">/* Ignore write failure; maybe the disk filled etc. */</span><br />
&nbsp; <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> str_write_loop<span style="color: #009900;">&#40;</span>p_str<span style="color: #339933;">,</span> fd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>tunable_no_log_lock<span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; vsf_sysutil_unlock_file<span style="color: #009900;">&#40;</span>fd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//如果字符是不可打印的，则将之替换为new_char</span><br />
<span style="color: #993333;">void</span><br />
str_replace_unprintable<span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> mystr<span style="color: #339933;">*</span> p_str<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> new_char<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> p_str<span style="color: #339933;">-&gt;</span>len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>vsf_sysutil_isprint<span style="color: #009900;">&#40;</span>p_str<span style="color: #339933;">-&gt;</span>p_buf<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; p_str<span style="color: #339933;">-&gt;</span>p_buf<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> new_char<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//判断是否可打印字符</span><br />
<span style="color: #666666; font-style: italic;">//是则返回1</span><br />
<span style="color: #666666; font-style: italic;">//不是则返回0</span><br />
<span style="color: #993333;">int</span><br />
vsf_sysutil_isprint<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> the_char<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #808080; font-style: italic;">/* From Solar - we know better than some libc's! Don't let any potential<br />
&nbsp; &nbsp;* control chars through<br />
&nbsp; &nbsp;*/</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> uc <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span><span style="color: #009900;">&#41;</span> the_char<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>uc <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">31</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>uc <span style="color: #339933;">==</span> <span style="color: #0000dd;">177</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>uc <span style="color: #339933;">&gt;=</span> <span style="color: #0000dd;">128</span> <span style="color: #339933;">&amp;&amp;</span> uc <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">159</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/isprint.html"><span style="color: #000066;">isprint</span></a><span style="color: #009900;">&#40;</span>the_char<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>我的解决办法是用下面自定义的一个函数替换其vsf_sysutil_isprint 函数中的 isprint :<br />
ps:utf-8 中三字节字符范围是： 0&#215;80-0xbf  ,   0xe0-0xef .<br />
按字节顺序： [0xe0-0xef]   [0x80-0xbf]  [0x80-0xbf]<br />
即第一个字节是以1110开头的，第二，第三字节都是以10开头的。</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">//注：此函数写得并不好，只是简单地跳过中文字符。</span><br />
<span style="color: #993333;">int</span> my_isprint<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> the_char<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">static</span> <span style="color: #993333;">int</span> is_cjk<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">static</span> <span style="color: #993333;">int</span> is_cjk2<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">static</span> <span style="color: #993333;">int</span> first_time<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>first_time<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; first_time<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> uc<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #009900;">&#41;</span> the_char<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>uc <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">31</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>uc <span style="color: #339933;">==</span> <span style="color: #0000dd;">177</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>uc <span style="color: #339933;">&gt;=</span><span style="color: #0000dd;">128</span> <span style="color: #339933;">&amp;&amp;</span> uc <span style="color: #339933;">&lt;=</span><span style="color: #0000dd;">159</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>uc <span style="color: #339933;">&gt;=</span> <span style="color: #208080;">0xe0</span> <span style="color: #339933;">&amp;&amp;</span> uc <span style="color: #339933;">&lt;=</span><span style="color: #208080;">0xef</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span> <span style="color: #208080;">0xE</span> <span style="color: #339933;">==</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span><span style="color: #009900;">&#41;</span> uc<span style="color: #339933;">&gt;&gt;</span><span style="color: #0000dd;">4</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; is_cjk<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>uc <span style="color: #339933;">&gt;=</span> <span style="color: #208080;">0x80</span> <span style="color: #339933;">&amp;&amp;</span> uc <span style="color: #339933;">&lt;=</span><span style="color: #208080;">0xbf</span> <span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #339933;">&amp;&amp;</span> is_cjk <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span> <span style="color: #208080;">0x2</span> <span style="color: #339933;">==</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span><span style="color: #009900;">&#41;</span> uc<span style="color: #339933;">&gt;&gt;</span><span style="color: #0000dd;">6</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; is_cjk2<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>first_time <span style="color: #339933;">&amp;&amp;</span> is_cjk <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>first_time <span style="color: #339933;">&amp;&amp;</span> is_cjk <span style="color: #339933;">&amp;&amp;</span> is_cjk2 <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/isprint.html"><span style="color: #000066;">isprint</span></a><span style="color: #009900;">&#40;</span>the_char<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>更简单的解决办法是，将logging.c中第163行的：  str_replace_unprintable(p_str, &#8216;?&#8217;); 注释掉。(<strong>不过，这样可能引发不安全的因素！</strong>)</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// &nbsp;str_replace_unprintable(p_str, '?');</span></div></td></tr></tbody></table></div>
<p>修改代码前与修改代码后的对比（图中显示？号的是修改代码前的log ）：<br />
<a href="http://static.ihacklog.com/wp-files/2010/09/vsftpd-log-Chinese-word.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2010/09/vsftpd-log-Chinese-word-550x189.png" alt="" title="vsftpd-log-Chinese-word" width="550" height="189" class="alignnone size-medium wp-image-4118" /> </a></p>
<p>其它的解决办法：<br />
<a target="_blank" href="http://www.ihacklog.com/l.php?url=forum.share-blog.com/viewthread.phporz__tid=40">解決 vsftpd Log檔亂碼的替代方式</a></p>
<blockquote><p>
vsftpd的log檔有問題~ 老是把中文顯現為?<br />
但是記載在 messages裡面是正常的</p>
<p>所以我先在 vsftpd.conf中設定<br />
syslog_enable=YES</p>
<p>讓log寫到messages裡面</p>
<p>然後寫二個小程式，丟到 /usr/local/bin 下面，讓大家都能用<br />
還算滿好用的~ 寫shell部份也可至SHELL程式區討論</p>
<p>程式一<br />
#!/bin/sh<br />
echo “查詢FTP的LOG檔”<br />
grep vsftpd /var/log/messages | grep Client</p>
<p>程式二<br />
#!/bin/sh<br />
echo “查詢FTP的LOG檔”<br />
echo “輸入欲查詢使用者名稱”<br />
echo “INPUT USER NAME”<br />
read username<br />
grep vsftpd /var/log/messages | grep Client |grep $username
</p></blockquote>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4116";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4116" title="Permanent Link to vsftpd.log 中文显示为问号?的解决办法" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4116</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2009年10月29日  //  <a href="http://ihacklog.com/php/wordpress/wpskills/use-highslide-for-wordpress.html" title="给wordpress图片加上Highslide特效(修正代码)">给wordpress图片加上Highslide特效(修正代码)</a> (30)</li><li>2011年06月6日  //  <a href="http://ihacklog.com/software/powerfulvim/php-manual-for-vim.html" title="PHP Manual for Vim">PHP Manual for Vim</a> (1)</li><li>2011年06月12日  //  <a href="http://ihacklog.com/xml_html_css_js_ajax/most-used-css-tricks.html" title="Most used CSS tricks">Most used CSS tricks</a> (0)</li><li>2009年10月28日  //  <a href="http://ihacklog.com/memory/music/sadness-or-happiness.html" title="忧伤还是快乐">忧伤还是快乐</a> (4)</li><li>2011年07月6日  //  <a href="http://ihacklog.com/php/yii-php/yii-config-use-ms-sql-server-2005-2008.html" title="Yii连接mssql 数据库配置">Yii连接mssql 数据库配置</a> (0)</li><li>2011年08月20日  //  <a href="http://ihacklog.com/php/habari/hb-smilies.html" title="原创插件：HB smilies">原创插件：HB smilies</a> (3)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/vsftpd-log-chinese-replacement-problem-solution.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>桌面IE图标无法删除的解决办法及IE主页被劫持的修复</title>
		<link>http://ihacklog.com/programming/c/delete-bad-desktop-ie-icon.html</link>
		<comments>http://ihacklog.com/programming/c/delete-bad-desktop-ie-icon.html#comments</comments>
		<pubDate>Thu, 17 Jun 2010 17:14:59 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[修复]]></category>
		<category><![CDATA[劫持]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/?p=3920</guid>
		<description><![CDATA[中招状况： 桌面ie图标删除不了，右键点击只有”属性“ 、打开等三个东东，没有删除、重命名什么的。 分别用VC 和 reg 写了解决这个讨厌鬼的东东： VC版: reg版： PS：就在我刚才在更新日志时发现WP提示：”WordPress 3.0 版本可用！“ ，跑到官方去看了下，暂时还只放出了英文版的。已经不早了（其实是很早，哈哈晚上1点50几），得睡觉去了，明天再来更新。 相关技术文档： 关于VC注册表操作注册表，可以参考： http://hi.baidu.com/walfer/blog/item/c5...]]></description>
			<content:encoded><![CDATA[<p>中招状况：<br />
桌面ie图标删除不了，右键点击只有”属性“ 、打开等三个东东，没有删除、重命名什么的。<br />
分别用VC 和 reg   写了解决这个讨厌鬼的东东：<br />
VC版:<br />
注意：该页面嵌入了下载文件，请访问 <a href="http://ihacklog.com/programming/c/delete-bad-desktop-ie-icon.html">该页面</a>下载该文件。</p>
<p>reg版：<br />
注意：该页面嵌入了下载文件，请访问 <a href="http://ihacklog.com/programming/c/delete-bad-desktop-ie-icon.html">该页面</a>下载该文件。<br />
PS：就在我刚才在更新日志时发现WP提示：<strong>”WordPress 3.0 版本可用！“ </strong> ，跑到官方去看了下，暂时还只放出了英文版的。已经不早了（其实是很早，哈哈晚上1点50几），得睡觉去了，明天再来更新。</p>
<p><a href="http://static.ihacklog.com/wp-files/2010/06/clear_bad_ie.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2010/06/clear_bad_ie-550x260.png" alt="" title="clear_bad_ie" width="550" height="260" class="alignnone size-medium wp-image-3922" /> </a></p>
<p>相关技术文档：<br />
关于VC注册表操作注册表，可以参考： <a target="_blank" href="http://www.ihacklog.com/l.php?url=hi.baidu.com/walfer/blog/item/c5a9cefd19754c44d6887dbb.html">http://hi.baidu.com/walfer/blog/item/c5a9cefd19754c44d6887dbb.html</a></p>
<p>使用注册项 (.reg) 文件添加、修改或删除注册表子项和值<br />
<a target="_blank" href="http://www.ihacklog.com/l.php?url=www.ihacklog.com/l.php?url=support.microsoft.com/kb/310516/zh-cn">http://support.microsoft.com/kb/310516/zh-cn</a></p>
<p>读写注册表  <a href="http://www.ihacklog.com/l.php?url=www.cppblog.com/kyelin/archive/2007/01/25/17998.html">http://www.cppblog.com/kyelin/archive/2007/01/25/17998.html</a></p>
<p>wcslen函数～宽字符和C <a href="http://www.ihacklog.com/l.php?url=www.ieing.cn/html-85961-1.html">http://www.ieing.cn/html-85961-1.html</a></p>
<p>关于wcslen的一个错觉! <a href="http://www.daxue.in/read-htm-tid-1895-fpage-491.html">http://www.daxue.in/read-htm-tid-1895-fpage-491.html</a></p>
<p>如何实现用于 Windows NT 的递归 RegDeleteKey <a target="_blank" href="http://www.ihacklog.com/l.php?url=support.microsoft.com/kb/142491/zh-cn">http://support.microsoft.com/kb/142491/zh-cn</a></p>
<p>枚举注册表搜索病毒痕迹的实现思路 <a target="_blank" href="http://www.ihacklog.com/l.php?url=huaidan.org/archives/1666.html">http://huaidan.org/archives/1666.html</a></p>
<p>VC删除文件，目录，设置隐藏属性  <a target="_blank" href="http://www.ihacklog.com/l.php?url=www.blogguy.cn/show-725-1.html">http://www.blogguy.cn/show-725-1.html</a></p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=3920";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=3920" title="Permanent Link to 桌面IE图标无法删除的解决办法及IE主页被劫持的修复" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=3920</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2010年05月7日  //  <a href="http://ihacklog.com/linux/ubuntu-desktop/fix-grub-for-ubuntu.html" title="折腾bootsect和grub4dos导致ubuntu的grub消失">折腾bootsect和grub4dos导致ubuntu的grub消失</a> (11)</li><li>2010年03月11日  //  <a href="http://ihacklog.com/software/tips/delete-evil-internet-explorer-iocn-on-desktop.html" title="删除流氓软件伪造的Internet Explorer">删除流氓软件伪造的Internet Explorer</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/delete-bad-desktop-ie-icon.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

