Changeset 3857 for trunk


Ignore:
Timestamp:
Apr 14, 2014, 4:02:50 AM (11 years ago)
Author:
bird
Message:

system() and popen() should use the same shells, preferablly /bin/sh when available (see unix spec). arg[0] shouldn't have a path, according to unix tradition (should be 'sh' to be more precise). Introduced a backend API for finding the desired shell and figure arguments.

Location:
trunk/libc
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/libc/include/klibc/backend.h

    r3816 r3857  
    13241324extern int __libc_Back_gfProcessHandlePCBatchScripts;
    13251325
     1326/**
     1327 * Gets the default shell for functions like system() and popen().
     1328 *
     1329 * @returns 0 on success, negative error number on failure.
     1330 * @param   pszShell        Where to put the path to the shell.
     1331 * @param   cbShell         The size of the buffer @a pszShell points to.
     1332 * @param   poffShellArg    Where to return the offset into @a pszShell of the
     1333 *                          first argument.  The system() and popen() calls has
     1334 *                          traditionally not included the path to /bin/sh.
     1335 * @param   pszCmdLineOpt   Where to put the shell option for specifying a
     1336 *                          command line it should execute.
     1337 * @param   cbCmdLineOpt    The size of the buffer @a pszCmdLineOpt points to.
     1338 */
     1339int __libc_Back_processGetDefaultShell(char *pszShell, size_t cbShell, size_t *poffShellArg,
     1340                                       char *pszCmdLineOpt, size_t cbCmdLineOpt);
     1341
    13261342/** @} */
    13271343
  • trunk/libc/include/klibc/initterm.h

    r3846 r3857  
    5454
    5555extern char ** _org_environ;
     56extern int __libc_gfNoUnix;
    5657
    5758__END_DECLS
  • trunk/libc/src/kNIX/Makefile.kmk

    r3845 r3857  
    7878    $(PATH_LIBC_SRC)/kNIX/b_processGetResourceLimit.c \
    7979    $(PATH_LIBC_SRC)/kNIX/b_processSetResourceLimit.c \
     80    $(PATH_LIBC_SRC)/kNIX/b_processGetDefaultShell.c \
    8081    $(PATH_LIBC_SRC)/kNIX/b_signalSendPid.c \
    8182    $(PATH_LIBC_SRC)/kNIX/fs.c \
  • trunk/libc/src/libc/io/popen.c

    r871 r3857  
    1010#include <fcntl.h>
    1111#include <errno.h>
     12#include <klibc/backend.h>
    1213
    1314static void restore (int org_handle, int org_private, int handle)
     
    2728  int i, org_handle, org_private;
    2829  FILE *f;
    29   const char *sh, *base, *opt;
     30  char szShell[260];
     31  size_t offShellArg;
     32  char szCmdLineOpt[8];
     33  int rc;
    3034
    3135  fcntl (pipe_local, F_SETFD, FD_CLOEXEC);
     
    5660      return NULL;
    5761    }
    58   sh = getenv ("EMXSHELL");
    59   if (sh == NULL)
    60     sh = getenv ("COMSPEC");
    61   if (sh == NULL)
     62  rc = __libc_Back_processGetDefaultShell (&szShell[0], sizeof(szShell), &offShellArg, &szCmdLineOpt[0], sizeof(szCmdLineOpt));
     63  if (rc != 0)
    6264    {
    6365      fclose (f);
    6466      restore (org_handle, org_private, handle);
    65       errno = ENOENT;
     67      errno = -rc;
    6668      return NULL;
    6769    }
    68   base = _getname (sh);
    69   if (stricmp (base, "cmd.exe") == 0 || stricmp (base, "4os2.exe") == 0)
    70     opt = "/c";
    71   else
    72     opt = "-c";
    73   i = spawnlp (P_NOWAIT, sh, sh, opt, command, (char *)0);
     70  i = spawnlp (P_NOWAIT, szShell, &szShell[offShellArg], szCmdLineOpt, command, (char *)0);
    7471  if (i == -1)
    7572    {
  • trunk/libc/src/libc/process/system.c

    r2254 r3857  
    1 /* system.c (emx+gcc) -- Copyright (c) 1990-1995 by Eberhard Mattes */
     1/* $Id: b_processSetResourceLimit.c 3770 2012-03-15 20:02:46Z bird $ */
     2/** @file
     3 * kNIX - get default shell for system() and popen().
     4 *
     5 * @copyright   Copyright (C) 2014 knut st. osmundsen <bird-klibc-spam-xiv@anduin.net>
     6 * @licenses    MIT, BSD2, BSD3, BSD4, LGPLv2.1, LGPLv3.
     7 */
    28
     9
     10/*******************************************************************************
     11*   Header Files                                                               *
     12*******************************************************************************/
     13#define __LIBC_LOG_GROUP __LIBC_LOG_GRP_PROCESS
    314#include "libc-alias.h"
    415#include <stdlib.h>
    5 #include <string.h>
    6 #include <process.h>
     16
    717#include <io.h>
    818#include <errno.h>
    9 #define __LIBC_LOG_GROUP __LIBC_LOG_GRP_PROCESS
    10 #include <InnoTekLIBC/logstrict.h>
     19#include <process.h>
     20#include <unistd.h>
     21#include <klibc/backend.h>
     22#include <klibc/logstrict.h>
    1123
    12 int _STD(system)(const char *name)
     24
     25int _STD(system)(const char *pszCmdLine)
    1326{
    14     LIBCLOG_ENTER("name=%s\n", name);
    15     int rc;
    16     const char *sh, *base, *opt;
    17 
    18     sh = getenv("EMXSHELL");
    19     if (sh == NULL)
    20         sh = getenv("COMSPEC");
    21     if (sh == NULL)
    22         sh = getenv("OS2_SHELL");
    23     if (sh == NULL)
    24         sh = getenv("SHELL");
    25     if (sh == NULL)
     27    LIBCLOG_ENTER("pszCmdLine=%s\n", pszCmdLine);
     28    char szCmdLineOpt[8];
     29    char szShell[260];
     30    size_t offShellArg;
     31    int rc = __libc_Back_processGetDefaultShell(szShell, sizeof(szShell), &offShellArg, szCmdLineOpt, sizeof(szCmdLineOpt));
     32    if (rc == 0)
    2633    {
    27         errno = ENOENT;
    28         LIBCLOG_ERROR_RETURN(-1, "ret -1 - no shell\n");
     34        LIBCLOG_MSG("using shell: %s\n", pszCmdLine);
     35        if (pszCmdLine == 0)
     36            rc = access(szShell, F_OK) == 0;
     37        else if (*pszCmdLine)
     38            rc = spawnlp(P_WAIT, szShell, &szShell[offShellArg], (void *)NULL);
     39        else
     40            rc = spawnlp(P_WAIT, szShell, &szShell[offShellArg], szCmdLineOpt, pszCmdLine, (void *)NULL);
    2941    }
    30     LIBCLOG_MSG("using shell: %s\n", sh);
    31     if (name == NULL)   /* Check for command interpreter */
    32     {
    33         LIBCLOG_MSG("check shell access\n");
    34         rc = access(sh, 0) == 0;
    35         LIBCLOG_RETURN_INT(rc);
    36     }
    37     if (*name == 0)
    38         rc = spawnlp(P_WAIT, sh, sh, (char *)0);
    3942    else
    4043    {
    41         base = _getname(sh);
    42         if (    stricmp(base, "cmd.exe") == 0
    43             ||  stricmp(base, "4os2.exe") == 0
    44             ||  stricmp(base, "command.com") == 0
    45             ||  stricmp(base, "4dos.com") == 0)
    46             opt = "/c";
    47         else
    48             opt = "-c";
    49         rc = spawnlp(P_WAIT, sh, sh, opt, name, (char *)0);
     44        errno = -rc;
     45        rc = -1;
    5046    }
    5147    LIBCLOG_RETURN_INT(rc);
    5248}
     49
  • trunk/libc/src/libc/startup/initterm.c

    r3846 r3857  
    6464static int32_t  g_cExecUsers = 0;
    6565
     66/** Whether the process was linked with -Zno-unix. */
     67int __libc_gfNoUnix = 0;
     68
    6669
    6770/*******************************************************************************
     
    148151     */
    149152    __libc_HeapVote(fFlags & __LIBC_INIT_FLAGS_MEM_HIGH);
    150     /*if (fFlags & __LIBC_INIT_FLAGS_NO_UNIX)
    151         __libc_gfNoUnix = 1;*/
     153    if (fFlags & __LIBC_INIT_FLAGS_PROCESS)
     154    {
     155        __libc_HeapEndVoting();
     156        __libc_gfNoUnix = !!(fFlags & __LIBC_INIT_FLAGS_NO_UNIX);
     157    }
    152158
    153159    /*
Note: See TracChangeset for help on using the changeset viewer.