variable scope

J

Joseph

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
 
V

Victor Bazarov

Joseph said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top