Tcl Source Code

Attachment Details
Login
Overview

Artifact ID: b6ef93f7462f8773f98234776acee661724e0513
Ticket: 929534ffffffffffffffffffffffffffffffffff
Date: 2004-04-05 10:43:46
User: tauvan
Artifact Attached: b73576e612d6b1c282a66161fecc5cb83988c4b5
Filename:tclUnixFCmd.c
Description:
Content:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
   100
   101
   102
   103
   104
   105
   106
   107
   108
   109
   110
   111
   112
   113
   114
   115
   116
   117
   118
   119
   120
   121
   122
   123
   124
   125
   126
   127
   128
   129
   130
   131
   132
   133
   134
   135
   136
   137
   138
   139
   140
   141
   142
   143
   144
   145
   146
   147
   148
   149
   150
   151
   152
   153
   154
   155
   156
   157
   158
   159
   160
   161
   162
   163
   164
   165
   166
   167
   168
   169
   170
   171
   172
   173
   174
   175
   176
   177
   178
   179
   180
   181
   182
   183
   184
   185
   186
   187
   188
   189
   190
   191
   192
   193
   194
   195
   196
   197
   198
   199
   200
   201
   202
   203
   204
   205
   206
   207
   208
   209
   210
   211
   212
   213
   214
   215
   216
   217
   218
   219
   220
   221
   222
   223
   224
   225
   226
   227
   228
   229
   230
   231
   232
   233
   234
   235
   236
   237
   238
   239
   240
   241
   242
   243
   244
   245
   246
   247
   248
   249
static int
findPath(const char *orgPath, char **retPath, int *idx) {

    struct stat curStatBuf;
    char *pathBuffer = *retPath;
    int pBdex = 0, oPdex = *idx, redex;
    int oPlen = strlen(orgPath);

    errno = 0;

    if (pathBuffer == NULL) {
        pathBuffer = (char *)malloc(MAXPATHLEN);
        bzero(pathBuffer, MAXPATHLEN);
        *retPath = pathBuffer;
    }

    // find the first '/' startAt should point to '/'
    if (orgPath[oPdex] != '/') {
        free(pathBuffer);
        *retPath = NULL;
        return 0;
    }
    if (oPdex) {
        oPdex--;
        while (oPdex > 0 && orgPath[oPdex] != '/')
            oPdex--;
        while (pBdex < oPdex)
            pathBuffer[pBdex++] = orgPath[pBdex];
    }
    // should be pointing @ first replacement '/' in pB & oP
    // ie... redex = pBdex = oPdex.  now find endex
    oPdex = pBdex;
findnext:
    redex = oPdex + 1;
    do {
        pathBuffer[pBdex] = orgPath[oPdex];
        pBdex++; oPdex++;
    } while (orgPath[oPdex] != '/' && oPdex < oPlen);

    // should have path to validate. redex pts @ replacement loc
    // opdex, bpdex, @ cur termination pts.
    // pb not terminated with '/'
    // if opdex == oplen then we're done (no '/' termination)
    if (oPdex == oPlen) {
        *idx = oPlen;
        return 0;
    }
    lstat(pathBuffer, &curStatBuf);
    if (S_ISLNK(curStatBuf.st_mode)) {
        char *newName = NULL;
        int normPathLen, i;
        normPathLen = readlink(pathBuffer, (&pathBuffer[redex]), (MAXPATHLEN - redex));
        pathBuffer[redex + normPathLen] = 0;
        if (pathBuffer[redex] == '/') {
            pBdex = 0;
            do {
                pathBuffer[pBdex] = pathBuffer[redex];
                pBdex++; redex++;
            } while (--normPathLen > 0);
            redex = pBdex + 1;
        } else {
            pBdex = redex + normPathLen;
	}
        do {
            pathBuffer[pBdex] = orgPath[oPdex];
            pBdex++; oPdex++;
        } while (oPdex < oPlen);
        pathBuffer[pBdex] = 0;
        i = redex - 1;
        findPath(pathBuffer, &newName, &i);
        free(pathBuffer);
        *retPath = newName;
        *idx = i;
        
        return 0;
    }

    if (errno) {
        *idx = redex - 1;
        free(pathBuffer);
        *retPath = NULL;
        return 0;
    }
    goto findnext;

    return 0;
}


/*
 *---------------------------------------------------------------------------
 *
 * TclpObjNormalizePath --
 *
 *	This function scans through a path specification and replaces
 *	it, in place, with a normalized version.  A normalized version
 *	is one in which all symlinks in the path are replaced with
 *	their expanded form (except a symlink at the very end of the
 *	path).
 *
 * Results:
 *	The new 'nextCheckpoint' value, giving as far as we could
 *	understand in the path.
 *
 * Side effects:
 *	The pathPtr string, is modified.
 *
 *---------------------------------------------------------------------------
 */
