Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Problems with ls command implemented in C

Avatar of vwWojtek
vwWojtek asked on
C
6 Comments1 Solution1441 ViewsLast Modified:
Hi Experts :)

I wrote a short program to list directory contents.
======================
#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
#include<stdlib.h>

int main(int argc, char *argv[])
{
int i;
DIR*dp;
struct dirent*dirp;

if (argc==2)
      {
      dp = opendir(argv[1]);
        while ((dirp = readdir(dp)) != NULL)
      printf("%s\n", dirp->d_name);
      }
else
      {
      if (argc == 1)
            {
            dp = opendir("./");
              while ( (dirp = readdir(dp)) != NULL)
                printf("%s\n", dirp->d_name);
            }
      else
            {
            printf("ls - Error - too many parameters\n");
            exit(0);
            }

      };
        closedir(dp);
        return 0;
}
======================
my main problem is, how to format output data - now it is line by line and at random. How to make it look like output of ls in unix ? Second problem is how to handle error if wrong path is given or not existed directory as a parameter ? Thanks in advance for tips how to solve it. Maybe you have an idea how to implement ls -l option to my code ? Thanks :)

cheers
Wojtek
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answers