/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/*
* This code is MKS code ported to Solaris originally with minimum
* modifications so that upgrades from MKS would readily integrate.
* The MKS basis for this modification was:
*
* $Id: wordexp.c 1.22 1994/11/21 18:24:50 miked
*
* Additional modifications have been made to this code to make it
* 64-bit clean.
*/
/*
* wordexp, wordfree -- POSIX.2 D11.2 word expansion routines.
*
* Copyright 1985, 1992 by Mortice Kern Systems Inc. All rights reserved.
* Modified by Roland Mainz <roland.mainz@nrubsig.org> to support ksh93.
*/
#include "lint.h"
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <alloca.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <wordexp.h>
#include <stdio.h>
#include <spawn.h>
#include <errno.h>
/*
* Needs no locking if fetched only once.
* See getenv()/putenv()/setenv().
*/
extern const char **_environ;
/* Local prototypes */
static char *mystpcpy(char *, const char *);
/*
* Do word expansion.
* We build a mini-script in |buff| which takes care of all details,
* the word expansion itself.
*/
int
{
int tmpalloc;
char *wd;
int cancel_state;
const char *path;
/*
* Do absolute minimum necessary for the REUSE flag. Eventually
* want to be able to actually avoid excessive malloc calls.
*/
if (flags & WRDE_REUSE)
/*
* Initialize wordexp_t
*
* XPG requires that the struct pointed to by wp not be modified
* unless wordexp() either succeeds, or fails on WRDE_NOSPACE.
* So we work with wptmp, and only copy wptmp to wp if one of the
* previously mentioned conditions is satisfied.
*/
/*
* Man page says:
* 2. All of the calls must set WRDE_DOOFFS, or all must not
* set it.
* Therefore, if it's not set, we_offs will always be reset.
*/
if ((flags & WRDE_DOOFFS) == 0)
/*
* If we get APPEND|REUSE, how should we do?
* allocating buffer anyway to avoid segfault.
*/
tmpalloc = 0;
return (WRDE_NOSPACE);
tmpalloc = 1;
}
/*
* The UNIX98 Posix conformance test suite requires
* |wordexp()| to not be a cancellation point.
*/
/*
* Make sure PWD is in the environment.
*/
/* can happen when processing a SunOS 4.x AOUT file */
n = 0;
} else {
break;
}
}
/* allocate a new environment */
goto cleanup;
for (i = 0; i < n; i++)
}
if (__xpg4) {
/*
* Turn flags into shell options
*/
i = 0;
options[i++] = '-';
if (flags & WRDE_UNDEF)
options[i++] = 'u';
if (flags & WRDE_NOCMD)
options[i++] = 'N';
options[i] = '\0';
} else {
char *buff;
/*
* Calculate size of required buffer (which is size of the
* input string (|word|) plus all string literals below;
* this value MUST be adjusted each time the literals are
* changed!!).
*/
i = 0;
/* Start filling the buffer */
buff[0] = '\0';
if (flags & WRDE_UNDEF)
if ((flags & WRDE_SHOWERR) == 0) {
/*
* The newline ('\n') is neccesary to make sure that
* the case the printf below contains a syntax
* error...
*/
}
/* Squish stdin */
if (flags & WRDE_NOCMD) {
/*
* Switch to restricted shell (rksh) mode here to
* put the word expansion into a "cage" which
* prevents users from executing external commands
* (outside those listed by ${PATH} (which we set
*/
"set -o restricted\n");
}
"print -f '%%s\\000' -- %s", word);
}
goto cleanup;
}
(void) posix_spawnattr_destroy(&attr);
goto cleanup;
}
/*
* Set up pipe from shell stdout to "fp" for us
*/
(void) posix_spawnattr_destroy(&attr);
(void) posix_spawn_file_actions_destroy(&fact);
goto cleanup;
}
/*
* Spawn shell
*/
if (error == 0)
if (error == 0)
(void) posix_spawnattr_destroy(&attr);
(void) posix_spawn_file_actions_destroy(&fact);
if (error) {
goto cleanup;
}
goto wait_cleanup;
}
/*
* Read words from shell, separated with '\0'.
* Since there is no way to disable IFS splitting,
* it would be possible to separate the output with '\n'.
*/
goto wait_cleanup;
}
rv = 0;
*cp++ = (char)i;
if (i == '\0') {
break;
}
}
char *nl;
rv = WRDE_NOSPACE;
break;
}
}
}
if (rv == 0)
rv = WRDE_ERRNO;
break;
}
}
if (rv == 0)
if (rv == 0)
else if (tmpalloc)
if (env) {
}
if (__xpg4) {
/*
* Map xpg4-sh errors to wordexp() errors
*/
if (rv == 4)
rv = WRDE_CMDSUB;
else if (rv == 5)
rv = WRDE_BADVAL;
else if (rv == 6)
rv = WRDE_SYNTAX;
} else {
/*
* Map ksh93 errors to |wordexp()| errors
*/
if (rv == 1)
rv = WRDE_BADVAL;
else if (rv == 127)
rv = WRDE_BADCHAR;
}
return (rv);
}
/*
* Free all space owned by wordexp_t.
*/
void
{
size_t i;
return;
}
/*
* Append a word to the wordexp_t structure, growing it as necessary.
*/
static int
{
char *cp;
char **nwp;
/*
* We will be adding one entry and later adding
* one more NULL. So we need 2 more free slots.
*/
return (WRDE_NOSPACE);
}
return (WRDE_NOSPACE);
return (0);
}
/*
* |mystpcpy| - like |strcpy()| but returns the end of the buffer
* as normal libc function.
*
* Copy string s2 to s1. s1 must be large enough.
* return s1-1 (position of string terminator ('\0') in destination buffer).
*/
static char *
{
;
return (s1-1);
}