Hello!

I am trying to make a mimic code of LS command in C.

It is compiled with gcc in ubuntu, and I have some questions to ask.

I have used opendir to open current directory, and then i read each directory using while loop to print all the directories inside that directory.
somehow it is showing .. and . as a directory.

I know .. is a back directory thingy but i dont need this to be outputted since it
is not what i need.


ive tried if ( directory != "..") but still outputs it...

is there any way to do this?

ill attach the code.

int main()
{
  char *example;
  example = (char*)malloc(1024);
  example = get_current_dir_name();/* this makes some error, change it later to cwd() function so that it wont make any errors.*/
  printf("test\n");
  DIR *dit;
  struct dirent *dip;
  dit = opendir(example);
  while ( ( dip = readdir(dit) ) != NULL )
  {
    char *temp;
    temp = dip->d_name;
    printf( "%s\n", temp);
    
  }
  //recursive_read_dir(example);
  
  return 0;
}

Edited 5 Years Ago by Narue: added code tags

Solved it .

Used strcmp function to compare.

guess directory != ".." is different with strcmp(directory, ".." ) != 0 . LOL

Hello!

I am trying to make a mimic code of LS command in C.

It is compiled with gcc in ubuntu, and I have some questions to ask.

I have used opendir to open current directory, and then i read each directory using while loop to print all the directories inside that directory.
somehow it is showing .. and . as a directory.

I know .. is a back directory thingy but i dont need this to be outputted since it
is not what i need.


ive tried if ( directory != "..") but still outputs it...

is there any way to do this?

ill attach the code.

int main()
{
  char *example;
  example = (char*)malloc(1024);
  example = get_current_dir_name();/* this makes some error, change it later to cwd() function so that it wont make any errors.*/
  printf("test\n");
  DIR *dit;
  struct dirent *dip;
  dit = opendir(example);
  while ( ( dip = readdir(dit) ) != NULL )
  {
    char *temp;
    temp = dip->d_name;
    printf( "%s\n", temp);
    
  }
  //recursive_read_dir(example);
  
  return 0;
}
This article has been dead for over six months. Start a new discussion instead.