os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/unix/tclUnixFCmd.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
 * tclUnixFCmd.c
sl@0
     3
 *
sl@0
     4
 *      This file implements the unix specific portion of file manipulation 
sl@0
     5
 *      subcommands of the "file" command.  All filename arguments should
sl@0
     6
 *	already be translated to native format.
sl@0
     7
 *
sl@0
     8
 * Copyright (c) 1996-1998 Sun Microsystems, Inc.
sl@0
     9
 * Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved.   
sl@0
    10
 *
sl@0
    11
 * See the file "license.terms" for information on usage and redistribution
sl@0
    12
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
sl@0
    13
 *
sl@0
    14
 * RCS: @(#) $Id: tclUnixFCmd.c,v 1.28.2.15 2007/04/29 02:19:51 das Exp $
sl@0
    15
 *
sl@0
    16
 * Portions of this code were derived from NetBSD source code which has
sl@0
    17
 * the following copyright notice:
sl@0
    18
 *
sl@0
    19
 * Copyright (c) 1988, 1993, 1994
sl@0
    20
 *      The Regents of the University of California.  All rights reserved.
sl@0
    21
 *
sl@0
    22
 * Redistribution and use in source and binary forms, with or without
sl@0
    23
 * modification, are permitted provided that the following conditions
sl@0
    24
 * are met:
sl@0
    25
 * 1. Redistributions of source code must retain the above copyright
sl@0
    26
 *    notice, this list of conditions and the following disclaimer.
sl@0
    27
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    28
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    29
 *    documentation and/or other materials provided with the distribution.
sl@0
    30
 * 3. All advertising materials mentioning features or use of this software
sl@0
    31
 *    must display the following acknowledgement:
sl@0
    32
 *      This product includes software developed by the University of
sl@0
    33
 *      California, Berkeley and its contributors.
sl@0
    34
 * 4. Neither the name of the University nor the names of its contributors
sl@0
    35
 *    may be used to endorse or promote products derived from this software
sl@0
    36
 *    without specific prior written permission.
sl@0
    37
 *
sl@0
    38
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
sl@0
    39
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    40
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    41
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
sl@0
    42
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    43
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    44
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    45
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    46
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    47
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    48
 * SUCH DAMAGE.
sl@0
    49
 */
sl@0
    50
sl@0
    51
#include "tclInt.h"
sl@0
    52
#include "tclPort.h"
sl@0
    53
#include <utime.h>
sl@0
    54
#include <grp.h>
sl@0
    55
#ifndef HAVE_ST_BLKSIZE
sl@0
    56
#ifndef NO_FSTATFS
sl@0
    57
#include <sys/statfs.h>
sl@0
    58
#endif
sl@0
    59
#endif
sl@0
    60
#ifdef HAVE_FTS
sl@0
    61
#include <fts.h>
sl@0
    62
#endif
sl@0
    63
sl@0
    64
#ifdef __SYMBIAN32__  
sl@0
    65
#include "convertPathSlashes.h"  
sl@0
    66
void TclPrint1(const char* aFmt, const char* aStr);
sl@0
    67
#endif
sl@0
    68
/*
sl@0
    69
 * The following constants specify the type of callback when
sl@0
    70
 * TraverseUnixTree() calls the traverseProc()
sl@0
    71
 */
sl@0
    72
sl@0
    73
#define DOTREE_PRED   1     /* pre-order directory  */
sl@0
    74
#define DOTREE_POSTD  2     /* post-order directory */
sl@0
    75
#define DOTREE_F      3     /* regular file */
sl@0
    76
sl@0
    77
/*
sl@0
    78
 * Callbacks for file attributes code.
sl@0
    79
 */
sl@0
    80
sl@0
    81
static int		GetGroupAttribute _ANSI_ARGS_((Tcl_Interp *interp,
sl@0
    82
			    int objIndex, Tcl_Obj *fileName,
sl@0
    83
			    Tcl_Obj **attributePtrPtr));
sl@0
    84
static int		GetOwnerAttribute _ANSI_ARGS_((Tcl_Interp *interp,
sl@0
    85
			    int objIndex, Tcl_Obj *fileName,
sl@0
    86
			    Tcl_Obj **attributePtrPtr));
sl@0
    87
static int		GetPermissionsAttribute _ANSI_ARGS_((
sl@0
    88
			    Tcl_Interp *interp, int objIndex,
sl@0
    89
			    Tcl_Obj *fileName, Tcl_Obj **attributePtrPtr));
sl@0
    90
static int		SetGroupAttribute _ANSI_ARGS_((Tcl_Interp *interp,
sl@0
    91
			    int objIndex, Tcl_Obj *fileName,
sl@0
    92
			    Tcl_Obj *attributePtr));
sl@0
    93
static int		SetOwnerAttribute _ANSI_ARGS_((Tcl_Interp *interp,
sl@0
    94
			    int objIndex, Tcl_Obj *fileName,
sl@0
    95
			    Tcl_Obj *attributePtr));
sl@0
    96
static int		SetPermissionsAttribute _ANSI_ARGS_((
sl@0
    97
			    Tcl_Interp *interp, int objIndex,
sl@0
    98
			    Tcl_Obj *fileName, Tcl_Obj *attributePtr));
sl@0
    99
static int		GetModeFromPermString _ANSI_ARGS_((
sl@0
   100
			    Tcl_Interp *interp, char *modeStringPtr,
sl@0
   101
			    mode_t *modePtr));
sl@0
   102
sl@0
   103
/*
sl@0
   104
 * Prototype for the TraverseUnixTree callback function.
sl@0
   105
 */
sl@0
   106
sl@0
   107
typedef int (TraversalProc) _ANSI_ARGS_((Tcl_DString *srcPtr,
sl@0
   108
	Tcl_DString *dstPtr, CONST Tcl_StatBuf *statBufPtr, int type,
sl@0
   109
	Tcl_DString *errorPtr));
sl@0
   110
sl@0
   111
/*
sl@0
   112
 * Constants and variables necessary for file attributes subcommand.
sl@0
   113
 */
sl@0
   114
sl@0
   115
enum {
sl@0
   116
    UNIX_GROUP_ATTRIBUTE,
sl@0
   117
    UNIX_OWNER_ATTRIBUTE,
sl@0
   118
    UNIX_PERMISSIONS_ATTRIBUTE
sl@0
   119
};
sl@0
   120
sl@0
   121
CONST char *tclpFileAttrStrings[] = {
sl@0
   122
    "-group",
sl@0
   123
    "-owner",
sl@0
   124
    "-permissions",
sl@0
   125
    (char *) NULL
sl@0
   126
};
sl@0
   127
sl@0
   128
CONST TclFileAttrProcs tclpFileAttrProcs[] = {
sl@0
   129
    {GetGroupAttribute,		SetGroupAttribute},
sl@0
   130
    {GetOwnerAttribute,		SetOwnerAttribute},
sl@0
   131
    {GetPermissionsAttribute,	SetPermissionsAttribute}
sl@0
   132
};
sl@0
   133
sl@0
   134
/*
sl@0
   135
 * This is the maximum number of consecutive readdir/unlink calls that can be
sl@0
   136
 * made (with no intervening rewinddir or closedir/opendir) before triggering
sl@0
   137
 * a bug that makes readdir return NULL even though some directory entries
sl@0
   138
 * have not been processed.  The bug afflicts SunOS's readdir when applied to
sl@0
   139
 * ufs file systems and Darwin 6.5's (and OSX v.10.3.8's) HFS+.  JH found the
sl@0
   140
 * Darwin readdir to reset at 147, so 130 is chosen to be conservative.  We
sl@0
   141
 * can't do a general rewind on failure as NFS can create special files that
sl@0
   142
 * recreate themselves when you try and delete them.  8.4.8 added a solution
sl@0
   143
 * that was affected by a single such NFS file, this solution should not be
sl@0
   144
 * affected by less than THRESHOLD such files. [Bug 1034337]
sl@0
   145
 */
sl@0
   146
sl@0
   147
#define MAX_READDIR_UNLINK_THRESHOLD 130
sl@0
   148
sl@0
   149
/*
sl@0
   150
 * Declarations for local procedures defined in this file:
sl@0
   151
 */
sl@0
   152
sl@0
   153
static int		CopyFile _ANSI_ARGS_((CONST char *src,
sl@0
   154
			    CONST char *dst, CONST Tcl_StatBuf *statBufPtr));
sl@0
   155
static int		CopyFileAtts _ANSI_ARGS_((CONST char *src,
sl@0
   156
			    CONST char *dst, CONST Tcl_StatBuf *statBufPtr));
sl@0
   157
static int		DoCopyFile _ANSI_ARGS_((CONST char *srcPtr,
sl@0
   158
			    CONST char *dstPtr, CONST Tcl_StatBuf *statBufPtr));
sl@0
   159
static int		DoCreateDirectory _ANSI_ARGS_((CONST char *pathPtr));
sl@0
   160
static int		DoRemoveDirectory _ANSI_ARGS_((Tcl_DString *pathPtr,
sl@0
   161
			    int recursive, Tcl_DString *errorPtr));
sl@0
   162
static int		DoRenameFile _ANSI_ARGS_((CONST char *src,
sl@0
   163
			    CONST char *dst));
sl@0
   164
static int		TraversalCopy _ANSI_ARGS_((Tcl_DString *srcPtr,
sl@0
   165
			    Tcl_DString *dstPtr, CONST Tcl_StatBuf *statBufPtr,
sl@0
   166
			    int type, Tcl_DString *errorPtr));
sl@0
   167
static int		TraversalDelete _ANSI_ARGS_((Tcl_DString *srcPtr,
sl@0
   168
			    Tcl_DString *dstPtr, CONST Tcl_StatBuf *statBufPtr,
sl@0
   169
			    int type, Tcl_DString *errorPtr));
sl@0
   170
static int		TraverseUnixTree _ANSI_ARGS_((
sl@0
   171
			    TraversalProc *traversalProc,
sl@0
   172
			    Tcl_DString *sourcePtr, Tcl_DString *destPtr,
sl@0
   173
			    Tcl_DString *errorPtr, int doRewind));
sl@0
   174
sl@0
   175
#ifdef PURIFY
sl@0
   176
/*
sl@0
   177
 * realpath and purify don't mix happily.  It has been noted that realpath
sl@0
   178
 * should not be used with purify because of bogus warnings, but just
sl@0
   179
 * memset'ing the resolved path will squelch those.  This assumes we are
sl@0
   180
 * passing the standard MAXPATHLEN size resolved arg.
sl@0
   181
 */
sl@0
   182
static char *		Realpath _ANSI_ARGS_((CONST char *path,
sl@0
   183
			    char *resolved));
sl@0
   184
sl@0
   185
char *
sl@0
   186
Realpath(path, resolved)
sl@0
   187
    CONST char *path;
sl@0
   188
    char *resolved;
sl@0
   189
{
sl@0
   190
    memset(resolved, 0, MAXPATHLEN);
sl@0
   191
    return realpath(path, resolved);
sl@0
   192
}
sl@0
   193
#else
sl@0
   194
#define Realpath realpath
sl@0
   195
#endif
sl@0
   196
sl@0
   197
#ifndef NO_REALPATH
sl@0
   198
#if defined(__APPLE__) && defined(TCL_THREADS) && \
sl@0
   199
	defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \
sl@0
   200
	MAC_OS_X_VERSION_MIN_REQUIRED < 1030
sl@0
   201
/*
sl@0
   202
 * prior to Darwin 7, realpath is not threadsafe, c.f. bug 711232;
sl@0
   203
 * if we might potentially be running on pre-10.3 OSX,
sl@0
   204
 * check Darwin release at runtime before using realpath.
sl@0
   205
 */
sl@0
   206
extern long tclMacOSXDarwinRelease;
sl@0
   207
#define haveRealpath (tclMacOSXDarwinRelease >= 7)
sl@0
   208
#else
sl@0
   209
#define haveRealpath 1
sl@0
   210
#endif
sl@0
   211
#endif /* NO_REALPATH */
sl@0
   212
sl@0
   213
#ifdef HAVE_FTS
sl@0
   214
#ifdef HAVE_STRUCT_STAT64
sl@0
   215
/* fts doesn't do stat64 */
sl@0
   216
#define noFtsStat 1
sl@0
   217
#elif defined(__APPLE__) && defined(__LP64__) && \
sl@0
   218
	defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \
sl@0
   219
	MAC_OS_X_VERSION_MIN_REQUIRED < 1050
sl@0
   220
/*
sl@0
   221
 * prior to Darwin 9, 64bit fts_open() without FTS_NOSTAT may crash (due to a
sl@0
   222
 * 64bit-unsafe ALIGN macro); if we could be running on pre-10.5 OSX, check
sl@0
   223
 * Darwin release at runtime and do a separate stat() if necessary.
sl@0
   224
 */
sl@0
   225
extern long tclMacOSXDarwinRelease;
sl@0
   226
#define noFtsStat (tclMacOSXDarwinRelease < 9)
sl@0
   227
#else
sl@0
   228
#define noFtsStat 0
sl@0
   229
#endif
sl@0
   230
#endif /* HAVE_FTS */
sl@0
   231
sl@0
   232

sl@0
   233
/*
sl@0
   234
 *---------------------------------------------------------------------------
sl@0
   235
 *
sl@0
   236
 * TclpObjRenameFile, DoRenameFile --
sl@0
   237
 *
sl@0
   238
 *      Changes the name of an existing file or directory, from src to dst.
sl@0
   239
 *	If src and dst refer to the same file or directory, does nothing
sl@0
   240
 *	and returns success.  Otherwise if dst already exists, it will be
sl@0
   241
 *	deleted and replaced by src subject to the following conditions:
sl@0
   242
 *	    If src is a directory, dst may be an empty directory.
sl@0
   243
 *	    If src is a file, dst may be a file.
sl@0
   244
 *	In any other situation where dst already exists, the rename will
sl@0
   245
 *	fail.  
sl@0
   246
 *
sl@0
   247
 * Results:
sl@0
   248
 *	If the directory was successfully created, returns TCL_OK.
sl@0
   249
 *	Otherwise the return value is TCL_ERROR and errno is set to
sl@0
   250
 *	indicate the error.  Some possible values for errno are:
sl@0
   251
 *
sl@0
   252
 *	EACCES:     src or dst parent directory can't be read and/or written.
sl@0
   253
 *	EEXIST:	    dst is a non-empty directory.
sl@0
   254
 *	EINVAL:	    src is a root directory or dst is a subdirectory of src.
sl@0
   255
 *	EISDIR:	    dst is a directory, but src is not.
sl@0
   256
 *	ENOENT:	    src doesn't exist, or src or dst is "".
sl@0
   257
 *	ENOTDIR:    src is a directory, but dst is not.  
sl@0
   258
 *	EXDEV:	    src and dst are on different filesystems.
sl@0
   259
 *	
sl@0
   260
 * Side effects:
sl@0
   261
 *	The implementation of rename may allow cross-filesystem renames,
sl@0
   262
 *	but the caller should be prepared to emulate it with copy and
sl@0
   263
 *	delete if errno is EXDEV.
sl@0
   264
 *
sl@0
   265
 *---------------------------------------------------------------------------
sl@0
   266
 */
sl@0
   267
sl@0
   268
int 
sl@0
   269
TclpObjRenameFile(srcPathPtr, destPathPtr)
sl@0
   270
    Tcl_Obj *srcPathPtr;
sl@0
   271
    Tcl_Obj *destPathPtr;
sl@0
   272
{
sl@0
   273
    return DoRenameFile(Tcl_FSGetNativePath(srcPathPtr), 
sl@0
   274
			Tcl_FSGetNativePath(destPathPtr));
sl@0
   275
}
sl@0
   276
sl@0
   277
static int
sl@0
   278
DoRenameFile(src, dst)
sl@0
   279
    CONST char *src;		/* Pathname of file or dir to be renamed
sl@0
   280
				 * (native). */
sl@0
   281
    CONST char *dst;		/* New pathname of file or directory
sl@0
   282
				 * (native). */
sl@0
   283
{
sl@0
   284
    if (rename(src, dst) == 0) {			/* INTL: Native. */
sl@0
   285
	return TCL_OK;
sl@0
   286
    }
sl@0
   287
    if (errno == ENOTEMPTY) {
sl@0
   288
	errno = EEXIST;
sl@0
   289
    }
sl@0
   290
sl@0
   291
    /*
sl@0
   292
     * IRIX returns EIO when you attept to move a directory into
sl@0
   293
     * itself.  We just map EIO to EINVAL get the right message on SGI.
sl@0
   294
     * Most platforms don't return EIO except in really strange cases.
sl@0
   295
     */
sl@0
   296
    
sl@0
   297
    if (errno == EIO) {
sl@0
   298
	errno = EINVAL;
sl@0
   299
    }
sl@0
   300
    
sl@0
   301
#ifndef NO_REALPATH
sl@0
   302
    /*
sl@0
   303
     * SunOS 4.1.4 reports overwriting a non-empty directory with a
sl@0
   304
     * directory as EINVAL instead of EEXIST (first rule out the correct
sl@0
   305
     * EINVAL result code for moving a directory into itself).  Must be
sl@0
   306
     * conditionally compiled because realpath() not defined on all systems.
sl@0
   307
     */
sl@0
   308
sl@0
   309
    if (errno == EINVAL && haveRealpath) {
sl@0
   310
	char srcPath[MAXPATHLEN], dstPath[MAXPATHLEN];
sl@0
   311
	DIR *dirPtr;
sl@0
   312
	Tcl_DirEntry *dirEntPtr;
sl@0
   313
sl@0
   314
	if ((Realpath((char *) src, srcPath) != NULL)	/* INTL: Native. */
sl@0
   315
		&& (Realpath((char *) dst, dstPath) != NULL) /* INTL: Native. */
sl@0
   316
		&& (strncmp(srcPath, dstPath, strlen(srcPath)) != 0)) {
sl@0
   317
	    dirPtr = opendir(dst);			/* INTL: Native. */
sl@0
   318
	    if (dirPtr != NULL) {
sl@0
   319
		while (1) {
sl@0
   320
		    dirEntPtr = TclOSreaddir(dirPtr); /* INTL: Native. */
sl@0
   321
		    if (dirEntPtr == NULL) {
sl@0
   322
			break;
sl@0
   323
		    }
sl@0
   324
		    if ((strcmp(dirEntPtr->d_name, ".") != 0) &&
sl@0
   325
			    (strcmp(dirEntPtr->d_name, "..") != 0)) {
sl@0
   326
			errno = EEXIST;
sl@0
   327
			closedir(dirPtr);
sl@0
   328
			return TCL_ERROR;
sl@0
   329
		    }
sl@0
   330
		}
sl@0
   331
		closedir(dirPtr);
sl@0
   332
	    }
sl@0
   333
	}
sl@0
   334
	errno = EINVAL;
sl@0
   335
    }
sl@0
   336
#endif	/* !NO_REALPATH */
sl@0
   337
sl@0
   338
    if (strcmp(src, "/") == 0) {
sl@0
   339
	/*
sl@0
   340
	 * Alpha reports renaming / as EBUSY and Linux reports it as EACCES,
sl@0
   341
	 * instead of EINVAL.
sl@0
   342
	 */
sl@0
   343
	 
sl@0
   344
	errno = EINVAL;
sl@0
   345
    }
sl@0
   346
sl@0
   347
    /*
sl@0
   348
     * DEC Alpha OSF1 V3.0 returns EACCES when attempting to move a
sl@0
   349
     * file across filesystems and the parent directory of that file is
sl@0
   350
     * not writable.  Most other systems return EXDEV.  Does nothing to
sl@0
   351
     * correct this behavior.
sl@0
   352
     */
sl@0
   353
sl@0
   354
    return TCL_ERROR;
sl@0
   355
}
sl@0
   356

sl@0
   357
/*
sl@0
   358
 *---------------------------------------------------------------------------
sl@0
   359
 *
sl@0
   360
 * TclpObjCopyFile, DoCopyFile --
sl@0
   361
 *
sl@0
   362
 *      Copy a single file (not a directory).  If dst already exists and
sl@0
   363
 *	is not a directory, it is removed.
sl@0
   364
 *
sl@0
   365
 * Results:
sl@0
   366
 *	If the file was successfully copied, returns TCL_OK.  Otherwise
sl@0
   367
 *	the return value is TCL_ERROR and errno is set to indicate the
sl@0
   368
 *	error.  Some possible values for errno are:
sl@0
   369
 *
sl@0
   370
 *	EACCES:     src or dst parent directory can't be read and/or written.
sl@0
   371
 *	EISDIR:	    src or dst is a directory.
sl@0
   372
 *	ENOENT:	    src doesn't exist.  src or dst is "".
sl@0
   373
 *
sl@0
   374
 * Side effects:
sl@0
   375
 *      This procedure will also copy symbolic links, block, and
sl@0
   376
 *      character devices, and fifos.  For symbolic links, the links 
sl@0
   377
 *      themselves will be copied and not what they point to.  For the
sl@0
   378
 *	other special file types, the directory entry will be copied and
sl@0
   379
 *	not the contents of the device that it refers to.
sl@0
   380
 *
sl@0
   381
 *---------------------------------------------------------------------------
sl@0
   382
 */
sl@0
   383
sl@0
   384
int 
sl@0
   385
TclpObjCopyFile(srcPathPtr, destPathPtr)
sl@0
   386
    Tcl_Obj *srcPathPtr;
sl@0
   387
    Tcl_Obj *destPathPtr;
sl@0
   388
{
sl@0
   389
    CONST char *src = Tcl_FSGetNativePath(srcPathPtr);
sl@0
   390
    Tcl_StatBuf srcStatBuf;
sl@0
   391
sl@0
   392
    if (TclOSlstat(src, &srcStatBuf) != 0) {		/* INTL: Native. */
sl@0
   393
	return TCL_ERROR;
sl@0
   394
    }
sl@0
   395
sl@0
   396
    return DoCopyFile(src, Tcl_FSGetNativePath(destPathPtr), &srcStatBuf);
sl@0
   397
}
sl@0
   398
sl@0
   399
static int
sl@0
   400
DoCopyFile(src, dst, statBufPtr)
sl@0
   401
    CONST char *src;	/* Pathname of file to be copied (native). */
sl@0
   402
    CONST char *dst;	/* Pathname of file to copy to (native). */
sl@0
   403
    CONST Tcl_StatBuf *statBufPtr;
sl@0
   404
			/* Used to determine filetype. */
sl@0
   405
{
sl@0
   406
    Tcl_StatBuf dstStatBuf;
sl@0
   407
sl@0
   408
    if (S_ISDIR(statBufPtr->st_mode)) {
sl@0
   409
	errno = EISDIR;
sl@0
   410
	return TCL_ERROR;
sl@0
   411
    }
sl@0
   412
sl@0
   413
    /*
sl@0
   414
     * symlink, and some of the other calls will fail if the target 
sl@0
   415
     * exists, so we remove it first
sl@0
   416
     */
sl@0
   417
    
sl@0
   418
    if (TclOSlstat(dst, &dstStatBuf) == 0) {		/* INTL: Native. */
sl@0
   419
	if (S_ISDIR(dstStatBuf.st_mode)) {
sl@0
   420
	    errno = EISDIR;
sl@0
   421
	    return TCL_ERROR;
sl@0
   422
	}
sl@0
   423
    }
sl@0
   424
    if (unlink(dst) != 0) {				/* INTL: Native. */
sl@0
   425
	if (errno != ENOENT) {
sl@0
   426
	    return TCL_ERROR;
sl@0
   427
	} 
sl@0
   428
    }
sl@0
   429
sl@0
   430
    switch ((int) (statBufPtr->st_mode & S_IFMT)) {
sl@0
   431
#ifndef DJGPP
sl@0
   432
        case S_IFLNK: {
sl@0
   433
	    char link[MAXPATHLEN];
sl@0
   434
	    int length;
sl@0
   435
sl@0
   436
	    length = readlink(src, link, sizeof(link)); /* INTL: Native. */
sl@0
   437
	    if (length == -1) {
sl@0
   438
		return TCL_ERROR;
sl@0
   439
	    }
sl@0
   440
	    link[length] = '\0';
sl@0
   441
	    if (symlink(link, dst) < 0) {		/* INTL: Native. */
sl@0
   442
		return TCL_ERROR;
sl@0
   443
	    }
sl@0
   444
#ifdef HAVE_COPYFILE
sl@0
   445
#ifdef WEAK_IMPORT_COPYFILE
sl@0
   446
	    if (copyfile != NULL)
sl@0
   447
#endif
sl@0
   448
	    copyfile(src, dst, NULL, COPYFILE_XATTR|COPYFILE_NOFOLLOW_SRC);
sl@0
   449
#endif
sl@0
   450
	    break;
sl@0
   451
	}
sl@0
   452
#endif
sl@0
   453
        case S_IFBLK:
sl@0
   454
        case S_IFCHR: {
sl@0
   455
#ifdef __SYMBIAN32__          
sl@0
   456
		// not supported by PIPS	    
sl@0
   457
		return TCL_ERROR;
sl@0
   458
#else
sl@0
   459
	    if (mknod(dst, statBufPtr->st_mode,		/* INTL: Native. */
sl@0
   460
		    statBufPtr->st_rdev) < 0) {
sl@0
   461
		return TCL_ERROR;
sl@0
   462
	    }
sl@0
   463
	    return CopyFileAtts(src, dst, statBufPtr);
sl@0
   464
#endif
sl@0
   465
	    }
sl@0
   466
        case S_IFIFO: {
sl@0
   467
	    if (mkfifo(dst, statBufPtr->st_mode) < 0) {	/* INTL: Native. */
sl@0
   468
		return TCL_ERROR;
sl@0
   469
	    }
sl@0
   470
	    return CopyFileAtts(src, dst, statBufPtr);
sl@0
   471
	}
sl@0
   472
        default: {
sl@0
   473
	    return CopyFile(src, dst, statBufPtr);
sl@0
   474
	}
sl@0
   475
    }
sl@0
   476
    return TCL_OK;
sl@0
   477
}
sl@0
   478

sl@0
   479
/*
sl@0
   480
 *----------------------------------------------------------------------
sl@0
   481
 *
sl@0
   482
 * CopyFile - 
sl@0
   483
 *
sl@0
   484
 *      Helper function for TclpCopyFile.  Copies one regular file,
sl@0
   485
 *	using read() and write().
sl@0
   486
 *
sl@0
   487
 * Results:
sl@0
   488
 *	A standard Tcl result.
sl@0
   489
 *
sl@0
   490
 * Side effects:
sl@0
   491
 *      A file is copied.  Dst will be overwritten if it exists.
sl@0
   492
 *
sl@0
   493
 *----------------------------------------------------------------------
sl@0
   494
 */
sl@0
   495
sl@0
   496
static int 
sl@0
   497
CopyFile(src, dst, statBufPtr) 
sl@0
   498
    CONST char *src;		/* Pathname of file to copy (native). */
sl@0
   499
    CONST char *dst;		/* Pathname of file to create/overwrite
sl@0
   500
				 * (native). */
sl@0
   501
    CONST Tcl_StatBuf *statBufPtr;
sl@0
   502
				/* Used to determine mode and blocksize. */
sl@0
   503
{
sl@0
   504
    int srcFd;
sl@0
   505
    int dstFd;
sl@0
   506
    unsigned blockSize;		/* Optimal I/O blocksize for filesystem */
sl@0
   507
    char *buffer;		/* Data buffer for copy */
sl@0
   508
    size_t nread;
sl@0
   509
sl@0
   510
    if ((srcFd = TclOSopen(src, O_RDONLY, 0)) < 0) {	/* INTL: Native. */
sl@0
   511
	return TCL_ERROR;
sl@0
   512
    }
sl@0
   513
sl@0
   514
    dstFd = TclOSopen(dst, O_CREAT|O_TRUNC|O_WRONLY,	/* INTL: Native. */
sl@0
   515
	    statBufPtr->st_mode);
sl@0
   516
    if (dstFd < 0) {
sl@0
   517
	close(srcFd); 
sl@0
   518
	return TCL_ERROR;
sl@0
   519
    }
sl@0
   520
sl@0
   521
#ifdef HAVE_ST_BLKSIZE
sl@0
   522
    blockSize = statBufPtr->st_blksize;
sl@0
   523
#else
sl@0
   524
#ifndef NO_FSTATFS
sl@0
   525
    {
sl@0
   526
	struct statfs fs;
sl@0
   527
	if (fstatfs(srcFd, &fs, sizeof(fs), 0) == 0) {
sl@0
   528
	    blockSize = fs.f_bsize;
sl@0
   529
	} else {
sl@0
   530
	    blockSize = 4096;
sl@0
   531
	}
sl@0
   532
    }
sl@0
   533
#else 
sl@0
   534
    blockSize = 4096;
sl@0
   535
#endif
sl@0
   536
#endif
sl@0
   537
sl@0
   538
    /* [SF Tcl Bug 1586470] Even if we HAVE_ST_BLKSIZE, there are
sl@0
   539
     * filesystems which report a bogus value for the blocksize.  An
sl@0
   540
     * example is the Andrew Filesystem (afs), reporting a blocksize
sl@0
   541
     * of 0. When detecting such a situation we now simply fall back
sl@0
   542
     * to a hardwired default size.
sl@0
   543
     */
sl@0
   544
sl@0
   545
    if (blockSize <= 0) {
sl@0
   546
        blockSize = 4096;
sl@0
   547
    }
sl@0
   548
    buffer = ckalloc(blockSize);
sl@0
   549
    while (1) {
sl@0
   550
	nread = read(srcFd, buffer, blockSize);
sl@0
   551
	if ((nread == (size_t)-1) || (nread == 0)) {
sl@0
   552
	    break;
sl@0
   553
	}
sl@0
   554
	if (write(dstFd, buffer, nread) != nread) {
sl@0
   555
	    nread = (size_t) -1;
sl@0
   556
	    break;
sl@0
   557
	}
sl@0
   558
    }
sl@0
   559
sl@0
   560
    ckfree(buffer);
sl@0
   561
    close(srcFd);
sl@0
   562
    if ((close(dstFd) != 0) || (nread == (size_t)-1)) {
sl@0
   563
	unlink(dst);					/* INTL: Native. */
sl@0
   564
	return TCL_ERROR;
sl@0
   565
    }
sl@0
   566
    if (CopyFileAtts(src, dst, statBufPtr) == TCL_ERROR) {
sl@0
   567
	/*
sl@0
   568
	 * The copy succeeded, but setting the permissions failed, so be in
sl@0
   569
	 * a consistent state, we remove the file that was created by the
sl@0
   570
	 * copy.
sl@0
   571
	 */
sl@0
   572
sl@0
   573
	unlink(dst);					/* INTL: Native. */
sl@0
   574
	return TCL_ERROR;
sl@0
   575
    }
sl@0
   576
    return TCL_OK;
sl@0
   577
}
sl@0
   578

sl@0
   579
/*
sl@0
   580
 *---------------------------------------------------------------------------
sl@0
   581
 *
sl@0
   582
 * TclpObjDeleteFile, TclpDeleteFile --
sl@0
   583
 *
sl@0
   584
 *      Removes a single file (not a directory).
sl@0
   585
 *
sl@0
   586
 * Results:
sl@0
   587
 *	If the file was successfully deleted, returns TCL_OK.  Otherwise
sl@0
   588
 *	the return value is TCL_ERROR and errno is set to indicate the
sl@0
   589
 *	error.  Some possible values for errno are:
sl@0
   590
 *
sl@0
   591
 *	EACCES:     a parent directory can't be read and/or written.
sl@0
   592
 *	EISDIR:	    path is a directory.
sl@0
   593
 *	ENOENT:	    path doesn't exist or is "".
sl@0
   594
 *
sl@0
   595
 * Side effects:
sl@0
   596
 *      The file is deleted, even if it is read-only.
sl@0
   597
 *
sl@0
   598
 *---------------------------------------------------------------------------
sl@0
   599
 */
sl@0
   600
sl@0
   601
int 
sl@0
   602
TclpObjDeleteFile(pathPtr)
sl@0
   603
    Tcl_Obj *pathPtr;
sl@0
   604
{
sl@0
   605
    return TclpDeleteFile(Tcl_FSGetNativePath(pathPtr));
sl@0
   606
}
sl@0
   607
sl@0
   608
int
sl@0
   609
TclpDeleteFile(path)
sl@0
   610
    CONST char *path;	/* Pathname of file to be removed (native). */
sl@0
   611
{
sl@0
   612
    if (unlink(path) != 0) {				/* INTL: Native. */
sl@0
   613
	return TCL_ERROR;
sl@0
   614
    }
sl@0
   615
    return TCL_OK;
sl@0
   616
}
sl@0
   617

sl@0
   618
/*
sl@0
   619
 *---------------------------------------------------------------------------
sl@0
   620
 *
sl@0
   621
 * TclpCreateDirectory, DoCreateDirectory --
sl@0
   622
 *
sl@0
   623
 *      Creates the specified directory.  All parent directories of the
sl@0
   624
 *	specified directory must already exist.  The directory is
sl@0
   625
 *	automatically created with permissions so that user can access
sl@0
   626
 *	the new directory and create new files or subdirectories in it.
sl@0
   627
 *
sl@0
   628
 * Results:
sl@0
   629
 *	If the directory was successfully created, returns TCL_OK.
sl@0
   630
 *	Otherwise the return value is TCL_ERROR and errno is set to
sl@0
   631
 *	indicate the error.  Some possible values for errno are:
sl@0
   632
 *
sl@0
   633
 *	EACCES:     a parent directory can't be read and/or written.
sl@0
   634
 *	EEXIST:	    path already exists.
sl@0
   635
 *	ENOENT:	    a parent directory doesn't exist.
sl@0
   636
 *
sl@0
   637
 * Side effects:
sl@0
   638
 *      A directory is created with the current umask, except that
sl@0
   639
 *	permission for u+rwx will always be added.
sl@0
   640
 *
sl@0
   641
 *---------------------------------------------------------------------------
sl@0
   642
 */
sl@0
   643
sl@0
   644
int 
sl@0
   645
TclpObjCreateDirectory(pathPtr)
sl@0
   646
    Tcl_Obj *pathPtr;
sl@0
   647
{
sl@0
   648
    return DoCreateDirectory(Tcl_FSGetNativePath(pathPtr));
sl@0
   649
}
sl@0
   650
sl@0
   651
static int
sl@0
   652
DoCreateDirectory(path)
sl@0
   653
    CONST char *path;	/* Pathname of directory to create (native). */
sl@0
   654
{
sl@0
   655
    mode_t mode;
sl@0
   656
sl@0
   657
    mode = umask(0);
sl@0
   658
    umask(mode);
sl@0
   659
sl@0
   660
    /*
sl@0
   661
     * umask return value is actually the inverse of the permissions.
sl@0
   662
     */
sl@0
   663
sl@0
   664
    mode = (0777 & ~mode) | S_IRUSR | S_IWUSR | S_IXUSR;
sl@0
   665
sl@0
   666
    if (mkdir(path, mode) != 0) {			/* INTL: Native. */
sl@0
   667
	return TCL_ERROR;
sl@0
   668
    }
sl@0
   669
    return TCL_OK;
sl@0
   670
}
sl@0
   671

sl@0
   672
/*
sl@0
   673
 *---------------------------------------------------------------------------
sl@0
   674
 *
sl@0
   675
 * TclpObjCopyDirectory --
sl@0
   676
 *
sl@0
   677
 *      Recursively copies a directory.  The target directory dst must
sl@0
   678
 *	not already exist.  Note that this function does not merge two
sl@0
   679
 *	directory hierarchies, even if the target directory is an an
sl@0
   680
 *	empty directory.
sl@0
   681
 *
sl@0
   682
 * Results:
sl@0
   683
 *	If the directory was successfully copied, returns TCL_OK.
sl@0
   684
 *	Otherwise the return value is TCL_ERROR, errno is set to indicate
sl@0
   685
 *	the error, and the pathname of the file that caused the error
sl@0
   686
 *	is stored in errorPtr.  See TclpObjCreateDirectory and 
sl@0
   687
 *	TclpObjCopyFile for a description of possible values for errno.
sl@0
   688
 *
sl@0
   689
 * Side effects:
sl@0
   690
 *      An exact copy of the directory hierarchy src will be created
sl@0
   691
 *	with the name dst.  If an error occurs, the error will
sl@0
   692
 *      be returned immediately, and remaining files will not be
sl@0
   693
 *	processed.
sl@0
   694
 *
sl@0
   695
 *---------------------------------------------------------------------------
sl@0
   696
 */
sl@0
   697
sl@0
   698
int 
sl@0
   699
TclpObjCopyDirectory(srcPathPtr, destPathPtr, errorPtr)
sl@0
   700
    Tcl_Obj *srcPathPtr;
sl@0
   701
    Tcl_Obj *destPathPtr;
sl@0
   702
    Tcl_Obj **errorPtr;
sl@0
   703
{
sl@0
   704
    Tcl_DString ds;
sl@0
   705
    Tcl_DString srcString, dstString;
sl@0
   706
    int ret;
sl@0
   707
    Tcl_Obj *transPtr;
sl@0
   708
    
sl@0
   709
    transPtr = Tcl_FSGetTranslatedPath(NULL,srcPathPtr);
sl@0
   710
    Tcl_UtfToExternalDString(NULL, 
sl@0
   711
			     (transPtr != NULL ? Tcl_GetString(transPtr) : NULL), 
sl@0
   712
			     -1, &srcString);
sl@0
   713
    if (transPtr != NULL) {
sl@0
   714
	Tcl_DecrRefCount(transPtr);
sl@0
   715
    }
sl@0
   716
    transPtr = Tcl_FSGetTranslatedPath(NULL,destPathPtr);
sl@0
   717
    Tcl_UtfToExternalDString(NULL, 
sl@0
   718
			     (transPtr != NULL ? Tcl_GetString(transPtr) : NULL), 
sl@0
   719
			     -1, &dstString);
sl@0
   720
    if (transPtr != NULL) {
sl@0
   721
	Tcl_DecrRefCount(transPtr);
sl@0
   722
    }
sl@0
   723
sl@0
   724
    ret = TraverseUnixTree(TraversalCopy, &srcString, &dstString, &ds, 0);
sl@0
   725
sl@0
   726
    Tcl_DStringFree(&srcString);
sl@0
   727
    Tcl_DStringFree(&dstString);
sl@0
   728
sl@0
   729
    if (ret != TCL_OK) {
sl@0
   730
	*errorPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds), -1);
sl@0
   731
	Tcl_DStringFree(&ds);
sl@0
   732
	Tcl_IncrRefCount(*errorPtr);
sl@0
   733
    }
