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
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