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