sl@0
   734
    return ret;
sl@0
   735
}
sl@0
   736
sl@0
   737

sl@0
   738
/*
sl@0
   739
 *---------------------------------------------------------------------------
sl@0
   740
 *
sl@0
   741
 * TclpRemoveDirectory, DoRemoveDirectory --
sl@0
   742
 *
sl@0
   743
 *	Removes directory (and its contents, if the recursive flag is set).
sl@0
   744
 *
sl@0
   745
 * Results:
sl@0
   746
 *	If the directory was successfully removed, returns TCL_OK.
sl@0
   747
 *	Otherwise the return value is TCL_ERROR, errno is set to indicate
sl@0
   748
 *	the error, and the pathname of the file that caused the error
sl@0
   749
 *	is stored in errorPtr.  Some possible values for errno are:
sl@0
   750
 *
sl@0
   751
 *	EACCES:     path directory can't be read and/or written.
sl@0
   752
 *	EEXIST:	    path is a non-empty directory.
sl@0
   753
 *	EINVAL:	    path is a root directory.
sl@0
   754
 *	ENOENT:	    path doesn't exist or is "".
sl@0
   755
 * 	ENOTDIR:    path is not a directory.
sl@0
   756
 *
sl@0
   757
 * Side effects:
sl@0
   758
 *	Directory removed.  If an error occurs, the error will be returned
sl@0
   759
 *	immediately, and remaining files will not be deleted.
sl@0
   760
 *
sl@0
   761
 *---------------------------------------------------------------------------
sl@0
   762
 */
