variable scope

From: Joseph (wutongjoe_at_hotmail.com)
Date: 10/12/04


Date: Tue, 12 Oct 2004 16:00:22 +0000 (UTC)


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



Relevant Pages

  • variable scope
    ... output the "exe" symbolic link using readlink(),but I have a problem ... int main (int argc, char* argv){ ... string into exec_name,then,the previous longer string left a "tail" ...
    (comp.os.linux.development.apps)
  • Re: Maintaining multivariable state (newbie question)
    ... extended with various options, and I've ended up making each function taking (..., int argc, char *argv) and then each function would parse it. ... argc, char *argv), but for some reason I needed to change some of the things temporarily, and restore them, so I've ended up doing something like a stack for each global variable. ... And when I started reading lisp I understand the wisdom behind special variables, and all my wasted hours behind emulating what's already have been done. ... That plus the multiple-value returns (Another invention that I needed these days in a C++ code to overcome total rewrite of foreign code portions in a much larger tool - mainly to avoid massive amounts of DLL's to be recompiled). ...
    (comp.lang.lisp)
  • Re: The contents of the file is not read..
    ... int main(int argc, char *argv) ... Buffer overflow protection. ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
    (Debian-User)
  • Re: Newbee needs once more again help with passing arrays out of a function
    ... int argc, char *argv) ... it's best to copy and paste a compilable program ... give them the starting address of some region in memory. ...
    (comp.lang.c)
  • Re: function
    ... main (int argc, char *argv) ... exit; ... Why the termination string character? ...
    (comp.lang.c)