|
发表于 2008-9-6 09:30:27
|
显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include <unistd.h>
#include <wait.h>
#include<gtk/gtk.h>
/*
** 编译使用以下命令:
** gcc -Wall `pkg-config --libs --cflags gtk+-2.0` icon.c -o icon
** 需要gtk2 >2.10.0
*/
void callback_icon(GtkStatusIcon *status_icon,gpointer data)
{
gtk_main_quit();
g_object_unref(status_icon);
}
char *get_proc_path(void)
{
char *link = NULL;
char *tmp = NULL;
static char path[1024]={0};
link = g_strdup_printf("/proc/%d/exe",getpid());
tmp = g_file_read_link(link,NULL);
strcpy(path,tmp);
g_free(link);
g_free(tmp);
return path;
}
int main(int argc,char **argv)
{
gtk_init(&argc,&argv);
char * cmd ="/bin/echo";
char *procpath = NULL;
GtkStatusIcon* icon;
pid_t pid ;
int status;
icon = gtk_status_icon_new_from_stock (GTK_STOCK_QUIT);
gtk_status_icon_set_tooltip(icon,"A Status Icon HelloWorld");
g_signal_connect((gpointer)icon, "activate", G_CALLBACK(callback_icon), NULL);
gtk_main();
// signal(SIGCHLD,SIG_IGN);
pid = fork();
if(pid==0){
execlp(cmd,cmd,NULL);
fprintf(stderr,"execlp():%s:%s\n",cmd,strerror(errno));
exit(-1);
}else if(pid == -1 ){ ///error .
fprintf(stderr,"fork():%s \n",strerror(errno));
return pid;
}
if(waitpid(pid,&status,0)==-1)
g_print("waitpid(pid = %d):%s",pid,strerror(errno));
if(fork()==0){
procpath = get_proc_path();
execlp(procpath,procpath,NULL);
}
g_print("return 0 .\n");
return 0;
} |
|