0

Ok i am trying to have the user give a directory via the command line (./a.out /usr/bin) and then output all the files in that directory.

I don't really know how to approach this but here is what i have tried.

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

using namespace std;

bool FileExists(char path[]) 
{
  	struct 	stat st;
  	bool 	blnReturn;
  	int 	intStat;

	if(stat(path, &st) == 0)
  	{
		blnReturn = true;
  	} 
  	else 
  	{	
  		blnReturn = false;
  	}
  
  	return(blnReturn);
}

void printData(char path[]) 
{
	struct 	stat st;
  	int 	intStat;

	if(stat(path, &st) == 0)
  	{
  		cout << st.st_dev << "\t";
		char ls[] = {"/bin/ls "};
		strcat(ls, path);
		execve(ls, NULL , NULL);
  	} 
  
}

int main(int argc, char* argv[], char *envp[])
{
	if(argc < 3)
	{
		cout << "Error with arguments" << endl;
		return 0;
	}	
	
	if(FileExists(argv[2]))
	{
		printData(argv[2], envp);
	}
	else	cout << "Directory does not exist" << endl;
	
}

Thanks fo the help.

3
Contributors
4
Replies
9
Views
7 Years
Discussion Span
Last Post by caut_baia
0

opendir() / readdir() / closedir() would seem to be a lot simpler, if all you're after is a list of names.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.