bool GetLocalProgramName(char* processname)
{
char processdir[1024] = {0}
char* path_end
size_t len = 1024
bool ret = false
do
{
if(readlink("/proc/self/exe", processdir,len) <=0)
{
fprintf(stderr, "[ERROR]can not get process name\n")
break
}
path_end = strrchr(processdir, '/') // 进程目录
if(path_end == NULL)
{
fprintf(stderr, "[ERROR]can not parse process name\n")
break
}
++path_end
*path_end = '\0'
strcpy(processname, path_end)
ret = true
}while(0)
return ret
}
这是我以前的代码,稍微改造一下就行。
opendir/readdir/closedir: 读取目录资讯--------------------------------------------------------------------------------
#include
DIR * opendir(const char * pathname)
int closedir(DIR *dir)
struct dirent * readdir(DIR *dir)
int rewinddir(DIR *dir)
struct dirent {
long d_ino /* inode number */
off_t d_off /* offset to this dirent */
unsigned short d_reclen/* length of this d_name */
char d_name [NAME_MAX+1] /* file name (null-terminated) */
}
opendir开启一个目录 *** 作DIR,closedir关闭之。
readdir则循序读取目录中的资讯。
以下是个标准例。
--------------------------------------------------------------------------------
#include <sys/types.h>
#include <direct.h>
char ** dirGetInfo(const char *pathname)
{
char ** filenames
DIR * dir
struct dirent * ent
int n = 0
filenames = (char **)malloc(sizeof(char*))
filenames[0]=NULL
dir = opendir(pathname)
if (!dir) return filenames
while ((ent = readdir(dir))) {
filenames = (char**)realloc(filenames,sizeof(char*)*(n+1))
filenames[n] = strdup(ent->d_name)
n++
}
closedir(dir)
filenames = (char **)realloc(filenames,sizeof(char*)*(n+1))
filenames[n] = NULL
return filenames
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)