linux goto函数使用

{
int ret = 0;
DIR *mdir = NULL;
struct dirent *mptr = NULL;
char filepath[512] = {0};
char buff[64] = {0};
char read_buff[64] = {0};
int fd = 0;
int fpid = 0;
//char *name = info;

mdir = opendir("/proc");
if(NULL != mdir){
do{
mptr = readdir(mdir);
if(mptr != NULL){
if(strcmp(mptr->d_name,".") == 0)
continue;

if(strcmp(mptr->d_name,"..") == 0)
continue;

if(DT_DIR != mptr->d_type)
continue;

snprintf(filepath, 512, "/proc/%s/cmdline",mptr->d_name);
Dprintf("filepath=%s\n",filepath);
fd = open(filepath,O_RDONLY);
if(fd >0 ){
memset(read_buff,0,64);
memset(buff,0,64);
int read_length = read(fd,read_buff,64);
int buff_strlen = strlen(read_buff);
TRACE_INFO("read_length=%d read_buff=%s strlen_read_buff=%d\n",read_length,read_buff,strlen(read_buff));
Dprintf("read_length=%d read_buff=%s strlen_read_buff=%d\n",read_length,read_buff,strlen(read_buff));
if(buff_strlen != (read_length-1))
{
snprintf(buff,64,"%s%s",&read_buff[0],&read_buff[buff_strlen+1]);
TRACE_INFO("have_param_buff=%s\n",buff);
Dprintf("have_param_buff=%s\n",buff);
}
else
{
memcpy(buff,read_buff,64);
TRACE_INFO("no_param_buff=%s\n",buff);
Dprintf("no_param_buff=%s\n",buff);
}
if(strcmp(buff,info) == 0){
fpid = atoi(mptr->d_name);
ret = fpid;
Dprintf("ret=%d\n",ret);
// break;
goto SUCCESS;
}
close(fd);
} else {
printf("Error %d: Failed to open file:%s\n", errno,filepath);
}
}
}while(mptr!=NULL);
}
else{
TRACE_ERR("openddir /proc error\n");
}

SUCCESS:
closedir(mdir);
close(fd);
return ret;
}

猜你喜欢

转载自www.cnblogs.com/hzijone/p/12206373.html