<?xml version="1.0" encoding="utf-8"?>
<!-- generator="FeedCreator 1.7.2" -->
<rss version="2.0">
    <channel>
        <title>Lowyat.NET: Latest topics by elilaw</title>
        <description></description>
        <link>http://forum.lowyat.net/</link>
        <lastBuildDate>Sun, 01 Nov 2026 10:09:55 +0800</lastBuildDate>
        <generator>FeedCreator 1.7.2</generator>
        <item>
            <title>Unix programming in C</title>
            <link>http://forum.lowyat.net/topic/346718</link>
            <description>Hi, this is my 1st time posting in Lowyat. I have quite alot of difficulty in trying to get this code to produce the list of directories &amp;amp; files out on the Unix terminal.&lt;br /&gt;&lt;br /&gt;Ok, this is actually Unix Programming with C.... The code should be emulating what the &amp;quot;find&amp;quot; command does. &lt;br /&gt;&lt;br /&gt;eg ... &lt;br /&gt;find -print      {in current directory}&lt;br /&gt;./hello.c&lt;br /&gt;./world.c&lt;br /&gt;./hello.exe&lt;br /&gt;&lt;br /&gt;Now i&amp;#39;m having difficulty trying to get my code doing that... except my code should list the current directory&amp;#39;s files &amp;amp; directories (including . and ..) in the terminal: &lt;br /&gt;&amp;#036;hello.c&lt;br /&gt;&amp;#036;world.c&lt;br /&gt;&amp;#036;hello.exe&lt;br /&gt;&lt;br /&gt;Pls note: I should also use the &amp;quot;getopt&amp;quot; command call in the code, but I don&amp;#39;t know how to.&lt;br /&gt;&lt;br /&gt;okie, this is my code at the moment: &lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;#include &amp;#60;stdio.h&amp;#62; &amp;nbsp; &amp;nbsp; /* for printf */&lt;br /&gt;#include &amp;#60;stdlib.h&amp;#62; &amp;nbsp; &amp;nbsp;/* for exit */&lt;br /&gt;#include &amp;#60;getopt.h&amp;#62;&lt;br /&gt;#include &amp;#60;dirent.h&amp;#62; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;#include &amp;#60;sys/types.h&amp;#62;&lt;br /&gt;&lt;br /&gt;int&lt;br /&gt;main &amp;#40;int argc, char **argv&amp;#41; {&lt;br /&gt; &amp;nbsp;int c;&lt;br /&gt; &amp;nbsp;int digit_optind = 0;&lt;br /&gt;&lt;br /&gt; &amp;nbsp;while &amp;#40;1&amp;#41; {&lt;br /&gt; &amp;nbsp; &amp;nbsp;int this_option_optind = optind ? optind &amp;#58; 1;&lt;br /&gt; &amp;nbsp; &amp;nbsp;int option_index = 0;&lt;br /&gt; &amp;nbsp; &amp;nbsp;/* You&amp;#39;ll need to read the manual to figure out what all the &lt;br /&gt; &amp;nbsp; &amp;nbsp; * arguments are&amp;#33;&lt;br /&gt; &amp;nbsp; &amp;nbsp; */&lt;br /&gt; &amp;nbsp; &amp;nbsp;static struct option long_options&amp;#91;&amp;#93; = {&lt;br /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&amp;#34;cnewer&amp;#34;, 1, 0, 0},&lt;br /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&amp;#34;cmin&amp;#34;, 1, 0, 0},&lt;br /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&amp;#34;ctime&amp;#34;, 1, 0, 0},&lt;br /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&amp;#34;help&amp;#34;, 0, 0, 0},&lt;br /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;{0, 0, 0, 0}&lt;br /&gt; &amp;nbsp; &amp;nbsp;};&lt;br /&gt; &amp;nbsp; &amp;nbsp;&lt;br /&gt;#define OPT_NEWER &amp;nbsp; &amp;nbsp;0&lt;br /&gt;#define OPT_CMIN &amp;nbsp; &amp;nbsp; 1&lt;br /&gt;#define OPT_CTIME &amp;nbsp; &amp;nbsp;2&lt;br /&gt;#define OPT_HELP &amp;nbsp; &amp;nbsp; 3&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;c = getopt_long_only &amp;#40;argc, argv, &amp;#34;&amp;#34;, long_options, &amp;amp;option_index&amp;#41;;&lt;br /&gt; &amp;nbsp; &amp;nbsp;&lt;br /&gt; &amp;nbsp; &amp;nbsp;if &amp;#40;c == -1&amp;#41;&lt;br /&gt; &amp;nbsp;printf&amp;#40;&amp;#34;c = -1 &amp;amp; that&amp;#39;s why we&amp;#39;re killing it&amp;#092;n&amp;#34;&amp;#41;;&lt;br /&gt; &amp;nbsp;//do_ls&amp;#40;&amp;#41;;&lt;br /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;break;&lt;br /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt; &amp;nbsp; &amp;nbsp;switch &amp;#40;c&amp;#41; {&lt;br /&gt; &amp;nbsp; &amp;nbsp;case 0&amp;#58;&lt;br /&gt; &amp;nbsp;printf &amp;#40;&amp;#34;option %s&amp;#34;, long_options&amp;#91;option_index&amp;#93;.name&amp;#41;;&lt;br /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;if &amp;#40;optarg&amp;#41;&lt;br /&gt; &amp;nbsp;printf &amp;#40;&amp;#34; with arg %s&amp;#34;, optarg&amp;#41;;&lt;br /&gt; &amp;nbsp;printf &amp;#40;&amp;#34;&amp;#092;n&amp;#34;&amp;#41;;&lt;br /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;if &amp;#40;option_index == OPT_HELP&amp;#41; usage&amp;#40;&amp;#41;;&lt;br /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;break;&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;case &amp;#39;?&amp;#39;&amp;#58;&lt;br /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;break;&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;default&amp;#58;&lt;br /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;printf &amp;#40;&amp;#34;?? getopt returned character code 0%o ??&amp;#092;n&amp;#34;, c&amp;#41;;&lt;br /&gt; &amp;nbsp; &amp;nbsp;}&lt;br /&gt; &amp;nbsp;}&lt;br /&gt;&lt;br /&gt; &amp;nbsp;if &amp;#40;optind &amp;#60; argc&amp;#41; {&lt;br /&gt; &amp;nbsp; &amp;nbsp;printf &amp;#40;&amp;#34;non-option ARGV-elements&amp;#58; &amp;#34;&amp;#41;;&lt;br /&gt; &amp;nbsp; &amp;nbsp;while &amp;#40;optind &amp;#60; argc&amp;#41;&lt;br /&gt; &amp;nbsp;printf &amp;#40;&amp;#34;%s &amp;#34;, argv&amp;#91;optind++&amp;#93;&amp;#41;;&lt;br /&gt; &amp;nbsp;printf &amp;#40;&amp;#34;&amp;#092;n&amp;#34;&amp;#41;;&lt;br /&gt; &amp;nbsp;}&lt;br /&gt;&lt;br /&gt; &amp;nbsp;exit &amp;#40;0&amp;#41;;&lt;br /&gt;}&lt;br /&gt;usage&amp;#40;&amp;#41; {&lt;br /&gt; &amp;nbsp;printf&amp;#40;&amp;#34;Usage&amp;#58; myfind &amp;#91;pathname&amp;#93; &amp;#91;args&amp;#93;&amp;#092;n&amp;#34;&amp;#41;;&lt;br /&gt; &amp;nbsp;printf&amp;#40;&amp;#34; pathname should probably be a directory&amp;#092;n&amp;#34;&amp;#41;;&lt;br /&gt; &amp;nbsp;printf&amp;#40;&amp;#34; args are any of the following &amp;#40;anded together&amp;#41;&amp;#58;&amp;#092;n&amp;#34;&amp;#41;;&lt;br /&gt; &amp;nbsp;printf&amp;#40;&amp;#34; -print, -cmin, -ctime, -cnewer, -gid, -group &amp;#092;n&amp;#34;&amp;#41;;&lt;br /&gt; &amp;nbsp;printf&amp;#40;&amp;#34; -uid, -user, -empty, -links, -name&amp;#092;n&amp;#34;&amp;#41;;&lt;br /&gt; &amp;nbsp;printf&amp;#40;&amp;#34;For details see find&amp;#40;1&amp;#41;&amp;#092;n&amp;#34;&amp;#41;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void do_ls &amp;#40;char dirname&amp;#91;&amp;#93;&amp;#41; {&lt;br /&gt;&lt;br /&gt;	DIR &amp;nbsp; &amp;nbsp;*dir_ptr;&lt;br /&gt;	struct dirent	*direntp;&lt;br /&gt;	&lt;br /&gt;	if &amp;#40; &amp;#40;dir_ptr = opendir&amp;#40;dirname&amp;#41;&amp;#41; == NULL&amp;#41;&lt;br /&gt; &amp;nbsp;fprintf&amp;#40;stderr,&amp;#34;ls1&amp;#58; cannot open %s&amp;#092;n&amp;#34;, dirname&amp;#41;;&lt;br /&gt;	&lt;br /&gt;	else&lt;br /&gt;	{&lt;br /&gt; &amp;nbsp;while &amp;#40;&amp;#40;direntp = readdir&amp;#40;dir_ptr&amp;#41;&amp;#41; &amp;#33;= NULL&amp;#41;&lt;br /&gt; &amp;nbsp;	printf&amp;#40;&amp;#34;%s&amp;#092;n&amp;#34;, direntp-&amp;#62;d_name&amp;#41;;&lt;br /&gt; &amp;nbsp;closedir&amp;#40;dir_ptr&amp;#41;;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;Can anyone pls help me?? I&amp;#39;m struggling with programming as itself already... It&amp;#39;s just wayyyy too hard &lt;!--emo&amp;:(--&gt;&lt;img src='http://static.lowyat.net/style_emoticons/default/sad.gif' border='0' style='vertical-align:middle' alt='sad.gif' /&gt;&lt;!--endemo--&gt;   Stupid me for taking this in uni....</description>
            <author>elilaw</author>
            <category>Codemasters</category>
            <pubDate>Thu, 28 Sep 2006 18:39:39 +0800</pubDate>
        </item>
    </channel>
</rss>
