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.