sl@0
   763
 
sl@0
   764
int 
sl@0
   765
TclpObjRemoveDirectory(pathPtr, recursive, errorPtr)
sl@0
   766
    Tcl_Obj *pathPtr;
sl@0
   767
    int recursive;
sl@0
   768
    Tcl_Obj **errorPtr;
sl@0
   769
{
sl@0
   770
    Tcl_DString ds;
sl@0
   771
    Tcl_DString pathString;
sl@0
   772
    int ret;
sl@0
   773
    Tcl_Obj *transPtr = Tcl_FSGetTranslatedPath(NULL, pathPtr);
sl@0
   774
sl@0
   775
    Tcl_UtfToExternalDString(NULL, 
sl@0
   776
			     (transPtr != NULL ? Tcl_GetString(transPtr) : NULL), 
sl@0
   777
			     -1, &pathString);
sl@0
   778
    if (transPtr != NULL) {
sl@0
   779
	Tcl_DecrRefCount(transPtr);
sl@0
   780
    }
sl@0
   781
    ret = DoRemoveDirectory(&pathString, recursive, &ds);
sl@0
   782
    Tcl_DStringFree(&pathString);
sl@0
   783
sl@0
   784
    if (ret != TCL_OK) {
sl@0
   785
	*errorPtr = Tcl_NewStringObj(Tcl_DStringValue(&ds), -1);
sl@0
   786
	Tcl_DStringFree(&ds);
sl@0
   787
	Tcl_IncrRefCount(*errorPtr);
sl@0
   788
    }
sl@0
   789
    return ret;
sl@0
   790
}
sl@0
   791
