Implementing ls commands in c
<p>I am struggling with my code. I need to implement <code>ls</code>, <code>ls -l</code>, <code>ls -i</code>, and <code>ls -R</code> commands in my C program. So far I only did <code>ls -l</code>, <code>ls -i</code>, and <code>ls -R</code> as I am not sure how to implement <code>ls</code> because there is no argument. The challenge is also that after executing the program, use should be able to type <code>% myls -l</code> and get the results as for command <code>ls -l</code>.</p>

<p>Also right now I get Segmentation error 11 :(</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;errno.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;dirent.h&gt;
#include &lt;grp.h&gt;
#include &lt;pwd.h&gt;
#include &lt;unistd.h&gt;
#include &lt;time.h&gt;
#include &lt;termios.h&gt;
#include &lt;sys/ioctl.h&gt;

DIR * dir;
struct dirent * ptr;

int main(int argc,char *argv[])
{
// declaration for different parameters

void _l(char *direct);
void _i(char *direct);
void _R(char *direct);

// get the argc[]
char para[20];
char direct[100];
memset(direct, 0, sizeof(direct));
// copy the parameter like -l
strcpy(para, argv[1]);
// copy the path like ./
strcpy(direct, argv[2]);

// call the corresponding function according to parameters

if (strcmp(para, "-l") == 0)
{
    _l(direct);
}
if (strcmp(para, "-i") == 0)
{
    _i(direct);
}
   if (strcmp(para, "-R") == 0)
{
    _R(direct);
}


}


void _l(char *direct)
{
void mode_to_letter(int mode, char *str);
// define struct stat
struct stat fst;
struct tm * mytime = (struct tm *) malloc(sizeof(struct tm));
char str[12];
dir = opendir(direct);
// skip . and ..
readdir(dir);
readdir(dir);
while((ptr = readdir(dir)) != NULL)
{
    stat(ptr-&gt;d_name, &amp;fst);
    // permission information
    mode_to_letter(fst.st_mode, str);
    // file type and permission
    printf("%s", str);
    // file hard links
    printf(" %d", fst.st_nlink);
    // file's owner
    printf(" %s", getpwuid(fst.st_uid)-&gt;pw_name);
    // file's owner group
    printf(" %s", getgrgid(fst.st_gid)-&gt;gr_name);
    // file size
    printf(" %ld", (long)fst.st_size);
    // file time
    mytime = localtime(&amp;fst.st_mtime);
    printf(" %d-%02d-%02d %02d:%02d", mytime-&gt;tm_year + 1900, mytime-&gt;tm_mon + 1, mytime-&gt;tm_mday, mytime-&gt;tm_hour, mytime-&gt;tm_min);
    // file name
    printf(" %s", ptr-&gt;d_name);
    printf("\n");
  }
}

void mode_to_letter(int mode, char *str)
{
str[0] = '-';

// judge file type
if(S_ISDIR(mode)) str[0] = 'd';
if(S_ISCHR(mode)) str[0] = 'c';
if(S_ISBLK(mode)) str[0] = 'b';

// judge permission for owner
if(mode &amp; S_IRUSR) str[1] = 'r';
else str[1] = '-';
if(mode &amp; S_IWUSR) str[2] = 'w';
else str[2] = '-';
if(mode &amp; S_IXUSR) str[3] = 'x';
else str[3] = '-';

// judge permission for owner group
if(mode &amp; S_IRGRP) str[4] = 'r';
else str[4] = '-';
if(mode &amp; S_IWGRP) str[5] = 'w';
else str[5] = '-';
if(mode &amp; S_IXGRP) str[6] = 'x';
else str[6] = '-';

// judge permission for others
if(mode &amp; S_IROTH) str[7] = 'r';
else str[7] = '-';
if(mode &amp; S_IWOTH) str[8] = 'w';
else str[8] = '-';
if(mode &amp; S_IXOTH) str[9] = 'x';
else str[9] = '-';

str[10] = '\0';
}

void _i(char *direct)
{
dir = opendir(direct);
readdir(dir);
readdir(dir);
while((ptr = readdir(dir)) != NULL)
{
    // get inode id
    printf("%ld ", (long)ptr-&gt;d_ino);
    printf("%s\n", ptr-&gt;d_name);
}
closedir(dir);
}



void _R(char *direct)
{
dir = opendir(direct);

while((ptr = readdir(dir)) != NULL)
{
    // skip . and ..
    if(!strcmp(ptr-&gt;d_name,".") || !strcmp(ptr-&gt;d_name,".."))
        continue;
    // if directory
    if (ptr-&gt;d_type &amp; DT_DIR)
    {
        printf("%s: \n", ptr-&gt;d_name);
        // do recursively
        _R(ptr-&gt;d_name);
    }
    else
        // print the name
        printf("%s ", ptr-&gt;d_name);
 }
}
</code></pre>