rendered paste bodyvoid display_dir(const char* path, ofstream& file, bool print)
{
struct dirent **contents;
char cwd[256];
unsigned int num_entries = 0;
if ((string)path == "")
num_entries = scandir(getcwd(cwd,sizeof(cwd)), &contents,NULL,alphasort);
else
num_entries = scandir(path,&contents,NULL,alphasort);
if(num_entries == -1 )
{
if(print)
file<<"ERROR: myshell: "<<path<<" does not exist."<<endl;
else
cout<<"ERROR: myshell: "<<path<<"does not exist."<<endl;
return;
}
if(print)
{
for(int i = 0; i <num_entries; i++)
{
if(contents[i]->d_name[0] != '.')
file<<contents[i]->d_name<<" ";
}
file<<endl;
}
else
{
for(int i = 0; i<num_entries; i++)
{
if(contents[i]->d_name[0] != '.')
cout<<contents[i]->d_name<<" ";
}
cout<<endl;
}
}