<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Chip's Tips for Developers » C and C++</title>
	
	<link>http://www.chipstips.com</link>
	<description>Contains coding, but not narcotic.</description>
	<lastBuildDate>Mon, 04 Feb 2013 20:50:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ChipsTipsC" /><feedburner:info uri="chipstipsc" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Sudoku snatched from the jaws of death</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/68iHZmwCSp4/</link>
		<comments>http://www.chipstips.com/?p=622#comments</comments>
		<pubDate>Wed, 24 Aug 2011 19:43:25 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[C and C++]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[curses]]></category>
		<category><![CDATA[c_sudoku]]></category>
		<category><![CDATA[deprecation]]></category>
		<category><![CDATA[eitanadler]]></category>
		<category><![CDATA[freebsd]]></category>
		<category><![CDATA[ports]]></category>
		<category><![CDATA[sudoku]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/?p=622</guid>
		<description><![CDATA[At the weekly IRC meeetings of the ##copyfree channel, Eitan Adler has been bugging encouraging us to assume maintainership of unmaintained FreeBSD ports. He provided the following script to list any ports that are installed on your system that have no maintainer: #!/bin/sh cd /usr/ports; grep -F "$(for o in $(pkg_info -qao) ; \ do [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=622"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=622" data-text="Sudoku snatched from the jaws of death" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D622&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=622";
			reddit_title = "Sudoku snatched from the jaws of death";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=622";</script>
		<script type="text/javascript">var dzone_title = "Sudoku snatched from the jaws of death";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>At the <a href="http://copyfree.org/community/">weekly IRC meeetings</a> of the ##copyfree channel, <a href="http://blog.eitanadler.com">Eitan Adler</a> has been <del>bugging</del> encouraging us to assume maintainership of unmaintained <a href="http://www.freebsd.org/ports/">FreeBSD ports</a>.  He provided the following script to list any ports that are installed on your system that have no maintainer:</p>
<pre><code>
#!/bin/sh
cd /usr/ports;
grep -F "$(for o in $(pkg_info -qao) ; \
    do
	echo "|/usr/ports/${o}|" ;
    done);" $(make -V INDEXFILE) | \
    grep -i \|ports@freebsd.org\| | cut -f 2 -d \|
</code></pre>
<p>In the list of 60 such ports on my system I happened to notice &#8220;games/sudoku&#8221; &#8212; a nice little curses console-based version of Sudoku that I play from time to time.  The responsiveness of its keyboard interface makes me like it even better than my own <a href="http://chipstips.com/?tag=jssudoku">Javascript version</a>.  I decided to adopt this little orphaned gem.</p>
<p>As soon as I attempted &#8216;port test&#8217;, however, I found that not only was this port an orphan, it was scheduled for execution!  It was marked &#8220;deprecated&#8221; because no more distfiles were available.  Apparently, the author&#8217;s site went 404.  Yet, I obviously had it installed on my system.  But did I still have the sources?  Most of the time, a port&#8217;s &#8220;work&#8221; directory gets cleaned after installing or upgrading, and I periodically purge my <code>/usr/ports/distfiles</code> directory, too.  How long had it been?</p>
<p>Unbelievably, I still had the tarball for sudoku in my <code>/usr/ports/distfiles</code>.  I told Eitan I would host it myself, and he walked me through the changes I should make to the port and submit as a PR.  The FreeBSD ports team approved the PR, and now I&#8217;m both the upstream and port maintainer for games/sudoku.</p>
<p>When I get some time, I plan to fully grok the code for this project, then look at enhancing it.  For one thing, I&#8217;d like to keep some sort of score.  With that in mind, I&#8217;ve put the sources into Bitbucket.  You can download it from there, or via the tarball, or simply install the FreeBSD port.</p>
<p>Michael Kennett, the original author, released this source into the public domain in 2005.  After I modify the code, I&#8217;ll release my version under the <a href="http://owl.apotheon.org">OWL</a>, as usual.</p>
<div class="downloadbuttons">
    <a href="http://bitbucket.org/sterlingcamden/sudoku"><img src="http://www.chipstips.com/images/bitbucket.png" alt="BitBucket repository" /></a><br />
    <a href="http://www.chipstips.com/download/sudoku-1.0.2.tbz"><img src="http://www.chipstips.com/images/tarball.png" alt="tarball" /></a>
</div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/68iHZmwCSp4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=622</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=622</feedburner:origLink></item>
		<item>
		<title>Per-CPU usage stats for FreeBSD revisited</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/DUw2VWqNFg4/</link>
		<comments>http://www.chipstips.com/?p=561#comments</comments>
		<pubDate>Sat, 30 Oct 2010 21:51:32 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[C and C++]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[cpu]]></category>
		<category><![CDATA[C_language]]></category>
		<category><![CDATA[freebsd]]></category>
		<category><![CDATA[stats]]></category>
		<category><![CDATA[sysctl]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/?p=561</guid>
		<description><![CDATA[Since the last time I wrote here about the pcpustat utility: I submitted it as a FreeBSD port, and got it accepted. I submitted its license (the OWL) to the FreeBSD license makefile, which was also committed. I updated pcpustat to version 1.4 and submitted a patch to the port, which &#8220;sunpoet&#8221; committed in record [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=561"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=561" data-text="Per-CPU usage stats for FreeBSD revisited" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D561&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=561";
			reddit_title = "Per-CPU usage stats for FreeBSD revisited";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=561";</script>
		<script type="text/javascript">var dzone_title = "Per-CPU usage stats for FreeBSD revisited";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>Since <a href="http://www.chipstips.com/?p=527">the last time I wrote here about the pcpustat utility</a>:</p>
<ul>
<li>I submitted it as a FreeBSD port, and got it accepted.</li>
<li>I submitted its license (the <a href="http://owl.apotheon.org">OWL</a>) to the FreeBSD license makefile, which was also committed.</li>
<li>I updated pcpustat to version 1.4 and submitted a patch to the port, which &#8220;sunpoet&#8221; committed in record time (<a href="http://www.freebsd.org/cgi/query-pr.cgi?pr=151838">less than 21 minutes!</a>)
</ul>
<p>Version 1.4 includes a new option, -d (&#8211;delim) that allows you to specify a delimiter between columns of output.  The default behavior remains the same:  the columns will be right-justified with spaces.  But if you specify a delimiter, no justification occurs.  The delimiter can be one or more characters, and you can get special characters past the shell by enclosing the string in quotes.</p>
<p>Thus, you can take the usual:</p>
<pre><code>
$ pcpustat
               cpu 0               cpu 1               cpu 2               cpu 3
  us  ni  sy  in  id  us  ni  sy  in  id  us  ni  sy  in  id  us  ni  sy  in  id
   2   0   1   0  98   0   0   2   0  98   1   0   2   0  98   0   0   5   0  95
</code></pre>
<p>and change it, for instance, into a CSV format:</p>
<pre><code>
$ pcpustat -d ,
cpu 0,,,,,cpu 1,,,,,cpu 2,,,,,cpu 3,,,,
us,ni,sy,in,id,us,ni,sy,in,id,us,ni,sy,in,id,us,ni,sy,in,id
0,0,4,0,96,1,0,2,0,98,2,0,2,0,97,0,0,1,0,99
</code></pre>
<p>Or, make a nice, compact string of &#8220;percent used&#8221; for a status bar:</p>
<pre><code>
$ pcpustat -xiqd :
4:1:5:2
</code></pre>
<p>If you&#8217;re on FreeBSD, you can just install or update the port.  Or:</p>
<div class="downloadbuttons">
    <a href="http://bitbucket.org/sterlingcamden/pcpustat"><img src="http://www.chipstips.com/images/bitbucket.png" alt="BitBucket repository" /></a><br />
    <a href="http://www.chipstips.com/download/pcpustat-1.4.tar.bz2"><img src="http://www.chipstips.com/images/tarball.png" alt="tarball" /></a>
</div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/DUw2VWqNFg4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=561</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=561</feedburner:origLink></item>
		<item>
		<title>Tiling Windows windows, my way</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/LL7wScwDkLY/</link>
		<comments>http://www.chipstips.com/?p=541#comments</comments>
		<pubDate>Sun, 01 Aug 2010 00:50:54 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[C and C++]]></category>
		<category><![CDATA[Wildly popular]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[cpptile]]></category>
		<category><![CDATA[C_language]]></category>
		<category><![CDATA[desktop]]></category>
		<category><![CDATA[tiling]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/?p=541</guid>
		<description><![CDATA[Now that I&#8217;ve been using XMonad as my window manager on FreeBSD for a while, I find that when I have to work on Windows one of the more painful aspects thereof is, ironically, window management. I spend way too much time hunting for the right window or arranging windows so I can clearly see [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=541"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=541" data-text="Tiling Windows windows, my way" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D541&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=541";
			reddit_title = "Tiling Windows windows, my way";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=541";</script>
		<script type="text/javascript">var dzone_title = "Tiling Windows windows, my way";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>Now that I&#8217;ve been using <a href="http://xmonad.org">XMonad</a> as my window manager on FreeBSD for a while, I find that when I have to work on Windows one of the more painful aspects thereof is, ironically, window management.  I spend way too much time hunting for the right window or arranging windows so I can clearly see all the ones I need at once.  Windows Vista and 7 have the ability to view windows side-by-side or stacked, but that&#8217;s not adequate to my needs and it virtually requires using the mouse.  I desperately want an automatic tiling window manager like XMonad.</p>
<p>So, I created the next best thing:  a command-line utility that will arrange all the windows in a tiled fashion, according to a pattern specified per monitor.  In the download below you will find all the sources, plus in the Release subdirectory I&#8217;ve included an executable built with Visual Studio 2010.</p>
<p>For usage, type &#8217;tile&#8217; at a command prompt without arguments.  But, to give you some examples:</p>
<p><code>tile 1 2</code></p>
<p>This tiles both the primary and second monitors using a column-based scheme, with no maximum of columns.  Each display will be divided into the number of columns needed to hold all of the windows on that monitor.</p>
<p><code>tile 1:2r 2:3c</code></p>
<p>This tiles the primary monitor into 2 rows, with each row split as evenly as possible to accomodate all of the windows.  Thus, if the primary window contains three windows, the first window will occupy the entire top row, and the second row will be split between the other two windows.  On monitor #2, we&#8217;ll have three columns &#8212; so if we have five windows there, we&#8217;ll get one window in the first column and two windows stacked in each of the two remaining columns.</p>
<p>If you specify more rows or columns than there are windows on that monitor, it&#8217;s automatically reduced to the number of windows.  Tile silently ignores most errors, such as specifying a monitor that doesn&#8217;t exist.  Tile does not affect windows that are maximized or minimized.  In order to be considered within a monitor, at least 10 pixels of the window in both directions must intersect the monitor&#8217;s rectangle.</p>
<p>As noted above, all parts of an argument except for the monitor number are optional.  If omitted, the max defaults to 99, and row/column defaults to column.</p>
<p>So, now I can choose my optimal layout, put it in a little batch file named &#8216;t.bat&#8217;, and whenever I need my windows to snap into place I can just type &#8216;t&#8217; at a command prompt.</p>
<div class="downloadbutton"><a href="http://www.chipstips.com/download/cpptile.zip"><img src="http://www.chipstips.com/images/download.gif" alt="download" /></a></div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/LL7wScwDkLY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=541</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=541</feedburner:origLink></item>
		<item>
		<title>Per-CPU usage stats for FreeBSD</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/SmJE6Fx3Mnk/</link>
		<comments>http://www.chipstips.com/?p=527#comments</comments>
		<pubDate>Mon, 12 Jul 2010 19:47:31 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[C and C++]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[cpu]]></category>
		<category><![CDATA[C_language]]></category>
		<category><![CDATA[c_pcpustat]]></category>
		<category><![CDATA[freebsd]]></category>
		<category><![CDATA[stats]]></category>
		<category><![CDATA[sysctl]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/?p=527</guid>
		<description><![CDATA[On my FreeBSD system, I like to keep various statistics displayed in my xmobar at all times. Most of the system statistics that are built into xmobar require the /proc filesystem procfs(5), which isn&#8217;t mounted by default in FreeBSD for security reasons. Thus, I have rolled my own pipes to grab this infomation from sysctl(8) [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=527"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=527" data-text="Per-CPU usage stats for FreeBSD" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D527&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=527";
			reddit_title = "Per-CPU usage stats for FreeBSD";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=527";</script>
		<script type="text/javascript">var dzone_title = "Per-CPU usage stats for FreeBSD";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>On my FreeBSD system, I like to keep various statistics displayed in my <a href="http://code.haskell.org/~arossato/xmobar/">xmobar</a> at all times.  Most of the system statistics that are built into xmobar require the /proc filesystem procfs(5), which isn&#8217;t mounted by default in FreeBSD for security reasons.  Thus, I have rolled my own pipes to grab this infomation from sysctl(8) and other places.  One such statistic that proved difficult was per-CPU usage.  The top(1) utility shows this information if you specify the -P option, but you can&#8217;t get that output to a pipe in order to filter or reformat it.</p>
<p>After digging around in top&#8217;s sources (/usr/src/contrib/top and /usr/src/usr.bin/top), I was able to clone top&#8217;s algorithm with an output that mimics iostat(8).  Behold:</p>
<pre><code>
% pcpustat -c 10
               cpu 0               cpu 1               cpu 2               cpu 3
  us  ni  sy  in  id  us  ni  sy  in  id  us  ni  sy  in  id  us  ni  sy  in  id
  12   1   5   0  83   2   1   3   0  94  79   1   7   1  13   1   0   0   0  99
  46   0   5   0  50   2   0   2   0  97  50   0   2   0  47   1   1   5   0  93
  30   1   5   1  64   3   1   4   0  92  44   0   5   0  51  14   0   5   0  81
   2   2   1   0  95   0   0   6   0  94  92   0   5   0   3   1   1   0   0  98
   6   1   6   0  87   8   2   4   0  87  76   0   8   0  16   1   0   5   0  95
   0   0   6   0  94  33   2   3   0  62  49   0   8   0  44  10   0   2   0  89
   2   1   3   0  94  26   0   5   0  70  69   1   5   0  25   0   0   1   0  99
   2   1   7   0  91  12   1   2   0  85  80   0   5   0  14   2   0   0   0  98
   1   0   6   0  93   7   1   3   0  88  86   1   8   0   5   1   0   1   0  98
  26   1   6   0  68  50   0   4   0  47   8   0   1   1  90  14   0   2   0  84
</code></pre>
<p>I have a &#8216;make buildworld&#8217; in progress, otherwise it would be almost all idle time.</p>
<p>I&#8217;ve included options to filter the states and CPUs you want to monitor, as well as to flip the percentages (i.e., <em>not</em> in that state).  For example:</p>
<p><code>pcpustat --not --idle --quiet --wait 2</code></p>
<p>generates four columns every 2 seconds, representing the overall utilization of each CPU.</p>
<p>Note how I have both long and short forms of each switch.  This utility provided a good chance for me to become familiar with the getopt_long(3) function in C.  It also serves as a good example of not only how to use it, but also how to provide help for it.</p>
<p>Rather than putting everything into the README, I decided this would be a good opportunity to write my first man(1) page.  I&#8217;ve included that, and if you want to read it right where it is (after you extract the tarball below or clone the <a href="http://bitbucket.org/sterlingcamden/pcpustat">BitBucket Repository</a>), you can use:</p>
<p><code>man -M <em>project-path</em>/man pcpustat</code></p>
<p>or copy it to /usr/local/man.</p>
<p>I&#8217;m thinking this utility might be a good candidate for my first FreeBSD port.  I don&#8217;t think it would port well to non-BSD systems, because it makes use of sysctl(3).</p>
<p>UPDATE 2010-09-13:  This utility is now available through FreeBSD ports as <a href="http://www.freshports.org/sysutils/pcpustat/">sysutils/pcpustat</a>.  Thanks to pgollucci for committing it!</p>
<p>UPDATE 2010-09-14:  Patched for a flaw in determining buffer size that caused a failure on some systems.</p>
<div class="downloadbutton"><a href="http://www.chipstips.com/download/pcpustat-1.3.tar.bz2"><img src="http://www.chipstips.com/images/download.gif" alt="download" /></a></div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/SmJE6Fx3Mnk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=527</wfw:commentRss>
		<slash:comments>29</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=527</feedburner:origLink></item>
		<item>
		<title>Masked coder saves the day with updates to tee utility for Windows</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/j7fqIsiRqXE/</link>
		<comments>http://www.chipstips.com/?p=430#comments</comments>
		<pubDate>Thu, 28 Jan 2010 19:05:37 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C and C++]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[cpptee]]></category>
		<category><![CDATA[pipe]]></category>
		<category><![CDATA[tee]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/?p=430</guid>
		<description><![CDATA[A commenter going by Anonymous Coward (I bet that’s not his real name, and I beg forgiveness if the masculine pronoun doesn’t apply) provided a solution to the double-spaced output problem users occasionally experienced with my tee utility for Windows. Naturally, this issue occurred because of the inconsistent usage of carriage-returns and linefeeds to terminate [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=430"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=430" data-text="Masked coder saves the day with updates to tee utility for Windows" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D430&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=430";
			reddit_title = "Masked coder saves the day with updates to tee utility for Windows";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=430";</script>
		<script type="text/javascript">var dzone_title = "Masked coder saves the day with updates to tee utility for Windows";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p><img style="margin: 0px 10px 0px 0px; display: inline" align="left" src="http://chipstips.com/wp-content/images/TeeutilityinRubythatalsoworksonWindows_BAA3/image05.png" />A commenter going by Anonymous Coward (I bet that’s not his real name, and I beg forgiveness if the masculine pronoun doesn’t apply) <a href="http://www.chipstips.com/?p=129#comment-96940">provided a solution</a> to the <a href="http://www.chipstips.com/?p=349">double-spaced output problem</a> users occasionally experienced with my <a href="http://www.chipstips.com/?p=129">tee utility for Windows</a>.</p>
<p>Naturally, this issue occurred because of the inconsistent usage of carriage-returns and linefeeds to terminate lines of text, combined with Console::ReadLine’s interpretation thereof.&#160; The solution, simply enough, was to use BinaryReader for input, and BinaryWriter for output, so no interpretation or translation of the characters would occur.</p>
<p>Like most good programmers, once he had inserted hands in the code, Anonymous Coward couldn’t leave this one alone.&#160; He coded up several more features and emailed them to me.&#160; I made a few corrections and included the new version here.</p>
<p>New features include:</p>
<ul>
<li>Duplicated file names are silently ignored, rather than causing a file open conflict.</li>
<li>/? or –help displays usage information.</li>
<li>-i or –ignore ignores cancel (Ctrl+C), instead of always ignoring it.</li>
<li>-f optionally marks the end of switches and interprets all remaining arguments as filenames.</li>
</ul>
<p>Thanks, Anonymous Coward – whoever you are!</p>
<div class="downloadbutton"><a href="http://www.chipstips.com/download/cpptee.zip"><img src="http://www.chipstips.com/images/download.gif" /></a></div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/j7fqIsiRqXE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=430</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=430</feedburner:origLink></item>
		<item>
		<title>Tee utility for Windows updated</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/VUaywpf8tPA/</link>
		<comments>http://www.chipstips.com/?p=349#comments</comments>
		<pubDate>Tue, 18 Aug 2009 23:32:07 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C and C++]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[clr]]></category>
		<category><![CDATA[clroldsyntax]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[cpptee]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[lambdas]]></category>
		<category><![CDATA[tee]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/?p=349</guid>
		<description><![CDATA[How could something so simple turn into something so complicated? One of my most popular (perhaps the most popular) post on this site is my tee utility for Windows.  I wrote it back in 2003 using the Visual Studio vintage of the same year.  Rather than including stdio.h and writing it in good old plain [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=349"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=349" data-text="Tee utility for Windows updated" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D349&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=349";
			reddit_title = "Tee utility for Windows updated";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=349";</script>
		<script type="text/javascript">var dzone_title = "Tee utility for Windows updated";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p><img style="margin: 0px 10px 0px 0px; display: inline" src="http://chipstips.com/wp-content/images/TeeutilityinRubythatalsoworksonWindows_BAA3/image05.png" alt="" align="left" />How could something so simple turn into something so complicated?</p>
<p>One of my most popular (perhaps <strong>the </strong>most popular) post on this site is my <a href="http://www.chipstips.com/?p=129">tee utility for Windows</a>.  I wrote it back in 2003 using the Visual Studio vintage of the same year.  Rather than including stdio.h and writing it in good old plain vanilla C code, I decided to employ the then-new managed extensions for C++ so it could &lt;scare-quotes&gt; benefit from &lt;/scare-quotes&gt; the .NET Framework 1.1.  That’s where my troubles were begotten – but it took a while for them to gestate.</p>
<p>The first problem I encountered was trying to run it from a network share.  Microsoft, in a truly dramatic performance of <a href="http://blogs.techrepublic.com.com/security/?p=1274">security theater</a>, decided to treat network shares as “Intranet Zone” and untrusted by default.  Sure, there were ways to establish a trusted zone, but it was a pain to remember.  Thankfully, <a href="http://adrianba.net/archive/2008/09/06/running-managed-code-from-a-network-share.aspx">they’ve solved that one for me</a>.</p>
<p>Then recently, user Alex Armstrong <a href="http://www.chipstips.com/?p=129#comment-80495">noticed a few anomalies</a> when using tee with ping.  He likes to use ping –t to repeat until interrupted, at which point ping prints out its statistics and then stops.  But when ping was piped to tee, Ctrl+C would interrupt tee instead – which closed the pipe and killed ping before it could relieve itself of its statistics.  Not only that, but since tee’s output files were not flushed, some of the output that had already been displayed on the console would not make it to the file(s).</p>
<p>I tried setting the Console property <a href="http://msdn.microsoft.com/en-us/library/system.console.treatcontrolcasinput.aspx">TreatControlCAsInput</a> to true, but when the input stream is a pipe that caused tee to go into la-la land.  Reading further, I found the <a href="http://msdn.microsoft.com/en-us/library/system.console.cancelkeypress.aspx">CancelKeyPress</a> event – but for some reason I couldn’t get it to compile with the old syntax that I had used back in 2003.  With a little research, I found that the only reason why the compiler was tolerating most of my code was because the project upgrade wizard had added the <a href="http://msmvps.com/blogs/vandooren/archive/2006/11/13/calling-labview-vis-through-c-style-callback-function-pointers.aspx">/clr:oldSyntax switch</a> when upgrading to VS2008.  When I removed that switch, all hell broke loose.  How could they change the syntax for writing managed code in C++ so drastically?  This little utility, which is only about 35 lines of real code, required changes on 15 of those lines.  Yes, they were mostly trivial – but how annoying.  It’s not the same language.  Yes, it’s probably better syntax, but it’s not the same language.</p>
<p>One change I would have liked to make is to use <a href="http://msdn.microsoft.com/en-us/library/bwabdf9z.aspx">List&lt;T&gt;.ForEach</a> to iterate through the list, rather than a for loop.  But I couldn’t find any way to write the Action code inline until <a href="http://msdn.microsoft.com/en-us/library/dd293608(VS.100).aspx">lambdas arrive in VS2010</a>.  Now that’s a syntax change I could get excited about.</p>
<p>Long story short, canceling the CancelKeyPress event (is that a double negative?) worked &#8212; Ctrl+C is now seen by ping, which prints out its statistics and closes the pipe, which stops tee normally.  If you should ever need to interrupt tee itself, use Ctrl+Break.</p>
<p>One more thing (as <a rel="nofollow" href="http://en.wikipedia.org/wiki/Columbo_(TV_series)">Columbo</a> says) – Alex also noticed that ping’s output is double-spaced when piped through tee.  That’s because ping ends each line with a double carriage-return.  Console::ReadLine treats each of those as a terminator.  I don’t know why the command prompt window doesn’t have an issue with that, but I do.  I can’t just ignore zero-length lines, because then I’d lose spacing from other programs that don’t use a double carriage-return.  Any ideas?  This is supposed to be simple.</p>
<div class="downloadbutton"><a href="http://www.chipstips.com/download/cpptee.zip"><img src="http://www.chipstips.com/images/download.gif" alt="" /></a></div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/VUaywpf8tPA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=349</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=349</feedburner:origLink></item>
		<item>
		<title>Point of style in C coding conventions</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/9In4cfmh3vw/</link>
		<comments>http://www.chipstips.com/?p=295#comments</comments>
		<pubDate>Fri, 12 Jun 2009 16:52:55 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[C and C++]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[conventions]]></category>
		<category><![CDATA[indirection]]></category>
		<category><![CDATA[pointers]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/?p=295</guid>
		<description><![CDATA[An interesting question came up on the Code Project’s Lounge: I&#8217;m trying to work out where to place the asterisk when declaring a pointer variable. Do you think the asterisk belongs next to the type name or the variable name? CType* Pointer = NULL; vs. CType *Pointer = NULL; At first I was inclined to [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=295"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=295" data-text="Point of style in C coding conventions" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D295&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=295";
			reddit_title = "Point of style in C coding conventions";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=295";</script>
		<script type="text/javascript">var dzone_title = "Point of style in C coding conventions";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>An interesting question came up on the <a href="http://www.codeproject.com/Lounge.aspx?msg=3075527#xx3075527xx">Code Project’s Lounge</a>:</p>
<blockquote><p>I&#8217;m trying to work out where to place the asterisk when declaring a pointer variable.    <br />Do you think the asterisk belongs next to the type name or the variable name?
<pre>CType* Pointer = NULL;</pre>
<p>vs. </p>
<pre>CType *Pointer = NULL;</pre>
</blockquote>
<p>At first I was inclined to favor the first form, because I think of “pointer to” as part of the variable’s type information, rather than part of its name.&#160; In fact, the Windows API is full of typedefs for pointers that allow you to declare them using a single token, like so:</p>
<p><code>LPSECURITY_ATTRIBUTES lpsa = NULL;&#160;&#160;&#160; // Pointer to a SECURITY_ATTRIBUTES structure</code></p>
<p>Unfortunately, this practice hides the fact that C/C++ requires you to apply the * to every pointer variable in a multi-variable declaration.&#160; Thus:</p>
<p><code>CType* Pointer1, Pointer2;</code></p>
<p>Doesn’t give you what you might expect, as Pointer2 is now an instance of CType, not a pointer to one.&#160; That’s even less obvious when using one of Microsoft’s pointer typedefs:</p>
<p><code>LPDWORD pWord1, pWord2;</code></p>
<p>where pWord2 is actually a DWORD.</p>
<p>The C language treats type and level of indirection as separate concepts.&#160; Thus, it makes sense to separate them syntactically.&#160; But rather than attaching the * sigil to the variable name, perhaps it acts as a <a href="http://weblog.raganwald.com/2007/11/programming-conventions-as-signals.html">better signal</a> that indirection is an independent characteristic of the variable to separate it from both its type and its name:</p>
<p><code>CType * Pointer = NULL;</code></p>
<p>Or perhaps the best solution is not to use pointers.</p>
<p>What do you think?</p>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/9In4cfmh3vw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=295</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=295</feedburner:origLink></item>
		<item>
		<title>CFile::GetStatus on root folder works sometimes</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/w0zylgECzq8/</link>
		<comments>http://www.chipstips.com/?p=267#comments</comments>
		<pubDate>Sun, 03 May 2009 17:04:42 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[C and C++]]></category>
		<category><![CDATA[MFC]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CFile]]></category>
		<category><![CDATA[GetFileAttributes]]></category>
		<category><![CDATA[GetStatus]]></category>
		<category><![CDATA[mfc]]></category>
		<category><![CDATA[SHGetFileInfo]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/?p=267</guid>
		<description><![CDATA[There’s nothing particularly magical about this tip, but its solution stumped me for a while so perhaps it will help out some other poor developer who is madly googling for an answer. An MFC application needs to determine whether the path entered by a user is a valid folder.  The user already has to be [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=267"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=267" data-text="CFile::GetStatus on root folder works sometimes" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D267&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=267";
			reddit_title = "CFile::GetStatus on root folder works sometimes";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=267";</script>
		<script type="text/javascript">var dzone_title = "CFile::GetStatus on root folder works sometimes";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>There’s nothing particularly magical about this tip, but its solution stumped me for a while so perhaps it will help out some other poor developer who is madly googling for an answer.</p>
<p>An MFC application needs to determine whether the path entered by a user is a valid folder.  The user already has to be an Administrator, so we don’t care about privileges.  All we want to know is, does this folder exist and is it indeed a folder and not a file or something else?</p>
<p>The MFC Way of doing this is to call CFile::GetStatus on the path, which fills in a CFileStatus structure and returns TRUE or FALSE.  The CFileStatus structure contains an m_attribute member that has bit values for whether its a folder, a file, etc.  This works fine most of the time.  But occasionally, calling it on the root folder of a device (e.g., “C:\”) will return FALSE.  The user reported that it failed only when the application was started from a command prompt instead of the Start menu.  That elicited from me a great big “Huh?”</p>
<p>Digging into it, I found that the problem reproduced when running from some command prompts, but not others.  From a command prompt where it normally failed, it wouldn’t fail if I started Visual Studio from that same prompt to debug it.  Thus, I resorted to the dreaded MessageBox debugging methodology.</p>
<p>I also googled this problem, found that others had run across it as well, but didn’t come up with any good solution.  One commenter suggested using SHGetFileInfo, which seemed to solve the problem until I realized that it also returned normally for folders that don’t exist.</p>
<p>I looked through the MFC sources and found that while the instance method for CFile::GetStatus simply uses GetFileAttributes, the static method that I was using does a FindFirstFile instead.  So I changed my application to call GetFileAttributes directly, and that works exactly as I would have expected.  It returns INVALID_FILE_ATTRIBUTES if the file or folder does not exist, and it returns a bit value that includes FILE_ATTRIBUTE_FOLDER if it is a folder of any kind (including a root folder).</p>
<p>GetFileAttributes provides a better solution anyway.  CFile::GetStatus just adds a bunch of extra code I didn’t need – even if I had used the instance version instead (which probably would also work).  The lesson here is that in an MFC application, it’s not always optimal to cling to MFC methods.  Take the blinders off and use the Win32 API when that provides precisely what you need.</p>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/w0zylgECzq8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=267</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=267</feedburner:origLink></item>
		<item>
		<title>RC4 encryption in C</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/jCh4PrYNGKU/</link>
		<comments>http://www.chipstips.com/?p=177#comments</comments>
		<pubDate>Thu, 24 Jan 2008 19:49:44 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[C and C++]]></category>
		<category><![CDATA[Wildly popular]]></category>
		<category><![CDATA[4guysfromrolla]]></category>
		<category><![CDATA[cryptography]]></category>
		<category><![CDATA[C_language]]></category>
		<category><![CDATA[c_rc4]]></category>
		<category><![CDATA[mikeshaffer]]></category>
		<category><![CDATA[rc4]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/?p=177</guid>
		<description><![CDATA[A couple of years ago, I published a routine to perform RC4 encryption in Synergy/DE, translated from the original VBScript of Mike Shaffer with his permission.&#160; Lots of people searching for the RC4 algorithm have landed on that post, most of them not knowing the Synergy/DE language.&#160; A couple of days ago, a reader going [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=177"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=177" data-text="RC4 encryption in C" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D177&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=177";
			reddit_title = "RC4 encryption in C";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=177";</script>
		<script type="text/javascript">var dzone_title = "RC4 encryption in C";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>
<div class="downloadbutton"><img style="margin: 0px 10px 0px 0px" height="71" alt="RC4" src="http://chipstips.com/wp-content/images/25d8e9369c73_D35E/clip_image0024.gif" width="104" align="left">A couple of years ago, I published a <a href="http://www.chipstips.com/?p=114">routine to perform RC4 encryption in Synergy/DE</a>, translated from <a href="http://www.4guysfromrolla.com/webtech/010100-1.shtml">the original VBScript of Mike Shaffer</a> with his permission.&nbsp; Lots of people searching for the RC4 algorithm have landed on that post, most of them not knowing the Synergy/DE language.&nbsp; A couple of days ago, a reader going by the alias &#8220;crazysoccer&#8221; asked if I could provide an example in C.</div>
<div class="downloadbutton">&nbsp;</div>
<div class="downloadbutton">The download below contains a straight C version of the famous &#8220;EnDeCrypt&#8221; function, along with a simple &#8220;main&#8221; test program.&nbsp; The flavor of C I used is vendor and platform neutral.&nbsp; It only presumes the availability of stdio and malloc.&nbsp; Naturally, you could tweak this code to use whatever I/O and memory allocation mechanisms your development framework prefers.</div>
<div class="downloadbutton">&nbsp;</div>
<div class="downloadbutton">I&#8217;ve been asked before about the legality of publishing&nbsp;cryptographic algorithms on the web.&nbsp; According to <a rel="nofollow" href="http://en.wikipedia.org/wiki/Export_of_cryptography">Wikipedia</a> (which is not a lawyer, nor do I think it has ever played one on TV), rules for exporting cryptography have been relaxed in recent years, including &#8220;the effective elimination of export controls on &#8230; open source software containing cryptography.&#8221;&nbsp; Since I&#8217;m making this freely available to all, and I welcome any contributions to improving&nbsp; the code, I&#8217;ll consider this &#8220;open source&#8221;.&nbsp; Besides, I wasn&#8217;t the first one to publish this algorithm on the web, and if the Bureau of Industry and Security wants to come after me, it&#8217;ll have to go get <a href="http://www.4guysfromrolla.com/">4GuysFromRolla</a>, too.&nbsp; But if you plan to use this algorithm in software that will be exported from the US or other nations with similar rules, you might want to consult your lawyer on any restrictions you need to follow.</div>
<div class="downloadbutton">&nbsp;</div>
<div class="downloadbutton">UPDATE: Changed the API to include a specified length, since the encrypted data can contain a null.</div>
<div class="downloadbutton">&nbsp;</div>
<div class="downloadbutton"><a href="http://www.chipstips.com/download/c_rc4.zip"><img src="http://www.chipstips.com/images/download.gif"></a></div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/jCh4PrYNGKU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=177</wfw:commentRss>
		<slash:comments>31</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=177</feedburner:origLink></item>
		<item>
		<title>PHP messiness: zero isn’t false, or is it?</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/tYa_aIKny9Q/</link>
		<comments>http://www.chipstips.com/?p=168#comments</comments>
		<pubDate>Tue, 18 Sep 2007 20:02:14 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[C and C++]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[SynergyDE]]></category>
		<category><![CDATA[Wildly popular]]></category>
		<category><![CDATA[boolean]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[false]]></category>
		<category><![CDATA[garyhart]]></category>
		<category><![CDATA[zero]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/?p=168</guid>
		<description><![CDATA[Several years ago, Gary Hart said something that has stuck with me ever since: &#8220;just because it&#8217;s messy doesn&#8217;t mean you don&#8217;t want to do it.&#8221;&#160; We were discussing plans for driving throughout the&#160;UK&#160;while there on business &#8212; but time and again I find this principle applies to other situations as well.&#160; Besides the obvious, [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=168"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=168" data-text="PHP messiness: zero isn&#8217;t false, or is it?" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D168&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=168";
			reddit_title = "PHP messiness: zero isn&#8217;t false, or is it?";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=168";</script>
		<script type="text/javascript">var dzone_title = "PHP messiness: zero isn&#8217;t false, or is it?";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>Several years ago, <a href="http://www.garyhartphotography.com/">Gary Hart</a> said something that has stuck with me ever since: &#8220;just because it&#8217;s messy doesn&#8217;t mean you don&#8217;t want to do it.&#8221;&nbsp; We were discussing plans for driving throughout the&nbsp;UK&nbsp;while there on business &#8212; but time and again I find this principle applies to other situations as well.&nbsp; Besides the obvious, um,&nbsp;recreational possibilities, I can think of many more: eating sloppy sandwiches, parenting, electing public officials, starting a relationship, and coding in&nbsp;PHP.</p>
<p>PHP powers a lot of the web, including such popular frameworks as <a href="http://wordpress.org">WordPress</a>, so you really need to have it in your programming portfolio.&nbsp; But it&#8217;s a messy language.&nbsp; &nbsp;I&#8217;m not just talking about the way that PHP tends to sprawl like semicolon-laden vines around your HTML, earning&nbsp;an alternative interpretation of &#8220;Page Hacked to Pasta&#8221;.&nbsp; Nor am I referring (this time) to&nbsp;its <a href="http://sob.apotheon.org/?p=224">sucky object model</a>.&nbsp; The language itself contains many little gotchas and &#8220;almost right&#8221;s that wait like&nbsp;snares for the feet of the unwary coder.</p>
<p>One <a href="http://www.chipstips.com/?p=167#comment-20532">I ran into yesterday</a> (not for the first time) is&nbsp;PHP&#8217;s partial distinction between false and zero.&nbsp; In PHP, false is boolean, and zero is numeric.&nbsp; Fine and good.&nbsp; But if you treat a zero value as a boolean expression, the zero gets <em>converted </em>to a boolean false.</p>
<p>This seems innocent and helpful enough until you get to a function like <a href="http://www.php.net/manual/en/function.strpos.php">strpos</a>, which returns the position of one string within another.&nbsp; If the target is not found in the source, it returns boolean false.&nbsp; So, to make sure that one string doesn&#8217;t&nbsp;contain another, you&#8217;d think you&#8217;d&nbsp;do something like this:</p>
<p><code>if (!strpos($source, $target))</code></p>
<p>But there&#8217;s a problem here.&nbsp; If $target starts at the beginning of $source, strpos will return 0.&nbsp; Because it&#8217;s in a conditional, that gets converted to boolean false, and the test passes.&nbsp; So, you need to explicitly test for false instead, right?</p>
<p><code>if (strpos($source, $target) == false)</code></p>
<p>Wrong.&nbsp; Because you&#8217;re comparing it against a boolean value, the zero still gets converted to a boolean false.&nbsp; The only right way to do this is:</p>
<p><code>if (strpos($source, $target) === false)</code></p>
<p>The&nbsp;triple&nbsp;equal&nbsp;sign tells PHP to&nbsp;only evaluate to true if both operands have the same type <em>and </em>the same value, so no conversion takes place.&nbsp; Granted, the PHP docs have a great big warning on <a href="http://www.php.net/manual/en/function.strpos.php">strpos</a> to this effect, but the&nbsp;net result is something less than intuitive coding.</p>
<p>Languages like Java and C#&nbsp;solve this problem by not allowing automatic conversion between numeric and boolean values.&nbsp; That&#8217;s sort of like castrating the entire population to prevent birth defects.&nbsp; So why doesn&#8217;t this create problems for other languages?</p>
<p>In Synergy/DE, indices start at 1, so the instr function returns zero for not found:</p>
<p><code>if (!instr(1,source, target))</code></p>
<p>Quite logical and linguistically elegant (except for having the starting index as the first parameter), but then you don&#8217;t get to have the <a href="http://www.panopticoncentral.net/archive/2004/03/17/290.aspx">secret handshake</a>.</p>
<p>Even though Ruby uses a starting index of 0, it prevents confusion in routines like String#index by returning nil instead of a number for not found.&nbsp; Zero is treated as an object &#8212; and thus is <em>not</em> false &#8212; while nil <em>does</em> evaluate to false.&nbsp; Thus,</p>
<p><code>print "good" if ("abcde".index("a"))<br />print "bad" if ("abcde".index("f"))</code></p>
<p>prints &#8220;good&#8221;, even though the index of &#8220;a&#8221; within &#8220;abcde&#8221; is 0.&nbsp; Why should 0 be false anyway?</p>
<p>In C and C++, indices start at 0, and 0 can be used as a stand-in for false (in fact, all falses <em>are</em> zero), yet I hardly ever make the mistake of &#8220;if (strpos(&#8230;))&#8221; in C.&nbsp; Why?&nbsp; Because the <a href="http://www.ai.mit.edu/courses/6.863/doc/cportlib.html#SEC41">documented</a> return value for &#8220;target not found&#8221; isn&#8217;t FALSE, it&#8217;s -1.&nbsp; I&nbsp;immediately know that to test for this return value I must say:</p>
<p><code>if (strpos(source, target) &lt; 0)</code></p>
<p>The significance of the result does not rely on a distinction between returned values that can be automatically converted from one to the other.&nbsp; PHP, on the other hand, requires the programmer to explicitly prevent that conversion from happening.&nbsp; Automatic conversions are supposed to be a convenience for the programmer, not a trap.</p>
<p>Maybe PHP stands for Purposely Hampers Programming.</p>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/tYa_aIKny9Q" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=168</wfw:commentRss>
		<slash:comments>52</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=168</feedburner:origLink></item>
		<item>
		<title>HACC string compression algorithm (C++)</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/xmByERvwQPw/</link>
		<comments>http://www.chipstips.com/?p=144#comments</comments>
		<pubDate>Wed, 25 Jan 2006 02:08:29 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[C and C++]]></category>
		<category><![CDATA[Wildly popular]]></category>
		<category><![CDATA[ascii]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[cpphacc]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/?p=144</guid>
		<description><![CDATA[The downloadable code below implements a form of text compression that I like to call HACC, for Half ASCII-Compatible Compression (say it fast). Only the low-order 8 bits of a character are used for compression op-codes and length, so it is only half as good as it could be in UNICODE environments. The reason for [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=144"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=144" data-text="HACC string compression algorithm (C++)" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D144&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=144";
			reddit_title = "HACC string compression algorithm (C++)";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=144";</script>
		<script type="text/javascript">var dzone_title = "HACC string compression algorithm (C++)";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>The downloadable code below implements a form of text compression that I like to call HACC, for Half ASCII-Compatible Compression (say it fast).  Only the low-order 8 bits of a character are used for compression op-codes and length, so it is only half as good as it could be in UNICODE environments.  The reason for this half-asciid approach is for compatibility with 8-bit character environments (like ASCII).  The compressed data can represent embedded nulls, but it never contains a null character.  This also helps when transferring data between environments, but reduces the compression efficiency ever so slightly to allow for the representation of &#8220;duplicate 1 null&#8221;.  Thus, lengths must be stored in a representation that allows for a minimum value of 1, whereas otherwise we could have imposed a minumum compression length of, say, 3, and thus have raised the ceiling on our length by 2 as well.</p>
<p>The compressed data takes the form of a series of (op-code character possibly followed by data). The op-code itself is a number between 1-7, which is stored in the low-order 3 bits (0&#215;07) of the character.  Most of the op-codes require a length.  If the length is in the range 1-16, then it is stored as 0-15 (reduced by 1) in bits 4-7 (0xF0) of the op-code byte.  If the length is in the range 17-4096, then it is reduced by 1 to a 12-bit number (16-4095) and the low-order 4 bits of that number are stored in bits 4-7.  Bit 3 (0&#215;08) is set on in that case, and the remaining 8 bits of the length (0x0FF0) are shifted four bits (to 0xFF) and stored in the next character.  Any remaining data required for the operation is stored in the characters that follow.  The op-codes include &#8220;duplicate a character&#8221; (with specific op-codes for the more common spaces, zeroes, and nulls) and &#8220;copy a string&#8221;, each taking a length.  Special op-codes mark the beginning and ending of compressed data, so that we can be sure that this is our data when decompressing.</p>
<p>For a C# implementation of this algorithm, which is fully compatible with the 8-bit version (provided a conversion between UNICODE and ASCII), <a href="http://www.chipstips.com/showtopic.asp?topic=cshacc">see this topic</a>. For an implementation in Ruby, <a href="http://www.chipstips.com/showtopic.asp?topic=rbcmpr">see this topic</a>.</p>
<div><a href="http://www.chipstips.com/download/cpphacc.zip"><img src="http://www.chipstips.com/images/download.gif" /></a></div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/xmByERvwQPw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=144</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=144</feedburner:origLink></item>
		<item>
		<title>UNIX-style tee utility for Windows</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/V2c1vYpSs9I/</link>
		<comments>http://www.chipstips.com/?p=129#comments</comments>
		<pubDate>Thu, 10 Nov 2005 01:10:20 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C and C++]]></category>
		<category><![CDATA[Wildly popular]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[cpptee]]></category>
		<category><![CDATA[mks]]></category>
		<category><![CDATA[streamwriter]]></category>
		<category><![CDATA[tee]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/new/?p=129</guid>
		<description><![CDATA[The downloadable code below contains the source and executable for a simple &#8216;tee&#8217; utility for Windows. The tee utility faced its first fairway on UNIX platforms, and has been ported to Windows by other providers of UNIX-style utilities. While my version may not be technically on a par with the MKS Toolkit version, mine definitely [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=129"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=129" data-text="UNIX-style tee utility for Windows" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D129&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=129";
			reddit_title = "UNIX-style tee utility for Windows";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=129";</script>
		<script type="text/javascript">var dzone_title = "UNIX-style tee utility for Windows";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>The downloadable code below contains the source and executable for a simple &#8216;tee&#8217; utility for Windows.  The tee utility faced its first fairway on UNIX platforms, and has been ported to Windows by other providers of UNIX-style utilities. While my version may not be technically on a par with the <a href="http://www.mkssoftware.com/">MKS Toolkit</a> version, mine definitely tops the leader board for price/performance ratio.</p>
<p>Now let&#8217;s sink the golfing puns, because the name for the tee utility actually derives from plumbing. Input and output can be &#8216;piped&#8217; from one process to another on UNIX and on Windows, and the tee utility creates a tee in the pipe.  Thus, in addition to sending output to standard out, you can also redirect it to zero or more files &#8212; very useful for keeping a log of a process while simultaneously being able to view its progress and/or send its output on to somewhere else. For example:</p>
<p>my100steps.bat | tee my100steps.log</p>
<p>can execute all 100 steps and send the output to the console while at the same time writing the output to my100steps.log.</p>
<p>The version you can download below uses the Microsoft .NET Framework (built using Visual Studio .NET 2008), and provides an example of using managed code in C++.  It also demonstrates a few of the capabilites of the System::IO::StreamWriter and System::Console classes.</p>
<p>Jeroen Pluimers <a href="http://wiert.wordpress.com/2010/04/07/netc-tee-filter-that-also-runs-on-windows-xp-embedded/">converted this to C#</a> so it could run on Windows XP Embedded.</p>
<p>For a version in Ruby, <a href="http://www.chipstips.com/?p=188">go here</a>.  How about a <a href="http://www.chipstips.com/?p=606">Haskell version</a>?</p>
<p>UPDATE 2009-07-27: Added -a (or &#8211;append) option as the first argument only to append files rather than overwriting them.  Also changed to a Unicode build under VS 2008.</p>
<p>UPDATE 2009-08-18: Upgraded syntax to eliminate /clr:oldsyntax and added an event handler for CancelKeyPress to ignore Ctrl+C, so the originating program in the pipe can handle it instead.</p>
<p>UPDATE 2010-01-25: Changed to use BinaryReader and BinaryWriter to eliminate CR/LF problems that resulted in double-spacing some forms of output.  Thanks to Anonymous Coward for the suggestion below.</p>
<p>UPDATE 2010-01-28: Included more suggestions from Anonymous Coward.  Use <code>tee --help</code> to see available options.</p>
<div><a href="http://www.chipstips.com/download/cpptee.zip"><img src="http://www.chipstips.com/images/download.gif" alt="" /></a></div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/V2c1vYpSs9I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=129</wfw:commentRss>
		<slash:comments>41</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=129</feedburner:origLink></item>
		<item>
		<title>ActiveX monitor for window messages</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/rKwOc98UQUc/</link>
		<comments>http://www.chipstips.com/?p=125#comments</comments>
		<pubDate>Thu, 01 Nov 2001 20:05:54 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[ActiveX]]></category>
		<category><![CDATA[C and C++]]></category>
		<category><![CDATA[MFC]]></category>
		<category><![CDATA[Wildly popular]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[activation]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[cppactx]]></category>
		<category><![CDATA[handle]]></category>
		<category><![CDATA[mfc]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/new/?p=125</guid>
		<description><![CDATA[Subclassing windows using the Windows Platform SDK directly can be a tricky business. You have to be sure that you follow the rules, and hope and pray that anybody else who is subclassing the window will do likewise. Furthermore, it can be difficult to tie your subclassing code into the rest of your application, because [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=125"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=125" data-text="ActiveX monitor for window messages" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D125&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=125";
			reddit_title = "ActiveX monitor for window messages";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=125";</script>
		<script type="text/javascript">var dzone_title = "ActiveX monitor for window messages";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>Subclassing windows using the Windows Platform SDK directly can be a tricky business. You have to be sure that you follow the rules, and hope and pray that anybody else who is subclassing the window will do likewise.  Furthermore, it can be difficult to tie your subclassing code into the rest of your application, because window messages are processed at such a low level.  Some languages are not even capable of operating at that level. Even if you are using a low-level language like C, it becomes very difficult to interface events from a subclass procedure without a fairly intimate knowledge of the behavior of the window in question.</p>
<p>ActiveX provides a more uniform and compatible event signaling mechanism.  The downloadable code below provides an example (using MFC) of how to subclass a window to fire ActiveX events via a monitor control.  The control has a &#8220;WindowHandle&#8221; property that contains the handle of the window to monitor, which must be in the current process.  Setting this property to a non-zero value will initiate monitoring on that window.  The first control attached to a window will create a CActivationXSubclassHandler object, which in turn subclasses the window to forward window messages back to itself, via an object pointer stored in the window as a window property.  The CActivationXSubclassHandler object also contains a list of the monitor controls attached to the window, so that messages can be forwarded to each one.</p>
<p>The example monitor control class, CActivationXCtrl, only acts on the WM_ACTIVATE message.  It fires two events: OnActivate when the window is activated, and OnDeactivate when the window is deactivated.  Each event is passed a single parameter, which is the content of the UserData property for the control.  Naturally, you can extend the control to monitor and act on whatever messages you need.</p>
<p>In order to facilitate testing and usage, I added two methods to this control.  The Activate method will activate a window (the monitored window if no handle is passed). The FindWindow method wraps the Windows Platform SDK function FindWindowEx, and adds the ability to specify process and thread.</p>
<p>Purely for fun, I made the OnDraw method for the control display the incoming window messages and parameters, along with simulated LED&#8217;s to represent their binary values.</p>
<p>For a simple example of an application that uses this control, see <a href="http://www.chipstips.com/showtopic.asp?topic=synshell"> Launch applications by associated file extension</a>, where the launching application waits for itself to become reactivated before trying to establish focus.</p>
<div><a href="http://www.chipstips.com/download/cppactx.zip"><img src="http://www.chipstips.com/images/download.gif" alt="" /></a></div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/rKwOc98UQUc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=125</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=125</feedburner:origLink></item>
		<item>
		<title>ActiveX control wrapper for DDE</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/cj6xQVK9CDo/</link>
		<comments>http://www.chipstips.com/?p=126#comments</comments>
		<pubDate>Tue, 03 Jul 2001 18:47:48 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[ActiveX]]></category>
		<category><![CDATA[C and C++]]></category>
		<category><![CDATA[MFC]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[cppddex]]></category>
		<category><![CDATA[dde]]></category>
		<category><![CDATA[ddeml]]></category>
		<category><![CDATA[handle]]></category>
		<category><![CDATA[mfc]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/new/?p=126</guid>
		<description><![CDATA[Anyone who has ever programmed Dynamic Data Exchange (DDE) applications has experienced pain. Originally, it was done only with Windows messages and global atoms. Since then, Microsoft has added the Dynamic Data Exchange Management Library (DDEML) which makes the job a little easier &#8212; using function calls instead of messages, and data handles instead of [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=126"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=126" data-text="ActiveX control wrapper for DDE" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D126&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=126";
			reddit_title = "ActiveX control wrapper for DDE";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=126";</script>
		<script type="text/javascript">var dzone_title = "ActiveX control wrapper for DDE";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>Anyone who has ever programmed Dynamic Data Exchange (DDE) applications has experienced pain. Originally, it was done only with Windows messages and global atoms.  Since then, Microsoft has added the Dynamic Data Exchange Management Library (DDEML) which makes the job a little easier &#8212; using function calls instead of messages, and data handles instead of global atoms.  But it&#8217;s still more work than it ought to be, what with transferring data to and from handles &#8212; and you still have to provide a callback to process the transactions, much like the message model.</p>
<p>The downloadable code below provides the sources and executable for an ActiveX control that wraps DDEML.  ActiveX provides a much cleaner interface in terms of calls in and calls back, and especially for marshalling data.  Best of all, any language that supports an ActiveX interface  (most these days) can use it.  This version only supports poke, execute, and request operations, with data in text format up to 255 characters (the limit of a DDE string handle).  However, it could easily be extended to support other formats and operations.  Rather than using a single callback for transactions, each transaction type signals an ActiveX event.</p>
<p>The single control can act as both a DDE client and a DDE server.  It includes methods for adding and removing supported services and events for servicing transactions as a server.  It also includes methods for connecting and disconnecting a conversation, as well as poking, executing, and requesting data within the conversation.  No limits are placed on the number of services the control can provide or connect to. Many design decisions were made to keep this example simple.  For instance, asynchronous operations are not provided for, nor is there any option to filter transactions.  These and others are feature that could be added in the future without affecting the existing interface.</p>
<p>One handy little by-product of this project was the CDDEString class.  CDDEString encapsulates a CString (the MFC string wrapper class) and provides operator overloads to convert to and from DDE string handle, VARIANT, and of course LPCTSTR (const char *, which also handles CString).</p>
<p>UPDATE 2009-08-22:  For a .NET solution, I recommend <a href="http://www.codeplex.com/ndde">NDde</a>.</p>
<div><a href="http://www.chipstips.com/download/cppddex.zip"><img src="http://www.chipstips.com/images/download.gif" alt="" /></a></div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/cj6xQVK9CDo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=126</wfw:commentRss>
		<slash:comments>60</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=126</feedburner:origLink></item>
		<item>
		<title>Escaping special characters in generated XML data</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/dDp8KM_HIXk/</link>
		<comments>http://www.chipstips.com/?p=137#comments</comments>
		<pubDate>Thu, 21 Jun 2001 22:22:52 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[C and C++]]></category>
		<category><![CDATA[OpenVMS]]></category>
		<category><![CDATA[SynergyDE]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[Wildly popular]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[escaping]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[xmlxltdata]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/new/?p=137</guid>
		<description><![CDATA[When generating XML data, you must be careful not to include special characters that will drive the parsers crazy. These are, as defined in section 2.4 (&#8220;Character Data and Markup&#8221;) of Extensible Markup Language (XML) 1.0 (Second Edition), &#8220;&#038;&#8221;, &#8220;&#8220;, which must be replaced by the escape sequences &#8220;&#38;&#8221;, &#8220;&#60;&#8221;, and &#8220;&#62;&#8221;, respectively. The downloadable [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=137"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=137" data-text="Escaping special characters in generated XML data" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D137&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=137";
			reddit_title = "Escaping special characters in generated XML data";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=137";</script>
		<script type="text/javascript">var dzone_title = "Escaping special characters in generated XML data";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>When generating XML data, you must be careful not to include special characters that will drive the parsers crazy.  These are, as defined in section 2.4 (&#8220;Character Data and Markup&#8221;) of <a href="http://www.w3.org/TR/REC-xml">Extensible Markup Language (XML) 1.0 (Second Edition)</a>, &#8220;&#038;&#8221;, &#8220;<", and ">&#8220;, which must be replaced by the escape sequences &#8220;&amp;&#8221;, &#8220;&lt;&#8221;, and &#8220;&gt;&#8221;, respectively.</p>
<p>The downloadable code below includes separate examples in Synergy/DE and C++ of how this conversion can be performed.  The Synergy/DE version is implemented as a function that converts the string in place.  The C++ version implements a class, an instance of which can be assigned a string value that is immediately converted on assignment. An extractor allows the converted string to be retrieved.</p>
<div><a href="http://www.chipstips.com/download/xmlxltdata.zip"><img src="http://www.chipstips.com/images/download.gif" /></a></div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/dDp8KM_HIXk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=137</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=137</feedburner:origLink></item>
		<item>
		<title>Randomly select desktop wallpaper</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/vYjilt1yW88/</link>
		<comments>http://www.chipstips.com/?p=128#comments</comments>
		<pubDate>Sun, 17 Jun 2001 01:09:04 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[C and C++]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[camdensoftware]]></category>
		<category><![CDATA[cpprandpaper]]></category>
		<category><![CDATA[utility]]></category>
		<category><![CDATA[wallpaper]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/new/?p=128</guid>
		<description><![CDATA[The downloadable code below provides the sources to a simple program that sets the Windows desktop wallpaper to a randomly selected bitmap file in the Windows directory. If the image is less than or equal to half the height or width of the screen, then it is tiled. Otherwise, if the ratio of the ratios [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=128"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=128" data-text="Randomly select desktop wallpaper" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D128&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=128";
			reddit_title = "Randomly select desktop wallpaper";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=128";</script>
		<script type="text/javascript">var dzone_title = "Randomly select desktop wallpaper";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>The downloadable code below provides the sources to a simple program that sets the Windows desktop wallpaper to a randomly selected bitmap file in the Windows directory.  If the image is less than or equal to half the height or width of the screen, then it is tiled.  Otherwise, if the ratio of the ratios of height to width for the image and the screen are within 0.20, the image is stretched.  Otherwise, it is centered.  I have this little program execute every 15 minutes in my Scheduled Tasks, just to make life more interesting.</p>
<p>To download just the executable version of this program, visit <a href="http://www.camdensoftware.com/Downloads.htm"> http://www.camdensoftware.com/Downloads.htm</a></p>
<div><a href="http://www.chipstips.com/download/cpprandpaper.zip"><img src="http://www.chipstips.com/images/download.gif" /></a></div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/vYjilt1yW88" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=128</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=128</feedburner:origLink></item>
		<item>
		<title>Heal the desktop!</title>
		<link>http://feedproxy.google.com/~r/ChipsTipsC/~3/5YmulWIb78o/</link>
		<comments>http://www.chipstips.com/?p=127#comments</comments>
		<pubDate>Sat, 16 Jun 2001 20:15:52 +0000</pubDate>
		<dc:creator>Sterling Camden</dc:creator>
				<category><![CDATA[C and C++]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[camdensoftware]]></category>
		<category><![CDATA[cppheal]]></category>
		<category><![CDATA[desktop]]></category>
		<category><![CDATA[heal]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://www.chipstips.com/new/?p=127</guid>
		<description><![CDATA[The downloadable code below provides the sources to a simple utility called heal.exe. Developers who spend a lot of their time creating graphical applications for Windows often get the desktop into a state where windows are half painted or pieces of now-extinct windows are still lying around. This sometimes happens when applications exit while they [...]]]></description>
				<content:encoded><![CDATA[<div class="socialize-in-content socialize-in-content-right"><div class="socialize-in-button socialize-in-button-right"><g:plusone size="tall" href="http://www.chipstips.com/?p=127"></g:plusone></div><div class="socialize-in-button socialize-in-button-right"><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.chipstips.com/?p=127" data-text="Heal the desktop!" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-right"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.chipstips.com%2F%3Fp%3D127&amp;send=&amp;layout=box_count&amp;width=50&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:50px; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.chipstips.com/?p=127";
			reddit_title = "Heal the desktop!";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-right"><script type="text/javascript">var dzone_url = "http://www.chipstips.com/?p=127";</script>
		<script type="text/javascript">var dzone_title = "Heal the desktop!";</script>
		<script type="text/javascript">
			<!-- 
			var dzone_style = "1";
			//-->
		</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div></div><p>The downloadable code below provides the sources to a simple utility called heal.exe. Developers who spend a lot of their time creating graphical applications for Windows often get the desktop into a state where windows are half painted or pieces of now-extinct windows are still lying around.  This sometimes happens when applications exit while they have redraw turned off.  Running the heal.exe utility calls the Windows API function InvalidateRect with the window handle as NULL, which causes all windows on the desktop and the desktop itself to repaint.  You can therefore also use the utility to see if a display anomaly in an  application is due to a failure to invalidate a region.</p>
<p>If you just want to get the executable for this utility and don&#8217;t care about seeing the admittedly simple but brilliant source code, visit <a href="http://www.camdensoftware.com/Downloads.htm"> http://www.camdensoftware.com/Downloads.htm</a></p>
<div><a href="http://www.chipstips.com/download/cppheal.zip"><img src="http://www.chipstips.com/images/download.gif" /></a></div>
<img src="http://feeds.feedburner.com/~r/ChipsTipsC/~4/5YmulWIb78o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.chipstips.com/?feed=rss2&amp;p=127</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.chipstips.com/?p=127</feedburner:origLink></item>
	</channel>
</rss>