sl@0
   792
static int
sl@0
   793
DoRemoveDirectory(pathPtr, recursive, errorPtr)
sl@0
   794
    Tcl_DString *pathPtr;	/* Pathname of directory to be removed
sl@0
   795
				 * (native). */
sl@0
   796
    int recursive;		/* If non-zero, removes directories that
sl@0
   797
				 * are nonempty.  Otherwise, will only remove
sl@0
   798
				 * empty directories. */
sl@0
   799
    Tcl_DString *errorPtr;	/* If non-NULL, uninitialized or free
sl@0
   800
				 * DString filled with UTF-8 name of file
sl@0
   801
				 * causing error. */
sl@0
   802
{
sl@0
   803
    CONST char *path;
sl@0
   804
    mode_t oldPerm = 0;
sl@0
   805
    int result;
sl@0
   806
    
sl@0
   807
    path = Tcl_DStringValue(pathPtr);
sl@0
   808
   
sl@0
   809
#ifdef __SYMBIAN32__  
sl@0
   810
    TclPrint1(" == DoRemoveDirectory() - \"%S\".\n", path);
sl@0
   811
#endif
sl@0
   812
    
sl@0
   813
    if (recursive != 0) {
sl@0
   814
	/* We should try to change permissions so this can be deleted */
sl@0
   815
	Tcl_StatBuf statBuf;
sl@0
   816
	int newPerm;
sl@0
   817
sl@0
   818
	if (TclOSstat(path, &statBuf) == 0) {
sl@0
   819
	    oldPerm = (mode_t) (statBuf.st_mode & 0x00007FFF);
sl@0
   820
	}
sl@0
   821
	
sl@0
   822
	newPerm = oldPerm | (64+128+256);
sl@0
   823
	chmod(path, (mode_t) newPerm);
sl@0
   824
    }
sl@0
   825
    
sl@0
   826
    if (rmdir(path) == 0) {				/* INTL: Native. */
sl@0
   827
	return TCL_OK;
sl@0
   828
    }
sl@0
   829
    if (errno == ENOTEMPTY) {
sl@0
   830
	errno = EEXIST;
sl@0
   831
    }
sl@0
   832
sl@0
   833
    result = TCL_OK;
sl@0
   834
    if ((errno != EEXIST) || (recursive == 0)) {
sl@0
   835
	if (errorPtr != NULL) {
sl@0
   836
	    Tcl_ExternalToUtfDString(NULL, path, -1, errorPtr);
sl@0
   837
	}
sl@0
   838
	result = TCL_ERROR;
sl@0
   839
    }
sl@0
   840
    
sl@0
   841
    /*
sl@0
   842
     * The directory is nonempty, but the recursive flag has been
sl@0
   843
     * specified, so we recursively remove all the files in the directory.
sl@0
   844
     */
sl@0
   845
sl@0
   846
    if (result == TCL_OK) {
sl@0
   847
	result = TraverseUnixTree(TraversalDelete, pathPtr, NULL, errorPtr, 1);
sl@0
   848
    }
sl@0
   849
    
sl@0
   850
    if ((result != TCL_OK) && (recursive != 0)) {
sl@0
   851
        /* Try to restore permissions */
sl@0
   852
        chmod(path, oldPerm);
sl@0
   853
    }
sl@0
   854
    return result;
sl@0
   855
}
sl@0
   856

sl@0
   857
/*
sl@0
   858
 *---------------------------------------------------------------------------
sl@0
   859
 *
sl@0
   860
 * TraverseUnixTree --
sl@0
   861
 *
sl@0
   862
 *      Traverse directory tree specified by sourcePtr, calling the function 
sl@0
   863
 *	traverseProc for each file and directory encountered.  If destPtr 
sl@0
   864
 *	is non-null, each of name in the sourcePtr directory is appended to 
sl@0
   865
 *	the directory specified by destPtr and passed as the second argument 
sl@0
   866
 *	to traverseProc() .
sl@0
   867
 *
sl@0
   868
 * Results:
sl@0
   869
 *      Standard Tcl result.
sl@0
   870
 *
sl@0
   871
 * Side effects:
sl@0
   872
 *      None caused by TraverseUnixTree, however the user specified 
sl@0
   873
 *	traverseProc() may change state.  If an error occurs, the error will
sl@0
   874
 *      be returned immediately, and remaining files will not be processed.
sl@0
   875
 *
sl@0
   876
 *---------------------------------------------------------------------------
sl@0
   877
 */
sl@0
   878
sl@0
   879
static int 
sl@0
   880
TraverseUnixTree(traverseProc, sourcePtr, targetPtr, errorPtr, doRewind)
sl@0
   881
    TraversalProc *traverseProc;/* Function to call for every file and
sl@0
   882
				 * directory in source hierarchy. */
sl@0
   883
    Tcl_DString *sourcePtr;	/* Pathname of source directory to be
sl@0
   884
				 * traversed (native). */
sl@0
   885
    Tcl_DString *targetPtr;	/* Pathname of directory to traverse in
sl@0
   886
				 * parallel with source directory (native). */
sl@0
   887
    Tcl_DString *errorPtr;	/* If non-NULL, uninitialized or free
sl@0
   888
				 * DString filled with UTF-8 name of file
sl@0
   889
				 * causing error. */
sl@0
   890
    int doRewind;		/* Flag indicating that to ensure complete
sl@0
   891
    				 * traversal of source hierarchy, the readdir
sl@0
   892
    				 * loop should be rewound whenever
sl@0
   893
    				 * traverseProc has returned TCL_OK; this is
sl@0
   894
    				 * required when traverseProc modifies the
sl@0
   895
    				 * source hierarchy, e.g. by deleting files. */
