|
- #include<dirent.h>
- #include<stdio.h>
- #include<string.h>
- #define SE_SUCCESS 0
- #define SE_ERROR -1
- #define CLRF "\r\n"
- #define MAXLEN 10240 //读取的文件最大字节数
- unsigned char g_NewVersion[10]; //版本号
- static char read_config(char * pParam, char * pValue, char * pFileName);
- static char* ReadVersion(char *dest);
- int main(int argc, char **argv[])
- {
- char *buf;
- ReadVersion(buf);
- }
- //寻找进程所属文件位置,并输出版本号
- char* ReadVersion(char *dest)
- {
- DIR *dir;
- int i=0;
- int pos=0;
- struct dirent *ptr;
- FILE *fp;
- char filepath[50];//大小随意,能装下cmdline文件的路径即可
- char filetext[50];//大小随意,能装下要识别的命令行文本即可
- char version[10]; //进程的版本号
- char *pVersion;
- char *ProcessName;
- char *s;
- dir = opendir("/proc"); //打开系统所有进程ID所在路径
- // printf("%s",dir->d_name);
- if (NULL != dir)
- {
- while ((ptr = readdir(dir)) != NULL) //循环读取路径下的每一个文件/文件夹
- {
- // i++;
- // printf("%s \n",ptr->d_name);
- //如果读取到的是"."或者".."则跳过,读取到的不是文件夹名字也跳过
- if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0)) continue;
- // if (DT_DIR != ptr->d_type) continue;
- if(!isdigit(ptr->d_name)) continue;
- sprintf(filepath, "/proc/%s/status", ptr->d_name);//生成要读取的版本配置文件的路径
- printf("%s \n",filepath);
- // fp = fopen(filepath, "r");//打开文件
- i++;
- // Version = read_version(("CUR_VERSION", pVersion, g_tInfo.VerName )); //读取版本文件
- if ( read_config("Process_VERSION", pVersion, filepath) == -1 ) //返回的是版本号
- {
- printf("read Process_VERSION error \n");
- // return -1;
- }
- else {
- strcpy(version, pVersion);
- // version = read_config("Process_VERSION", pVersion, filepath);
- // return version;
- }
-
- if ( read_config("Name", pVersion, filepath) == -1)
- {
- printf("read Name error \n");
- // return -1;
- }
- else {
- strcpy(ProcessName, pVersion);
- // ProcessName = read_config("Name", pVersion, filepath);
- // return version;
- }
- sprintf(s, "%s%s",ProcessName,version);
- // strcpy(temp,s);
- // dest[pos++] = s;
- return dest;
-
- /* if (NULL != fp)
- {
- fread(filetext, 1, 50, fp);//读取文件
- filetext[49] = '\0';//给读出的内容加上字符串结束符
- //如果文件内容满足要求则打印路径的名字(即进程的PID)
- if (filetext == strstr(filetext, "status.ini")) //若读取内容是版本文件
-
-
- printf("PID: %s\n", ptr->d_name);
- */
- // fclose(fp);
- // }
-
- }
- closedir(dir);//关闭路径
- }
- }
- //读取文件
- char read_config(char * pParam,
- char * pValue,
- char * pFileName)
- {
- FILE *fp;
- int accLen=0,bytes_read;
- char buffer[10240]="\0";
- char tmpValue[128]="\0";
- char* match;
- fp = fopen(pFileName,"r");
- if (fp <= 0)
- {
- printf("%s\n","Read config File Fail......");
- return SE_ERROR;
- }
- bytes_read = fread(buffer, 1, sizeof(buffer),fp);
-
- fclose(fp);
-
- if (bytes_read == 0 || bytes_read == sizeof(buffer)) //可能存在问题
- {
- return SE_ERROR;
- }
- buffer[bytes_read] = '\0';
-
- match =(char *) strstr(buffer, pParam);
- if (match == NULL)
- {
- return SE_ERROR;
- }
-
- match =(char *) strchr(match, ':');
-
- if (match == NULL)
- {
- return SE_ERROR;
- }
-
- accLen = strcspn(match+1,CLRF);
- if(accLen>0)
- {
- strncpy(tmpValue,match+1,accLen);
- strcpy(pValue, tmpValue);
- // return accLen;
- }
- return SE_SUCCESS;
- }
复制代码
编译后,出现‘ Segmentation fault (core dumped)’的问题
|
|