【发布时间】:2014-03-20 02:46:41
【问题描述】:
我正在尝试精简 C。
我已经制作了这个 lille 程序/守护进程。
意思是你的'Taskfolder'中的任何.pdf-->(ROOTFOLDER)。
将通过“evince”在启动时开放/午餐。
我需要引导中止线程还是有更好的方法来做到这一点?
我尝试过类似 system("envice 1.pdf && envice 2.pdf ...");
但这仍然只是一次打开一个pdf文件。
代码如下所示。
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#define ROOTFOLDER ((const unsigned char *)"/home/myusername/Taskfolder/")
#define LUNCHERAPP ((const unsigned char *)"evince ")
int main()
{
DIR *d;
struct dirent *dir;
char *ext;
d = opendir(ROOTFOLDER);
if(d)
{
int i=0;
char *tmp;
while((dir = readdir(d)) != NULL)
{
ext = strchr(dir->d_name, '.');
if((ext != NULL) && (ext != 0) && (strcmp(strchr(ext ,'.'), ".pdf") == 0))
{
tmp = malloc(strlen(LUNCHERAPP) + strlen(ROOTFOLDER) + strlen(dir->d_name) + 1);
strcat(tmp, LUNCHERAPP);
strcat(tmp, ROOTFOLDER);
strcat(tmp, dir->d_name);
printf("pdf== %s\n", tmp);
system(tmp);// <-- 'evince ~/Taskfolder/filename.pdf'
free(tmp);
++i;
}
}
closedir(d);
}
return 0;
}
更新代码:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#define APP ((const unsigned char *)"evince")
#define ROOTFOLDER ((const unsigned char *)"./")
int main()
{
DIR *d;
struct dirent *dir;
char *ext;
d = opendir(ROOTFOLDER);
if(d)
{
char *cmd = malloc(1);
strcat(cmd, APP);
while((dir = readdir(d)) != NULL)
{
ext = strchr(dir->d_name, '.');
if((ext != NULL) && (ext != 0) && (strcmp(ext, ".pdf") == 0))
{
cmd = realloc(cmd, strlen(cmd)+strlen(ROOTFOLDER)+strlen(dir->d_name)+1);
strcat(cmd, " ");
strcat(cmd, ROOTFOLDER);
strcat(cmd, dir->d_name);
}
}
closedir(d);
printf("\nrun:\n%s\n", cmd);
system(cmd);
free(cmd);
}
return 0;
}
【问题讨论】:
-
strchr(ext ,'.') 可能返回 0。
-
谢谢 :) 所以我需要添加一个 if(ext != 0)..