<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: pwdn: show last n dirs of current directory</title>
	<atom:link href="http://muffinresearch.co.uk/archives/2008/04/02/pwdn-show-last-n-dirs-of-current-directory/feed/" rel="self" type="application/rss+xml" />
	<link>http://muffinresearch.co.uk/archives/2008/04/02/pwdn-show-last-n-dirs-of-current-directory/</link>
	<description>the personal blog of Stuart Colville covering modern web development techniques and best practices</description>
	<pubDate>Fri, 29 Aug 2008 03:29:30 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Stuart Colville</title>
		<link>http://muffinresearch.co.uk/archives/2008/04/02/pwdn-show-last-n-dirs-of-current-directory/#comment-64207</link>
		<dc:creator>Stuart Colville</dc:creator>
		<pubDate>Tue, 08 Apr 2008 01:56:58 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2008/04/02/pwdn-show-last-n-dirs-of-current-directory/#comment-64207</guid>
		<description>@James: Thanks for the corrections, and I've updated Caius' code accordingly.</description>
		<content:encoded><![CDATA[<p>@James: Thanks for the corrections, and I&#8217;ve updated Caius&#8217; code accordingly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James Aylett</title>
		<link>http://muffinresearch.co.uk/archives/2008/04/02/pwdn-show-last-n-dirs-of-current-directory/#comment-64205</link>
		<dc:creator>James Aylett</dc:creator>
		<pubDate>Mon, 07 Apr 2008 15:15:53 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2008/04/02/pwdn-show-last-n-dirs-of-current-directory/#comment-64205</guid>
		<description>1. strtoi vs strtol. strtoi isn't part of C99, and doesn't exist on my linux box, Mac OS 10.4, or Solaris 10. (I haven't seen strtoi anywhere, come to think of it.)

You probably actually want strtoul. There's nothing you're doing that will be problematic with an unsigned long, after all.

2. free(NULL) isn't a great idea ;-) (you do it if malloc fails)

3. pre- versus post- increment. Umm. In most cases, the compiler will happily optimise the difference away. In some cases it might not be able to, although with out of order compilation (ie: independent actions happen out of sequence) it can still get it right in a lot of cases. You can probably trip it up with something horrible, and you can always force the issue using &lt;code&gt;volatile&lt;/code&gt;, providing you don't mind people looking at you strangely.

In C++ it makes more of a difference, because your type might be a class not a primitive, and most people implement pre-increment in terms of post-increment, so you're paying an extra method call.

It's a stylistic thing that under certain circumstances will make things faster, and shouldn't ever make them slower. I'll freely admit that I don't always bother ;-)

4. that ruby doesn't work for me - you want &lt;code&gt;ARGV[0].to_i&lt;/code&gt; on line 3.</description>
		<content:encoded><![CDATA[<p>1. strtoi vs strtol. strtoi isn&#8217;t part of C99, and doesn&#8217;t exist on my linux box, Mac OS 10.4, or Solaris 10. (I haven&#8217;t seen strtoi anywhere, come to think of it.)</p>
<p>You probably actually want strtoul. There&#8217;s nothing you&#8217;re doing that will be problematic with an unsigned long, after all.</p>
<p>2. free(NULL) isn&#8217;t a great idea <img src='http://muffinresearch.co.uk/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> (you do it if malloc fails)</p>
<p>3. pre- versus post- increment. Umm. In most cases, the compiler will happily optimise the difference away. In some cases it might not be able to, although with out of order compilation (ie: independent actions happen out of sequence) it can still get it right in a lot of cases. You can probably trip it up with something horrible, and you can always force the issue using <code>volatile</code>, providing you don&#8217;t mind people looking at you strangely.</p>
<p>In C++ it makes more of a difference, because your type might be a class not a primitive, and most people implement pre-increment in terms of post-increment, so you&#8217;re paying an extra method call.</p>
<p>It&#8217;s a stylistic thing that under certain circumstances will make things faster, and shouldn&#8217;t ever make them slower. I&#8217;ll freely admit that I don&#8217;t always bother <img src='http://muffinresearch.co.uk/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>4. that ruby doesn&#8217;t work for me - you want <code>ARGV[0].to_i</code> on line 3.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Caius Durling</title>
		<link>http://muffinresearch.co.uk/archives/2008/04/02/pwdn-show-last-n-dirs-of-current-directory/#comment-64204</link>
		<dc:creator>Caius Durling</dc:creator>
		<pubDate>Mon, 07 Apr 2008 11:06:16 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2008/04/02/pwdn-show-last-n-dirs-of-current-directory/#comment-64204</guid>
		<description>A ruby version.

&lt;pre&gt;&lt;code&gt;#!/usr/bin/env ruby

# Prints out the last n dirs of current directory

# The number of dirs to show in path
dirnum = (ARGV[0] &#124;&#124; 2).to_i
# Grab current directory path and convert to array
pwd = Dir.pwd.split(&#34;/&#34;)
# Chop off the first element if its an empty string
pwd = pwd.slice( 1, pwd.length ).compact if pwd.length &#62; 1 &#38;&#38; pwd[0].empty?

if pwd.length &#62; dirnum
 # The path is truncated
 puts &#34;.../&#34; + pwd.slice( -dirnum, dirnum).join(&#34;/&#34;)
else
 # The path is absolute
 puts &#34;/&#34; + pwd.join(&#34;/&#34;)
end &lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>A ruby version.</p>
<pre><code>#!/usr/bin/env ruby

# Prints out the last n dirs of current directory

# The number of dirs to show in path
dirnum = (ARGV[0] || 2).to_i
# Grab current directory path and convert to array
pwd = Dir.pwd.split(&quot;/&quot;)
# Chop off the first element if its an empty string
pwd = pwd.slice( 1, pwd.length ).compact if pwd.length &gt; 1 &amp;&amp; pwd[0].empty?

if pwd.length &gt; dirnum
 # The path is truncated
 puts &quot;&#8230;/&quot; + pwd.slice( -dirnum, dirnum).join(&quot;/&quot;)
else
 # The path is absolute
 puts &quot;/&quot; + pwd.join(&quot;/&quot;)
end </code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stuart Colville</title>
		<link>http://muffinresearch.co.uk/archives/2008/04/02/pwdn-show-last-n-dirs-of-current-directory/#comment-64203</link>
		<dc:creator>Stuart Colville</dc:creator>
		<pubDate>Mon, 07 Apr 2008 08:28:07 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2008/04/02/pwdn-show-last-n-dirs-of-current-directory/#comment-64203</guid>
		<description>@James: Thanks for the pointers!! (No pun intended) I've added some basic error handling, fixed the missing brackets, return type of main. When you say strtol don't you mean strtoi I don't want to return a long? 

The printf was PHP eating slashes - fixes that now so it displays correctly.

Re: pre-increment isn't the small performance increase removed by the compiler?

Fixed the bug in the python version - that'll teach me for throwing the comparison together :-)</description>
		<content:encoded><![CDATA[<p>@James: Thanks for the pointers!! (No pun intended) I&#8217;ve added some basic error handling, fixed the missing brackets, return type of main. When you say strtol don&#8217;t you mean strtoi I don&#8217;t want to return a long? </p>
<p>The printf was PHP eating slashes - fixes that now so it displays correctly.</p>
<p>Re: pre-increment isn&#8217;t the small performance increase removed by the compiler?</p>
<p>Fixed the bug in the python version - that&#8217;ll teach me for throwing the comparison together <img src='http://muffinresearch.co.uk/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James Aylett</title>
		<link>http://muffinresearch.co.uk/archives/2008/04/02/pwdn-show-last-n-dirs-of-current-directory/#comment-64202</link>
		<dc:creator>James Aylett</dc:creator>
		<pubDate>Sun, 06 Apr 2008 22:44:31 +0000</pubDate>
		<guid isPermaLink="false">http://muffinresearch.co.uk/archives/2008/04/02/pwdn-show-last-n-dirs-of-current-directory/#comment-64202</guid>
		<description>Cool :-) -- welcome to C.

Some stylistic things: you should use strtol() in preference to atoi(); also ++n is generally better practice than n++ (it makes no difference in a lot of cases, and certainly shouldn't in this, but it's good practice unless you actually need post-increment). You have char* slash, delim; when you only need delim (I think). Finally, you have one if(...) without using braces, which I'd advise against because of the potential for a dangling else in future.

One buggette in cut and paste: printf("n"); probably wants to be printf("\n"); two lines from the end of main().

You aren't handling errors from things like malloc, pathconf (unlikely), getcwd (change permissions underneath the calling shell)  -- this will result in abends (I was expecting a segfault from getcwd failure, but I actually got a bus error; whichever, it isn't good).

Your python script has a bug in it as well: invoke without parameters and you get an IndexError. This isn't the case in the C version, because argv will always be terminated by a NULL pointer at the end of the list (at argv[argc] -- at least in C99, see 5.1.2.2.1 2 of WG14/N1124 for instance). In theory argc can be 0, in which case argv[1] isn't valid; however that's not going to happen on any Unix I'm aware of. (While talking about this, main should really have a return type of int.)

All minor things, though. I love the fact that the C code will be more readable to people with no knowledge of the language than the PS1 value that uses it :-)</description>
		<content:encoded><![CDATA[<p>Cool <img src='http://muffinresearch.co.uk/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> &#8212; welcome to C.</p>
<p>Some stylistic things: you should use strtol() in preference to atoi(); also ++n is generally better practice than n++ (it makes no difference in a lot of cases, and certainly shouldn&#8217;t in this, but it&#8217;s good practice unless you actually need post-increment). You have char* slash, delim; when you only need delim (I think). Finally, you have one if(&#8230;) without using braces, which I&#8217;d advise against because of the potential for a dangling else in future.</p>
<p>One buggette in cut and paste: printf(&#8221;n&#8221;); probably wants to be printf(&#8221;\n&#8221;); two lines from the end of main().</p>
<p>You aren&#8217;t handling errors from things like malloc, pathconf (unlikely), getcwd (change permissions underneath the calling shell)  &#8212; this will result in abends (I was expecting a segfault from getcwd failure, but I actually got a bus error; whichever, it isn&#8217;t good).</p>
<p>Your python script has a bug in it as well: invoke without parameters and you get an IndexError. This isn&#8217;t the case in the C version, because argv will always be terminated by a NULL pointer at the end of the list (at argv[argc] &#8212; at least in C99, see 5.1.2.2.1 2 of WG14/N1124 for instance). In theory argc can be 0, in which case argv[1] isn&#8217;t valid; however that&#8217;s not going to happen on any Unix I&#8217;m aware of. (While talking about this, main should really have a return type of int.)</p>
<p>All minor things, though. I love the fact that the C code will be more readable to people with no knowledge of the language than the PS1 value that uses it <img src='http://muffinresearch.co.uk/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
</channel>
</rss>
