home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / tcl / tcl+tk+t / tcl7.3l1 / tcl7 / tcl7.3 / tclUnixAZ.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-15  |  50.1 KB  |  1,999 lines

  1. /* 
  2.  * tclUnixAZ.c --
  3.  *
  4.  *    This file contains the top-level command procedures for
  5.  *    commands in the Tcl core that require UNIX facilities
  6.  *    such as files and process execution.  Much of the code
  7.  *    in this file is based on earlier versions contributed
  8.  *    by Karl Lehenbauer, Mark Diekhans and Peter da Silva.
  9.  *
  10.  * Copyright (c) 1991-1993 The Regents of the University of California.
  11.  * All rights reserved.
  12.  *
  13.  * Permission is hereby granted, without written agreement and without
  14.  * license or royalty fees, to use, copy, modify, and distribute this
  15.  * software and its documentation for any purpose, provided that the
  16.  * above copyright notice and the following two paragraphs appear in
  17.  * all copies of this software.
  18.  * 
  19.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  20.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  21.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  22.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23.  *
  24.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  25.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  26.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  27.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  28.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  29.  */
  30.  
  31. #ifndef lint
  32. static char rcsid[] = "$Header: /user6/ouster/tcl/RCS/tclUnixAZ.c,v 1.70 93/09/24 16:47:39 ouster Exp $ SPRITE (Berkeley)";
  33. #endif /* not lint */
  34.  
  35. #include "tclInt.h"
  36. #include "tclUnix.h"
  37.  
  38. /*
  39.  * The variable below caches the name of the current working directory
  40.  * in order to avoid repeated calls to getcwd.  The string is malloc-ed.
  41.  * NULL means the cache needs to be refreshed.
  42.  */
  43.  
  44. static char *currentDir =  NULL;
  45.  
  46. /*
  47.  * If the system doesn't define the EWOULDBLOCK errno, just #define it
  48.  * to a bogus value that will never occur.
  49.  */
  50.  
  51. #ifndef EWOULDBLOCK
  52. #define EWOULDBLOCK -1901
  53. #endif
  54.  
  55. /*
  56.  * Prototypes for local procedures defined in this file:
  57.  */
  58.  
  59. static int        CleanupChildren _ANSI_ARGS_((Tcl_Interp *interp,
  60.                 int numPids, int *pidPtr, int errorId,
  61.                 int keepNewline));
  62. static char *        GetFileType _ANSI_ARGS_((int mode));
  63. static char *        GetOpenMode _ANSI_ARGS_((Tcl_Interp *interp,
  64.                 char *string, int *modePtr));
  65. static int        StoreStatData _ANSI_ARGS_((Tcl_Interp *interp,
  66.                 char *varName, struct stat *statPtr));
  67.  
  68. /*
  69.  *----------------------------------------------------------------------
  70.  *
  71.  * Tcl_CdCmd --
  72.  *
  73.  *    This procedure is invoked to process the "cd" Tcl command.
  74.  *    See the user documentation for details on what it does.
  75.  *
  76.  * Results:
  77.  *    A standard Tcl result.
  78.  *
  79.  * Side effects:
  80.  *    See the user documentation.
  81.  *
  82.  *----------------------------------------------------------------------
  83.  */
  84.  
  85.     /* ARGSUSED */
  86. int
  87. Tcl_CdCmd(dummy, interp, argc, argv)
  88.     ClientData dummy;            /* Not used. */
  89.     Tcl_Interp *interp;            /* Current interpreter. */
  90.     int argc;                /* Number of arguments. */
  91.     char **argv;            /* Argument strings. */
  92. {
  93.     char *dirName;
  94.     Tcl_DString buffer;
  95.     int result;
  96.  
  97.     if (argc > 2) {
  98.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  99.         " dirName\"", (char *) NULL);
  100.     return TCL_ERROR;
  101.     }
  102.  
  103.     if (argc == 2) {
  104.     dirName = argv[1];
  105.     } else {
  106.     dirName = "~";
  107.     }
  108.     dirName = Tcl_TildeSubst(interp, dirName, &buffer);
  109.     if (dirName == NULL) {
  110.     return TCL_ERROR;
  111.     }
  112.     if (currentDir != NULL) {
  113.     ckfree(currentDir);
  114.     currentDir = NULL;
  115.     }
  116.     result = TCL_OK;
  117.     if (chdir(dirName) != 0) {
  118.     Tcl_AppendResult(interp, "couldn't change working directory to \"",
  119.         dirName, "\": ", Tcl_PosixError(interp), (char *) NULL);
  120.     result = TCL_ERROR;
  121.     }
  122.     Tcl_DStringFree(&buffer);
  123.     return result;
  124. }
  125.  
  126. /*
  127.  *----------------------------------------------------------------------
  128.  *
  129.  * Tcl_CloseCmd --
  130.  *
  131.  *    This procedure is invoked to process the "close" Tcl command.
  132.  *    See the user documentation for details on what it does.
  133.  *
  134.  * Results:
  135.  *    A standard Tcl result.
  136.  *
  137.  * Side effects:
  138.  *    See the user documentation.
  139.  *
  140.  *----------------------------------------------------------------------
  141.  */
  142.  
  143.     /* ARGSUSED */
  144. int
  145. Tcl_CloseCmd(dummy, interp, argc, argv)
  146.     ClientData dummy;            /* Not used. */
  147.     Tcl_Interp *interp;            /* Current interpreter. */
  148.     int argc;                /* Number of arguments. */
  149.     char **argv;            /* Argument strings. */
  150. {
  151.     OpenFile *oFilePtr;
  152.     int result = TCL_OK;
  153.     FILE *f;
  154.  
  155.     if (argc != 2) {
  156.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  157.         " fileId\"", (char *) NULL);
  158.     return TCL_ERROR;
  159.     }
  160.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  161.     return TCL_ERROR;
  162.     }
  163.     oFilePtr = tclOpenFiles[fileno(f)];
  164.     tclOpenFiles[fileno(f)] = NULL;
  165.  
  166.     /*
  167.      * First close the file (in the case of a process pipeline, there may
  168.      * be two files, one for the pipe at each end of the pipeline).
  169.      */
  170.  
  171.     if (oFilePtr->f2 != NULL) {
  172.     clearerr(oFilePtr->f2);
  173.     if (fclose(oFilePtr->f2) == EOF) {
  174.         Tcl_AppendResult(interp, "error closing \"", argv[1],
  175.             "\": ", Tcl_PosixError(interp), "\n", (char *) NULL);
  176.         result = TCL_ERROR;
  177.     }
  178.     }
  179.     clearerr(oFilePtr->f);
  180.     if (fclose(oFilePtr->f) == EOF) {
  181.     Tcl_AppendResult(interp, "error closing \"", argv[1],
  182.         "\": ", Tcl_PosixError(interp), "\n", (char *) NULL);
  183.     result = TCL_ERROR;
  184.     }
  185.  
  186.     /*
  187.      * If the file was a connection to a pipeline, clean up everything
  188.      * associated with the child processes.
  189.      */
  190.  
  191.     if (oFilePtr->numPids > 0) {
  192.     if (CleanupChildren(interp, oFilePtr->numPids, oFilePtr->pidPtr,
  193.         oFilePtr->errorId, 0) != TCL_OK) {
  194.         result = TCL_ERROR;
  195.     }
  196.     }
  197.  
  198.     ckfree((char *) oFilePtr);
  199.     return result;
  200. }
  201.  
  202. /*
  203.  *----------------------------------------------------------------------
  204.  *
  205.  * Tcl_EofCmd --
  206.  *
  207.  *    This procedure is invoked to process the "eof" Tcl command.
  208.  *    See the user documentation for details on what it does.
  209.  *
  210.  * Results:
  211.  *    A standard Tcl result.
  212.  *
  213.  * Side effects:
  214.  *    See the user documentation.
  215.  *
  216.  *----------------------------------------------------------------------
  217.  */
  218.  
  219.     /* ARGSUSED */
  220. int
  221. Tcl_EofCmd(notUsed, interp, argc, argv)
  222.     ClientData notUsed;            /* Not used. */
  223.     Tcl_Interp *interp;            /* Current interpreter. */
  224.     int argc;                /* Number of arguments. */
  225.     char **argv;            /* Argument strings. */
  226. {
  227.     FILE *f;
  228.  
  229.     if (argc != 2) {
  230.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  231.         " fileId\"", (char *) NULL);
  232.     return TCL_ERROR;
  233.     }
  234.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  235.     return TCL_ERROR;
  236.     }
  237.     if (feof(f)) {
  238.     interp->result = "1";
  239.     } else {
  240.     interp->result = "0";
  241.     }
  242.     return TCL_OK;
  243. }
  244.  
  245. /*
  246.  *----------------------------------------------------------------------
  247.  *
  248.  * Tcl_ExecCmd --
  249.  *
  250.  *    This procedure is invoked to process the "exec" Tcl command.
  251.  *    See the user documentation for details on what it does.
  252.  *
  253.  * Results:
  254.  *    A standard Tcl result.
  255.  *
  256.  * Side effects:
  257.  *    See the user documentation.
  258.  *
  259.  *----------------------------------------------------------------------
  260.  */
  261.  
  262.     /* ARGSUSED */
  263. int
  264. Tcl_ExecCmd(dummy, interp, argc, argv)
  265.     ClientData dummy;            /* Not used. */
  266.     Tcl_Interp *interp;            /* Current interpreter. */
  267.     int argc;                /* Number of arguments. */
  268.     char **argv;            /* Argument strings. */
  269. {
  270.     int outputId;            /* File id for output pipe.  -1
  271.                      * means command overrode. */
  272.     int errorId;            /* File id for temporary file
  273.                      * containing error output. */
  274.     int *pidPtr;
  275.     int numPids, result, keepNewline;
  276.     int firstWord;
  277.  
  278.     /*
  279.      * Check for a leading "-keepnewline" argument.
  280.      */
  281.  
  282.     keepNewline = 0;
  283.     for (firstWord = 1; (firstWord < argc) && (argv[firstWord][0] == '-');
  284.         firstWord++) {
  285.     if (strcmp(argv[firstWord], "-keepnewline") == 0) {
  286.         keepNewline = 1;
  287.     } else if (strcmp(argv[firstWord], "--") == 0) {
  288.         firstWord++;
  289.         break;
  290.     } else {
  291.         Tcl_AppendResult(interp, "bad switch \"", argv[firstWord],
  292.             "\": must be -keepnewline or --", (char *) NULL);
  293.         return TCL_ERROR;
  294.     }
  295.     }
  296.  
  297.     if (argc <= firstWord) {
  298.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  299.         " ?switches? arg ?arg ...?\"", (char *) NULL);
  300.     return TCL_ERROR;
  301.     }
  302.  
  303.     /*
  304.      * See if the command is to be run in background;  if so, create
  305.      * the command, detach it, and return a list of pids.
  306.      */
  307.  
  308.     if ((argv[argc-1][0] == '&') && (argv[argc-1][1] == 0)) {
  309.     int i;
  310.     char id[50];
  311.  
  312.     argc--;
  313.     argv[argc] = NULL;
  314.     numPids = Tcl_CreatePipeline(interp, argc-firstWord, argv+firstWord,
  315.         &pidPtr, (int *) NULL, (int *) NULL, (int *) NULL);
  316.     if (numPids < 0) {
  317.         return TCL_ERROR;
  318.     }
  319.     Tcl_DetachPids(numPids, pidPtr);
  320.     for (i = 0; i < numPids; i++) {
  321.         sprintf(id, "%d", pidPtr[i]);
  322.         Tcl_AppendElement(interp, id);
  323.     }
  324.     ckfree((char *) pidPtr);
  325.     return TCL_OK;
  326.     }
  327.  
  328.     /*
  329.      * Create the command's pipeline.
  330.      */
  331.  
  332.     numPids = Tcl_CreatePipeline(interp, argc-firstWord, argv+firstWord,
  333.         &pidPtr, (int *) NULL, &outputId, &errorId);
  334.     if (numPids < 0) {
  335.     return TCL_ERROR;
  336.     }
  337.  
  338.     /*
  339.      * Read the child's output (if any) and put it into the result.
  340.      */
  341.  
  342.     result = TCL_OK;
  343.     if (outputId != -1) {
  344.     while (1) {
  345. #        define BUFFER_SIZE 1000
  346.         char buffer[BUFFER_SIZE+1];
  347.         int count;
  348.     
  349.         count = read(outputId, buffer, (size_t) BUFFER_SIZE);
  350.     
  351.         if (count == 0) {
  352.         break;
  353.         }
  354.         if (count < 0) {
  355.         Tcl_ResetResult(interp);
  356.         Tcl_AppendResult(interp,
  357.             "error reading from output pipe: ",
  358.             Tcl_PosixError(interp), (char *) NULL);
  359.         result = TCL_ERROR;
  360.         break;
  361.         }
  362.         buffer[count] = 0;
  363.         Tcl_AppendResult(interp, buffer, (char *) NULL);
  364.     }
  365.     close(outputId);
  366.     }
  367.  
  368.     if (CleanupChildren(interp, numPids, pidPtr, errorId, keepNewline)
  369.         != TCL_OK) {
  370.     result = TCL_ERROR;
  371.     }
  372.     return result;
  373. }
  374.  
  375. /*
  376.  *----------------------------------------------------------------------
  377.  *
  378.  * Tcl_ExitCmd --
  379.  *
  380.  *    This procedure is invoked to process the "exit" Tcl command.
  381.  *    See the user documentation for details on what it does.
  382.  *
  383.  * Results:
  384.  *    A standard Tcl result.
  385.  *
  386.  * Side effects:
  387.  *    See the user documentation.
  388.  *
  389.  *----------------------------------------------------------------------
  390.  */
  391.  
  392.     /* ARGSUSED */
  393. int
  394. Tcl_ExitCmd(dummy, interp, argc, argv)
  395.     ClientData dummy;            /* Not used. */
  396.     Tcl_Interp *interp;            /* Current interpreter. */
  397.     int argc;                /* Number of arguments. */
  398.     char **argv;            /* Argument strings. */
  399. {
  400.     int value;
  401.  
  402.     if ((argc != 1) && (argc != 2)) {
  403.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  404.         " ?returnCode?\"", (char *) NULL);
  405.     return TCL_ERROR;
  406.     }
  407.     if (argc == 1) {
  408.     exit(0);
  409.     }
  410.     if (Tcl_GetInt(interp, argv[1], &value) != TCL_OK) {
  411.     return TCL_ERROR;
  412.     }
  413.     exit(value);
  414.     /*NOTREACHED*/
  415.     return TCL_OK;            /* Better not ever reach this! */
  416. }
  417.  
  418. /*
  419.  *----------------------------------------------------------------------
  420.  *
  421.  * Tcl_FileCmd --
  422.  *
  423.  *    This procedure is invoked to process the "file" Tcl command.
  424.  *    See the user documentation for details on what it does.
  425.  *
  426.  * Results:
  427.  *    A standard Tcl result.
  428.  *
  429.  * Side effects:
  430.  *    See the user documentation.
  431.  *
  432.  *----------------------------------------------------------------------
  433.  */
  434.  
  435.     /* ARGSUSED */
  436. int
  437. Tcl_FileCmd(dummy, interp, argc, argv)
  438.     ClientData dummy;            /* Not used. */
  439.     Tcl_Interp *interp;            /* Current interpreter. */
  440.     int argc;                /* Number of arguments. */
  441.     char **argv;            /* Argument strings. */
  442. {
  443.     char *p;
  444.     int length, statOp, result;
  445.     int mode = 0;            /* Initialized only to prevent
  446.                      * compiler warning message. */
  447.     struct stat statBuf;
  448.     char *fileName, c;
  449.     Tcl_DString buffer;
  450.  
  451.     if (argc < 3) {
  452.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  453.         " option name ?arg ...?\"", (char *) NULL);
  454.     return TCL_ERROR;
  455.     }
  456.     c = argv[1][0];
  457.     length = strlen(argv[1]);
  458.     result = TCL_OK;
  459.  
  460.     /*
  461.      * First handle operations on the file name.
  462.      */
  463.  
  464.     fileName = Tcl_TildeSubst(interp, argv[2], &buffer);
  465.     if (fileName == NULL) {
  466.     result = TCL_ERROR;
  467.     goto done;
  468.     }
  469.     if ((c == 'd') && (strncmp(argv[1], "dirname", length) == 0)) {
  470.     if (argc != 3) {
  471.         argv[1] = "dirname";
  472.         not3Args:
  473.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  474.             " ", argv[1], " name\"", (char *) NULL);
  475.         result = TCL_ERROR;
  476.         goto done;
  477.     }
  478.     p = strrchr(fileName, '/');
  479.     if (p == NULL) {
  480.         interp->result = ".";
  481.     } else if (p == fileName) {
  482.         interp->result = "/";
  483.     } else {
  484.         *p = 0;
  485.         Tcl_SetResult(interp, fileName, TCL_VOLATILE);
  486.         *p = '/';
  487.     }
  488.     goto done;
  489.     } else if ((c == 'r') && (strncmp(argv[1], "rootname", length) == 0)
  490.         && (length >= 2)) {
  491.     char *lastSlash;
  492.  
  493.     if (argc != 3) {
  494.         argv[1] = "rootname";
  495.         goto not3Args;
  496.     }
  497.     p = strrchr(fileName, '.');
  498.     lastSlash = strrchr(fileName, '/');
  499.     if ((p == NULL) || ((lastSlash != NULL) && (lastSlash > p))) {
  500.         Tcl_SetResult(interp, fileName, TCL_VOLATILE);
  501.     } else {
  502.         *p = 0;
  503.         Tcl_SetResult(interp, fileName, TCL_VOLATILE);
  504.         *p = '.';
  505.     }
  506.     goto done;
  507.     } else if ((c == 'e') && (strncmp(argv[1], "extension", length) == 0)
  508.         && (length >= 3)) {
  509.     char *lastSlash;
  510.  
  511.     if (argc != 3) {
  512.         argv[1] = "extension";
  513.         goto not3Args;
  514.     }
  515.     p = strrchr(fileName, '.');
  516.     lastSlash = strrchr(fileName, '/');
  517.     if ((p != NULL) && ((lastSlash == NULL) || (lastSlash < p))) {
  518.         Tcl_SetResult(interp, p, TCL_VOLATILE);
  519.     }
  520.     goto done;
  521.     } else if ((c == 't') && (strncmp(argv[1], "tail", length) == 0)
  522.         && (length >= 2)) {
  523.     if (argc != 3) {
  524.         argv[1] = "tail";
  525.         goto not3Args;
  526.     }
  527.     p = strrchr(fileName, '/');
  528.     if (p != NULL) {
  529.         Tcl_SetResult(interp, p+1, TCL_VOLATILE);
  530.     } else {
  531.         Tcl_SetResult(interp, fileName, TCL_VOLATILE);
  532.     }
  533.     goto done;
  534.     }
  535.  
  536.     /*
  537.      * Next, handle operations that can be satisfied with the "access"
  538.      * kernel call.
  539.      */
  540.  
  541.     if (fileName == NULL) {
  542.     result = TCL_ERROR;
  543.     goto done;
  544.     }
  545.     if ((c == 'r') && (strncmp(argv[1], "readable", length) == 0)
  546.         && (length >= 5)) {
  547.     if (argc != 3) {
  548.         argv[1] = "readable";
  549.         goto not3Args;
  550.     }
  551.     mode = R_OK;
  552.     checkAccess:
  553.     if (access(fileName, mode) == -1) {
  554.         interp->result = "0";
  555.     } else {
  556.         interp->result = "1";
  557.     }
  558.     goto done;
  559.     } else if ((c == 'w') && (strncmp(argv[1], "writable", length) == 0)) {
  560.     if (argc != 3) {
  561.         argv[1] = "writable";
  562.         goto not3Args;
  563.     }
  564.     mode = W_OK;
  565.     goto checkAccess;
  566.     } else if ((c == 'e') && (strncmp(argv[1], "executable", length) == 0)
  567.         && (length >= 3)) {
  568.     if (argc != 3) {
  569.         argv[1] = "executable";
  570.         goto not3Args;
  571.     }
  572.     mode = X_OK;
  573.     goto checkAccess;
  574.     } else if ((c == 'e') && (strncmp(argv[1], "exists", length) == 0)
  575.         && (length >= 3)) {
  576.     if (argc != 3) {
  577.         argv[1] = "exists";
  578.         goto not3Args;
  579.     }
  580.     mode = F_OK;
  581.     goto checkAccess;
  582.     }
  583.  
  584.     /*
  585.      * Lastly, check stuff that requires the file to be stat-ed.
  586.      */
  587.  
  588.     if ((c == 'a') && (strncmp(argv[1], "atime", length) == 0)) {
  589.     if (argc != 3) {
  590.         argv[1] = "atime";
  591.         goto not3Args;
  592.     }
  593.     if (stat(fileName, &statBuf) == -1) {
  594.         goto badStat;
  595.     }
  596.     sprintf(interp->result, "%ld", statBuf.st_atime);
  597.     goto done;
  598.     } else if ((c == 'i') && (strncmp(argv[1], "isdirectory", length) == 0)
  599.         && (length >= 3)) {
  600.     if (argc != 3) {
  601.         argv[1] = "isdirectory";
  602.         goto not3Args;
  603.     }
  604.     statOp = 2;
  605.     } else if ((c == 'i') && (strncmp(argv[1], "isfile", length) == 0)
  606.         && (length >= 3)) {
  607.     if (argc != 3) {
  608.         argv[1] = "isfile";
  609.         goto not3Args;
  610.     }
  611.     statOp = 1;
  612.     } else if ((c == 'l') && (strncmp(argv[1], "lstat", length) == 0)) {
  613.     if (argc != 4) {
  614.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  615.             " lstat name varName\"", (char *) NULL);
  616.         result = TCL_ERROR;
  617.         goto done;
  618.     }
  619.  
  620.     if (lstat(fileName, &statBuf) == -1) {
  621.         Tcl_AppendResult(interp, "couldn't lstat \"", argv[2],
  622.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  623.         result = TCL_ERROR;
  624.         goto done;
  625.     }
  626.     result = StoreStatData(interp, argv[3], &statBuf);
  627.     goto done;
  628.     } else if ((c == 'm') && (strncmp(argv[1], "mtime", length) == 0)) {
  629.     if (argc != 3) {
  630.         argv[1] = "mtime";
  631.         goto not3Args;
  632.     }
  633.     if (stat(fileName, &statBuf) == -1) {
  634.         goto badStat;
  635.     }
  636.     sprintf(interp->result, "%ld", statBuf.st_mtime);
  637.     goto done;
  638.     } else if ((c == 'o') && (strncmp(argv[1], "owned", length) == 0)) {
  639.     if (argc != 3) {
  640.         argv[1] = "owned";
  641.         goto not3Args;
  642.     }
  643.     statOp = 0;
  644.     } else if ((c == 'r') && (strncmp(argv[1], "readlink", length) == 0)
  645.         && (length >= 5)) {
  646.     char linkValue[MAXPATHLEN+1];
  647.     int linkLength;
  648.  
  649.     if (argc != 3) {
  650.         argv[1] = "readlink";
  651.         goto not3Args;
  652.     }
  653.  
  654.     /*
  655.      * If S_IFLNK isn't defined it means that the machine doesn't
  656.      * support symbolic links, so the file can't possibly be a
  657.      * symbolic link.  Generate an EINVAL error, which is what
  658.      * happens on machines that do support symbolic links when
  659.      * you invoke readlink on a file that isn't a symbolic link.
  660.      */
  661.  
  662. #ifndef S_IFLNK
  663.     linkLength = -1;
  664.     errno = EINVAL;
  665. #else
  666.     linkLength = readlink(fileName, linkValue, sizeof(linkValue) - 1);
  667. #endif /* S_IFLNK */
  668.     if (linkLength == -1) {
  669.         Tcl_AppendResult(interp, "couldn't readlink \"", argv[2],
  670.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  671.         result = TCL_ERROR;
  672.         goto done;
  673.     }
  674.     linkValue[linkLength] = 0;
  675.     Tcl_SetResult(interp, linkValue, TCL_VOLATILE);
  676.     goto done;
  677.     } else if ((c == 's') && (strncmp(argv[1], "size", length) == 0)
  678.         && (length >= 2)) {
  679.     if (argc != 3) {
  680.         argv[1] = "size";
  681.         goto not3Args;
  682.     }
  683.     if (stat(fileName, &statBuf) == -1) {
  684.         goto badStat;
  685.     }
  686.     sprintf(interp->result, "%ld", statBuf.st_size);
  687.     goto done;
  688.     } else if ((c == 's') && (strncmp(argv[1], "stat", length) == 0)
  689.         && (length >= 2)) {
  690.     if (argc != 4) {
  691.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  692.             " stat name varName\"", (char *) NULL);
  693.         result = TCL_ERROR;
  694.         goto done;
  695.     }
  696.  
  697.     if (stat(fileName, &statBuf) == -1) {
  698.         badStat:
  699.         Tcl_AppendResult(interp, "couldn't stat \"", argv[2],
  700.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  701.         result = TCL_ERROR;
  702.         goto done;
  703.     }
  704.     result = StoreStatData(interp, argv[3], &statBuf);
  705.     goto done;
  706.     } else if ((c == 't') && (strncmp(argv[1], "type", length) == 0)
  707.         && (length >= 2)) {
  708.     if (argc != 3) {
  709.         argv[1] = "type";
  710.         goto not3Args;
  711.     }
  712.     if (lstat(fileName, &statBuf) == -1) {
  713.         goto badStat;
  714.     }
  715.     interp->result = GetFileType((int) statBuf.st_mode);
  716.     goto done;
  717.     } else {
  718.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  719.         "\": should be atime, dirname, executable, exists, ",
  720.         "extension, isdirectory, isfile, lstat, mtime, owned, ",
  721.         "readable, readlink, ",
  722.         "root, size, stat, tail, type, ",
  723.         "or writable",
  724.         (char *) NULL);
  725.     result = TCL_ERROR;
  726.     goto done;
  727.     }
  728.     if (stat(fileName, &statBuf) == -1) {
  729.     interp->result = "0";
  730.     goto done;
  731.     }
  732.     switch (statOp) {
  733.     case 0:
  734.         mode = (geteuid() == statBuf.st_uid);
  735.         break;
  736.     case 1:
  737.         mode = S_ISREG(statBuf.st_mode);
  738.         break;
  739.     case 2:
  740.         mode = S_ISDIR(statBuf.st_mode);
  741.         break;
  742.     }
  743.     if (mode) {
  744.     interp->result = "1";
  745.     } else {
  746.     interp->result = "0";
  747.     }
  748.  
  749.     done:
  750.     Tcl_DStringFree(&buffer);
  751.     return result;
  752. }
  753.  
  754. /*
  755.  *----------------------------------------------------------------------
  756.  *
  757.  * StoreStatData --
  758.  *
  759.  *    This is a utility procedure that breaks out the fields of a
  760.  *    "stat" structure and stores them in textual form into the
  761.  *    elements of an associative array.
  762.  *
  763.  * Results:
  764.  *    Returns a standard Tcl return value.  If an error occurs then
  765.  *    a message is left in interp->result.
  766.  *
  767.  * Side effects:
  768.  *    Elements of the associative array given by "varName" are modified.
  769.  *
  770.  *----------------------------------------------------------------------
  771.  */
  772.  
  773. static int
  774. StoreStatData(interp, varName, statPtr)
  775.     Tcl_Interp *interp;            /* Interpreter for error reports. */
  776.     char *varName;            /* Name of associative array variable
  777.                      * in which to store stat results. */
  778.     struct stat *statPtr;        /* Pointer to buffer containing
  779.                      * stat data to store in varName. */
  780. {
  781.     char string[30];
  782.  
  783.     sprintf(string, "%d", statPtr->st_dev);
  784.     if (Tcl_SetVar2(interp, varName, "dev", string, TCL_LEAVE_ERR_MSG)
  785.         == NULL) {
  786.     return TCL_ERROR;
  787.     }
  788.     sprintf(string, "%d", statPtr->st_ino);
  789.     if (Tcl_SetVar2(interp, varName, "ino", string, TCL_LEAVE_ERR_MSG)
  790.         == NULL) {
  791.     return TCL_ERROR;
  792.     }
  793.     sprintf(string, "%d", statPtr->st_mode);
  794.     if (Tcl_SetVar2(interp, varName, "mode", string, TCL_LEAVE_ERR_MSG)
  795.         == NULL) {
  796.     return TCL_ERROR;
  797.     }
  798.     sprintf(string, "%d", statPtr->st_nlink);
  799.     if (Tcl_SetVar2(interp, varName, "nlink", string, TCL_LEAVE_ERR_MSG)
  800.         == NULL) {
  801.     return TCL_ERROR;
  802.     }
  803.     sprintf(string, "%d", statPtr->st_uid);
  804.     if (Tcl_SetVar2(interp, varName, "uid", string, TCL_LEAVE_ERR_MSG)
  805.         == NULL) {
  806.     return TCL_ERROR;
  807.     }
  808.     sprintf(string, "%d", statPtr->st_gid);
  809.     if (Tcl_SetVar2(interp, varName, "gid", string, TCL_LEAVE_ERR_MSG)
  810.         == NULL) {
  811.     return TCL_ERROR;
  812.     }
  813.     sprintf(string, "%ld", statPtr->st_size);
  814.     if (Tcl_SetVar2(interp, varName, "size", string, TCL_LEAVE_ERR_MSG)
  815.         == NULL) {
  816.     return TCL_ERROR;
  817.     }
  818.     sprintf(string, "%ld", statPtr->st_atime);
  819.     if (Tcl_SetVar2(interp, varName, "atime", string, TCL_LEAVE_ERR_MSG)
  820.         == NULL) {
  821.     return TCL_ERROR;
  822.     }
  823.     sprintf(string, "%ld", statPtr->st_mtime);
  824.     if (Tcl_SetVar2(interp, varName, "mtime", string, TCL_LEAVE_ERR_MSG)
  825.         == NULL) {
  826.     return TCL_ERROR;
  827.     }
  828.     sprintf(string, "%ld", statPtr->st_ctime);
  829.     if (Tcl_SetVar2(interp, varName, "ctime", string, TCL_LEAVE_ERR_MSG)
  830.         == NULL) {
  831.     return TCL_ERROR;
  832.     }
  833.     if (Tcl_SetVar2(interp, varName, "type",
  834.         GetFileType((int) statPtr->st_mode), TCL_LEAVE_ERR_MSG) == NULL) {
  835.     return TCL_ERROR;
  836.     }
  837.     return TCL_OK;
  838. }
  839.  
  840. /*
  841.  *----------------------------------------------------------------------
  842.  *
  843.  * GetFileType --
  844.  *
  845.  *    Given a mode word, returns a string identifying the type of a
  846.  *    file.
  847.  *
  848.  * Results:
  849.  *    A static text string giving the file type from mode.
  850.  *
  851.  * Side effects:
  852.  *    None.
  853.  *
  854.  *----------------------------------------------------------------------
  855.  */
  856.  
  857. static char *
  858. GetFileType(mode)
  859.     int mode;
  860. {
  861.     if (S_ISREG(mode)) {
  862.     return "file";
  863.     } else if (S_ISDIR(mode)) {
  864.     return "directory";
  865.     } else if (S_ISCHR(mode)) {
  866.     return "characterSpecial";
  867.     } else if (S_ISBLK(mode)) {
  868.     return "blockSpecial";
  869.     } else if (S_ISFIFO(mode)) {
  870.     return "fifo";
  871.     } else if (S_ISLNK(mode)) {
  872.     return "link";
  873.     } else if (S_ISSOCK(mode)) {
  874.     return "socket";
  875.     }
  876.     return "unknown";
  877. }
  878.  
  879. /*
  880.  *----------------------------------------------------------------------
  881.  *
  882.  * Tcl_FlushCmd --
  883.  *
  884.  *    This procedure is invoked to process the "flush" Tcl command.
  885.  *    See the user documentation for details on what it does.
  886.  *
  887.  * Results:
  888.  *    A standard Tcl result.
  889.  *
  890.  * Side effects:
  891.  *    See the user documentation.
  892.  *
  893.  *----------------------------------------------------------------------
  894.  */
  895.  
  896.     /* ARGSUSED */
  897. int
  898. Tcl_FlushCmd(notUsed, interp, argc, argv)
  899.     ClientData notUsed;            /* Not used. */
  900.     Tcl_Interp *interp;            /* Current interpreter. */
  901.     int argc;                /* Number of arguments. */
  902.     char **argv;            /* Argument strings. */
  903. {
  904.     FILE *f;
  905.  
  906.     if (argc != 2) {
  907.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  908.         " fileId\"", (char *) NULL);
  909.     return TCL_ERROR;
  910.     }
  911.     if (Tcl_GetOpenFile(interp, argv[1], 1, 1, &f) != TCL_OK) {
  912.     return TCL_ERROR;
  913.     }
  914.     clearerr(f);
  915.     if (fflush(f) == EOF) {
  916.     Tcl_AppendResult(interp, "error flushing \"", argv[1],
  917.         "\": ", Tcl_PosixError(interp), (char *) NULL);
  918.     return TCL_ERROR;
  919.     }
  920.     return TCL_OK;
  921. }
  922.  
  923. /*
  924.  *----------------------------------------------------------------------
  925.  *
  926.  * Tcl_GetsCmd --
  927.  *
  928.  *    This procedure is invoked to process the "gets" Tcl command.
  929.  *    See the user documentation for details on what it does.
  930.  *
  931.  * Results:
  932.  *    A standard Tcl result.
  933.  *
  934.  * Side effects:
  935.  *    See the user documentation.
  936.  *
  937.  *----------------------------------------------------------------------
  938.  */
  939.  
  940.     /* ARGSUSED */
  941. int
  942. Tcl_GetsCmd(notUsed, interp, argc, argv)
  943.     ClientData notUsed;            /* Not used. */
  944.     Tcl_Interp *interp;            /* Current interpreter. */
  945.     int argc;                /* Number of arguments. */
  946.     char **argv;            /* Argument strings. */
  947. {
  948. #   define BUF_SIZE 200
  949.     char buffer[BUF_SIZE+1];
  950.     int totalCount, done, flags;
  951.     FILE *f;
  952.  
  953.     if ((argc != 2) && (argc != 3)) {
  954.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  955.         " fileId ?varName?\"", (char *) NULL);
  956.     return TCL_ERROR;
  957.     }
  958.     if (Tcl_GetOpenFile(interp, argv[1], 0, 1, &f) != TCL_OK) {
  959.     return TCL_ERROR;
  960.     }
  961.  
  962.     /*
  963.      * We can't predict how large a line will be, so read it in
  964.      * pieces, appending to the current result or to a variable.
  965.      */
  966.  
  967.     totalCount = 0;
  968.     done = 0;
  969.     flags = 0;
  970.     clearerr(f);
  971.     while (!done) {
  972.     register int c, count;
  973.     register char *p;
  974.  
  975.     for (p = buffer, count = 0; count < BUF_SIZE-1; count++, p++) {
  976.         c = getc(f);
  977.         if (c == EOF) {
  978.         if (ferror(f)) {
  979.             /*
  980.              * If the file is in non-blocking mode, return any
  981.              * bytes that were read before a block would occur.
  982.              */
  983.  
  984.             if ((errno == EWOULDBLOCK)
  985.                 && ((count > 0 || totalCount > 0))) {
  986.             done = 1;
  987.             break;
  988.             }
  989.             Tcl_ResetResult(interp);
  990.             Tcl_AppendResult(interp, "error reading \"", argv[1],
  991.                 "\": ", Tcl_PosixError(interp), (char *) NULL);
  992.             return TCL_ERROR;
  993.         } else if (feof(f)) {
  994.             if ((totalCount == 0) && (count == 0)) {
  995.             totalCount = -1;
  996.             }
  997.             done = 1;
  998.             break;
  999.         }
  1000.         }
  1001.         if (c == '\n') {
  1002.         done = 1;
  1003.         break;
  1004.         }
  1005.         *p = c;
  1006.     }
  1007.     *p = 0;
  1008.     if (argc == 2) {
  1009.         Tcl_AppendResult(interp, buffer, (char *) NULL);
  1010.     } else {
  1011.         if (Tcl_SetVar(interp, argv[2], buffer, flags|TCL_LEAVE_ERR_MSG)
  1012.             == NULL) {
  1013.         return TCL_ERROR;
  1014.         }
  1015.         flags = TCL_APPEND_VALUE;
  1016.     }
  1017.     totalCount += count;
  1018.     }
  1019.  
  1020.     if (argc == 3) {
  1021.     sprintf(interp->result, "%d", totalCount);
  1022.     }
  1023.     return TCL_OK;
  1024. }
  1025.  
  1026. /*
  1027.  *----------------------------------------------------------------------
  1028.  *
  1029.  * Tcl_OpenCmd --
  1030.  *
  1031.  *    This procedure is invoked to process the "open" Tcl command.
  1032.  *    See the user documentation for details on what it does.
  1033.  *
  1034.  * Results:
  1035.  *    A standard Tcl result.
  1036.  *
  1037.  * Side effects:
  1038.  *    See the user documentation.
  1039.  *
  1040.  *----------------------------------------------------------------------
  1041.  */
  1042.  
  1043.     /* ARGSUSED */
  1044. int
  1045. Tcl_OpenCmd(notUsed, interp, argc, argv)
  1046.     ClientData notUsed;            /* Not used. */
  1047.     Tcl_Interp *interp;            /* Current interpreter. */
  1048.     int argc;                /* Number of arguments. */
  1049.     char **argv;            /* Argument strings. */
  1050. {
  1051.     int pipeline, fd, mode, prot, readWrite, permissions;
  1052.     char *access;
  1053.     FILE *f, *f2;
  1054.  
  1055.     if ((argc < 2) || (argc > 4)) {
  1056.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1057.         " filename ?access? ?permissions?\"", (char *) NULL);
  1058.     return TCL_ERROR;
  1059.     }
  1060.     prot = 0666;
  1061.     if (argc == 2) {
  1062.     mode = O_RDONLY;
  1063.     access = "r";
  1064.     } else {
  1065.     access = GetOpenMode(interp, argv[2], &mode);
  1066.     if (access == NULL) {
  1067.         return TCL_ERROR;
  1068.     }
  1069.     if (argc == 4) {
  1070.         if (Tcl_GetInt(interp, argv[3], &prot) != TCL_OK) {
  1071.         return TCL_ERROR;
  1072.         }
  1073.     }
  1074.     }
  1075.  
  1076.     f = f2 = NULL;
  1077.     readWrite = mode & (O_RDWR|O_RDONLY|O_WRONLY);
  1078.     if (readWrite == O_RDONLY) {
  1079.     permissions = TCL_FILE_READABLE;
  1080.     } else if (readWrite == O_WRONLY) {
  1081.     permissions = TCL_FILE_WRITABLE;
  1082.     } else {
  1083.     permissions = TCL_FILE_READABLE|TCL_FILE_WRITABLE;
  1084.     }
  1085.  
  1086.     pipeline = 0;
  1087.     if (argv[1][0] == '|') {
  1088.     pipeline = 1;
  1089.     }
  1090.  
  1091.     /*
  1092.      * Open the file or create a process pipeline.
  1093.      */
  1094.  
  1095.     if (!pipeline) {
  1096.     char *fileName;
  1097.     Tcl_DString buffer;
  1098.  
  1099.     fileName = Tcl_TildeSubst(interp, argv[1], &buffer);
  1100.     if (fileName == NULL) {
  1101.         return TCL_ERROR;
  1102.     }
  1103.     fd = open(fileName, mode, prot);
  1104.     Tcl_DStringFree(&buffer);
  1105.     if (fd < 0) {
  1106.         Tcl_AppendResult(interp, "couldn't open \"", argv[1],
  1107.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  1108.         return TCL_ERROR;
  1109.     }
  1110.     f = fdopen(fd, access);
  1111.     if (f == NULL) {
  1112.         close(fd);
  1113.         return TCL_ERROR;
  1114.     }
  1115.     Tcl_EnterFile(interp, f, permissions);
  1116.     } else {
  1117.     int *inPipePtr, *outPipePtr;
  1118.     int cmdArgc, inPipe, outPipe, numPids, *pidPtr, errorId;
  1119.     char **cmdArgv;
  1120.     OpenFile *oFilePtr;
  1121.  
  1122.     if (Tcl_SplitList(interp, argv[1]+1, &cmdArgc, &cmdArgv) != TCL_OK) {
  1123.         return TCL_ERROR;
  1124.     }
  1125.     inPipePtr = (permissions & TCL_FILE_WRITABLE) ? &inPipe : NULL;
  1126.     outPipePtr = (permissions & TCL_FILE_READABLE) ? &outPipe : NULL;
  1127.     inPipe = outPipe = errorId = -1;
  1128.     numPids = Tcl_CreatePipeline(interp, cmdArgc, cmdArgv,
  1129.         &pidPtr, inPipePtr, outPipePtr, &errorId);
  1130.     ckfree((char *) cmdArgv);
  1131.     if (numPids < 0) {
  1132.         pipelineError:
  1133.         if (f != NULL) {
  1134.         fclose(f);
  1135.         }
  1136.         if (f2 != NULL) {
  1137.         fclose(f2);
  1138.         }
  1139.         if (numPids > 0) {
  1140.         Tcl_DetachPids(numPids, pidPtr);
  1141.         ckfree((char *) pidPtr);
  1142.         }
  1143.         if (errorId != -1) {
  1144.         close(errorId);
  1145.         }
  1146.         return TCL_ERROR;
  1147.     }
  1148.     if (permissions & TCL_FILE_READABLE) {
  1149.         if (outPipe == -1) {
  1150.         if (inPipe != -1) {
  1151.             close(inPipe);
  1152.         }
  1153.         Tcl_AppendResult(interp, "can't read output from command:",
  1154.             " standard output was redirected", (char *) NULL);
  1155.         goto pipelineError;
  1156.         }
  1157.         f = fdopen(outPipe, "r");
  1158.     }
  1159.     if (permissions & TCL_FILE_WRITABLE) {
  1160.         if (inPipe == -1) {
  1161.         Tcl_AppendResult(interp, "can't write input to command:",
  1162.             " standard input was redirected", (char *) NULL);
  1163.         goto pipelineError;
  1164.         }
  1165.         if (f != NULL) {
  1166.         f2 = fdopen(inPipe, "w");
  1167.         } else {
  1168.         f = fdopen(inPipe, "w");
  1169.         }
  1170.     }
  1171.     Tcl_EnterFile(interp, f, permissions);
  1172.     oFilePtr = tclOpenFiles[fileno(f)];
  1173.     oFilePtr->f2 = f2;
  1174.     oFilePtr->numPids = numPids;
  1175.     oFilePtr->pidPtr = pidPtr;
  1176.     oFilePtr->errorId = errorId;
  1177.     }
  1178.     return TCL_OK;
  1179. }
  1180.  
  1181. /*
  1182.  *----------------------------------------------------------------------
  1183.  *
  1184.  * GetOpenMode --
  1185.  *
  1186.  *    description.
  1187.  *
  1188.  * Results:
  1189.  *    Normally, sets *modePtr to an access mode for passing to "open",
  1190.  *    and returns a string that can be used as the access mode in a
  1191.  *    subsequent call to "fdopen".  If an error occurs, then returns
  1192.  *    NULL and sets interp->result to an error message.
  1193.  *
  1194.  * Side effects:
  1195.  *    None.
  1196.  *
  1197.  * Special note:
  1198.  *    This code is based on a prototype implementation contributed
  1199.  *    by Mark Diekhans.
  1200.  *
  1201.  *----------------------------------------------------------------------
  1202.  */
  1203.  
  1204. static char *
  1205. GetOpenMode(interp, string, modePtr)
  1206.     Tcl_Interp *interp;            /* Interpreter to use for error
  1207.                      * reporting. */
  1208.     char *string;            /* Mode string, e.g. "r+" or
  1209.                      * "RDONLY CREAT". */
  1210.     int *modePtr;            /* Where to store mode corresponding
  1211.                      * to string. */
  1212. {
  1213.     int mode, modeArgc, c, i, gotRW;
  1214.     char **modeArgv, *flag;
  1215. #define RW_MODES (O_RDONLY|O_WRONLY|O_RDWR)
  1216.  
  1217.     /*
  1218.      * Check for the simpler fopen-like access modes (e.g. "r").  They
  1219.      * are distinguished from the POSIX access modes by the presence
  1220.      * of a lower-case first letter.
  1221.      */
  1222.  
  1223.     mode = 0;
  1224.     if (islower(UCHAR(string[0]))) {
  1225.     switch (string[0]) {
  1226.         case 'r':
  1227.         mode = O_RDONLY;
  1228.         break;
  1229.         case 'w':
  1230.         mode = O_WRONLY|O_CREAT|O_TRUNC;
  1231.         break;
  1232.         case 'a':
  1233.         mode = O_WRONLY|O_CREAT|O_APPEND;
  1234.         break;
  1235.         default:
  1236.         error:
  1237.         Tcl_AppendResult(interp,
  1238.             "illegal access mode \"", string, "\"", (char *) NULL);
  1239.         return NULL;
  1240.     }
  1241.     if (string[1] == '+') {
  1242.         mode &= ~(O_RDONLY|O_WRONLY);
  1243.         mode |= O_RDWR;
  1244.         if (string[2] != 0) {
  1245.         goto error;
  1246.         }
  1247.     } else if (string[1] != 0) {
  1248.         goto error;
  1249.     }
  1250.     *modePtr = mode;
  1251.     return string;
  1252.     }
  1253.  
  1254.     /*
  1255.      * The access modes are specified using a list of POSIX modes
  1256.      * such as O_CREAT.
  1257.      */
  1258.  
  1259.     if (Tcl_SplitList(interp, string, &modeArgc, &modeArgv) != TCL_OK) {
  1260.     Tcl_AddErrorInfo(interp, "\n    while processing open access modes \"");
  1261.     Tcl_AddErrorInfo(interp, string);
  1262.     Tcl_AddErrorInfo(interp, "\"");
  1263.     return NULL;
  1264.     }
  1265.     gotRW = 0;
  1266.     for (i = 0; i < modeArgc; i++) {
  1267.     flag = modeArgv[i];
  1268.     c = flag[0];
  1269.     if ((c == 'R') && (strcmp(flag, "RDONLY") == 0)) {
  1270.         mode = (mode & ~RW_MODES) | O_RDONLY;
  1271.         gotRW = 1;
  1272.     } else if ((c == 'W') && (strcmp(flag, "WRONLY") == 0)) {
  1273.         mode = (mode & ~RW_MODES) | O_WRONLY;
  1274.         gotRW = 1;
  1275.     } else if ((c == 'R') && (strcmp(flag, "RDWR") == 0)) {
  1276.         mode = (mode & ~RW_MODES) | O_RDWR;
  1277.         gotRW = 1;
  1278.     } else if ((c == 'A') && (strcmp(flag, "APPEND") == 0)) {
  1279.         mode |= O_APPEND;
  1280.     } else if ((c == 'C') && (strcmp(flag, "CREAT") == 0)) {
  1281.         mode |= O_CREAT;
  1282.     } else if ((c == 'E') && (strcmp(flag, "EXCL") == 0)) {
  1283.         mode |= O_EXCL;
  1284.     } else if ((c == 'N') && (strcmp(flag, "NOCTTY") == 0)) {
  1285. #ifdef O_NOCTTY
  1286.         mode |= O_NOCTTY;
  1287. #else
  1288.         Tcl_AppendResult(interp, "access mode \"", flag,
  1289.             "\" not supported by this system", (char *) NULL);
  1290.         ckfree((char *) modeArgv);
  1291.         return NULL;
  1292. #endif
  1293.     } else if ((c == 'N') && (strcmp(flag, "NONBLOCK") == 0)) {
  1294. #ifdef O_NONBLOCK
  1295.         mode |= O_NONBLOCK;
  1296. #else
  1297.         mode |= O_NDELAY;
  1298. #endif
  1299.     } else if ((c == 'T') && (strcmp(flag, "TRUNC") == 0)) {
  1300.         mode |= O_TRUNC;
  1301.     } else {
  1302.         Tcl_AppendResult(interp, "invalid access mode \"", flag,
  1303.             "\": must be RDONLY, WRONLY, RDWR, APPEND, CREAT",
  1304.             " EXCL, NOCTTY, NONBLOCK, or TRUNC", (char *) NULL);
  1305.         ckfree((char *) modeArgv);
  1306.         return NULL;
  1307.     }
  1308.     }
  1309.     ckfree((char *) modeArgv);
  1310.     if (!gotRW) {
  1311.     Tcl_AppendResult(interp, "access mode must include either",
  1312.         " RDONLY, WRONLY, or RDWR", (char *) NULL);
  1313.     return NULL;
  1314.     }
  1315.     *modePtr = mode;
  1316.  
  1317.     /*
  1318.      * The calculation of fdopen access mode below isn't really correct,
  1319.      * but it doesn't have to be.  All it has to do is to disinguish
  1320.      * read and write permissions, plus indicate append mode.
  1321.      */
  1322.  
  1323.     i = mode & RW_MODES;
  1324.     if (i == O_RDONLY) {
  1325.     return "r";
  1326.     }
  1327.     if (mode & O_APPEND) {
  1328.     if (i == O_WRONLY) {
  1329.         return "a";
  1330.     } else {
  1331.         return "a+";
  1332.     }
  1333.     }
  1334.     if (i == O_WRONLY) {
  1335.     return "w";
  1336.     }
  1337.     return "r+";
  1338. }
  1339.  
  1340. /*
  1341.  *----------------------------------------------------------------------
  1342.  *
  1343.  * Tcl_PidCmd --
  1344.  *
  1345.  *    This procedure is invoked to process the "pid" Tcl command.
  1346.  *    See the user documentation for details on what it does.
  1347.  *
  1348.  * Results:
  1349.  *    A standard Tcl result.
  1350.  *
  1351.  * Side effects:
  1352.  *    See the user documentation.
  1353.  *
  1354.  *----------------------------------------------------------------------
  1355.  */
  1356.  
  1357.     /* ARGSUSED */
  1358. int
  1359. Tcl_PidCmd(dummy, interp, argc, argv)
  1360.     ClientData dummy;            /* Not used. */
  1361.     Tcl_Interp *interp;            /* Current interpreter. */
  1362.     int argc;                /* Number of arguments. */
  1363.     char **argv;            /* Argument strings. */
  1364. {
  1365.     FILE *f;
  1366.     OpenFile *oFilePtr;
  1367.     int i;
  1368.     char string[50];
  1369.  
  1370.     if (argc > 2) {
  1371.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  1372.         argv[0], " ?fileId?\"", (char *) NULL);
  1373.     return TCL_ERROR;
  1374.     }
  1375.     if (argc == 1) {
  1376.     sprintf(interp->result, "%d", getpid());
  1377.     } else {
  1378.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  1379.         return TCL_ERROR;
  1380.     }
  1381.     oFilePtr = tclOpenFiles[fileno(f)];
  1382.     for (i = 0; i < oFilePtr->numPids; i++) {
  1383.         sprintf(string, "%d", oFilePtr->pidPtr[i]);
  1384.         Tcl_AppendElement(interp, string);
  1385.     }
  1386.     }
  1387.     return TCL_OK;
  1388. }
  1389.  
  1390. /*
  1391.  *----------------------------------------------------------------------
  1392.  *
  1393.  * Tcl_PutsCmd --
  1394.  *
  1395.  *    This procedure is invoked to process the "puts" Tcl command.
  1396.  *    See the user documentation for details on what it does.
  1397.  *
  1398.  * Results:
  1399.  *    A standard Tcl result.
  1400.  *
  1401.  * Side effects:
  1402.  *    See the user documentation.
  1403.  *
  1404.  *----------------------------------------------------------------------
  1405.  */
  1406.  
  1407.     /* ARGSUSED */
  1408. int
  1409. Tcl_PutsCmd(dummy, interp, argc, argv)
  1410.     ClientData dummy;            /* Not used. */
  1411.     Tcl_Interp *interp;            /* Current interpreter. */
  1412.     int argc;                /* Number of arguments. */
  1413.     char **argv;            /* Argument strings. */
  1414. {
  1415.     FILE *f;
  1416.     int i, newline;
  1417.     char *fileId;
  1418.  
  1419.     i = 1;
  1420.     newline = 1;
  1421.     if ((argc >= 2) && (strcmp(argv[1], "-nonewline") == 0)) {
  1422.     newline = 0;
  1423.     i++;
  1424.     }
  1425.     if ((i < (argc-3)) || (i >= argc)) {
  1426.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1427.         "\" ?-nonewline? ?fileId? string", (char *) NULL);
  1428.     return TCL_ERROR;
  1429.     }
  1430.  
  1431.     /*
  1432.      * The code below provides backwards compatibility with an old
  1433.      * form of the command that is no longer recommended or documented.
  1434.      */
  1435.  
  1436.     if (i == (argc-3)) {
  1437.     if (strncmp(argv[i+2], "nonewline", strlen(argv[i+2])) != 0) {
  1438.         Tcl_AppendResult(interp, "bad argument \"", argv[i+2],
  1439.             "\": should be \"nonewline\"", (char *) NULL);
  1440.         return TCL_ERROR;
  1441.     }
  1442.     newline = 0;
  1443.     }
  1444.     if (i == (argc-1)) {
  1445.     fileId = "stdout";
  1446.     } else {
  1447.     fileId = argv[i];
  1448.     i++;
  1449.     }
  1450.  
  1451.     if (Tcl_GetOpenFile(interp, fileId, 1, 1, &f) != TCL_OK) {
  1452.     return TCL_ERROR;
  1453.     }
  1454.  
  1455.     clearerr(f);
  1456.     fputs(argv[i], f);
  1457.     if (newline) {
  1458.     fputc('\n', f);
  1459.     }
  1460.     if (ferror(f)) {
  1461.     Tcl_AppendResult(interp, "error writing \"", fileId,
  1462.         "\": ", Tcl_PosixError(interp), (char *) NULL);
  1463.     return TCL_ERROR;
  1464.     }
  1465.     return TCL_OK;
  1466. }
  1467.  
  1468. /*
  1469.  *----------------------------------------------------------------------
  1470.  *
  1471.  * Tcl_PwdCmd --
  1472.  *
  1473.  *    This procedure is invoked to process the "pwd" Tcl command.
  1474.  *    See the user documentation for details on what it does.
  1475.  *
  1476.  * Results:
  1477.  *    A standard Tcl result.
  1478.  *
  1479.  * Side effects:
  1480.  *    See the user documentation.
  1481.  *
  1482.  *----------------------------------------------------------------------
  1483.  */
  1484.  
  1485.     /* ARGSUSED */
  1486. int
  1487. Tcl_PwdCmd(dummy, interp, argc, argv)
  1488.     ClientData dummy;            /* Not used. */
  1489.     Tcl_Interp *interp;            /* Current interpreter. */
  1490.     int argc;                /* Number of arguments. */
  1491.     char **argv;            /* Argument strings. */
  1492. {
  1493.     char buffer[MAXPATHLEN+1];
  1494.  
  1495.     if (argc != 1) {
  1496.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  1497.         argv[0], "\"", (char *) NULL);
  1498.     return TCL_ERROR;
  1499.     }
  1500.     if (currentDir == NULL) {
  1501.     if (getcwd(buffer, MAXPATHLEN+1) == NULL) {
  1502.         if (errno == ERANGE) {
  1503.         interp->result = "working directory name is too long";
  1504.         } else {
  1505.         Tcl_AppendResult(interp,
  1506.             "error getting working directory name: ",
  1507.             Tcl_PosixError(interp), (char *) NULL);
  1508.         }
  1509.         return TCL_ERROR;
  1510.     }
  1511.     currentDir = (char *) ckalloc((unsigned) (strlen(buffer) + 1));
  1512.     strcpy(currentDir, buffer);
  1513.     }
  1514.     interp->result = currentDir;
  1515.     return TCL_OK;
  1516. }
  1517.  
  1518. /*
  1519.  *----------------------------------------------------------------------
  1520.  *
  1521.  * Tcl_ReadCmd --
  1522.  *
  1523.  *    This procedure is invoked to process the "read" Tcl command.
  1524.  *    See the user documentation for details on what it does.
  1525.  *
  1526.  * Results:
  1527.  *    A standard Tcl result.
  1528.  *
  1529.  * Side effects:
  1530.  *    See the user documentation.
  1531.  *
  1532.  *----------------------------------------------------------------------
  1533.  */
  1534.  
  1535.     /* ARGSUSED */
  1536. int
  1537. Tcl_ReadCmd(dummy, interp, argc, argv)
  1538.     ClientData dummy;            /* Not used. */
  1539.     Tcl_Interp *interp;            /* Current interpreter. */
  1540.     int argc;                /* Number of arguments. */
  1541.     char **argv;            /* Argument strings. */
  1542. {
  1543.     int bytesLeft, bytesRead, count;
  1544. #define READ_BUF_SIZE 4096
  1545.     char buffer[READ_BUF_SIZE+1];
  1546.     int newline, i;
  1547.     FILE *f;
  1548.  
  1549.     if ((argc != 2) && (argc != 3)) {
  1550.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1551.         " fileId ?numBytes?\" or \"", argv[0],
  1552.         " ?-nonewline? fileId\"", (char *) NULL);
  1553.     return TCL_ERROR;
  1554.     }
  1555.     i = 1;
  1556.     newline = 1;
  1557.     if ((argc == 3) && (strcmp(argv[1], "-nonewline") == 0)) {
  1558.     newline = 0;
  1559.     i++;
  1560.     }
  1561.     if (Tcl_GetOpenFile(interp, argv[i], 0, 1, &f) != TCL_OK) {
  1562.     return TCL_ERROR;
  1563.     }
  1564.  
  1565.     /*
  1566.      * Compute how many bytes to read, and see whether the final
  1567.      * newline should be dropped.
  1568.      */
  1569.  
  1570.     if ((argc >= (i + 2)) && isdigit(UCHAR(argv[i+1][0]))) {
  1571.     if (Tcl_GetInt(interp, argv[i+1], &bytesLeft) != TCL_OK) {
  1572.         return TCL_ERROR;
  1573.     }
  1574.     } else {
  1575.     bytesLeft = 1<<30;
  1576.  
  1577.     /*
  1578.      * The code below provides backward compatibility for an
  1579.      * archaic earlier version of this command.
  1580.      */
  1581.  
  1582.     if (argc >= (i + 2)) {
  1583.         if (strncmp(argv[i+1], "nonewline", strlen(argv[i+1])) == 0) {
  1584.         newline = 0;
  1585.         } else {
  1586.         Tcl_AppendResult(interp, "bad argument \"", argv[i+1],
  1587.             "\": should be \"nonewline\"", (char *) NULL);
  1588.         return TCL_ERROR;
  1589.         }
  1590.     }
  1591.     }
  1592.  
  1593.     /*
  1594.      * Read the file in one or more chunks.
  1595.      */
  1596.  
  1597.     bytesRead = 0;
  1598.     clearerr(f);
  1599.     while (bytesLeft > 0) {
  1600.     count = READ_BUF_SIZE;
  1601.     if (bytesLeft < READ_BUF_SIZE) {
  1602.         count = bytesLeft;
  1603.     }
  1604.     count = fread(buffer, 1, count, f);
  1605.     if (ferror(f)) {
  1606.         /*
  1607.          * If the file is in non-blocking mode, break out of the
  1608.          * loop and return any bytes that were read.
  1609.          */
  1610.  
  1611.         if ((errno == EWOULDBLOCK) && ((count > 0) || (bytesRead > 0))) {
  1612.         clearerr(f);
  1613.         bytesLeft = count;
  1614.         } else {
  1615.         Tcl_ResetResult(interp);
  1616.         Tcl_AppendResult(interp, "error reading \"", argv[i],
  1617.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  1618.         return TCL_ERROR;
  1619.         }
  1620.     }
  1621.     if (count == 0) {
  1622.         break;
  1623.     }
  1624.     buffer[count] = 0;
  1625.     Tcl_AppendResult(interp, buffer, (char *) NULL);
  1626.     bytesLeft -= count;
  1627.     bytesRead += count;
  1628.     }
  1629.     if ((newline == 0) && (bytesRead > 0)
  1630.         && (interp->result[bytesRead-1] == '\n')) {
  1631.     interp->result[bytesRead-1] = 0;
  1632.     }
  1633.     return TCL_OK;
  1634. }
  1635.  
  1636. /*
  1637.  *----------------------------------------------------------------------
  1638.  *
  1639.  * Tcl_SeekCmd --
  1640.  *
  1641.  *    This procedure is invoked to process the "seek" Tcl command.
  1642.  *    See the user documentation for details on what it does.
  1643.  *
  1644.  * Results:
  1645.  *    A standard Tcl result.
  1646.  *
  1647.  * Side effects:
  1648.  *    See the user documentation.
  1649.  *
  1650.  *----------------------------------------------------------------------
  1651.  */
  1652.  
  1653.     /* ARGSUSED */
  1654. int
  1655. Tcl_SeekCmd(notUsed, interp, argc, argv)
  1656.     ClientData notUsed;            /* Not used. */
  1657.     Tcl_Interp *interp;            /* Current interpreter. */
  1658.     int argc;                /* Number of arguments. */
  1659.     char **argv;            /* Argument strings. */
  1660. {
  1661.     FILE *f;
  1662.     int offset, mode;
  1663.  
  1664.     if ((argc != 3) && (argc != 4)) {
  1665.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1666.         " fileId offset ?origin?\"", (char *) NULL);
  1667.     return TCL_ERROR;
  1668.     }
  1669.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  1670.     return TCL_ERROR;
  1671.     }
  1672.     if (Tcl_GetInt(interp, argv[2], &offset) != TCL_OK) {
  1673.     return TCL_ERROR;
  1674.     }
  1675.     mode = SEEK_SET;
  1676.     if (argc == 4) {
  1677.     int length;
  1678.     char c;
  1679.  
  1680.     length = strlen(argv[3]);
  1681.     c = argv[3][0];
  1682.     if ((c == 's') && (strncmp(argv[3], "start", length) == 0)) {
  1683.         mode = SEEK_SET;
  1684.     } else if ((c == 'c') && (strncmp(argv[3], "current", length) == 0)) {
  1685.         mode = SEEK_CUR;
  1686.     } else if ((c == 'e') && (strncmp(argv[3], "end", length) == 0)) {
  1687.         mode = SEEK_END;
  1688.     } else {
  1689.         Tcl_AppendResult(interp, "bad origin \"", argv[3],
  1690.             "\": should be start, current, or end", (char *) NULL);
  1691.         return TCL_ERROR;
  1692.     }
  1693.     }
  1694.     clearerr(f);
  1695.     if (fseek(f, (long) offset, mode) == -1) {
  1696.     Tcl_AppendResult(interp, "error during seek: ",
  1697.         Tcl_PosixError(interp), (char *) NULL);
  1698.     return TCL_ERROR;
  1699.     }
  1700.  
  1701.     return TCL_OK;
  1702. }
  1703.  
  1704. /*
  1705.  *----------------------------------------------------------------------
  1706.  *
  1707.  * Tcl_SourceCmd --
  1708.  *
  1709.  *    This procedure is invoked to process the "source" Tcl command.
  1710.  *    See the user documentation for details on what it does.
  1711.  *
  1712.  * Results:
  1713.  *    A standard Tcl result.
  1714.  *
  1715.  * Side effects:
  1716.  *    See the user documentation.
  1717.  *
  1718.  *----------------------------------------------------------------------
  1719.  */
  1720.  
  1721.     /* ARGSUSED */
  1722. int
  1723. Tcl_SourceCmd(dummy, interp, argc, argv)
  1724.     ClientData dummy;            /* Not used. */
  1725.     Tcl_Interp *interp;            /* Current interpreter. */
  1726.     int argc;                /* Number of arguments. */
  1727.     char **argv;            /* Argument strings. */
  1728. {
  1729.     if (argc != 2) {
  1730.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1731.         " fileName\"", (char *) NULL);
  1732.     return TCL_ERROR;
  1733.     }
  1734.     return Tcl_EvalFile(interp, argv[1]);
  1735. }
  1736.  
  1737. /*
  1738.  *----------------------------------------------------------------------
  1739.  *
  1740.  * Tcl_TellCmd --
  1741.  *
  1742.  *    This procedure is invoked to process the "tell" Tcl command.
  1743.  *    See the user documentation for details on what it does.
  1744.  *
  1745.  * Results:
  1746.  *    A standard Tcl result.
  1747.  *
  1748.  * Side effects:
  1749.  *    See the user documentation.
  1750.  *
  1751.  *----------------------------------------------------------------------
  1752.  */
  1753.  
  1754.     /* ARGSUSED */
  1755. int
  1756. Tcl_TellCmd(notUsed, interp, argc, argv)
  1757.     ClientData notUsed;            /* Not used. */
  1758.     Tcl_Interp *interp;            /* Current interpreter. */
  1759.     int argc;                /* Number of arguments. */
  1760.     char **argv;            /* Argument strings. */
  1761. {
  1762.     FILE *f;
  1763.  
  1764.     if (argc != 2) {
  1765.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1766.         " fileId\"", (char *) NULL);
  1767.     return TCL_ERROR;
  1768.     }
  1769.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  1770.     return TCL_ERROR;
  1771.     }
  1772.     sprintf(interp->result, "%d", ftell(f));
  1773.     return TCL_OK;
  1774. }
  1775.  
  1776. /*
  1777.  *----------------------------------------------------------------------
  1778.  *
  1779.  * Tcl_TimeCmd --
  1780.  *
  1781.  *    This procedure is invoked to process the "time" Tcl command.
  1782.  *    See the user documentation for details on what it does.
  1783.  *
  1784.  * Results:
  1785.  *    A standard Tcl result.
  1786.  *
  1787.  * Side effects:
  1788.  *    See the user documentation.
  1789.  *
  1790.  *----------------------------------------------------------------------
  1791.  */
  1792.  
  1793.     /* ARGSUSED */
  1794. int
  1795. Tcl_TimeCmd(dummy, interp, argc, argv)
  1796.     ClientData dummy;            /* Not used. */
  1797.     Tcl_Interp *interp;            /* Current interpreter. */
  1798.     int argc;                /* Number of arguments. */
  1799.     char **argv;            /* Argument strings. */
  1800. {
  1801.     int count, i, result;
  1802.     double timePer;
  1803. #if NO_GETTOD
  1804.     struct tms dummy2;
  1805.     long start, stop;
  1806. #else
  1807.     struct timeval start, stop;
  1808.     struct timezone tz;
  1809.     int micros;
  1810. #endif
  1811.  
  1812.     if (argc == 2) {
  1813.     count = 1;
  1814.     } else if (argc == 3) {
  1815.     if (Tcl_GetInt(interp, argv[2], &count) != TCL_OK) {
  1816.         return TCL_ERROR;
  1817.     }
  1818.     } else {
  1819.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1820.         " command ?count?\"", (char *) NULL);
  1821.     return TCL_ERROR;
  1822.     }
  1823. #if NO_GETTOD
  1824.     start = times(&dummy2);
  1825. #else
  1826.     gettimeofday(&start, &tz);
  1827. #endif
  1828.     for (i = count ; i > 0; i--) {
  1829.     result = Tcl_Eval(interp, argv[1]);
  1830.     if (result != TCL_OK) {
  1831.         if (result == TCL_ERROR) {
  1832.         char msg[60];
  1833.         sprintf(msg, "\n    (\"time\" body line %d)",
  1834.             interp->errorLine);
  1835.         Tcl_AddErrorInfo(interp, msg);
  1836.         }
  1837.         return result;
  1838.     }
  1839.     }
  1840. #if NO_GETTOD
  1841.     stop = times(&dummy2);
  1842.     timePer = (((double) (stop - start))*1000000.0)/CLK_TCK;
  1843. #else
  1844.     gettimeofday(&stop, &tz);
  1845.     micros = (stop.tv_sec - start.tv_sec)*1000000
  1846.         + (stop.tv_usec - start.tv_usec);
  1847.     timePer = micros;
  1848. #endif
  1849.     Tcl_ResetResult(interp);
  1850.     sprintf(interp->result, "%.0f microseconds per iteration", timePer/count);
  1851.     return TCL_OK;
  1852. }
  1853.  
  1854. /*
  1855.  *----------------------------------------------------------------------
  1856.  *
  1857.  * CleanupChildren --
  1858.  *
  1859.  *    This is a utility procedure used to wait for child processes
  1860.  *    to exit, record information about abnormal exits, and then
  1861.  *    collect any stderr output generated by them.
  1862.  *
  1863.  * Results:
  1864.  *    The return value is a standard Tcl result.  If anything at
  1865.  *    weird happened with the child processes, TCL_ERROR is returned
  1866.  *    and a message is left in interp->result.
  1867.  *
  1868.  * Side effects:
  1869.  *    If the last character of interp->result is a newline, then it
  1870.  *    is removed unless keepNewline is non-zero.  File errorId gets
  1871.  *    closed, and pidPtr is freed back to the storage allocator.
  1872.  *
  1873.  *----------------------------------------------------------------------
  1874.  */
  1875.  
  1876. static int
  1877. CleanupChildren(interp, numPids, pidPtr, errorId, keepNewline)
  1878.     Tcl_Interp *interp;        /* Used for error messages. */
  1879.     int numPids;        /* Number of entries in pidPtr array. */
  1880.     int *pidPtr;        /* Array of process ids of children. */
  1881.     int errorId;        /* File descriptor index for file containing
  1882.                  * stderr output from pipeline.  -1 means
  1883.                  * there isn't any stderr output. */
  1884.     int keepNewline;        /* Non-zero means don't discard trailing
  1885.                  * newline. */
  1886. {
  1887.     int result = TCL_OK;
  1888.     int i, pid, length, abnormalExit;
  1889.     WAIT_STATUS_TYPE waitStatus;
  1890.  
  1891.     abnormalExit = 0;
  1892.     for (i = 0; i < numPids; i++) {
  1893.     pid = waitpid(pidPtr[i], (int *) &waitStatus, 0);
  1894.     if (pid == -1) {
  1895.         Tcl_AppendResult(interp, "error waiting for process to exit: ",
  1896.             Tcl_PosixError(interp), (char *) NULL);
  1897.         continue;
  1898.     }
  1899.  
  1900.     /*
  1901.      * Create error messages for unusual process exits.  An
  1902.      * extra newline gets appended to each error message, but
  1903.      * it gets removed below (in the same fashion that an
  1904.      * extra newline in the command's output is removed).
  1905.      */
  1906.  
  1907.     if (!WIFEXITED(waitStatus) || (WEXITSTATUS(waitStatus) != 0)) {
  1908.         char msg1[20], msg2[20];
  1909.  
  1910.         result = TCL_ERROR;
  1911.         sprintf(msg1, "%d", pid);
  1912.         if (WIFEXITED(waitStatus)) {
  1913.         sprintf(msg2, "%d", WEXITSTATUS(waitStatus));
  1914.         Tcl_SetErrorCode(interp, "CHILDSTATUS", msg1, msg2,
  1915.             (char *) NULL);
  1916.         abnormalExit = 1;
  1917.         } else if (WIFSIGNALED(waitStatus)) {
  1918.         char *p;
  1919.     
  1920.         p = Tcl_SignalMsg((int) (WTERMSIG(waitStatus)));
  1921.         Tcl_SetErrorCode(interp, "CHILDKILLED", msg1,
  1922.             Tcl_SignalId((int) (WTERMSIG(waitStatus))), p,
  1923.             (char *) NULL);
  1924.         Tcl_AppendResult(interp, "child killed: ", p, "\n",
  1925.             (char *) NULL);
  1926.         } else if (WIFSTOPPED(waitStatus)) {
  1927.         char *p;
  1928.  
  1929.         p = Tcl_SignalMsg((int) (WSTOPSIG(waitStatus)));
  1930.         Tcl_SetErrorCode(interp, "CHILDSUSP", msg1,
  1931.             Tcl_SignalId((int) (WSTOPSIG(waitStatus))), p, (char *) NULL);
  1932.         Tcl_AppendResult(interp, "child suspended: ", p, "\n",
  1933.             (char *) NULL);
  1934.         } else {
  1935.         Tcl_AppendResult(interp,
  1936.             "child wait status didn't make sense\n",
  1937.             (char *) NULL);
  1938.         }
  1939.     }
  1940.     }
  1941.     ckfree((char *) pidPtr);
  1942.  
  1943.     /*
  1944.      * Read the standard error file.  If there's anything there,
  1945.      * then return an error and add the file's contents to the result
  1946.      * string.
  1947.      */
  1948.  
  1949.     if (errorId >= 0) {
  1950.     while (1) {
  1951. #        define BUFFER_SIZE 1000
  1952.         char buffer[BUFFER_SIZE+1];
  1953.         int count;
  1954.     
  1955.         count = read(errorId, buffer, (size_t) BUFFER_SIZE);
  1956.     
  1957.         if (count == 0) {
  1958.         break;
  1959.         }
  1960.         result = TCL_ERROR;
  1961.         if (count < 0) {
  1962.         Tcl_AppendResult(interp,
  1963.             "error reading stderr output file: ",
  1964.             Tcl_PosixError(interp), (char *) NULL);
  1965.         break;
  1966.         }
  1967.         buffer[count] = 0;
  1968.         Tcl_AppendResult(interp, buffer, (char *) NULL);
  1969.     }
  1970.     close(errorId);
  1971.     }
  1972.  
  1973.     /*
  1974.      * If a child exited abnormally but didn't output any error information
  1975.      * at all, generate an error message here.
  1976.      */
  1977.  
  1978.     if (abnormalExit && (*interp->result == 0)) {
  1979.     Tcl_AppendResult(interp, "child process exited abnormally",
  1980.         (char *) NULL);
  1981.     }
  1982.  
  1983.     /*
  1984.      * If the last character of interp->result is a newline, then remove
  1985.      * the newline character (the newline would just confuse things).
  1986.      * Special hack: must replace the old terminating null character
  1987.      * as a signal to Tcl_AppendResult et al. that we've mucked with
  1988.      * the string.
  1989.      */
  1990.  
  1991.     length = strlen(interp->result);
  1992.     if (!keepNewline && (length > 0) && (interp->result[length-1] == '\n')) {
  1993.     interp->result[length-1] = '\0';
  1994.     interp->result[length] = 'x';
  1995.     }
  1996.  
  1997.     return result;
  1998. }
  1999.