1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/*
* tclUnixFile.c --
*
* This file contains wrappers around UNIX file handling functions.
* These wrappers mask differences between Windows and UNIX.
*
* Copyright (c) 1995-1998 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclUnixFile.c,v 1.10 2001/07/31 19:12:07 vincentdarley Exp $
*/
#include "tclInt.h"
#include "tclPort.h"
char * TclpGetCwd _ANSI_ARGS_((Tcl_Interp *interp, Tcl_DString *bufferPtr));
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/*
* tclUnixFile.c --
*
* This file contains wrappers around UNIX file handling functions.
* These wrappers mask differences between Windows and UNIX.
*
* Copyright (c) 1995-1998 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclUnixFile.c,v 1.11 2001/08/23 17:37:08 vincentdarley Exp $
*/
#include "tclInt.h"
#include "tclPort.h"
char * TclpGetCwd _ANSI_ARGS_((Tcl_Interp *interp, Tcl_DString *bufferPtr));
|
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
char *native, *fname, *dirName;
DIR *d;
Tcl_DString ds;
struct stat statBuf;
int matchHidden;
int result = TCL_OK;
Tcl_DString dsOrig;
char *fileName;
int baseLength;
fileName = Tcl_FSGetTranslatedPath(interp, pathPtr);
if (fileName == NULL) {
return TCL_ERROR;
}
Tcl_DStringInit(&dsOrig);
Tcl_DStringAppend(&dsOrig, fileName, -1);
baseLength = Tcl_DStringLength(&dsOrig);
/*
* Make sure that the directory part of the name really is a
* directory. If the directory name is "", use the name "."
* instead, because some UNIX systems don't treat "" like "."
* automatically. Keep the "" for use in generating file names,
|
|
|
|
|
|
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
char *native, *fname, *dirName;
DIR *d;
Tcl_DString ds;
struct stat statBuf;
int matchHidden;
int result = TCL_OK;
Tcl_DString dsOrig;
Tcl_Obj *fileNamePtr;
int baseLength;
fileNamePtr = Tcl_FSGetTranslatedPath(interp, pathPtr);
if (fileNamePtr == NULL) {
return TCL_ERROR;
}
Tcl_DStringInit(&dsOrig);
Tcl_DStringAppend(&dsOrig, Tcl_GetString(fileNamePtr), -1);
baseLength = Tcl_DStringLength(&dsOrig);
/*
* Make sure that the directory part of the name really is a
* directory. If the directory name is "", use the name "."
* instead, because some UNIX systems don't treat "" like "."
* automatically. Keep the "" for use in generating file names,
|
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
* Don't match names starting with "." unless the "." is
* present in the pattern.
*/
continue;
}
/*
* Now check to see if the file matches. If there are more
* characters to be processed, then ensure matching files are
* directories before calling TclDoGlob. Otherwise, just add
* the file to the result.
*/
utf = Tcl_ExternalToUtfDString(NULL, entryPtr->d_name, -1, &ds);
if (Tcl_StringMatch(utf, pattern) != 0) {
int typeOk = 1;
Tcl_DStringSetLength(&dsOrig, baseLength);
Tcl_DStringAppend(&dsOrig, utf, -1);
fname = Tcl_DStringValue(&dsOrig);
if (types != NULL) {
if (types->perm != 0) {
struct stat buf;
if (TclpStat(fname, &buf) != 0) {
panic("stat failed on known file");
}
/*
* readonly means that there are NO write permissions
* (even for user), but execute is OK for anybody
*/
if (
((types->perm & TCL_GLOB_PERM_RONLY) &&
(buf.st_mode & (S_IWOTH|S_IWGRP|S_IWUSR))) ||
((types->perm & TCL_GLOB_PERM_R) &&
(TclpAccess(fname, R_OK) != 0)) ||
((types->perm & TCL_GLOB_PERM_W) &&
(TclpAccess(fname, W_OK) != 0)) ||
((types->perm & TCL_GLOB_PERM_X) &&
(TclpAccess(fname, X_OK) != 0))
) {
typeOk = 0;
}
}
if (typeOk && (types->type != 0)) {
struct stat buf;
/*
* We must match at least one flag to be listed
*/
typeOk = 0;
if (TclpLstat(fname, &buf) >= 0) {
/*
* In order bcdpfls as in 'find -t'
*/
if (
((types->type & TCL_GLOB_TYPE_BLOCK) &&
S_ISBLK(buf.st_mode)) ||
((types->type & TCL_GLOB_TYPE_CHAR) &&
S_ISCHR(buf.st_mode)) ||
((types->type & TCL_GLOB_TYPE_DIR) &&
S_ISDIR(buf.st_mode)) ||
((types->type & TCL_GLOB_TYPE_PIPE) &&
S_ISFIFO(buf.st_mode)) ||
((types->type & TCL_GLOB_TYPE_FILE) &&
S_ISREG(buf.st_mode))
#ifdef S_ISLNK
|| ((types->type & TCL_GLOB_TYPE_LINK) &&
S_ISLNK(buf.st_mode))
#endif
#ifdef S_ISSOCK
|| ((types->type & TCL_GLOB_TYPE_SOCK) &&
S_ISSOCK(buf.st_mode))
#endif
) {
typeOk = 1;
}
} else {
/* Posix error occurred */
}
}
}
if (typeOk) {
Tcl_ListObjAppendElement(interp, resultPtr,
Tcl_NewStringObj(fname, Tcl_DStringLength(&dsOrig)));
}
|
|
<
<
|
<
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
|
|
>
|
>
|
<
<
|
>
>
|
<
<
<
<
>
>
>
>
>
>
>
|
|
>
>
|
<
>
|
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
* Don't match names starting with "." unless the "." is
* present in the pattern.
*/
continue;
}
/*
* Now check to see if the file matches, according to both type
* and pattern. If so, add the file to the result.
*/
utf = Tcl_ExternalToUtfDString(NULL, entryPtr->d_name, -1, &ds);
if (Tcl_StringMatch(utf, pattern) != 0) {
int typeOk = 1;
Tcl_DStringSetLength(&dsOrig, baseLength);
Tcl_DStringAppend(&dsOrig, utf, -1);
fname = Tcl_DStringValue(&dsOrig);
if (types != NULL) {
struct stat buf;
if (types->perm != 0) {
if (TclpStat(fname, &buf) != 0) {
/*
* Either the file has disappeared between the
* 'readdir' call and the 'TclpStat' call, or
* the file is a link to a file which doesn't
* exist (which we could ascertain with
* TclpLstat), or there is some other strange
* problem. In all these cases, we define this
* to mean the file does not match any defined
* permission, and therefore it is not
* added to the list of files to return.
*/
typeOk = 0;
}
/*
* readonly means that there are NO write permissions
* (even for user), but execute is OK for anybody
*/
if (typeOk && (
((types->perm & TCL_GLOB_PERM_RONLY) &&
(buf.st_mode & (S_IWOTH|S_IWGRP|S_IWUSR))) ||
((types->perm & TCL_GLOB_PERM_R) &&
(TclpAccess(fname, R_OK) != 0)) ||
((types->perm & TCL_GLOB_PERM_W) &&
(TclpAccess(fname, W_OK) != 0)) ||
((types->perm & TCL_GLOB_PERM_X) &&
(TclpAccess(fname, X_OK) != 0))
)) {
typeOk = 0;
}
}
if (typeOk && (types->type != 0)) {
if (types->perm == 0) {
/* We haven't yet done a stat on the file */
if (TclpStat(fname, &buf) != 0) {
/* Posix error occurred */
typeOk = 0;
}
}
if (typeOk) {
/*
* In order bcdpfls as in 'find -t'
*/
if (
((types->type & TCL_GLOB_TYPE_BLOCK) &&
S_ISBLK(buf.st_mode)) ||
((types->type & TCL_GLOB_TYPE_CHAR) &&
S_ISCHR(buf.st_mode)) ||
((types->type & TCL_GLOB_TYPE_DIR) &&
S_ISDIR(buf.st_mode)) ||
((types->type & TCL_GLOB_TYPE_PIPE) &&
S_ISFIFO(buf.st_mode)) ||
((types->type & TCL_GLOB_TYPE_FILE) &&
S_ISREG(buf.st_mode))
#ifdef S_ISSOCK
|| ((types->type & TCL_GLOB_TYPE_SOCK) &&
S_ISSOCK(buf.st_mode))
#endif
) {
/* Do nothing -- this file is ok */
} else {
typeOk = 0;
#ifdef S_ISLNK
if (types->type & TCL_GLOB_TYPE_LINK) {
if (TclpLstat(fname, &buf) == 0) {
if (S_ISLNK(buf.st_mode)) {
typeOk = 1;
}
}
}
#endif
}
}
}
}
if (typeOk) {
Tcl_ListObjAppendElement(interp, resultPtr,
Tcl_NewStringObj(fname, Tcl_DStringLength(&dsOrig)));
}
|
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
return access(path, mode);
}
}
#ifdef S_IFLNK
Tcl_Obj*
TclpObjReadlink(pathPtr)
Tcl_Obj *pathPtr;
{
char link[MAXPATHLEN];
int length;
char *native;
Tcl_Obj* linkPtr;
if (Tcl_FSGetTranslatedPath(NULL, pathPtr) == NULL) {
return NULL;
}
length = readlink(Tcl_FSGetNativePath(pathPtr), link, sizeof(link));
if (length < 0) {
return NULL;
}
/*
* Allocate and copy the name, taking care since the
* name need not be null terminated.
*/
native = (char*)ckalloc((unsigned)(1+length));
strncpy(native, link, (unsigned)length);
native[length] = '\0';
linkPtr = Tcl_FSNewNativePath(pathPtr, native);
Tcl_IncrRefCount(linkPtr);
return linkPtr;
}
#endif
|
|
>
>
>
>
>
>
|
|
|
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
|
return access(path, mode);
}
}
#ifdef S_IFLNK
Tcl_Obj*
TclpObjLink(pathPtr, toPtr)
Tcl_Obj *pathPtr;
Tcl_Obj *toPtr;
{
Tcl_Obj* linkPtr = NULL;
if (toPtr != NULL) {
return NULL;
} else {
char link[MAXPATHLEN];
int length;
char *native;
if (Tcl_FSGetTranslatedPath(NULL, pathPtr) == NULL) {
return NULL;
}
length = readlink(Tcl_FSGetNativePath(pathPtr), link, sizeof(link));
if (length < 0) {
return NULL;
}
/*
* Allocate and copy the name, taking care since the
* name need not be null terminated.
*/
native = (char*)ckalloc((unsigned)(1+length));
strncpy(native, link, (unsigned)length);
native[length] = '\0';
linkPtr = Tcl_FSNewNativePath(pathPtr, native);
Tcl_IncrRefCount(linkPtr);
}
return linkPtr;
}
#endif
|