os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/generic/tclFCmd.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2  * tclFCmd.c
     3  *
     4  *      This file implements the generic portion of file manipulation 
     5  *      subcommands of the "file" command. 
     6  *
     7  * Copyright (c) 1996-1998 Sun Microsystems, Inc.
     8  *
     9  * See the file "license.terms" for information on usage and redistribution
    10  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    11  *
    12  * RCS: @(#) $Id: tclFCmd.c,v 1.20.2.2 2005/08/17 17:46:36 hobbs Exp $
    13  */
    14 
    15 #include "tclInt.h"
    16 #include "tclPort.h"
    17 
    18 /*
    19  * Declarations for local procedures defined in this file:
    20  */
    21 
    22 static int		CopyRenameOneFile _ANSI_ARGS_((Tcl_Interp *interp,
    23 			    Tcl_Obj *srcPathPtr, Tcl_Obj *destPathPtr, 
    24 			    int copyFlag, int force));
    25 static Tcl_Obj *	FileBasename _ANSI_ARGS_((Tcl_Interp *interp,
    26 			    Tcl_Obj *pathPtr));
    27 static int		FileCopyRename _ANSI_ARGS_((Tcl_Interp *interp,
    28 			    int objc, Tcl_Obj *CONST objv[], int copyFlag));
    29 static int		FileForceOption _ANSI_ARGS_((Tcl_Interp *interp,
    30 			    int objc, Tcl_Obj *CONST objv[], int *forcePtr));
    31 
    32 /*
    33  *---------------------------------------------------------------------------
    34  *
    35  * TclFileRenameCmd
    36  *
    37  *	This procedure implements the "rename" subcommand of the "file"
    38  *      command.  Filename arguments need to be translated to native
    39  *	format before being passed to platform-specific code that
    40  *	implements rename functionality.
    41  *
    42  * Results:
    43  *	A standard Tcl result.
    44  *
    45  * Side effects:
    46  *	See the user documentation.
    47  *
    48  *---------------------------------------------------------------------------
    49  */
    50 
    51 int
    52 TclFileRenameCmd(interp, objc, objv)
    53     Tcl_Interp *interp;		/* Interp for error reporting. */
    54     int objc;			/* Number of arguments. */
    55     Tcl_Obj *CONST objv[];	/* Argument strings passed to Tcl_FileCmd. */
    56 {
    57     return FileCopyRename(interp, objc, objv, 0);
    58 }
    59 
    60 /*
    61  *---------------------------------------------------------------------------
    62  *
    63  * TclFileCopyCmd
    64  *
    65  *	This procedure implements the "copy" subcommand of the "file"
    66  *	command.  Filename arguments need to be translated to native
    67  *	format before being passed to platform-specific code that
    68  *	implements copy functionality.
    69  *
    70  * Results:
    71  *	A standard Tcl result.
    72  *
    73  * Side effects:
    74  *	See the user documentation.
    75  *
    76  *---------------------------------------------------------------------------
    77  */
    78 
    79 int
    80 TclFileCopyCmd(interp, objc, objv)
    81     Tcl_Interp *interp;		/* Used for error reporting */
    82     int objc;			/* Number of arguments. */
    83     Tcl_Obj *CONST objv[];	/* Argument strings passed to Tcl_FileCmd. */
    84 {
    85     return FileCopyRename(interp, objc, objv, 1);
    86 }
    87 
    88 /*
    89  *---------------------------------------------------------------------------
    90  *
    91  * FileCopyRename --
    92  *
    93  *	Performs the work of TclFileRenameCmd and TclFileCopyCmd.
    94  *	See comments for those procedures.
    95  *
    96  * Results:
    97  *	See above.
    98  *
    99  * Side effects:
   100  *	See above.
   101  *
   102  *---------------------------------------------------------------------------
   103  */
   104 
   105 static int
   106 FileCopyRename(interp, objc, objv, copyFlag)
   107     Tcl_Interp *interp;		/* Used for error reporting. */
   108     int objc;			/* Number of arguments. */
   109     Tcl_Obj *CONST objv[];	/* Argument strings passed to Tcl_FileCmd. */
   110     int copyFlag;		/* If non-zero, copy source(s).  Otherwise,
   111 				 * rename them. */
   112 {
   113     int i, result, force;
   114     Tcl_StatBuf statBuf; 
   115     Tcl_Obj *target;
   116 
   117     i = FileForceOption(interp, objc - 2, objv + 2, &force);
   118     if (i < 0) {
   119 	return TCL_ERROR;
   120     }
   121     i += 2;
   122     if ((objc - i) < 2) {
   123 	Tcl_AppendResult(interp, "wrong # args: should be \"", 
   124 		Tcl_GetString(objv[0]), " ", Tcl_GetString(objv[1]), 
   125 		" ?options? source ?source ...? target\"", 
   126 		(char *) NULL);
   127 	return TCL_ERROR;
   128     }
   129 
   130     /*
   131      * If target doesn't exist or isn't a directory, try the copy/rename.
   132      * More than 2 arguments is only valid if the target is an existing
   133      * directory.
   134      */
   135 
   136     target = objv[objc - 1];
   137     if (Tcl_FSConvertToPathType(interp, target) != TCL_OK) {
   138 	return TCL_ERROR;
   139     }
   140 
   141     result = TCL_OK;
   142 
   143     /*
   144      * Call Tcl_FSStat() so that if target is a symlink that points to a
   145      * directory we will put the sources in that directory instead of
   146      * overwriting the symlink.
   147      */
   148 
   149     if ((Tcl_FSStat(target, &statBuf) != 0) || !S_ISDIR(statBuf.st_mode)) {
   150 	if ((objc - i) > 2) {
   151 	    errno = ENOTDIR;
   152 	    Tcl_PosixError(interp);
   153 	    Tcl_AppendResult(interp, "error ",
   154 		    ((copyFlag) ? "copying" : "renaming"), ": target \"",
   155 		    Tcl_GetString(target), "\" is not a directory", 
   156 		    (char *) NULL);
   157 	    result = TCL_ERROR;
   158 	} else {
   159 	    /*
   160 	     * Even though already have target == translated(objv[i+1]),
   161 	     * pass the original argument down, so if there's an error, the
   162 	     * error message will reflect the original arguments.
   163 	     */
   164 
   165 	    result = CopyRenameOneFile(interp, objv[i], objv[i + 1], copyFlag,
   166 		    force);
   167 	}
   168 	return result;
   169     }
   170     
   171     /*
   172      * Move each source file into target directory.  Extract the basename
   173      * from each source, and append it to the end of the target path.
   174      */
   175 
   176     for ( ; i < objc - 1; i++) {
   177 	Tcl_Obj *jargv[2];
   178 	Tcl_Obj *source, *newFileName;
   179 	Tcl_Obj *temp;
   180 	
   181 	source = FileBasename(interp, objv[i]);
   182 	if (source == NULL) {
   183 	    result = TCL_ERROR;
   184 	    break;
   185 	}
   186 	jargv[0] = objv[objc - 1];
   187 	jargv[1] = source;
   188 	temp = Tcl_NewListObj(2, jargv);
   189 	newFileName = Tcl_FSJoinPath(temp, -1);
   190 	Tcl_IncrRefCount(newFileName);
   191 	result = CopyRenameOneFile(interp, objv[i], newFileName, copyFlag,
   192 		force);
   193 	Tcl_DecrRefCount(newFileName);
   194 	Tcl_DecrRefCount(temp);
   195 	Tcl_DecrRefCount(source);
   196 
   197 	if (result == TCL_ERROR) {
   198 	    break;
   199 	}
   200     }
   201     return result;
   202 }
   203 
   204 /*
   205  *---------------------------------------------------------------------------
   206  *
   207  * TclFileMakeDirsCmd
   208  *
   209  *	This procedure implements the "mkdir" subcommand of the "file"
   210  *      command.  Filename arguments need to be translated to native
   211  *	format before being passed to platform-specific code that
   212  *	implements mkdir functionality.
   213  *
   214  * Results:
   215  *	A standard Tcl result.
   216  *
   217  * Side effects:
   218  *	See the user documentation.
   219  *
   220  *----------------------------------------------------------------------
   221  */
   222 int
   223 TclFileMakeDirsCmd(interp, objc, objv)
   224     Tcl_Interp *interp;		/* Used for error reporting. */
   225     int objc;			/* Number of arguments */
   226     Tcl_Obj *CONST objv[];	/* Argument strings passed to Tcl_FileCmd. */
   227 {
   228     Tcl_Obj *errfile;
   229     int result, i, j, pobjc;
   230     Tcl_Obj *split = NULL;
   231     Tcl_Obj *target = NULL;
   232     Tcl_StatBuf statBuf;
   233 
   234     errfile = NULL;
   235 
   236     result = TCL_OK;
   237     for (i = 2; i < objc; i++) {
   238 	if (Tcl_FSConvertToPathType(interp, objv[i]) != TCL_OK) {
   239 	    result = TCL_ERROR;
   240 	    break;
   241 	}
   242 
   243 	split = Tcl_FSSplitPath(objv[i],&pobjc);
   244 	if (pobjc == 0) {
   245 	    errno = ENOENT;
   246 	    errfile = objv[i];
   247 	    break;
   248 	}
   249 	for (j = 0; j < pobjc; j++) {
   250 	    target = Tcl_FSJoinPath(split, j + 1);
   251 	    Tcl_IncrRefCount(target);
   252 	    /*
   253 	     * Call Tcl_FSStat() so that if target is a symlink that
   254 	     * points to a directory we will create subdirectories in
   255 	     * that directory.
   256 	     */
   257 
   258 	    if (Tcl_FSStat(target, &statBuf) == 0) {
   259 		if (!S_ISDIR(statBuf.st_mode)) {
   260 		    errno = EEXIST;
   261 		    errfile = target;
   262 		    goto done;
   263 		}
   264 	    } else if (errno != ENOENT) {
   265 		/*
   266 		 * If Tcl_FSStat() failed and the error is anything
   267 		 * other than non-existence of the target, throw the
   268 		 * error.
   269 		 */
   270 		errfile = target;
   271 		goto done;
   272 	    } else if (Tcl_FSCreateDirectory(target) != TCL_OK) {
   273 		/*
   274 		 * Create might have failed because of being in a race
   275 		 * condition with another process trying to create the
   276 		 * same subdirectory.
   277 		 */
   278 		if (errno == EEXIST) {
   279 		    if ((Tcl_FSStat(target, &statBuf) == 0)
   280 			    && S_ISDIR(statBuf.st_mode)) {
   281 			/*
   282 			 * It is a directory that wasn't there before,
   283 			 * so keep going without error.
   284 			 */
   285 			Tcl_ResetResult(interp);
   286 		    } else {
   287 			errfile = target;
   288 			goto done;
   289 		    }
   290 		} else {
   291 		    errfile = target;
   292 		    goto done;
   293 		}
   294 	    }
   295  	    /* Forget about this sub-path */
   296 	    Tcl_DecrRefCount(target);
   297 	    target = NULL;
   298 	}
   299 	Tcl_DecrRefCount(split);
   300 	split = NULL;
   301     }
   302 
   303     done:
   304     if (errfile != NULL) {
   305 	Tcl_AppendResult(interp, "can't create directory \"",
   306 		Tcl_GetString(errfile), "\": ", Tcl_PosixError(interp), 
   307 		(char *) NULL);
   308 	result = TCL_ERROR;
   309     }
   310     if (split != NULL) {
   311 	Tcl_DecrRefCount(split);
   312     }
   313     if (target != NULL) {
   314 	Tcl_DecrRefCount(target);
   315     }
   316     return result;
   317 }
   318 
   319 /*
   320  *----------------------------------------------------------------------
   321  *
   322  * TclFileDeleteCmd
   323  *
   324  *	This procedure implements the "delete" subcommand of the "file"
   325  *      command.
   326  *
   327  * Results:
   328  *	A standard Tcl result.
   329  *
   330  * Side effects:
   331  *	See the user documentation.
   332  *
   333  *----------------------------------------------------------------------
   334  */
   335 
   336 int
   337 TclFileDeleteCmd(interp, objc, objv)
   338     Tcl_Interp *interp;		/* Used for error reporting */
   339     int objc;			/* Number of arguments */
   340     Tcl_Obj *CONST objv[];	/* Argument strings passed to Tcl_FileCmd. */
   341 {
   342     int i, force, result;
   343     Tcl_Obj *errfile;
   344     Tcl_Obj *errorBuffer = NULL;
   345     
   346     i = FileForceOption(interp, objc - 2, objv + 2, &force);
   347     if (i < 0) {
   348 	return TCL_ERROR;
   349     }
   350     i += 2;
   351     if ((objc - i) < 1) {
   352 	Tcl_AppendResult(interp, "wrong # args: should be \"", 
   353 		Tcl_GetString(objv[0]), " ", Tcl_GetString(objv[1]), 
   354 		" ?options? file ?file ...?\"", (char *) NULL);
   355 	return TCL_ERROR;
   356     }
   357 
   358     errfile = NULL;
   359     result = TCL_OK;
   360 
   361     for ( ; i < objc; i++) {
   362 	Tcl_StatBuf statBuf;
   363 
   364 	errfile = objv[i];
   365 	if (Tcl_FSConvertToPathType(interp, objv[i]) != TCL_OK) {
   366 	    result = TCL_ERROR;
   367 	    goto done;
   368 	}
   369 
   370 	/*
   371 	 * Call lstat() to get info so can delete symbolic link itself.
   372 	 */
   373 
   374 	if (Tcl_FSLstat(objv[i], &statBuf) != 0) {
   375 	    /*
   376 	     * Trying to delete a file that does not exist is not
   377 	     * considered an error, just a no-op
   378 	     */
   379 
   380 	    if (errno != ENOENT) {
   381 		result = TCL_ERROR;
   382 	    }
   383 	} else if (S_ISDIR(statBuf.st_mode)) {
   384 	    /* 
   385 	     * We own a reference count on errorBuffer, if it was set
   386 	     * as a result of this call. 
   387 	     */
   388 	    result = Tcl_FSRemoveDirectory(objv[i], force, &errorBuffer);
   389 	    if (result != TCL_OK) {
   390 		if ((force == 0) && (errno == EEXIST)) {
   391 		    Tcl_AppendResult(interp, "error deleting \"", 
   392 			    Tcl_GetString(objv[i]),
   393 			    "\": directory not empty", (char *) NULL);
   394 		    Tcl_PosixError(interp);
   395 		    goto done;
   396 		}
   397 
   398 		/* 
   399 		 * If possible, use the untranslated name for the file.
   400 		 */
   401 		 
   402 		errfile = errorBuffer;
   403 		/* FS supposed to check between translated objv and errfile */
   404 		if (Tcl_FSEqualPaths(objv[i], errfile)) {
   405 		    errfile = objv[i];
   406 		}
   407 	    }
   408 	} else {
   409 	    result = Tcl_FSDeleteFile(objv[i]);
   410 	}
   411 	
   412 	if (result != TCL_OK) {
   413 	    result = TCL_ERROR;
   414 	    /* 
   415 	     * It is important that we break on error, otherwise we
   416 	     * might end up owning reference counts on numerous
   417 	     * errorBuffers.
   418 	     */
   419 	    break;
   420 	}
   421     }
   422     if (result != TCL_OK) {
   423 	if (errfile == NULL) {
   424 	    /* 
   425 	     * We try to accomodate poor error results from our 
   426 	     * Tcl_FS calls 
   427 	     */
   428 	    Tcl_AppendResult(interp, "error deleting unknown file: ", 
   429 		    Tcl_PosixError(interp), (char *) NULL);
   430 	} else {
   431 	    Tcl_AppendResult(interp, "error deleting \"", 
   432 		    Tcl_GetString(errfile), "\": ", 
   433 		    Tcl_PosixError(interp), (char *) NULL);
   434 	}
   435     } 
   436     done:
   437     if (errorBuffer != NULL) {
   438 	Tcl_DecrRefCount(errorBuffer);
   439     }
   440     return result;
   441 }
   442 
   443 /*
   444  *---------------------------------------------------------------------------
   445  *
   446  * CopyRenameOneFile
   447  *
   448  *	Copies or renames specified source file or directory hierarchy
   449  *	to the specified target.  
   450  *
   451  * Results:
   452  *	A standard Tcl result.
   453  *
   454  * Side effects:
   455  *	Target is overwritten if the force flag is set.  Attempting to
   456  *	copy/rename a file onto a directory or a directory onto a file
   457  *	will always result in an error.  
   458  *
   459  *----------------------------------------------------------------------
   460  */
   461 
   462 static int
   463 CopyRenameOneFile(interp, source, target, copyFlag, force) 
   464     Tcl_Interp *interp;		/* Used for error reporting. */
   465     Tcl_Obj *source;		/* Pathname of file to copy.  May need to
   466 				 * be translated. */
   467     Tcl_Obj *target;		/* Pathname of file to create/overwrite.
   468 				 * May need to be translated. */
   469     int copyFlag;		/* If non-zero, copy files.  Otherwise,
   470 				 * rename them. */
   471     int force;			/* If non-zero, overwrite target file if it
   472 				 * exists.  Otherwise, error if target already
   473 				 * exists. */
   474 {
   475     int result;
   476     Tcl_Obj *errfile, *errorBuffer;
   477     /* If source is a link, then this is the real file/directory */
   478     Tcl_Obj *actualSource = NULL;
   479     Tcl_StatBuf sourceStatBuf, targetStatBuf;
   480 
   481     if (Tcl_FSConvertToPathType(interp, source) != TCL_OK) {
   482 	return TCL_ERROR;
   483     }
   484     if (Tcl_FSConvertToPathType(interp, target) != TCL_OK) {
   485 	return TCL_ERROR;
   486     }
   487     
   488     errfile = NULL;
   489     errorBuffer = NULL;
   490     result = TCL_ERROR;
   491     
   492     /*
   493      * We want to copy/rename links and not the files they point to, so we
   494      * use lstat(). If target is a link, we also want to replace the 
   495      * link and not the file it points to, so we also use lstat() on the
   496      * target.
   497      */
   498 
   499     if (Tcl_FSLstat(source, &sourceStatBuf) != 0) {
   500 	errfile = source;
   501 	goto done;
   502     }
   503     if (Tcl_FSLstat(target, &targetStatBuf) != 0) {
   504 	if (errno != ENOENT) {
   505 	    errfile = target;
   506 	    goto done;
   507 	}
   508     } else {
   509 	if (force == 0) {
   510 	    errno = EEXIST;
   511 	    errfile = target;
   512 	    goto done;
   513 	}
   514 
   515         /* 
   516          * Prevent copying or renaming a file onto itself.  Under Windows, 
   517          * stat always returns 0 for st_ino.  However, the Windows-specific 
   518          * code knows how to deal with copying or renaming a file on top of
   519          * itself.  It might be a good idea to write a stat that worked.
   520          */
   521      
   522         if ((sourceStatBuf.st_ino != 0) && (targetStatBuf.st_ino != 0)) {
   523             if ((sourceStatBuf.st_ino == targetStatBuf.st_ino) &&
   524             	    (sourceStatBuf.st_dev == targetStatBuf.st_dev)) {
   525             	result = TCL_OK;
   526             	goto done;
   527             }
   528         }
   529 
   530 	/*
   531 	 * Prevent copying/renaming a file onto a directory and
   532 	 * vice-versa.  This is a policy decision based on the fact that
   533 	 * existing implementations of copy and rename on all platforms
   534 	 * also prevent this.
   535 	 */
   536 
   537 	if (S_ISDIR(sourceStatBuf.st_mode)
   538                 && !S_ISDIR(targetStatBuf.st_mode)) {
   539 	    errno = EISDIR;
   540 	    Tcl_AppendResult(interp, "can't overwrite file \"", 
   541 		    Tcl_GetString(target), "\" with directory \"", 
   542 		    Tcl_GetString(source), "\"", (char *) NULL);
   543 	    goto done;
   544 	}
   545 	if (!S_ISDIR(sourceStatBuf.st_mode)
   546 	        && S_ISDIR(targetStatBuf.st_mode)) {
   547 	    errno = EISDIR;
   548 	    Tcl_AppendResult(interp, "can't overwrite directory \"", 
   549 		    Tcl_GetString(target), "\" with file \"", 
   550 		    Tcl_GetString(source), "\"", (char *) NULL);
   551 	    goto done;
   552 	}
   553     }
   554 
   555     if (copyFlag == 0) {
   556 	result = Tcl_FSRenameFile(source, target);
   557 	if (result == TCL_OK) {
   558 	    goto done;
   559 	}
   560 	    
   561 	if (errno == EINVAL) {
   562 	    Tcl_AppendResult(interp, "error renaming \"", 
   563 		    Tcl_GetString(source), "\" to \"",
   564 		    Tcl_GetString(target), "\": trying to rename a volume or ",
   565 		    "move a directory into itself", (char *) NULL);
   566 	    goto done;
   567 	} else if (errno != EXDEV) {
   568 	    errfile = target;
   569 	    goto done;
   570 	}
   571 	
   572 	/*
   573 	 * The rename failed because the move was across file systems.
   574 	 * Fall through to copy file and then remove original.  Note that
   575 	 * the low-level Tcl_FSRenameFileProc in the filesystem is allowed 
   576 	 * to implement cross-filesystem moves itself, if it desires.
   577 	 */
   578     }
   579 
   580     actualSource = source;
   581     Tcl_IncrRefCount(actualSource);
   582 #if 0
   583 #ifdef S_ISLNK
   584     /* 
   585      * To add a flag to make 'copy' copy links instead of files, we could
   586      * add a condition to ignore this 'if' here.
   587      */
   588     if (copyFlag && S_ISLNK(sourceStatBuf.st_mode)) {
   589 	/* 
   590 	 * We want to copy files not links.  Therefore we must follow the
   591 	 * link.  There are two purposes to this 'stat' call here.  First
   592 	 * we want to know if the linked-file/dir actually exists, and
   593 	 * second, in the block of code which follows, some 20 lines
   594 	 * down, we want to check if the thing is a file or directory.
   595 	 */
   596 	if (Tcl_FSStat(source, &sourceStatBuf) != 0) {
   597 	    /* Actual file doesn't exist */
   598 	    Tcl_AppendResult(interp, 
   599 		    "error copying \"", Tcl_GetString(source), 
   600 		    "\": the target of this link doesn't exist",
   601 		    (char *) NULL);
   602 	    goto done;
   603 	} else {
   604 	    int counter = 0;
   605 	    while (1) {
   606 		Tcl_Obj *path = Tcl_FSLink(actualSource, NULL, 0);
   607 		if (path == NULL) {
   608 		    break;
   609 		}
   610 		Tcl_DecrRefCount(actualSource);
   611 		actualSource = path;
   612 		counter++;
   613 		/* Arbitrary limit of 20 links to follow */
   614 		if (counter > 20) {
   615 		    /* Too many links */
   616 		    Tcl_SetErrno(EMLINK);
   617 		    errfile = source;
   618 		    goto done;
   619 		}
   620 	    }
   621 	    /* Now 'actualSource' is the correct file */
   622 	}
   623     }
   624 #endif
   625 #endif
   626 
   627     if (S_ISDIR(sourceStatBuf.st_mode)) {
   628 	result = Tcl_FSCopyDirectory(actualSource, target, &errorBuffer);
   629 	if (result != TCL_OK) {
   630 	    if (errno == EXDEV) {
   631 		/* 
   632 		 * The copy failed because we're trying to do a
   633 		 * cross-filesystem copy.  We do this through our Tcl
   634 		 * library.
   635 		 */
   636 		Tcl_SavedResult savedResult;
   637 		Tcl_Obj *copyCommand = Tcl_NewListObj(0,NULL);
   638 		Tcl_IncrRefCount(copyCommand);
   639 		Tcl_ListObjAppendElement(interp, copyCommand, 
   640 			Tcl_NewStringObj("::tcl::CopyDirectory",-1));
   641 		if (copyFlag) {
   642 		    Tcl_ListObjAppendElement(interp, copyCommand, 
   643 					     Tcl_NewStringObj("copying",-1));
   644 		} else {
   645 		    Tcl_ListObjAppendElement(interp, copyCommand, 
   646 					     Tcl_NewStringObj("renaming",-1));
   647 		}
   648 		Tcl_ListObjAppendElement(interp, copyCommand, source);
   649 		Tcl_ListObjAppendElement(interp, copyCommand, target);
   650 		Tcl_SaveResult(interp, &savedResult);
   651 		result = Tcl_EvalObjEx(interp, copyCommand, 
   652 				       TCL_EVAL_GLOBAL | TCL_EVAL_DIRECT);
   653 		Tcl_DecrRefCount(copyCommand);
   654 		if (result != TCL_OK) {
   655 		    /* 
   656 		     * There was an error in the Tcl-level copy.
   657 		     * We will pass on the Tcl error message and
   658 		     * can ensure this by setting errfile to NULL
   659 		     */
   660 		    Tcl_DiscardResult(&savedResult);
   661 		    errfile = NULL;
   662 		} else {
   663 		    /* The copy was successful */
   664 		    Tcl_RestoreResult(interp, &savedResult);
   665 		}
   666 	    } else {
   667 		errfile = errorBuffer;
   668 		if (Tcl_FSEqualPaths(errfile, source)) {
   669 		    errfile = source;
   670 		} else if (Tcl_FSEqualPaths(errfile, target)) {
   671 		    errfile = target;
   672 		}
   673 	    }
   674 	}
   675     } else {
   676 	result = Tcl_FSCopyFile(actualSource, target);
   677 	if ((result != TCL_OK) && (errno == EXDEV)) {
   678 	    result = TclCrossFilesystemCopy(interp, source, target);
   679 	}
   680 	if (result != TCL_OK) {
   681 	    /* 
   682 	     * We could examine 'errno' to double-check if the problem
   683 	     * was with the target, but we checked the source above,
   684 	     * so it should be quite clear 
   685 	     */
   686 	    errfile = target;
   687 	    /* 
   688 	     * We now need to reset the result, because the above call,
   689 	     * if it failed, may have put an error message in place.
   690 	     * (Ideally we would prefer not to pass an interpreter in
   691 	     * above, but the channel IO code used by
   692 	     * TclCrossFilesystemCopy currently requires one)
   693 	     */
   694 	    Tcl_ResetResult(interp);
   695 	}
   696     }
   697     if ((copyFlag == 0) && (result == TCL_OK)) {
   698 	if (S_ISDIR(sourceStatBuf.st_mode)) {
   699 	    result = Tcl_FSRemoveDirectory(source, 1, &errorBuffer);
   700 	    if (result != TCL_OK) {
   701 		if (Tcl_FSEqualPaths(errfile, source) == 0) {
   702 		    errfile = source;
   703 		}
   704 	    }
   705 	} else {
   706 	    result = Tcl_FSDeleteFile(source);
   707 	    if (result != TCL_OK) {
   708 		errfile = source;
   709 	    }
   710 	}
   711 	if (result != TCL_OK) {
   712 	    Tcl_AppendResult(interp, "can't unlink \"", 
   713 		Tcl_GetString(errfile), "\": ",
   714 		Tcl_PosixError(interp), (char *) NULL);
   715 	    errfile = NULL;
   716 	}
   717     }
   718     
   719     done:
   720     if (errfile != NULL) {
   721 	Tcl_AppendResult(interp, 
   722 		((copyFlag) ? "error copying \"" : "error renaming \""),
   723 		 Tcl_GetString(source), (char *) NULL);
   724 	if (errfile != source) {
   725 	    Tcl_AppendResult(interp, "\" to \"", Tcl_GetString(target), 
   726 			     (char *) NULL);
   727 	    if (errfile != target) {
   728 		Tcl_AppendResult(interp, "\": \"", Tcl_GetString(errfile), 
   729 				 (char *) NULL);
   730 	    }
   731 	}
   732 	Tcl_AppendResult(interp, "\": ", Tcl_PosixError(interp),
   733 		(char *) NULL);
   734     }
   735     if (errorBuffer != NULL) {
   736         Tcl_DecrRefCount(errorBuffer);
   737     }
   738     if (actualSource != NULL) {
   739 	Tcl_DecrRefCount(actualSource);
   740     }
   741     return result;
   742 }
   743 
   744 /*
   745  *---------------------------------------------------------------------------
   746  *
   747  * FileForceOption --
   748  *
   749  *	Helps parse command line options for file commands that take
   750  *	the "-force" and "--" options.
   751  *
   752  * Results:
   753  *	The return value is how many arguments from argv were consumed
   754  *	by this function, or -1 if there was an error parsing the
   755  *	options.  If an error occurred, an error message is left in the
   756  *	interp's result.
   757  *
   758  * Side effects:
   759  *	None.
   760  *
   761  *---------------------------------------------------------------------------
   762  */
   763 
   764 static int
   765 FileForceOption(interp, objc, objv, forcePtr)
   766     Tcl_Interp *interp;		/* Interp, for error return. */
   767     int objc;			/* Number of arguments. */
   768     Tcl_Obj *CONST objv[];	/* Argument strings.  First command line
   769 				 * option, if it exists, begins at 0. */
   770     int *forcePtr;		/* If the "-force" was specified, *forcePtr
   771 				 * is filled with 1, otherwise with 0. */
   772 {
   773     int force, i;
   774     
   775     force = 0;
   776     for (i = 0; i < objc; i++) {
   777 	if (Tcl_GetString(objv[i])[0] != '-') {
   778 	    break;
   779 	}
   780 	if (strcmp(Tcl_GetString(objv[i]), "-force") == 0) {
   781 	    force = 1;
   782 	} else if (strcmp(Tcl_GetString(objv[i]), "--") == 0) {
   783 	    i++;
   784 	    break;
   785 	} else {
   786 	    Tcl_AppendResult(interp, "bad option \"", Tcl_GetString(objv[i]), 
   787 		    "\": should be -force or --", (char *)NULL);
   788 	    return -1;
   789 	}
   790     }
   791     *forcePtr = force;
   792     return i;
   793 }
   794 /*
   795  *---------------------------------------------------------------------------
   796  *
   797  * FileBasename --
   798  *
   799  *	Given a path in either tcl format (with / separators), or in the
   800  *	platform-specific format for the current platform, return all the
   801  *	characters in the path after the last directory separator.  But,
   802  *	if path is the root directory, returns no characters.
   803  *
   804  * Results:
   805  *	Returns the string object that represents the basename.  If there 
   806  *	is an error, an error message is left in interp, and NULL is 
   807  *	returned.
   808  *
   809  * Side effects:
   810  *	None.
   811  *
   812  *---------------------------------------------------------------------------
   813  */
   814 
   815 static Tcl_Obj *
   816 FileBasename(interp, pathPtr)
   817     Tcl_Interp *interp;		/* Interp, for error return. */
   818     Tcl_Obj *pathPtr;		/* Path whose basename to extract. */
   819 {
   820     int objc;
   821     Tcl_Obj *splitPtr;
   822     Tcl_Obj *resultPtr = NULL;
   823     
   824     splitPtr = Tcl_FSSplitPath(pathPtr, &objc);
   825 
   826     if (objc != 0) {
   827 	if ((objc == 1) && (*Tcl_GetString(pathPtr) == '~')) {
   828 	    Tcl_DecrRefCount(splitPtr);
   829 	    if (Tcl_FSConvertToPathType(interp, pathPtr) != TCL_OK) {
   830 		return NULL;
   831 	    }
   832 	    splitPtr = Tcl_FSSplitPath(pathPtr, &objc);
   833 	}
   834 
   835 	/*
   836 	 * Return the last component, unless it is the only component, and it
   837 	 * is the root of an absolute path.
   838 	 */
   839 
   840 	if (objc > 0) {
   841 	    Tcl_ListObjIndex(NULL, splitPtr, objc-1, &resultPtr);
   842 	    if ((objc == 1) &&
   843 	      (Tcl_FSGetPathType(resultPtr) != TCL_PATH_RELATIVE)) {
   844 		resultPtr = NULL;
   845 	    }
   846 	}
   847     }
   848     if (resultPtr == NULL) {
   849 	resultPtr = Tcl_NewObj();
   850     }
   851     Tcl_IncrRefCount(resultPtr);
   852     Tcl_DecrRefCount(splitPtr);
   853     return resultPtr;
   854 }
   855 
   856 /*
   857  *----------------------------------------------------------------------
   858  *
   859  * TclFileAttrsCmd --
   860  *
   861  *      Sets or gets the platform-specific attributes of a file.  The
   862  *      objc-objv points to the file name with the rest of the command
   863  *      line following.  This routine uses platform-specific tables of
   864  *      option strings and callbacks.  The callback to get the
   865  *      attributes take three parameters:
   866  *	    Tcl_Interp *interp;	    The interp to report errors with.
   867  *				    Since this is an object-based API,
   868  *				    the object form of the result should 
   869  *				    be used.
   870  *	    CONST char *fileName;   This is extracted using
   871  *				    Tcl_TranslateFileName.
   872  *	    TclObj **attrObjPtrPtr; A new object to hold the attribute
   873  *				    is allocated and put here.
   874  *	The first two parameters of the callback used to write out the
   875  *	attributes are the same. The third parameter is:
   876  *	    CONST *attrObjPtr;	    A pointer to the object that has
   877  *				    the new attribute.
   878  *	They both return standard TCL errors; if the routine to get
   879  *	an attribute fails, no object is allocated and *attrObjPtrPtr
   880  *	is unchanged.
   881  *
   882  * Results:
   883  *      Standard TCL error.
   884  *
   885  * Side effects:
   886  *      May set file attributes for the file name.
   887  *      
   888  *----------------------------------------------------------------------
   889  */
   890 
   891 int
   892 TclFileAttrsCmd(interp, objc, objv)
   893     Tcl_Interp *interp;		/* The interpreter for error reporting. */
   894     int objc;			/* Number of command line arguments. */
   895     Tcl_Obj *CONST objv[];	/* The command line objects. */
   896 {
   897     int result;
   898     CONST char ** attributeStrings;
   899     Tcl_Obj* objStrings = NULL;
   900     int numObjStrings = -1;
   901     Tcl_Obj *filePtr;
   902     
   903     if (objc < 3) {
   904 	Tcl_WrongNumArgs(interp, 2, objv,
   905 		"name ?option? ?value? ?option value ...?");
   906 	return TCL_ERROR;
   907     }
   908 
   909     filePtr = objv[2];
   910     if (Tcl_FSConvertToPathType(interp, filePtr) != TCL_OK) {
   911     	return TCL_ERROR;
   912     }
   913     
   914     objc -= 3;
   915     objv += 3;
   916     result = TCL_ERROR;
   917     Tcl_SetErrno(0);
   918     attributeStrings = Tcl_FSFileAttrStrings(filePtr, &objStrings);
   919     if (attributeStrings == NULL) {
   920 	int index;
   921 	Tcl_Obj *objPtr;
   922 	if (objStrings == NULL) {
   923 	    if (Tcl_GetErrno() != 0) {
   924 		/* 
   925 		 * There was an error, probably that the filePtr is
   926 		 * not accepted by any filesystem
   927 		 */
   928 		Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), 
   929 			"could not read \"", Tcl_GetString(filePtr), 
   930 			"\": ", Tcl_PosixError(interp), 
   931 			(char *) NULL);
   932 		return TCL_ERROR;
   933 	    }
   934 	    goto end;
   935 	}
   936 	/* We own the object now */
   937 	Tcl_IncrRefCount(objStrings);
   938         /* Use objStrings as a list object */
   939 	if (Tcl_ListObjLength(interp, objStrings, &numObjStrings) != TCL_OK) {
   940 	    goto end;
   941 	}
   942 	attributeStrings = (CONST char **)
   943 		ckalloc ((1+numObjStrings) * sizeof(char*));
   944 	for (index = 0; index < numObjStrings; index++) {
   945 	    Tcl_ListObjIndex(interp, objStrings, index, &objPtr);
   946 	    attributeStrings[index] = Tcl_GetString(objPtr);
   947 	}
   948 	attributeStrings[index] = NULL;
   949     }
   950     if (objc == 0) {
   951 	/*
   952 	 * Get all attributes.
   953 	 */
   954 
   955 	int index;
   956 	Tcl_Obj *listPtr;
   957 	 
   958 	listPtr = Tcl_NewListObj(0, NULL);
   959 	for (index = 0; attributeStrings[index] != NULL; index++) {
   960 	    Tcl_Obj *objPtr = Tcl_NewStringObj(attributeStrings[index], -1);
   961 	    Tcl_ListObjAppendElement(interp, listPtr, objPtr);
   962 	    /* We now forget about objPtr, it is in the list */
   963 	    objPtr = NULL;
   964 	    if (Tcl_FSFileAttrsGet(interp, index, filePtr,
   965 		    &objPtr) != TCL_OK) {
   966 		Tcl_DecrRefCount(listPtr);
   967 		goto end;
   968 	    }
   969 	    Tcl_ListObjAppendElement(interp, listPtr, objPtr);
   970 	}
   971     	Tcl_SetObjResult(interp, listPtr);
   972     } else if (objc == 1) {
   973 	/*
   974 	 * Get one attribute.
   975 	 */
   976 
   977 	int index;
   978 	Tcl_Obj *objPtr = NULL;
   979 
   980 	if (numObjStrings == 0) {
   981 	    Tcl_AppendResult(interp, "bad option \"",
   982 		    Tcl_GetString(objv[0]), "\", there are no file attributes"
   983 			     " in this filesystem.", (char *) NULL);
   984 	    goto end;
   985 	}
   986 
   987 	if (Tcl_GetIndexFromObj(interp, objv[0], attributeStrings,
   988 		"option", 0, &index) != TCL_OK) {
   989 	    goto end;
   990 	}
   991 	if (Tcl_FSFileAttrsGet(interp, index, filePtr,
   992 		&objPtr) != TCL_OK) {
   993 	    goto end;
   994 	}
   995 	Tcl_SetObjResult(interp, objPtr);
   996     } else {
   997 	/*
   998 	 * Set option/value pairs.
   999 	 */
  1000 
  1001 	int i, index;
  1002         
  1003 	if (numObjStrings == 0) {
  1004 	    Tcl_AppendResult(interp, "bad option \"",
  1005 		    Tcl_GetString(objv[0]), "\", there are no file attributes"
  1006 			     " in this filesystem.", (char *) NULL);
  1007 	    goto end;
  1008 	}
  1009 
  1010     	for (i = 0; i < objc ; i += 2) {
  1011     	    if (Tcl_GetIndexFromObj(interp, objv[i], attributeStrings,
  1012 		    "option", 0, &index) != TCL_OK) {
  1013 		goto end;
  1014     	    }
  1015 	    if (i + 1 == objc) {
  1016 		Tcl_AppendResult(interp, "value for \"",
  1017 			Tcl_GetString(objv[i]), "\" missing",
  1018 			(char *) NULL);
  1019 		goto end;
  1020 	    }
  1021     	    if (Tcl_FSFileAttrsSet(interp, index, filePtr,
  1022     	    	    objv[i + 1]) != TCL_OK) {
  1023 		goto end;
  1024     	    }
  1025     	}
  1026     }
  1027     result = TCL_OK;
  1028 
  1029     end:
  1030     if (numObjStrings != -1) {
  1031 	/* Free up the array we allocated */
  1032 	ckfree((char*)attributeStrings);
  1033 	/* 
  1034 	 * We don't need this object that was passed to us
  1035 	 * any more.
  1036 	 */
  1037 	if (objStrings != NULL) {
  1038 	    Tcl_DecrRefCount(objStrings);
  1039 	}
  1040     }
  1041     return result;
  1042 }