wordexp.c revision 64e3e6f9b3dab76d6b2407a88555c745b7fea73c
/*
* 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
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* 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.
*
*/
#include "synonyms.h"
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wordexp.h>
#include <stdio.h>
#include <errno.h>
/*
* Do word expansion.
* We just pass our arguments to shell with -E option. Note that the
* underlying shell must recognize the -E option, and do the right thing
* with it.
*/
int
{
static char *args[4];
const char *path;
int i;
int rv = WRDE_ERRNO;
int status;
/*
* Do absolute minimum neccessary 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;
}
/*
* Turn flags into shell options
*/
if (flags & WRDE_UNDEF)
*optendp++ = 'u';
if (flags & WRDE_NOCMD)
*optendp++ = 'N';
*optendp = '\0';
goto cleanup;
}
/*
* Set up pipe from shell stdout to "fp" for us
*/
goto cleanup;
/*
*/
goto cleanup;
}
if (pid == 0) { /* child */
/*
* fork1 handler takes care of __environ_lock.
* Thus we can safely call putenv().
*/
}
if ((flags & WRDE_SHOWERR) == 0) {
int devnull;
if (devnull != 2)
}
_exit(127);
}
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'.
*/
rv = WRDE_NOSPACE;
goto wait_cleanup;
}
rv = 0;
*cp++ = (char)i;
if (i == '\0') {
break;
}
}
char *nl;
rv = WRDE_NOSPACE;
break;
}
}
}
rv = WRDE_ERRNO;
else if (rv == 0)
if (rv == 0)
else {
if (tmpalloc)
}
if (wd)
/*
* Map ksh errors to wordexp() errors
*/
if (rv == 4)
rv = WRDE_CMDSUB;
else if (rv == 5)
rv = WRDE_BADVAL;
else if (rv == 6)
rv = WRDE_SYNTAX;
return (rv);
}
/*
* Append a word to the wordexp_t structure, growing it as neccessary.
*/
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);
}
/*
* Free all space owned by wordexp_t.
*/
void
{
size_t i;
return;
}