int
TclpObjNormalizePath(interp, pathPtr, nextCheckpoint)
    Tcl_Interp *interp;
    Tcl_Obj *pathPtr;
    int nextCheckpoint;
{
    int pathLen;
    char *currentPathEndPosition = NULL;
    char *path = Tcl_GetStringFromObj(pathPtr, &pathLen);
#if 1

    findPath(path, &currentPathEndPosition, &nextCheckpoint);
    if (currentPathEndPosition) {
        Tcl_DString ds;
        Tcl_ExternalToUtfDString(NULL, currentPathEndPosition, nextCheckpoint, &ds);
        Tcl_SetStringObj(pathPtr, Tcl_DStringValue(&ds), Tcl_DStringLength(&ds));
        Tcl_DStringFree(&ds);
    }
    
#else

#ifndef NO_REALPATH
    char normPath[MAXPATHLEN];
    Tcl_DString ds;
    CONST char *nativePath; 
#endif /* !NO_REALPATH */
    /* 
     * We add '1' here because if nextCheckpoint is zero we know
     * that '/' exists, and if it isn't zero, it must point at
     * a directory separator which we also know exists.
     */
    currentPathEndPosition = path + nextCheckpoint;
    if (*currentPathEndPosition == '/') {
	currentPathEndPosition++;
    }

#ifndef NO_REALPATH
    /* For speed, try to get the entire path in one go */
    if (nextCheckpoint == 0) {
        char *lastDir = strrchr(currentPathEndPosition, '/');
	if (lastDir != NULL) {
	    nativePath = Tcl_UtfToExternalDString(NULL, path, 
						  lastDir - path, &ds);
	    if (Realpath(nativePath, normPath) != NULL) {
		nextCheckpoint = lastDir - path;
		goto wholeStringOk;
	    }
	}
    }
    /* Else do it the slow way */
#endif /* !NO_REALPATH */
    
    while (1) {
	char cur = *currentPathEndPosition;
	if ((cur == '/') && (path != currentPathEndPosition)) {
	    /* Reached directory separator */
	    Tcl_DString ds;
	    CONST char *nativePath;
	    int accessOk;

	    nativePath = Tcl_UtfToExternalDString(NULL, path, 
		    currentPathEndPosition - path, &ds);
	    accessOk = access(nativePath, F_OK);
	    Tcl_DStringFree(&ds);
	    if (accessOk != 0) {
		/* File doesn't exist */
		break;
	    }
	    /* Update the acceptable point */
	    nextCheckpoint = currentPathEndPosition - path;
	} else if (cur == 0) {
	    /* Reached end of string */
	    break;
	}
	currentPathEndPosition++;
    }
    /* 
     * We should really now convert this to a canonical path.  We do
     * that with 'realpath' if we have it available.  Otherwise we could
     * step through every single path component, checking whether it is a 
     * symlink, but that would be a lot of work, and most modern OSes 
     * have 'realpath'.
     */
#ifndef NO_REALPATH
    /* 
     * If we only had '/foo' or '/' then we never increment nextCheckpoint
     * and we don't need or want to go through 'Realpath'.  Also, on some
     * platforms, passing an empty string to 'Realpath' will give us the
     * normalized pwd, which is not what we want at all!
     */
    if (nextCheckpoint == 0) return 0;
    
    nativePath = Tcl_UtfToExternalDString(NULL, path, nextCheckpoint, &ds);
    if (Realpath(nativePath, normPath) != NULL) {
	int newNormLen;
	wholeStringOk:
	newNormLen = strlen(normPath);
	if ((newNormLen == Tcl_DStringLength(&ds))
		&& (strcmp(normPath, nativePath) == 0)) {
	    /* String is unchanged */
	    Tcl_DStringFree(&ds);
	    if (path[nextCheckpoint] != '\0') {
		nextCheckpoint++;
	    }
	    return nextCheckpoint;
	}
	
	/* 
	 * Free up the native path and put in its place the
	 * converted, normalized path.
	 */
	Tcl_DStringFree(&ds);
	Tcl_ExternalToUtfDString(NULL, normPath, (int) newNormLen, &ds);

	if (path[nextCheckpoint] != '\0') {
	    /* not at end, append remaining path */
	    int normLen = Tcl_DStringLength(&ds);
	    Tcl_DStringAppend(&ds, path + nextCheckpoint,
		    pathLen - nextCheckpoint);
	    /* 
	     * We recognise up to and including the directory
	     * separator.
	     */	
	    nextCheckpoint = normLen + 1;
	} else {
	    /* We recognise the whole string */ 
	    nextCheckpoint = Tcl_DStringLength(&ds);
	}
	/* 
	 * Overwrite with the normalized path.
	 */
	Tcl_SetStringObj(pathPtr, Tcl_DStringValue(&ds),
		Tcl_DStringLength(&ds));
    }
    Tcl_DStringFree(&ds);
#endif	/* !NO_REALPATH */
#endif  /* 0 or 1 */

    return nextCheckpoint;
}