variable scope

Discussion in 'C++' started by Joseph, Oct 12, 2004.

  1. Joseph

    Joseph Guest

    Hi all,

    I am writting a program which loops each sub-dir inside the /proc and
    output the "exe" symbolic link using readlink() ,but I have a problem
    with the output .I think it's the problem of variable's scope but I have
    no idea how to deal with that.Any one could help me with that?


    ==========================
    #include <fstream>
    #include <assert.h>
    #include <dirent.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <bitset>
    #include <iostream>
    #include <list>
    using namespace std;
    int main (int argc, char* argv[]){
    char* dir_path;
    DIR* dir;
    struct dirent* entry;
    char entry_path[PATH_MAX + 1];
    size_t path_len;
    pid_t pid;
    dir = opendir ("/proc/");
    while ((entry = readdir (dir)) != NULL) {
    const char* type;

    strncpy (entry_path + path_len, entry->d_name, sizeof (entry_path) -
    path_len);
    const char* name;
    name=entry->d_name;

    if(strspn(name,"0123456789")==strlen(name))
    {
    char proc_path[7]="/proc/";
    strcat(proc_path,entry_path);
    strcat(proc_path,"/exe");
    int temp=strlen(proc_path);
    printf("%s\n",proc_path);
    proc_path[temp+1]='\0';
    char exec_name[PATH_MAX];
    readlink(proc_path,exec_name,sizeof(exec_name));
    printf("%s\n",exec_name);

    }
    }
    return 0;
    }



    ==========================


    the problem is on "exec_name" ,I think.every time,if it reads a shorter
    string into exec_name,then ,the previous longer string left a "tail"
    inside it.

    How could I fix it?





    Thanks a lot!
    Joseph
     
    Joseph, Oct 12, 2004
    #1
    1. Advertising

  2. Joseph wrote:
    > I am writting a program which loops each sub-dir inside the /proc and
    > output the "exe" symbolic link using readlink() ,but I have a problem
    > with the output .I think it's the problem of variable's scope but I have
    > no idea how to deal with that.Any one could help me with that?
    >
    >
    > ==========================
    > #include <fstream>
    > #include <assert.h>
    > #include <dirent.h>
    > #include <stdio.h>
    > #include <string.h>
    > #include <sys/stat.h>
    > #include <sys/types.h>
    > #include <unistd.h>
    > #include <stdlib.h>
    > #include <bitset>
    > #include <iostream>
    > #include <list>
    > using namespace std;
    > int main (int argc, char* argv[]){
    > char* dir_path;
    > DIR* dir;
    > struct dirent* entry;
    > char entry_path[PATH_MAX + 1];


    Are you sure that 'PATH_MAX + 1' is enough? Besides, your array is
    uninitialised. I'd do

    char entry_path[PATH_MAX + 1] = {0};

    > size_t path_len;
    > pid_t pid;
    > dir = opendir ("/proc/");
    > while ((entry = readdir (dir)) != NULL) {
    > const char* type;


    What's that for?

    >
    > strncpy (entry_path + path_len, entry->d_name, sizeof (entry_path) -
    > path_len);


    Shouldn't this be

    strcpy(entry_path + path_len, entry->d_name);

    ?

    I think that's what you're trying to do here, but I don't think you got
    the size correctly. Otherwise, if you think you did get the size right,
    you still need to zero-terminate the array after you copied.

    > const char* name;
    > name=entry->d_name;
    >
    > if(strspn(name,"0123456789")==strlen(name))
    > {
    > char proc_path[7]="/proc/";
    > strcat(proc_path,entry_path);


    'proc_path' is only SEVEN chars. Where is the catenated part stored?

    > strcat(proc_path,"/exe");


    Again, you're appending more stuff there. Where does it get stored?

    Why are you not using 'std::string' here?

    > int temp=strlen(proc_path);
    > printf("%s\n",proc_path);
    > proc_path[temp+1]='\0';
    > char exec_name[PATH_MAX];
    > readlink(proc_path,exec_name,sizeof(exec_name));
    > printf("%s\n",exec_name);
    >
    > }
    > }
    > return 0;
    > }
    >
    >
    >
    > ==========================
    >
    >
    > the problem is on "exec_name" ,I think.every time,if it reads a shorter
    > string into exec_name,then ,the previous longer string left a "tail"
    > inside it.
    >
    > How could I fix it?


    Stop using pointers to char and switch to 'std::string'.

    V
     
    Victor Bazarov, Oct 12, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    972
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    682
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    502
  4. David Filmer
    Replies:
    19
    Views:
    256
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    205
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page