1 /* Copyright 1986-1992 Emmet P. Gray.
2 * Copyright 1996-2002,2007,2009 Alain Knaff.
3 * This file is part of mtools.
5 * Mtools is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * Mtools is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
18 * Do filename expansion with the shell.
21 #define EXPAND_BUF 2048
23 #include "sysincludes.h"
26 #ifndef OS_mingw32msvc
27 int safePopenOut(const char **command, char *output, int len)
44 close(2); /* avoid nasty error messages on stderr */
45 if(dup(pipefd[1]) < 0) {
50 execvp(command[0], (char**)(command+1));
56 last=read(pipefd[0], output, len);
67 const char *expand(const char *input, char *ans)
69 #ifndef OS_mingw32msvc
72 const char *command[] = { "/bin/sh", "sh", "-c", 0, 0 };
74 ans[EXPAND_BUF-1]='\0';
80 /* any thing to expand? */
81 if (!strpbrk(input, "$*(){}[]\\?`~")) {
82 strncpy(ans, input, EXPAND_BUF-1);
87 snprintf(buf, 255, "echo %s", input);
89 sprintf(buf, "echo %s", input);
93 last=safePopenOut(command, ans, EXPAND_BUF-1);
95 perror("Pipe read error");
101 strncpy(ans, input, EXPAND_BUF-1);
104 strncpy(ans, input, EXPAND_BUF-1);
105 ans[EXPAND_BUF-1]='\0';