sl@0
   896
{
sl@0
   897
    Tcl_StatBuf statBuf;
sl@0
   898
    CONST char *source, *errfile;
sl@0
   899
    int result, sourceLen;
sl@0
   900
    int targetLen;
sl@0
   901
#ifndef HAVE_FTS
sl@0
   902
    int numProcessed = 0;
sl@0
   903
    Tcl_DirEntry *dirEntPtr;
sl@0
   904
    DIR *dirPtr;
sl@0
   905
#else
sl@0
   906
    CONST char *paths[2] = {NULL, NULL};
sl@0
   907
    FTS *fts = NULL;
sl@0
   908
    FTSENT *ent;
sl@0
   909
#endif
sl@0
   910
sl@0
   911
    errfile = NULL;
sl@0
   912
    result = TCL_OK;
sl@0
   913
    targetLen = 0;		/* lint. */
sl@0
   914
sl@0
   915
    source = Tcl_DStringValue(sourcePtr);
sl@0
   916
    if (TclOSlstat(source, &statBuf) != 0) {		/* INTL: Native. */
sl@0
   917
	errfile = source;
sl@0
   918
	goto end;
sl@0
   919
    }
sl@0
   920
    if (!S_ISDIR(statBuf.st_mode)) {
sl@0
   921
	/*
sl@0
   922
	 * Process the regular file
sl@0
   923
	 */
sl@0
   924
sl@0
   925
	return (*traverseProc)(sourcePtr, targetPtr, &statBuf, DOTREE_F,
sl@0
   926
		errorPtr);
sl@0
   927
    }
sl@0
   928
#ifndef HAVE_FTS
sl@0
   929
    dirPtr = opendir(source);				/* INTL: Native. */
sl@0
   930
    if (dirPtr == NULL) {
sl@0
   931
	/* 
sl@0
   932
	 * Can't read directory
sl@0
   933
	 */
sl@0
   934
sl@0
   935
	errfile = source;
sl@0
   936
	goto end;
sl@0
   937
    }
sl@0
   938
    result = (*traverseProc)(sourcePtr, targetPtr, &statBuf, DOTREE_PRED,
sl@0
   939
	    errorPtr);
sl@0
   940
    if (result != TCL_OK) {
sl@0
   941
	closedir(dirPtr);
sl@0
   942
	return result;
sl@0
   943
    }
sl@0
   944
sl@0
   945
    Tcl_DStringAppend(sourcePtr, "/", 1);
sl@0
   946
    sourceLen = Tcl_DStringLength(sourcePtr);
sl@0
   947
sl@0
   948
    if (targetPtr != NULL) {
sl@0
   949
	Tcl_DStringAppend(targetPtr, "/", 1);
sl@0
   950
	targetLen = Tcl_DStringLength(targetPtr);
sl@0
   951
    }
sl@0
   952
sl@0
   953
    while ((dirEntPtr = TclOSreaddir(dirPtr)) != NULL) { /* INTL: Native. */
sl@0
   954
	if ((dirEntPtr->d_name[0] == '.')
sl@0
   955
		&& ((dirEntPtr->d_name[1] == '\0')
sl@0
   956
			|| (strcmp(dirEntPtr->d_name, "..") == 0))) {
sl@0
   957
	    continue;
sl@0
   958
	}
sl@0
   959
sl@0
   960
	/*
sl@0
   961
	 * Append name after slash, and recurse on the file.
sl@0
   962
	 */
sl@0
   963
sl@0
   964
	Tcl_DStringAppend(sourcePtr, dirEntPtr->d_name, -1);
sl@0
   965
	if (targetPtr != NULL) {
sl@0
   966
	    Tcl_DStringAppend(targetPtr, dirEntPtr->d_name, -1);
sl@0
   967
	}
sl@0
   968
	result = TraverseUnixTree(traverseProc, sourcePtr, targetPtr,
sl@0
   969
		errorPtr, doRewind);
sl@0
   970
	if (result != TCL_OK) {
sl@0
   971
	    break;
sl@0
   972
	} else {
sl@0
   973
	    numProcessed++;
sl@0
   974
	}
sl@0
   975
sl@0
   976
	/*
sl@0
   977
	 * Remove name after slash.
sl@0
   978
	 */
sl@0
   979
sl@0
   980
	Tcl_DStringSetLength(sourcePtr, sourceLen);
sl@0
   981
	if (targetPtr != NULL) {
sl@0
   982
	    Tcl_DStringSetLength(targetPtr, targetLen);
sl@0
   983
	}
sl@0
   984
	if (doRewind && (numProcessed > MAX_READDIR_UNLINK_THRESHOLD)) {
sl@0
   985
	    /*
sl@0
   986
	     * Call rewinddir if we've called unlink or rmdir so many times
sl@0
   987
	     * (since the opendir or the previous rewinddir), to avoid
sl@0
   988
	     * a NULL-return that may a symptom of a buggy readdir.
sl@0
   989
	     */
sl@0
   990
	    rewinddir(dirPtr);
sl@0
   991
	    numProcessed = 0;
sl@0
   992
	}
sl@0
   993
    }
sl@0
   994
    closedir(dirPtr);
sl@0
   995
sl@0
   996
    /*
sl@0
   997
     * Strip off the trailing slash we added
sl@0
   998
     */
sl@0
   999
sl@0
  1000
    Tcl_DStringSetLength(sourcePtr, sourceLen - 1);
sl@0
  1001
    if (targetPtr != NULL) {
sl@0
  1002
	Tcl_DStringSetLength(targetPtr, targetLen - 1);
sl@0
  1003
    }
sl@0
  1004
sl@0
  1005
    if (result == TCL_OK) {
sl@0
  1006
	/*
sl@0
  1007
	 * Call traverseProc() on a directory after visiting all the
sl@0
  1008
	 * files in that directory.
sl@0
  1009
	 */
sl@0
  1010
sl@0
  1011
	result = (*traverseProc)(sourcePtr, targetPtr, &statBuf, DOTREE_POSTD,
sl@0
  1012
		errorPtr);
sl@0
  1013
    }
sl@0
  1014
#else /* HAVE_FTS */
sl@0
  1015
    paths[0] = source;
sl@0
  1016
    fts = fts_open((char**)paths, FTS_PHYSICAL | FTS_NOCHDIR |
sl@0
  1017
	    (noFtsStat || doRewind ? FTS_NOSTAT : 0),  NULL);
sl@0
  1018
    if (fts == NULL) {
sl@0
  1019
	errfile = source;
sl@0
  1020
	goto end;
sl@0
  1021
    }
sl@0
  1022
sl@0
  1023
    sourceLen = Tcl_DStringLength(sourcePtr);
sl@0
  1024
    if (targetPtr != NULL) {
sl@0
  1025
	targetLen = Tcl_DStringLength(targetPtr);
sl@0
  1026
    }
sl@0
  1027
sl@0
  1028
    while ((ent = fts_read(fts)) != NULL) {
sl@0
  1029
	unsigned short info = ent->fts_info;
sl@0
  1030
	char * path = ent->fts_path + sourceLen;
sl@0
  1031
	unsigned short pathlen = ent->fts_pathlen - sourceLen;
sl@0
  1032
	int type;
sl@0
  1033
	Tcl_StatBuf *statBufPtr = NULL;
sl@0
  1034
	
sl@0
  1035
	if (info == FTS_DNR || info == FTS_ERR || info == FTS_NS) {
sl@0
  1036
	    errfile = ent->fts_path;
sl@0
  1037
	    break;
sl@0
  1038
	}
sl@0
  1039
	Tcl_DStringAppend(sourcePtr, path, pathlen);
sl@0
  1040
	if (targetPtr != NULL) {
sl@0
  1041
	    Tcl_DStringAppend(targetPtr, path, pathlen);
sl@0
  1042
	}
sl@0
  1043
	switch (info) {
sl@0
  1044
	    case FTS_D:
sl@0
  1045
		type = DOTREE_PRED;
sl@0
  1046
		break;
sl@0
  1047
	    case FTS_DP:
sl@0
  1048
		type = DOTREE_POSTD;
sl@0
  1049
		break;
sl@0
  1050
	    default:
sl@0
  1051
		type = DOTREE_F;
sl@0
  1052
		break;
sl@0
  1053
	}
sl@0
  1054
	if (!doRewind) { /* no need to stat for delete */
sl@0
  1055
	    if (noFtsStat) {
sl@0
  1056
		statBufPtr = &statBuf;
sl@0
  1057
		if (TclOSlstat(ent->fts_path, statBufPtr) != 0) {
sl@0
  1058
		    errfile = ent->fts_path;
sl@0
  1059
		    break;
sl@0
  1060
		}
sl@0
  1061
	    } else {
sl@0
  1062
		statBufPtr = ent->fts_statp;
sl@0
  1063
	    }
sl@0
  1064
	}
sl@0
  1065
	result = (*traverseProc)(sourcePtr, targetPtr, statBufPtr, type,
sl@0
  1066
		errorPtr);
sl@0
  1067
	if (result != TCL_OK) {
sl@0
  1068
	    break;
sl@0
  1069
	}
sl@0
  1070
	Tcl_DStringSetLength(sourcePtr, sourceLen);
sl@0
  1071
	if (targetPtr != NULL) {
sl@0
  1072
	    Tcl_DStringSetLength(targetPtr, targetLen);
sl@0
  1073
	}
sl@0
  1074
    }
sl@0
  1075
#endif /* HAVE_FTS */
sl@0
  1076
sl@0
  1077
    end:
sl@0
  1078
    if (errfile != NULL) {
sl@0
  1079
	if (errorPtr != NULL) {
sl@0
  1080
	    Tcl_ExternalToUtfDString(NULL, errfile, -1, errorPtr);
sl@0
  1081
	}
sl@0
  1082
	result = TCL_ERROR;
sl@0
  1083
    }
sl@0
  1084
#ifdef HAVE_FTS
sl@0
  1085
    if (fts != NULL) {
sl@0
  1086
	fts_close(fts);
sl@0
  1087
    }
sl@0
  1088
#endif /* HAVE_FTS */
sl@0
  1089
sl@0
  1090
    return result;
sl@0
  1091
}
sl@0
  1092

sl@0
  1093
/*
sl@0
  1094
 *----------------------------------------------------------------------
sl@0
  1095
 *
sl@0
  1096
 * TraversalCopy
sl@0
  1097
 *
sl@0
  1098
 *      Called from TraverseUnixTree in order to execute a recursive copy
sl@0
  1099
 *      of a directory.
sl@0
  1100
 *
sl@0
  1101
 * Results:
sl@0
  1102
 *      Standard Tcl result.
sl@0
  1103
 *
sl@0
  1104
 * Side effects:
sl@0
  1105
 *      The file or directory src may be copied to dst, depending on 
sl@0
  1106
 *      the value of type.
sl@0
  1107
 *      
sl@0
  1108
 *----------------------------------------------------------------------
sl@0
  1109
 */
sl@0
  1110
sl@0
  1111
static int 
sl@0
  1112
TraversalCopy(srcPtr, dstPtr, statBufPtr, type, errorPtr) 
sl@0
  1113
    Tcl_DString *srcPtr;	/* Source pathname to copy (native). */
sl@0
  1114
    Tcl_DString *dstPtr;	/* Destination pathname of copy (native). */
sl@0
  1115
    CONST Tcl_StatBuf *statBufPtr;
sl@0
  1116
				/* Stat info for file specified by srcPtr. */
sl@0
  1117
    int type;                   /* Reason for call - see TraverseUnixTree(). */
sl@0
  1118
    Tcl_DString *errorPtr;	/* If non-NULL, uninitialized or free
sl@0
  1119
				 * DString filled with UTF-8 name of file
sl@0
  1120
				 * causing error. */
sl@0
  1121
{
sl@0
  1122
    switch (type) {
sl@0
  1123
	case DOTREE_F:
sl@0
  1124
	    if (DoCopyFile(Tcl_DStringValue(srcPtr), Tcl_DStringValue(dstPtr),
sl@0
  1125
		    statBufPtr) == TCL_OK) {
sl@0
  1126
		return TCL_OK;
sl@0
  1127
	    }
sl@0
  1128
	    break;
sl@0
  1129
sl@0
  1130
	case DOTREE_PRED:
sl@0
  1131
	    if (DoCreateDirectory(Tcl_DStringValue(dstPtr)) == TCL_OK) {
sl@0
  1132
		return TCL_OK;
sl@0
  1133
	    }
sl@0
  1134
	    break;
sl@0
  1135
sl@0
  1136
	case DOTREE_POSTD:
sl@0
  1137
	    if (CopyFileAtts(Tcl_DStringValue(srcPtr),
sl@0
  1138
		    Tcl_DStringValue(dstPtr), statBufPtr) == TCL_OK) {
sl@0
  1139
		return TCL_OK;
sl@0
  1140
	    }
sl@0
  1141
	    break;
sl@0
  1142
sl@0
  1143
    }
sl@0
  1144
sl@0
  1145
    /*
sl@0
  1146
     * There shouldn't be a problem with src, because we already checked it
sl@0
  1147
     * to get here.
sl@0
  1148
     */
sl@0
  1149
sl@0
  1150
    if (errorPtr != NULL) {
sl@0
  1151
	Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(dstPtr),
sl@0
  1152
		Tcl_DStringLength(dstPtr), errorPtr);
sl@0
  1153
    }
sl@0
  1154
    return TCL_ERROR;
sl@0
  1155
}
sl@0
  1156

sl@0
  1157
/*
sl@0
  1158
 *---------------------------------------------------------------------------
sl@0
  1159
 *
sl@0
  1160
 * TraversalDelete --
sl@0
  1161
 *
sl@0
  1162
 *      Called by procedure TraverseUnixTree for every file and directory
sl@0
  1163
 *	that it encounters in a directory hierarchy. This procedure unlinks
sl@0
  1164
 *      files, and removes directories after all the containing files 
sl@0
  1165
 *      have been processed.
sl@0
  1166
 *
sl@0
  1167
 * Results:
sl@0
  1168
 *      Standard Tcl result.
sl@0
  1169
 *
sl@0
  1170
 * Side effects:
sl@0
  1171
 *      Files or directory specified by src will be deleted.
sl@0
  1172
 *
sl@0
  1173
 *----------------------------------------------------------------------
sl@0
  1174
 */
sl@0
  1175
sl@0
  1176
static int
sl@0
  1177
TraversalDelete(srcPtr, ignore, statBufPtr, type, errorPtr) 
sl@0
  1178
    Tcl_DString *srcPtr;	/* Source pathname (native). */
sl@0
  1179
    Tcl_DString *ignore;	/* Destination pathname (not used). */
sl@0
  1180
    CONST Tcl_StatBuf *statBufPtr;
sl@0
  1181
				/* Stat info for file specified by srcPtr. */
sl@0
  1182
    int type;                   /* Reason for call - see TraverseUnixTree(). */
sl@0
  1183
    Tcl_DString *errorPtr;	/* If non-NULL, uninitialized or free
sl@0
  1184
				 * DString filled with UTF-8 name of file
sl@0
  1185
				 * causing error. */
sl@0
  1186
{
sl@0
  1187
    switch (type) {
sl@0
  1188
        case DOTREE_F: {
sl@0
  1189
	    if (TclpDeleteFile(Tcl_DStringValue(srcPtr)) == 0) {
sl@0
  1190
		return TCL_OK;
sl@0
  1191
	    }
sl@0
  1192
	    break;
sl@0
  1193
	}
sl@0
  1194
        case DOTREE_PRED: {
sl@0
  1195
	    return TCL_OK;
sl@0
  1196
	}
sl@0
  1197
        case DOTREE_POSTD: {
sl@0
  1198
	    if (DoRemoveDirectory(srcPtr, 0, NULL) == 0) {
sl@0
  1199
		return TCL_OK;
sl@0
  1200
	    }
sl@0
  1201
	    break;
sl@0
  1202
	}	    
sl@0
  1203
    }
sl@0
  1204
    if (errorPtr != NULL) {
sl@0
  1205
	Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(srcPtr),
sl@0
  1206
		Tcl_DStringLength(srcPtr), errorPtr);
sl@0
  1207
    }
sl@0
  1208
    return TCL_ERROR;
sl@0
  1209
}
sl@0
  1210

sl@0
  1211
/*
sl@0
  1212
 *---------------------------------------------------------------------------
sl@0
  1213
 *
sl@0
  1214
 * CopyFileAtts --
sl@0
  1215
 *
sl@0
  1216
 *	Copy the file attributes such as owner, group, permissions,
sl@0
  1217
 *	and modification date from one file to another.
sl@0
  1218
 *
sl@0
  1219
 * Results:
sl@0
  1220
 *	Standard Tcl result.
sl@0
  1221
 *
sl@0
  1222
 * Side effects:
sl@0
  1223
 *	user id, group id, permission bits, last modification time, and
sl@0
  1224
 *	last access time are updated in the new file to reflect the
sl@0
  1225
 *	old file.
sl@0
  1226
 *
sl@0
  1227
 *---------------------------------------------------------------------------
sl@0
  1228
 */
sl@0
  1229
sl@0
  1230
static int
sl@0
  1231
CopyFileAtts(src, dst, statBufPtr) 
sl@0
  1232
    CONST char *src;		/* Path name of source file (native). */
sl@0
  1233
    CONST char *dst;		/* Path name of target file (native). */
sl@0
  1234
    CONST Tcl_StatBuf *statBufPtr;
