0

I'm trying to read all files (pictures) in a given directory and pass it to execv. I want to start the framebuffer image viewer (fbi).

For now it works when i start it from the directory the pictures are in. How can i manipulate the vector so it not only holds the filenames but the path + filename?

int startSlideshow (char* pathtoFBI) {
pid_t fbiPid;

fbiPid = fork();

if (fbiPid == 0) {
    /* this is the child process */
    DIR *pictureDirectory;
    struct dirent *dir;
    int i = 0;
    vector<char*> argVector;
    char pDirectory[] = "/home/pi/bilder/";
    argVector.push_back(pathtoFBI);         /* first Argument is Program name */
    pictureDirectory = opendir(pDirectory);
    if (pictureDirectory){
        while ((dir = readdir(pictureDirectory)) != NULL) {
            i++;
            if (i > 2) {            /* first element is "." second is ".." */
                argVector.push_back(dir->d_name);
            }
        }
        //i -= 2;

    } else ERREXIT("reading picture directory");

    closedir(pictureDirectory);

    argVector.push_back(NULL);
    char** fbiArgv = &argVector[0];
    execvp(pathtoFBI, fbiArgv);         /* pid is taken over by fbi */
    _exit(EXIT_FAILURE);
}
j0k
  • 22,600
  • 28
  • 79
  • 90

1 Answers1

1

Given your code, you just need to prepend pDirectory to every file name:

--- c1.cc       2013-02-04 02:35:47.000000000 +0400
+++ c2.cc       2013-02-04 02:39:03.000000000 +0400
@@ -8,7 +8,7 @@
     DIR *pictureDirectory;
     struct dirent *dir;
     int i = 0;
-    vector<char*> argVector;
+    vector<char*> argVector; vector<string> fpVector;
     char pDirectory[] = "/home/pi/bilder/";
     argVector.push_back(pathtoFBI);         /* first Argument is Program name */
     pictureDirectory = opendir(pDirectory);
@@ -16,7 +16,8 @@
         while ((dir = readdir(pictureDirectory)) != NULL) {
             i++;
             if (i > 2) {            /* first element is "." second is ".." */
-                argVector.push_back(dir->d_name);
+                fpVector.push_back(string(pDirectory) + dir->d_name);
+                argVector.push_back((char*) fpVector.back().c_str());
             }
         }
         //i -= 2;

More generally, there's Boost Filesystem's absolute and canonical.

ArtemGr
  • 11,684
  • 3
  • 52
  • 85
  • Thanks! This works well. However i replaced 'vector fpVector' with 'string fpString' and in the while loop 'fpString = string(pDirectory) + dir->d_name;' to avoid having 2 vectors. –  Feb 04 '13 at 09:01
  • 1
    You shouldn't do this because execvp needs cstrings which are allocated by strings and if your strings are temporary then the cstrings are dangling. – ArtemGr Feb 04 '13 at 12:35