/* exec.c - generic call-a-program code
- * Copyright (C) 2001, 2002 Free Software Foundation, Inc.
+ * Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
*
* This file is part of GnuPG.
*
int exec_read(struct exec_info *info) { return G10ERR_GENERAL; }
int exec_finish(struct exec_info *info) { return G10ERR_GENERAL; }
-int set_exec_path(const char *path) { return G10ERR_GENERAL; }
+int set_exec_path(const char *path,int method) { return G10ERR_GENERAL; }
#else /* ! NO_EXEC */
char *mkdtemp(char *template);
#endif
-#if defined (__MINGW32__) || defined (__CYGWIN32__)
+#if defined (_WIN32)
/* This is a nicer system() for windows that waits for programs to
return before returning control to the caller. I hate helpful
computers. */
}
#endif
-int set_exec_path(const char *path)
+/* method==0 to replace current $PATH, and 1 to append to current
+ $PATH. */
+int set_exec_path(const char *path,int method)
{
- /* Notice that path is never freed. That is intentional due to the
- way putenv() works. */
- char *p=m_alloc(5+strlen(path)+1);
+ char *p,*curpath=NULL;
+ size_t curlen=0;
+
+ if(method==1 && (curpath=getenv("PATH")))
+ curlen=strlen(curpath)+1;
+
+ p=m_alloc(5+curlen+strlen(path)+1);
strcpy(p,"PATH=");
+
+ if(curpath)
+ {
+ strcat(p,curpath);
+ strcat(p,":");
+ }
+
strcat(p,path);
+
+ if(DBG_EXTPROG)
+ log_debug("set_exec_path method %d: %s\n",method,p);
+
+ /* Notice that path is never freed. That is intentional due to the
+ way putenv() works. This leaks a few bytes if we call
+ set_exec_path multiple times. */
+
if(putenv(p)!=0)
return G10ERR_GENERAL;
else
if(tmp==NULL)
{
-#if defined (__MINGW32__) || defined (__CYGWIN32__)
+#if defined (_WIN32)
tmp=m_alloc(256);
if(GetTempPath(256,tmp)==0)
strcpy(tmp,"c:\\windows\\temp");
sprintf(info->tempdir,"%s" DIRSEP_S "gpg-XXXXXX",tmp);
-#if defined (__MINGW32__) || defined (__CYGWIN32__)
+#if defined (_WIN32)
m_free(tmp);
#endif
if(mkdtemp(info->tempdir)==NULL)
- log_error(_("%s: can't create directory: %s\n"),
+ log_error(_("can't create directory `%s': %s\n"),
info->tempdir,strerror(errno));
else
{
if(append)
{
- while(strlen(append)+len>size-1)
+ size_t applen=strlen(append);
+
+ if(applen+len>size-1)
{
- size+=100;
+ if(applen<100)
+ applen=100;
+
+ size+=applen;
info->command=m_realloc(info->command,size);
}
if(program==NULL && args_in==NULL)
BUG();
-#ifdef USE_EXEC_PATH
- set_exec_path(USE_EXEC_PATH);
-#endif
-
*info=m_alloc_clear(sizeof(struct exec_info));
if(name)
if(DBG_EXTPROG)
log_debug("execlp: %s\n",program);
- execlp(program,program,NULL);
+ execlp(program,program,(void *)NULL);
}
else
{
if(DBG_EXTPROG)
log_debug("execlp: %s -c %s\n",shell,(*info)->command);
- execlp(shell,shell,"-c",(*info)->command,NULL);
+ execlp(shell,shell,"-c",(*info)->command,(void *)NULL);
}
/* If we get this far the exec failed. Clean up and return. */
#endif /* !EXEC_TEMPFILE_ONLY */
if(DBG_EXTPROG)
- log_debug("using temp file \"%s\"\n",(*info)->tempfile_in);
+ log_debug("using temp file `%s'\n",(*info)->tempfile_in);
/* It's not fork/exec/pipe, so create a temp file */
(*info)->tochild=fopen((*info)->tempfile_in,binary?"wb":"w");
if((*info)->tochild==NULL)
{
- log_error(_("%s: can't create: %s\n"),
+ log_error(_("can't create `%s': %s\n"),
(*info)->tempfile_in,strerror(errno));
ret=G10ERR_WRITE_FILE;
goto fail;
if(DBG_EXTPROG)
log_debug("system() command is %s\n",info->command);
-#if defined (__MINGW32__) || defined (__CYGWIN32__)
+#if defined (_WIN32)
info->progreturn=win_system(info->command);
#else
info->progreturn=system(info->command);
if(info->tempfile_in)
{
if(unlink(info->tempfile_in)==-1)
- log_info(_("Warning: unable to remove tempfile (%s) \"%s\": %s\n"),
+ log_info(_("WARNING: unable to remove tempfile (%s) `%s': %s\n"),
"in",info->tempfile_in,strerror(errno));
}
if(info->tempfile_out)
{
if(unlink(info->tempfile_out)==-1)
- log_info(_("Warning: unable to remove tempfile (%s) \"%s\": %s\n"),
+ log_info(_("WARNING: unable to remove tempfile (%s) `%s': %s\n"),
"out",info->tempfile_out,strerror(errno));
}
if(rmdir(info->tempdir)==-1)
- log_info(_("Warning: unable to remove temp directory \"%s\": %s\n"),
+ log_info(_("WARNING: unable to remove temp directory `%s': %s\n"),
info->tempdir,strerror(errno));
}