sl@0
  1235
				/* Stat info for source file */
sl@0
  1236
{
sl@0
  1237
    struct utimbuf tval;
sl@0
  1238
    mode_t newMode;
sl@0
  1239
    
sl@0
  1240
    newMode = statBufPtr->st_mode
sl@0
  1241
	    & (S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO);
sl@0
  1242
	
sl@0
  1243
    /* 
sl@0
  1244
     * Note that if you copy a setuid file that is owned by someone
sl@0
  1245
     * else, and you are not root, then the copy will be setuid to you.
sl@0
  1246
     * The most correct implementation would probably be to have the
sl@0
  1247
     * copy not setuid to anyone if the original file was owned by 
sl@0
  1248
     * someone else, but this corner case isn't currently handled.
sl@0
  1249
     * It would require another lstat(), or getuid().
sl@0
  1250
     */
sl@0
  1251
    
sl@0
  1252
    if (chmod(dst, newMode)) {				/* INTL: Native. */
sl@0
  1253
	newMode &= ~(S_ISUID | S_ISGID);
sl@0
  1254
	if (chmod(dst, newMode)) {			/* INTL: Native. */
sl@0
  1255
	    return TCL_ERROR;
sl@0
  1256
	}
sl@0
  1257
    }
sl@0
  1258
sl@0
  1259
    tval.actime = statBufPtr->st_atime; 
sl@0
  1260
    tval.modtime = statBufPtr->st_mtime; 
sl@0
  1261
sl@0
  1262
    if (utime(dst, &tval)) {				/* INTL: Native. */
sl@0
  1263
	return TCL_ERROR;
sl@0
  1264
    }
sl@0
  1265
#ifdef HAVE_COPYFILE
sl@0
  1266
#ifdef WEAK_IMPORT_COPYFILE
sl@0
  1267
    if (copyfile != NULL)
sl@0
  1268
#endif
sl@0
  1269
    copyfile(src, dst, NULL, COPYFILE_XATTR|COPYFILE_ACL);
sl@0
  1270
#endif
sl@0
  1271
    return TCL_OK;
sl@0
  1272
}
sl@0
  1273
sl@0
  1274

sl@0
  1275
/*
sl@0
  1276
 *----------------------------------------------------------------------
sl@0
  1277
 *
sl@0
  1278
 * GetGroupAttribute
sl@0
  1279
 *
sl@0
  1280
 *      Gets the group attribute of a file.
sl@0
  1281
 *
sl@0
  1282
 * Results:
sl@0
  1283
 *      Standard TCL result. Returns a new Tcl_Obj in attributePtrPtr
sl@0
  1284
 *	if there is no error.
sl@0
  1285
 *
sl@0
  1286
 * Side effects:
sl@0
  1287
 *      A new object is allocated.
sl@0
  1288
 *      
sl@0
  1289
 *----------------------------------------------------------------------
sl@0
  1290
 */
sl@0
  1291
sl@0
  1292
static int
sl@0
  1293
GetGroupAttribute(interp, objIndex, fileName, attributePtrPtr)
sl@0
  1294
    Tcl_Interp *interp;		/* The interp we are using for errors. */
sl@0
  1295
    int objIndex;		/* The index of the attribute. */
sl@0
  1296
    Tcl_Obj *fileName;  	/* The name of the file (UTF-8). */
sl@0
  1297
    Tcl_Obj **attributePtrPtr;	/* A pointer to return the object with. */
sl@0
  1298
{
sl@0
  1299
    Tcl_StatBuf statBuf;
sl@0
  1300
    struct group *groupPtr;
sl@0
  1301
    int result;
sl@0
  1302
sl@0
  1303
    result = TclpObjStat(fileName, &statBuf);
sl@0
  1304
    
sl@0
  1305
    if (result != 0) {
sl@0
  1306
	Tcl_AppendResult(interp, "could not read \"", 
sl@0
  1307
		Tcl_GetString(fileName), "\": ",
sl@0
  1308
		Tcl_PosixError(interp), (char *) NULL);
sl@0
  1309
	return TCL_ERROR;
sl@0
  1310
    }
sl@0
  1311
sl@0
  1312
    groupPtr = TclpGetGrGid(statBuf.st_gid);
sl@0
  1313
sl@0
  1314
    if (result == -1 || groupPtr == NULL) {
sl@0
  1315
	*attributePtrPtr = Tcl_NewIntObj((int) statBuf.st_gid);
sl@0
  1316
    } else {
sl@0
  1317
	Tcl_DString ds;
sl@0
  1318
	CONST char *utf;
sl@0
  1319
sl@0
  1320
	utf = Tcl_ExternalToUtfDString(NULL, groupPtr->gr_name, -1, &ds); 
sl@0
  1321
	*attributePtrPtr = Tcl_NewStringObj(utf, -1);
sl@0
  1322
	Tcl_DStringFree(&ds);
sl@0
  1323
    }
sl@0
  1324
    endgrent();
sl@0
  1325
    return TCL_OK;
sl@0
  1326
}
sl@0
  1327

sl@0
  1328
/*
sl@0
  1329
 *----------------------------------------------------------------------
sl@0
  1330
 *
sl@0
  1331
 * GetOwnerAttribute
sl@0
  1332
 *
sl@0
  1333
 *      Gets the owner attribute of a file.
sl@0
  1334
 *
sl@0
  1335
 * Results:
sl@0
  1336
 *      Standard TCL result. Returns a new Tcl_Obj in attributePtrPtr
sl@0
  1337
 *	if there is no error.
sl@0
  1338
 *
sl@0
  1339
 * Side effects:
sl@0
  1340
 *      A new object is allocated.
sl@0
  1341
 *      
sl@0
  1342
 *----------------------------------------------------------------------
sl@0
  1343
 */
sl@0
  1344
sl@0
  1345
static int
sl@0
  1346
GetOwnerAttribute(interp, objIndex, fileName, attributePtrPtr)
sl@0
  1347
    Tcl_Interp *interp;		/* The interp we are using for errors. */
sl@0
  1348
    int objIndex;		/* The index of the attribute. */
sl@0
  1349
    Tcl_Obj *fileName;  	/* The name of the file (UTF-8). */
sl@0
  1350
    Tcl_Obj **attributePtrPtr;	/* A pointer to return the object with. */
sl@0
  1351
{
sl@0
  1352
    Tcl_StatBuf statBuf;
sl@0
  1353
    struct passwd *pwPtr;
sl@0
  1354
    int result;
sl@0
  1355
sl@0
  1356
    result = TclpObjStat(fileName, &statBuf);
sl@0
  1357
    
sl@0
  1358
    if (result != 0) {
sl@0
  1359
	Tcl_AppendResult(interp, "could not read \"", 
sl@0
  1360
		Tcl_GetString(fileName), "\": ",
sl@0
  1361
		Tcl_PosixError(interp), (char *) NULL);
sl@0
  1362
	return TCL_ERROR;
sl@0
  1363
    }
sl@0
  1364
sl@0
  1365
    pwPtr = TclpGetPwUid(statBuf.st_uid);
sl@0
  1366
sl@0
  1367
    if (result == -1 || pwPtr == NULL) {
sl@0
  1368
	*attributePtrPtr = Tcl_NewIntObj((int) statBuf.st_uid);
sl@0
  1369
    } else {
sl@0
  1370
	Tcl_DString ds;
sl@0
  1371
	CONST char *utf;
sl@0
  1372
sl@0
  1373
	utf = Tcl_ExternalToUtfDString(NULL, pwPtr->pw_name, -1, &ds); 
sl@0
  1374
	*attributePtrPtr = Tcl_NewStringObj(utf, Tcl_DStringLength(&ds));
sl@0
  1375
	Tcl_DStringFree(&ds);
sl@0
  1376
    }
sl@0
  1377
    endpwent();
sl@0
  1378
    return TCL_OK;
sl@0
  1379
}
sl@0
  1380

sl@0
  1381
/*
sl@0
  1382
 *----------------------------------------------------------------------
sl@0
  1383
 *
sl@0
  1384
 * GetPermissionsAttribute
sl@0
  1385
 *
sl@0
  1386
 *      Gets the group attribute of a file.
sl@0
  1387
 *
sl@0
  1388
 * Results:
sl@0
  1389
 *      Standard TCL result. Returns a new Tcl_Obj in attributePtrPtr
sl@0
  1390
 *	if there is no error. The object will have ref count 0.
sl@0
  1391
 *
sl@0
  1392
 * Side effects:
sl@0
  1393
 *      A new object is allocated.
sl@0
  1394
 *      
sl@0
  1395
 *----------------------------------------------------------------------
sl@0
  1396
 */
sl@0
  1397
sl@0
  1398
static int
sl@0
  1399
GetPermissionsAttribute(interp, objIndex, fileName, attributePtrPtr)
sl@0
  1400
    Tcl_Interp *interp;		    /* The interp we are using for errors. */
sl@0
  1401
    int objIndex;		    /* The index of the attribute. */
sl@0
  1402
    Tcl_Obj *fileName;  	    /* The name of the file (UTF-8). */
sl@0
  1403
    Tcl_Obj **attributePtrPtr;	    /* A pointer to return the object with. */
sl@0
  1404
{
sl@0
  1405
    Tcl_StatBuf statBuf;
sl@0
  1406
    char returnString[7];
sl@0
  1407
    int result;
sl@0
  1408
sl@0
  1409
    result = TclpObjStat(fileName, &statBuf);
sl@0
  1410
    
sl@0
  1411
    if (result != 0) {
sl@0
  1412
	Tcl_AppendResult(interp, "could not read \"", 
sl@0
  1413
		Tcl_GetString(fileName), "\": ",
sl@0
  1414
		Tcl_PosixError(interp), (char *) NULL);
sl@0
  1415
	return TCL_ERROR;
sl@0
  1416
    }
sl@0
  1417
sl@0
  1418
    sprintf(returnString, "%0#5lo", (long) (statBuf.st_mode & 0x00007FFF));
sl@0
  1419
sl@0
  1420
    *attributePtrPtr = Tcl_NewStringObj(returnString, -1);
sl@0
  1421
    
sl@0
  1422
    return TCL_OK;
sl@0
  1423
}
sl@0
  1424

sl@0
  1425
/*
sl@0
  1426
 *---------------------------------------------------------------------------
sl@0
  1427
 *
sl@0
  1428
 * SetGroupAttribute --
sl@0
  1429
 *
sl@0
  1430
 *      Sets the group of the file to the specified group.
sl@0
  1431
 *
sl@0
  1432
 * Results:
sl@0
  1433
 *      Standard TCL result.
sl@0
  1434
 *
sl@0
  1435
 * Side effects:
sl@0
  1436
 *      As above.
sl@0
  1437
 *      
sl@0
  1438
 *---------------------------------------------------------------------------
sl@0
  1439
 */
sl@0
  1440
sl@0
  1441
static int
sl@0
  1442
SetGroupAttribute(interp, objIndex, fileName, attributePtr)
sl@0
  1443
    Tcl_Interp *interp;		    /* The interp for error reporting. */
sl@0
  1444
    int objIndex;		    /* The index of the attribute. */
sl@0
  1445
    Tcl_Obj *fileName;	            /* The name of the file (UTF-8). */
sl@0
  1446
    Tcl_Obj *attributePtr;	    /* New group for file. */
sl@0
  1447
{
sl@0
  1448
    long gid;
sl@0
  1449
    int result;
sl@0
  1450
    CONST char *native;
sl@0
  1451
sl@0
  1452
    if (Tcl_GetLongFromObj(NULL, attributePtr, &gid) != TCL_OK) {
sl@0
  1453
	Tcl_DString ds;
sl@0
  1454
	struct group *groupPtr;
sl@0
  1455
	CONST char *string;
sl@0
  1456
	int length;
sl@0
  1457
sl@0
  1458
	string = Tcl_GetStringFromObj(attributePtr, &length);
sl@0
  1459
	native = Tcl_UtfToExternalDString(NULL, string, length, &ds);
sl@0
  1460
	groupPtr = TclpGetGrNam(native); /* INTL: Native. */
sl@0
  1461
	Tcl_DStringFree(&ds);
sl@0
  1462
sl@0
  1463
	if (groupPtr == NULL) {
sl@0
  1464
	    endgrent();
sl@0
  1465
	    Tcl_AppendResult(interp, "could not set group for file \"",
sl@0
  1466
		    Tcl_GetString(fileName), "\": group \"", 
sl@0
  1467
		    string, "\" does not exist",
sl@0
  1468
		    (char *) NULL);
sl@0
  1469
	    return TCL_ERROR;
sl@0
  1470
	}
sl@0
  1471
	gid = groupPtr->gr_gid;
sl@0
  1472
    }
sl@0
  1473
sl@0
  1474
    native = Tcl_FSGetNativePath(fileName);
sl@0
  1475
    result = chown(native, (uid_t) -1, (gid_t) gid);	/* INTL: Native. */
sl@0
  1476
sl@0
  1477
    endgrent();
sl@0
  1478
    if (result != 0) {
sl@0
  1479
	Tcl_AppendResult(interp, "could not set group for file \"",
sl@0
  1480
	    Tcl_GetString(fileName), "\": ", Tcl_PosixError(interp), 
sl@0
  1481
	    (char *) NULL);
sl@0
  1482
	return TCL_ERROR;
sl@0
  1483
    }    
sl@0
  1484
    return TCL_OK;
sl@0
  1485
}
sl@0
  1486

