variable scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joseph

    #1

    variable scope


    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_pat h,entry_path);
    strcat(proc_pat h,"/exe");
    int temp=strlen(pro c_path);
    printf("%s\n",p roc_path);
    proc_path[temp+1]='\0';
    char exec_name[PATH_MAX];
    readlink(proc_p ath,exec_name,s izeof(exec_name ));
    printf("%s\n",e xec_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

  • Victor Bazarov

    #2
    Re: variable scope

    Joseph wrote:[color=blue]
    > 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];[/color]

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

    char entry_path[PATH_MAX + 1] = {0};
    [color=blue]
    > size_t path_len;
    > pid_t pid;
    > dir = opendir ("/proc/");
    > while ((entry = readdir (dir)) != NULL) {
    > const char* type;[/color]

    What's that for?
    [color=blue]
    >
    > strncpy (entry_path + path_len, entry->d_name, sizeof (entry_path) -
    > path_len);[/color]

    Shouldn't this be

    strcpy(entry_pa th + 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.
    [color=blue]
    > const char* name;
    > name=entry->d_name;
    >
    > if(strspn(name, "0123456789")== strlen(name))
    > {
    > char proc_path[7]="/proc/";
    > strcat(proc_pat h,entry_path);[/color]

    'proc_path' is only SEVEN chars. Where is the catenated part stored?
    [color=blue]
    > strcat(proc_pat h,"/exe");[/color]

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

    Why are you not using 'std::string' here?
    [color=blue]
    > int temp=strlen(pro c_path);
    > printf("%s\n",p roc_path);
    > proc_path[temp+1]='\0';
    > char exec_name[PATH_MAX];
    > readlink(proc_p ath,exec_name,s izeof(exec_name ));
    > printf("%s\n",e xec_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?[/color]

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

    V

    Comment

    Working...