1*8810SCasper.Dik@Sun.COM /* 2*8810SCasper.Dik@Sun.COM * CDDL HEADER START 3*8810SCasper.Dik@Sun.COM * 4*8810SCasper.Dik@Sun.COM * The contents of this file are subject to the terms of the 5*8810SCasper.Dik@Sun.COM * Common Development and Distribution License (the "License"). 6*8810SCasper.Dik@Sun.COM * You may not use this file except in compliance with the License. 7*8810SCasper.Dik@Sun.COM * 8*8810SCasper.Dik@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*8810SCasper.Dik@Sun.COM * or http://www.opensolaris.org/os/licensing. 10*8810SCasper.Dik@Sun.COM * See the License for the specific language governing permissions 11*8810SCasper.Dik@Sun.COM * and limitations under the License. 12*8810SCasper.Dik@Sun.COM * 13*8810SCasper.Dik@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each 14*8810SCasper.Dik@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*8810SCasper.Dik@Sun.COM * If applicable, add the following below this CDDL HEADER, with the 16*8810SCasper.Dik@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying 17*8810SCasper.Dik@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner] 18*8810SCasper.Dik@Sun.COM * 19*8810SCasper.Dik@Sun.COM * CDDL HEADER END 20*8810SCasper.Dik@Sun.COM */ 21*8810SCasper.Dik@Sun.COM 22*8810SCasper.Dik@Sun.COM /* 23*8810SCasper.Dik@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24*8810SCasper.Dik@Sun.COM * Use is subject to license terms. 25*8810SCasper.Dik@Sun.COM */ 26*8810SCasper.Dik@Sun.COM 27*8810SCasper.Dik@Sun.COM /* 28*8810SCasper.Dik@Sun.COM * alias.c is a C version of the alias.sh wrapper (which links ksh 29*8810SCasper.Dik@Sun.COM * builtins to commands in /usr/bin/, e.g. calling this wrapper as 30*8810SCasper.Dik@Sun.COM * /usr/bin/alias will call the ksh "alias" builtin, running it as 31*8810SCasper.Dik@Sun.COM * /usr/bin/cut will call the ksh "cut" builtin etc. 32*8810SCasper.Dik@Sun.COM */ 33*8810SCasper.Dik@Sun.COM 34*8810SCasper.Dik@Sun.COM #include <shell.h> 35*8810SCasper.Dik@Sun.COM #include <nval.h> 36*8810SCasper.Dik@Sun.COM #include <stdio.h> 37*8810SCasper.Dik@Sun.COM 38*8810SCasper.Dik@Sun.COM /* Builtin script, original derived from alias.sh */ 39*8810SCasper.Dik@Sun.COM static const char *script = "\n" 40*8810SCasper.Dik@Sun.COM /* Get name of builtin */ 41*8810SCasper.Dik@Sun.COM "builtin basename\n" 42*8810SCasper.Dik@Sun.COM "typeset cmd=\"$(basename \"$0\")\"\n" 43*8810SCasper.Dik@Sun.COM /* 44*8810SCasper.Dik@Sun.COM * If the requested command is not an alias load it explicitly 45*8810SCasper.Dik@Sun.COM * to make sure it is not bound to a path (those built-ins which 46*8810SCasper.Dik@Sun.COM * are mapped via shell aliases point to commands which are 47*8810SCasper.Dik@Sun.COM * "special shell built-ins" which cannot be bound to a specific 48*8810SCasper.Dik@Sun.COM * PATH element) - otherwise we may execute the wrong command 49*8810SCasper.Dik@Sun.COM * if an executable with the same name sits in a PATH element 50*8810SCasper.Dik@Sun.COM * before /usr/bin (e.g. /usr/xpg4/bin/ls would be executed 51*8810SCasper.Dik@Sun.COM * before /usr/bin/ls if the path was something like 52*8810SCasper.Dik@Sun.COM * PATH=/usr/xpg4/bin:/usr/bin). 53*8810SCasper.Dik@Sun.COM */ 54*8810SCasper.Dik@Sun.COM "if [[ \"${cmd}\" != ~(Elr)(alias|unalias|command) ]] && " 55*8810SCasper.Dik@Sun.COM "! alias \"${cmd}\" >/dev/null 2>&1 ; then\n" 56*8810SCasper.Dik@Sun.COM " builtin \"${cmd}\"\n" 57*8810SCasper.Dik@Sun.COM "fi\n" 58*8810SCasper.Dik@Sun.COM /* command is a keyword and needs to be handled separately */ 59*8810SCasper.Dik@Sun.COM "if [[ \"${cmd}\" == \"command\" ]] ; then\n" 60*8810SCasper.Dik@Sun.COM " command \"$@\"\n" 61*8810SCasper.Dik@Sun.COM "else\n" 62*8810SCasper.Dik@Sun.COM " \"${cmd}\" \"$@\"\n" 63*8810SCasper.Dik@Sun.COM "fi\n" 64*8810SCasper.Dik@Sun.COM "exitval=$?"; 65*8810SCasper.Dik@Sun.COM 66*8810SCasper.Dik@Sun.COM int 67*8810SCasper.Dik@Sun.COM main(int argc, char *argv[]) 68*8810SCasper.Dik@Sun.COM { 69*8810SCasper.Dik@Sun.COM int i; 70*8810SCasper.Dik@Sun.COM Shell_t *shp; 71*8810SCasper.Dik@Sun.COM Namval_t *np; 72*8810SCasper.Dik@Sun.COM int exitval; 73*8810SCasper.Dik@Sun.COM 74*8810SCasper.Dik@Sun.COM /* 75*8810SCasper.Dik@Sun.COM * Create copy of |argv| array shifted by one position to 76*8810SCasper.Dik@Sun.COM * emulate $ /usr/bin/sh <scriptname> <args1> <arg2> ... #. 77*8810SCasper.Dik@Sun.COM * First position is set to "/usr/bin/sh" since other 78*8810SCasper.Dik@Sun.COM * values may trigger special shell modes (e.g. *rsh* will 79*8810SCasper.Dik@Sun.COM * trigger "restricted" shell mode etc.). 80*8810SCasper.Dik@Sun.COM */ 81*8810SCasper.Dik@Sun.COM char *xargv[argc+2]; 82*8810SCasper.Dik@Sun.COM xargv[0] = "/usr/bin/sh"; 83*8810SCasper.Dik@Sun.COM xargv[1] = "scriptname"; 84*8810SCasper.Dik@Sun.COM for (i = 0; i < argc; i++) { 85*8810SCasper.Dik@Sun.COM xargv[i+1] = argv[i]; 86*8810SCasper.Dik@Sun.COM } 87*8810SCasper.Dik@Sun.COM xargv[i+1] = NULL; 88*8810SCasper.Dik@Sun.COM 89*8810SCasper.Dik@Sun.COM shp = sh_init(argc+1, xargv, 0); 90*8810SCasper.Dik@Sun.COM if (!shp) 91*8810SCasper.Dik@Sun.COM error(ERROR_exit(1), "shell initialisation failed."); 92*8810SCasper.Dik@Sun.COM (void) sh_trap(script, 0); 93*8810SCasper.Dik@Sun.COM 94*8810SCasper.Dik@Sun.COM np = nv_open("exitval", shp->var_tree, 0); 95*8810SCasper.Dik@Sun.COM if (!np) 96*8810SCasper.Dik@Sun.COM error(ERROR_exit(1), "variable %s not found.", "exitval"); 97*8810SCasper.Dik@Sun.COM exitval = (int)nv_getnum(np); 98*8810SCasper.Dik@Sun.COM nv_close(np); 99*8810SCasper.Dik@Sun.COM 100*8810SCasper.Dik@Sun.COM return (exitval); 101*8810SCasper.Dik@Sun.COM } 102