sl@0
  1487
/*
sl@0
  1488
 *---------------------------------------------------------------------------
sl@0
  1489
 *
sl@0
  1490
 * SetOwnerAttribute --
sl@0
  1491
 *
sl@0
  1492
 *      Sets the owner of the file to the specified owner.
sl@0
  1493
 *
sl@0
  1494
 * Results:
sl@0
  1495
 *      Standard TCL result.
sl@0
  1496
 *
sl@0
  1497
 * Side effects:
sl@0
  1498
 *      As above.
sl@0
  1499
 *      
sl@0
  1500
 *---------------------------------------------------------------------------
sl@0
  1501
 */
sl@0
  1502
sl@0
  1503
static int
sl@0
  1504
SetOwnerAttribute(interp, objIndex, fileName, attributePtr)
sl@0
  1505
    Tcl_Interp *interp;		    /* The interp for error reporting. */
sl@0
  1506
    int objIndex;		    /* The index of the attribute. */
sl@0
  1507
    Tcl_Obj *fileName;   	    /* The name of the file (UTF-8). */
sl@0
  1508
    Tcl_Obj *attributePtr;	    /* New owner for file. */
sl@0
  1509
{
sl@0
  1510
    long uid;
sl@0
  1511
    int result;
sl@0
  1512
    CONST char *native;
sl@0
  1513
sl@0
  1514
    if (Tcl_GetLongFromObj(NULL, attributePtr, &uid) != TCL_OK) {
sl@0
  1515
	Tcl_DString ds;
sl@0
  1516
	struct passwd *pwPtr;
sl@0
  1517
	CONST char *string;
sl@0
  1518
	int length;
sl@0
  1519
sl@0
  1520
	string = Tcl_GetStringFromObj(attributePtr, &length);
sl@0
  1521
	native = Tcl_UtfToExternalDString(NULL, string, length, &ds);
sl@0
  1522
	pwPtr = TclpGetPwNam(native); /* INTL: Native. */
sl@0
  1523
	Tcl_DStringFree(&ds);
sl@0
  1524
sl@0
  1525
	if (pwPtr == NULL) {
sl@0
  1526
	    endpwent();
sl@0
  1527
	    Tcl_AppendResult(interp, "could not set owner for file \"",
sl@0
  1528
			     Tcl_GetString(fileName), "\": user \"", 
sl@0
  1529
			     string, "\" does not exist",
sl@0
  1530
		    (char *) NULL);
sl@0
  1531
	    return TCL_ERROR;
sl@0
  1532
	}
sl@0
  1533
	uid = pwPtr->pw_uid;
sl@0
  1534
    }
sl@0
  1535
sl@0
  1536
    native = Tcl_FSGetNativePath(fileName);
sl@0
  1537
    result = chown(native, (uid_t) uid, (gid_t) -1);   /* INTL: Native. */
sl@0
  1538
    
sl@0
  1539
    endpwent();
sl@0
  1540
    if (result != 0) {
sl@0
  1541
	Tcl_AppendResult(interp, "could not set owner for file \"", 
sl@0
  1542
			 Tcl_GetString(fileName), "\": ", 
sl@0
  1543
			 Tcl_PosixError(interp), (char *) NULL);
sl@0
  1544
	return TCL_ERROR;
sl@0
  1545
    }
sl@0
  1546
    return TCL_OK;
sl@0
  1547
}
sl@0
  1548

sl@0
  1549
/*
sl@0
  1550
 *---------------------------------------------------------------------------
sl@0
  1551
 *
sl@0
  1552
 * SetPermissionsAttribute
sl@0
  1553
 *
sl@0
  1554
 *      Sets the file to the given permission.
sl@0
  1555
 *
sl@0
  1556
 * Results:
sl@0
  1557
 *      Standard TCL result.
sl@0
  1558
 *
sl@0
  1559
 * Side effects:
sl@0
  1560
 *      The permission of the file is changed.
sl@0
  1561
 *      
sl@0
  1562
 *---------------------------------------------------------------------------
sl@0
  1563
 */
sl@0
  1564
sl@0
  1565
static int
sl@0
  1566
SetPermissionsAttribute(interp, objIndex, fileName, attributePtr)
sl@0
  1567
    Tcl_Interp *interp;		    /* The interp we are using for errors. */
sl@0
  1568
    int objIndex;		    /* The index of the attribute. */
sl@0
  1569
    Tcl_Obj *fileName;  	    /* The name of the file (UTF-8). */
sl@0
  1570
    Tcl_Obj *attributePtr;	    /* The attribute to set. */
sl@0
  1571
{
sl@0
  1572
    long mode;
sl@0
  1573
    mode_t newMode;
sl@0
  1574
    int result;
sl@0
  1575
    CONST char *native;
sl@0
  1576
sl@0
  1577
    /*
sl@0
  1578
     * First try if the string is a number
sl@0
  1579
     */
sl@0
  1580
    if (Tcl_GetLongFromObj(NULL, attributePtr, &mode) == TCL_OK) {
sl@0
  1581
        newMode = (mode_t) (mode & 0x00007FFF);
sl@0
  1582
    } else {
sl@0
  1583
	Tcl_StatBuf buf;
sl@0
  1584
	char *modeStringPtr = Tcl_GetString(attributePtr);
sl@0
  1585
sl@0
  1586
	/*
sl@0
  1587
	 * Try the forms "rwxrwxrwx" and "ugo=rwx"
sl@0
  1588
	 *
sl@0
  1589
	 * We get the current mode of the file, in order to allow for
sl@0
  1590
	 * ug+-=rwx style chmod strings.
sl@0
  1591
	 */
sl@0
  1592
	result = TclpObjStat(fileName, &buf);
sl@0
  1593
	if (result != 0) {
sl@0
  1594
	    Tcl_AppendResult(interp, "could not read \"", 
sl@0
  1595
		    Tcl_GetString(fileName), "\": ",
sl@0
  1596
		    Tcl_PosixError(interp), (char *) NULL);
sl@0
  1597
	    return TCL_ERROR;
sl@0
  1598
	}
sl@0
  1599
	newMode = (mode_t) (buf.st_mode & 0x00007FFF);
sl@0
  1600
sl@0
  1601
	if (GetModeFromPermString(NULL, modeStringPtr, &newMode) != TCL_OK) {
sl@0
  1602
	    Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
sl@0
  1603
		    "unknown permission string format \"",
sl@0
  1604
		    modeStringPtr, "\"", (char *) NULL);
sl@0
  1605
	    return TCL_ERROR;
sl@0
  1606
	}
sl@0
  1607
    }
sl@0
  1608
sl@0
  1609
    native = Tcl_FSGetNativePath(fileName);
sl@0
  1610
    result = chmod(native, newMode);		/* INTL: Native. */
sl@0
  1611
    if (result != 0) {
sl@0
  1612
	Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
sl@0
  1613
		"could not set permissions for file \"", 
sl@0
  1614
		Tcl_GetString(fileName), "\": ",
sl@0
  1615
		Tcl_PosixError(interp), (char *) NULL);
sl@0
  1616
	return TCL_ERROR;
sl@0
  1617
    }
sl@0
  1618
    return TCL_OK;
sl@0
  1619
}
sl@0
  1620

sl@0
  1621
/*
sl@0
  1622
 *---------------------------------------------------------------------------
sl@0
  1623
 *
sl@0
  1624
 * TclpObjListVolumes --
sl@0
  1625
 *
sl@0
  1626
 *	Lists the currently mounted volumes, which on UNIX is just /.
sl@0
  1627
 *
sl@0
  1628
 * Results:
sl@0
  1629
 *	The list of volumes.
sl@0
  1630
 *
sl@0
  1631
 * Side effects:
sl@0
  1632
 *	None.
sl@0
  1633
 *
sl@0
  1634
 *---------------------------------------------------------------------------
sl@0
  1635
 */
sl@0
  1636
sl@0
  1637
Tcl_Obj*
sl@0
  1638
TclpObjListVolumes(void)
sl@0
  1639
{
sl@0
  1640
    Tcl_Obj *resultPtr = Tcl_NewStringObj("/",1);
sl@0
  1641
sl@0
  1642
    Tcl_IncrRefCount(resultPtr);
sl@0
  1643
    return resultPtr;
sl@0
  1644
}
sl@0
  1645

sl@0
  1646
/*
sl@0
  1647
 *----------------------------------------------------------------------
sl@0
  1648
 *
sl@0
  1649
 * GetModeFromPermString --
sl@0
  1650
 *
sl@0
  1651
 *	This procedure is invoked to process the "file permissions"
sl@0
  1652
 *	Tcl command, to check for a "rwxrwxrwx" or "ugoa+-=rwxst" string.
sl@0
  1653
 *	See the user documentation for details on what it does.
sl@0
  1654
 *
sl@0
  1655
 * Results:
sl@0
  1656
 *	A standard Tcl result.
sl@0
  1657
 *
sl@0
  1658
 * Side effects:
sl@0
  1659
 *	See the user documentation.
sl@0
  1660
 *
sl@0
  1661
 *----------------------------------------------------------------------
sl@0
  1662
 */
sl@0
  1663
sl@0
  1664
static int
sl@0
  1665
GetModeFromPermString(interp, modeStringPtr, modePtr)
sl@0
  1666
    Tcl_Interp *interp;		/* The interp we are using for errors. */
sl@0
  1667
    char *modeStringPtr;	/* Permissions string */
sl@0
  1668
    mode_t *modePtr;		/* pointer to the mode value */
sl@0
  1669
{
sl@0
  1670
    mode_t newMode;
sl@0
  1671
    mode_t oldMode;		/* Storage for the value of the old mode
sl@0
  1672
				 * (that is passed in), to allow for the
sl@0
  1673
				 * chmod style manipulation */
sl@0
  1674
    int i,n, who, op, what, op_found, who_found;
sl@0
  1675
sl@0
  1676
    /*
sl@0
  1677
     * We start off checking for an "rwxrwxrwx" style permissions string
sl@0
  1678
     */
sl@0
  1679
    if (strlen(modeStringPtr) != 9) {
sl@0
  1680
        goto chmodStyleCheck;
sl@0
  1681
    }
sl@0
  1682
sl@0
  1683
    newMode = 0;
sl@0
  1684
    for (i = 0; i < 9; i++) {
sl@0
  1685
	switch (*(modeStringPtr+i)) {
sl@0
  1686
	    case 'r':
sl@0
  1687
		if ((i%3) != 0) {
sl@0
  1688
		    goto chmodStyleCheck;
sl@0
  1689
		}
sl@0
  1690
		newMode |= (1<<(8-i));
sl@0
  1691
		break;
sl@0
  1692
	    case 'w':
sl@0
  1693
		if ((i%3) != 1) {
sl@0
  1694
		    goto chmodStyleCheck;
sl@0
  1695
		}
sl@0
  1696
		newMode |= (1<<(8-i));
sl@0
  1697
		break;
sl@0
  1698
	    case 'x':
sl@0
  1699
		if ((i%3) != 2) {
sl@0
  1700
		    goto chmodStyleCheck;
sl@0
  1701
		}
sl@0
  1702
		newMode |= (1<<(8-i));
sl@0
  1703
		break;
sl@0
  1704
	    case 's':
sl@0
  1705
		if (((i%3) != 2) || (i > 5)) {
sl@0
  1706
		    goto chmodStyleCheck;
sl@0
  1707
		}
sl@0
  1708
		newMode |= (1<<(8-i));
sl@0
  1709
		newMode |= (1<<(11-(i/3)));
sl@0
  1710
		break;
sl@0
  1711
	    case 'S':
sl@0
  1712
		if (((i%3) != 2) || (i > 5)) {
sl@0
  1713
		    goto chmodStyleCheck;
sl@0
  1714
		}
sl@0
  1715
		newMode |= (1<<(11-(i/3)));
sl@0
  1716
		break;
sl@0
  1717
	    case 't':
sl@0
  1718
		if (i != 8) {
sl@0
  1719
		    goto chmodStyleCheck;
sl@0
  1720
		}
sl@0
  1721
		newMode |= (1<<(8-i));
sl@0
  1722
		newMode |= (1<<9);
sl@0
  1723
		break;
sl@0
  1724
	    case 'T':
sl@0
  1725
		if (i != 8) {
sl@0
  1726
		    goto chmodStyleCheck;
sl@0
  1727
		}
sl@0
  1728
		newMode |= (1<<9);
sl@0
  1729
		break;
sl@0
  1730
	    case '-':
sl@0
  1731
		break;
sl@0
  1732
	    default:
sl@0
  1733
		/*
sl@0
  1734
		 * Oops, not what we thought it was, so go on
sl@0
  1735
		 */
sl@0
  1736
		goto chmodStyleCheck;
sl@0
  1737
	}
sl@0
  1738
    }
sl@0
  1739
    *modePtr = newMode;
sl@0
  1740
    return TCL_OK;
sl@0
  1741
sl@0
  1742
    chmodStyleCheck:
sl@0
  1743
    /*
sl@0
  1744
     * We now check for an "ugoa+-=rwxst" style permissions string
sl@0
  1745
     */
sl@0
  1746
sl@0
  1747
    for (n = 0 ; *(modeStringPtr+n) != '\0' ; n = n + i) {
sl@0
  1748
	oldMode = *modePtr;
sl@0
  1749
	who = op = what = op_found = who_found = 0;
sl@0
  1750
	for (i = 0 ; *(modeStringPtr+n+i) != '\0' ; i++ ) {
sl@0
  1751
	    if (!who_found) {
sl@0
  1752
		/* who */
sl@0
  1753
		switch (*(modeStringPtr+n+i)) {
sl@0
  1754
		    case 'u' :
sl@0
  1755
			who |= 0x9c0;
sl@0
  1756
			continue;
sl@0
  1757
		    case 'g' :
sl@0
  1758
			who |= 0x438;
sl@0
  1759
			continue;
sl@0
  1760
		    case 'o' :
sl@0
  1761
			who |= 0x207;
sl@0
  1762
			continue;
sl@0
  1763
		    case 'a' :
sl@0
  1764
			who |= 0xfff;
sl@0
  1765
			continue;
sl@0
  1766
		}
sl@0
  1767
	    }
sl@0
  1768
	    who_found = 1;
sl@0
  1769
	    if (who == 0) {
sl@0
  1770
		who = 0xfff;
sl@0
  1771
	    }
sl@0
  1772
	    if (!op_found) {
sl@0
  1773
		/* op */
sl@0
  1774
		switch (*(modeStringPtr+n+i)) {
sl@0
  1775
		    case '+' :
sl@0
  1776
			op = 1;
sl@0
  1777
			op_found = 1;
sl@0
  1778
			continue;
sl@0
  1779
		    case '-' :
sl@0
  1780
			op = 2;
sl@0
  1781
			op_found = 1;
sl@0
  1782
			continue;
sl@0
  1783
		    case '=' :
sl@0
  1784
			op = 3;
sl@0
  1785
			op_found = 1;
sl@0
  1786
			continue;
sl@0
  1787
		    default  :
sl@0
  1788
			return TCL_ERROR;
sl@0
  1789
		}
sl@0
  1790
	    }
sl@0
  1791
	    /* what */
sl@0
  1792
	    switch (*(modeStringPtr+n+i)) {
sl@0
  1793
		case 'r' :
sl@0
  1794
		    what |= 0x124;
sl@0
  1795
		    continue;
sl@0
  1796
		case 'w' :
sl@0
  1797
		    what |= 0x92;
sl@0
  1798
		    continue;
sl@0
  1799
		case 'x' :
sl@0
  1800
		    what |= 0x49;
sl@0
  1801
		    continue;
sl@0
  1802
		case 's' :
sl@0
  1803
		    what |= 0xc00;
sl@0
  1804
		    continue;
sl@0
  1805
		case 't' :
sl@0
  1806
		    what |= 0x200;
sl@0
  1807
		    continue;
sl@0
  1808
		case ',' :
sl@0
  1809
		    break;
sl@0
  1810
		default  :
sl@0
  1811
		    return TCL_ERROR;
sl@0
  1812
	    }
sl@0
  1813
	    if (*(modeStringPtr+n+i) == ',') {
sl@0
  1814
		i++;
sl@0
  1815
		break;
sl@0
  1816
	    }
sl@0
  1817
	}
sl@0
  1818
	switch (op) {
sl@0
  1819
	    case 1 :
sl@0
  1820
		*modePtr = oldMode | (who & what);
sl@0
  1821
		continue;
sl@0
  1822
	    case 2 :
sl@0
  1823
		*modePtr = oldMode & ~(who & what);
sl@0
  1824
		continue;
sl@0
  1825
	    case 3 :
sl@0
  1826
		*modePtr = (oldMode & ~who) | (who & what);
sl@0
  1827
		continue;
sl@0
  1828
	}
sl@0
  1829
    }
sl@0
  1830
    return TCL_OK;
sl@0
  1831
}
sl@0
  1832

sl@0
  1833
/*
sl@0
  1834
 *---------------------------------------------------------------------------
sl@0
  1835
 *
sl@0
  1836
 * TclpObjNormalizePath --
sl@0
  1837
 *
sl@0
  1838
 *	This function scans through a path specification and replaces
sl@0
  1839
 *	it, in place, with a normalized version.  A normalized version
sl@0
  1840
 *	is one in which all symlinks in the path are replaced with
sl@0
  1841
 *	their expanded form (except a symlink at the very end of the
sl@0
  1842
 *	path).
sl@0
  1843
 *
sl@0
  1844
 * Results:
sl@0
  1845
 *	The new 'nextCheckpoint' value, giving as far as we could
sl@0
  1846
 *	understand in the path.
sl@0
  1847
 *
sl@0
  1848
 * Side effects:
sl@0
  1849
 *	The pathPtr string, is modified.
sl@0
  1850
 *
sl@0
  1851
 *---------------------------------------------------------------------------
sl@0
  1852
 */
sl@0
  1853
int
sl@0
  1854
TclpObjNormalizePath(interp, pathPtr, nextCheckpoint)
sl@0
  1855
    Tcl_Interp *interp;
sl@0
  1856
    Tcl_Obj *pathPtr;
sl@0
  1857
    int nextCheckpoint;
sl@0
  1858
{
sl@0
  1859
    char *currentPathEndPosition;
sl@0
  1860
    int pathLen;
sl@0
  1861
    char cur;
sl@0
  1862
    char *path = Tcl_GetStringFromObj(pathPtr, &pathLen);
sl@0
  1863
#ifndef NO_REALPATH
sl@0
  1864
    char normPath[MAXPATHLEN];
sl@0
  1865
    Tcl_DString ds;
sl@0
  1866
    CONST char *nativePath; 
sl@0
  1867
#endif
sl@0
  1868
    /* 
sl@0
  1869
     * We add '1' here because if nextCheckpoint is zero we know
sl@0
  1870
     * that '/' exists, and if it isn't zero, it must point at
sl@0
  1871
     * a directory separator which we also know exists.
sl@0
  1872
     */
sl@0
  1873
    currentPathEndPosition = path + nextCheckpoint;
sl@0
  1874
    if (*currentPathEndPosition == '/') {
sl@0
  1875
	currentPathEndPosition++;
sl@0
  1876
    }
sl@0
  1877
sl@0
  1878
#ifndef NO_REALPATH
sl@0
  1879
    /* For speed, try to get the entire path in one go */
sl@0
  1880
    if (nextCheckpoint == 0 && haveRealpath) {
sl@0
  1881
        char *lastDir = strrchr(currentPathEndPosition, '/');
sl@0
  1882
	if (lastDir != NULL) {
sl@0
  1883
	    nativePath = Tcl_UtfToExternalDString(NULL, path, 
sl@0
  1884
						  lastDir - path, &ds);
sl@0
  1885
sl@0
  1886
		if (Realpath(nativePath, normPath) != NULL) { 
sl@0
  1887
sl@0
  1888
		if (*nativePath != '/' && *normPath == '/') {
sl@0
  1889
		    /*
sl@0
  1890
		     * realpath has transformed a relative path into an
sl@0
  1891
		     * absolute path, we do not know how to handle this.
sl@0
  1892
		     */
sl@0
  1893
		} else {
sl@0
  1894
		    nextCheckpoint = lastDir - path;
sl@0
  1895
		    goto wholeStringOk;
sl@0
  1896
		}
sl@0
  1897
	    }
sl@0
  1898
	    Tcl_DStringFree(&ds);
sl@0
  1899
	}
sl@0
  1900
    }
sl@0
  1901
    /* Else do it the slow way */
sl@0
  1902
#endif
sl@0
  1903
    
sl@0
  1904
    while (1) {
sl@0
  1905
	cur = *currentPathEndPosition;
sl@0
  1906
	if ((cur == '/') && (path != currentPathEndPosition)) {
sl@0
  1907
	    /* Reached directory separator */
sl@0
  1908
	    Tcl_DString ds;
sl@0
  1909
	    CONST char *nativePath;
sl@0
  1910
	    int accessOk;
sl@0
  1911
sl@0
  1912
	    nativePath = Tcl_UtfToExternalDString(NULL, path, 
sl@0
  1913
		    currentPathEndPosition - path, &ds);
sl@0
  1914
	    accessOk = access(nativePath, F_OK);
sl@0
  1915
	    Tcl_DStringFree(&ds);
sl@0
  1916
	    if (accessOk != 0) {
sl@0
  1917
		/* File doesn't exist */
sl@0
  1918
		break;
sl@0
  1919
	    }
sl@0
  1920
	    /* Update the acceptable point */
sl@0
  1921
	    nextCheckpoint = currentPathEndPosition - path;
sl@0
  1922
	} else if (cur == 0) {
sl@0
  1923
	    /* Reached end of string */
sl@0
  1924
	    break;
sl@0
  1925
	}
sl@0
  1926
	currentPathEndPosition++;
sl@0
  1927
    }
sl@0
  1928
    /* 
sl@0
  1929
     * We should really now convert this to a canonical path.  We do
sl@0
  1930
     * that with 'realpath' if we have it available.  Otherwise we could
sl@0
  1931
     * step through every single path component, checking whether it is a 
sl@0
  1932
     * symlink, but that would be a lot of work, and most modern OSes 
sl@0
  1933
     * have 'realpath'.
sl@0
  1934
     */
sl@0
  1935
#ifndef NO_REALPATH
sl@0
  1936
    if (haveRealpath) {
sl@0
  1937
	/* 
sl@0
  1938
	 * If we only had '/foo' or '/' then we never increment nextCheckpoint
sl@0
  1939
	 * and we don't need or want to go through 'Realpath'.  Also, on some
sl@0
  1940
	 * platforms, passing an empty string to 'Realpath' will give us the
sl@0
  1941
	 * normalized pwd, which is not what we want at all!
sl@0
  1942
	 */
sl@0
  1943
	if (nextCheckpoint == 0) return 0;
sl@0
  1944
	
sl@0
  1945
	nativePath = Tcl_UtfToExternalDString(NULL, path, nextCheckpoint, &ds);
sl@0
  1946
sl@0
  1947
	if (Realpath(nativePath, normPath) != NULL) { 
sl@0
  1948
	    int newNormLen;
sl@0
  1949
	    wholeStringOk:
sl@0
  1950
	    newNormLen = strlen(normPath);
sl@0
  1951
	    if ((newNormLen == Tcl_DStringLength(&ds))
sl@0
  1952
		    && (strcmp(normPath, nativePath) == 0)) {
sl@0
  1953
		/* String is unchanged */
sl@0
  1954
		Tcl_DStringFree(&ds);
sl@0
  1955
		if (path[nextCheckpoint] != '\0') {
sl@0
  1956
		    nextCheckpoint++;
sl@0
  1957
		}
sl@0
  1958
		return nextCheckpoint;
sl@0
  1959
	    }
sl@0
  1960
	    
sl@0
  1961
	    /* 
sl@0
  1962
	     * Free up the native path and put in its place the
sl@0
  1963
	     * converted, normalized path.
sl@0
  1964
	     */
sl@0
  1965
	    Tcl_DStringFree(&ds);
sl@0
  1966
	    Tcl_ExternalToUtfDString(NULL, normPath, (int) newNormLen, &ds);
sl@0
  1967
    
sl@0
  1968
	    if (path[nextCheckpoint] != '\0') {
sl@0
  1969
		/* not at end, append remaining path */
sl@0
  1970
		int normLen = Tcl_DStringLength(&ds);
sl@0
  1971
		Tcl_DStringAppend(&ds, path + nextCheckpoint,
sl@0
  1972
			pathLen - nextCheckpoint);
sl@0
  1973
		/* 
sl@0
  1974
		 * We recognise up to and including the directory
sl@0
  1975
		 * separator.
sl@0
  1976
		 */	
sl@0
  1977
		nextCheckpoint = normLen + 1;
sl@0
  1978
	    } else {
sl@0
  1979
		/* We recognise the whole string */ 
sl@0
  1980
		nextCheckpoint = Tcl_DStringLength(&ds);
sl@0
  1981
	    }
sl@0
  1982
	    /* 
sl@0
  1983
	     * Overwrite with the normalized path.
sl@0
  1984
	     */
sl@0
  1985
	    Tcl_SetStringObj(pathPtr, Tcl_DStringValue(&ds),
sl@0
  1986
		    Tcl_DStringLength(&ds));
sl@0
  1987
	}
sl@0
  1988
	Tcl_DStringFree(&ds);
sl@0
  1989
    }
sl@0
  1990
#endif	/* !NO_REALPATH */
sl@0
  1991
sl@0
  1992
    return nextCheckpoint;
sl@0
  1993
}