#include "tclInt.h"
#include "tclPort.h"
static int CopyRenameOneFile _ANSI_ARGS_((Tcl_Interp *interp,
char *source, char *dest, int copyFlag,
int force));
static char * FileBasename _ANSI_ARGS_((Tcl_Interp *interp,
char *path, Tcl_DString *bufferPtr));
static int FileCopyRename _ANSI_ARGS_((Tcl_Interp *interp,
int argc, char **argv, int copyFlag));
static int FileForceOption _ANSI_ARGS_((Tcl_Interp *interp,
int argc, char **argv, int *forcePtr));
int
TclFileRenameCmd(interp, argc, argv)
Tcl_Interp *interp;
int argc;
char **argv;
{
return FileCopyRename(interp, argc, argv, 0);
}
int
TclFileCopyCmd(interp, argc, argv)
Tcl_Interp *interp;
int argc;
char **argv;
{
return FileCopyRename(interp, argc, argv, 1);
}
static int
FileCopyRename(interp, argc, argv, copyFlag)
Tcl_Interp *interp;
int argc;
char **argv;
int copyFlag;
{
int i, result, force;
struct stat statBuf;
Tcl_DString targetBuffer;
char *target;
i = FileForceOption(interp, argc - 2, argv + 2, &force);
if (i < 0) {
return TCL_ERROR;
}
i += 2;
if ((argc - i) < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" ", argv[1], " ?options? source ?source ...? target\"",
(char *) NULL);
return TCL_ERROR;
}
target = Tcl_TranslateFileName(interp, argv[argc - 1], &targetBuffer);
if (target == NULL) {
return TCL_ERROR;
}
result = TCL_OK;
if ((TclStat(target, &statBuf) != 0) || !S_ISDIR(statBuf.st_mode)) {
if ((argc - i) > 2) {
errno = ENOTDIR;
Tcl_PosixError(interp);
Tcl_AppendResult(interp, "error ",
((copyFlag) ? "copying" : "renaming"), ": target \"",
argv[argc - 1], "\" is not a directory", (char *) NULL);
result = TCL_ERROR;
} else {
result = CopyRenameOneFile(interp, argv[i], argv[i + 1], copyFlag,
force);
}
Tcl_DStringFree(&targetBuffer);
return result;
}
for ( ; i < argc - 1; i++) {
char *jargv[2];
char *source, *newFileName;
Tcl_DString sourceBuffer, newFileNameBuffer;
source = FileBasename(interp, argv[i], &sourceBuffer);
if (source == NULL) {
result = TCL_ERROR;
break;
}
jargv[0] = argv[argc - 1];
jargv[1] = source;
Tcl_DStringInit(&newFileNameBuffer);
newFileName = Tcl_JoinPath(2, jargv, &newFileNameBuffer);
result = CopyRenameOneFile(interp, argv[i], newFileName, copyFlag,
force);
Tcl_DStringFree(&sourceBuffer);
Tcl_DStringFree(&newFileNameBuffer);
if (result == TCL_ERROR) {
break;
}
}
Tcl_DStringFree(&targetBuffer);
return result;
}
int
TclFileMakeDirsCmd(interp, argc, argv)
Tcl_Interp *interp;
int argc;
char **argv;
{
Tcl_DString nameBuffer, targetBuffer;
char *errfile;
int result, i, j, pargc;
char **pargv;
struct stat statBuf;
pargv = NULL;
errfile = NULL;
Tcl_DStringInit(&nameBuffer);
Tcl_DStringInit(&targetBuffer);
result = TCL_OK;
for (i = 2; i < argc; i++) {
char *name = Tcl_TranslateFileName(interp, argv[i], &nameBuffer);
if (name == NULL) {
result = TCL_ERROR;
break;
}
Tcl_SplitPath(name, &pargc, &pargv);
if (pargc == 0) {
errno = ENOENT;
errfile = argv[i];
break;
}
for (j = 0; j < pargc; j++) {
char *target = Tcl_JoinPath(j + 1, pargv, &targetBuffer);
if (TclStat(target, &statBuf) == 0) {
if (!S_ISDIR(statBuf.st_mode)) {
errno = EEXIST;
errfile = target;
goto done;
}
} else if ((errno != ENOENT)
|| (TclpCreateDirectory(target) != TCL_OK)) {
errfile = target;
goto done;
}
Tcl_DStringFree(&targetBuffer);
}
ckfree((char *) pargv);
pargv = NULL;
Tcl_DStringFree(&nameBuffer);
}
done:
if (errfile != NULL) {
Tcl_AppendResult(interp, "can't create directory \"",
errfile, "\": ", Tcl_PosixError(interp), (char *) NULL);
result = TCL_ERROR;
}
Tcl_DStringFree(&nameBuffer);
Tcl_DStringFree(&targetBuffer);
if (pargv != NULL) {
ckfree((char *) pargv);
}
return result;
}
int
TclFileDeleteCmd(interp, argc, argv)
Tcl_Interp *interp;
int argc;
char **argv;
{
Tcl_DString nameBuffer, errorBuffer;
int i, force, result;
char *errfile;
i = FileForceOption(interp, argc - 2, argv + 2, &force);
if (i < 0) {
return TCL_ERROR;
}
i += 2;
if ((argc - i) < 1) {
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" ", argv[1], " ?options? file ?file ...?\"", (char *) NULL);
return TCL_ERROR;
}
errfile = NULL;
result = TCL_OK;
Tcl_DStringInit(&errorBuffer);
Tcl_DStringInit(&nameBuffer);
for ( ; i < argc; i++) {
struct stat statBuf;
char *name;
errfile = argv[i];
Tcl_DStringSetLength(&nameBuffer, 0);
name = Tcl_TranslateFileName(interp, argv[i], &nameBuffer);
if (name == NULL) {
result = TCL_ERROR;
goto done;
}
if (lstat(name, &statBuf) != 0) {
if (errno != ENOENT) {
result = TCL_ERROR;
}
} else if (S_ISDIR(statBuf.st_mode)) {
result = TclpRemoveDirectory(name, force, &errorBuffer);
if (result != TCL_OK) {
if ((force == 0) && (errno == EEXIST)) {
Tcl_AppendResult(interp, "error deleting \"", argv[i],
"\": directory not empty", (char *) NULL);
Tcl_PosixError(interp);
goto done;
}
errfile = Tcl_DStringValue(&errorBuffer);
if (strcmp(name, errfile) == 0) {
errfile = argv[i];
}
}
} else {
result = TclpDeleteFile(name);
}
if (result == TCL_ERROR) {
break;
}
}
if (result != TCL_OK) {
Tcl_AppendResult(interp, "error deleting \"", errfile,
"\": ", Tcl_PosixError(interp), (char *) NULL);
}
done:
Tcl_DStringFree(&errorBuffer);
Tcl_DStringFree(&nameBuffer);
return result;
}
static int
CopyRenameOneFile(interp, source, target, copyFlag, force)
Tcl_Interp *interp;
char *source;
char *target;
int copyFlag;
int force;
{
int result;
Tcl_DString sourcePath, targetPath, errorBuffer;
char *targetName, *sourceName, *errfile;
struct stat sourceStatBuf, targetStatBuf;
sourceName = Tcl_TranslateFileName(interp, source, &sourcePath);
if (sourceName == NULL) {
return TCL_ERROR;
}
targetName = Tcl_TranslateFileName(interp, target, &targetPath);
if (targetName == NULL) {
Tcl_DStringFree(&sourcePath);
return TCL_ERROR;
}
errfile = NULL;
result = TCL_ERROR;
Tcl_DStringInit(&errorBuffer);
if (lstat(sourceName, &sourceStatBuf) != 0) {
errfile = source;
goto done;
}
if (lstat(targetName, &targetStatBuf) != 0) {
if (errno != ENOENT) {
errfile = target;
goto done;
}
} else {
if (force == 0) {
errno = EEXIST;
errfile = target;
goto done;
}
if ((sourceStatBuf.st_ino != 0) && (targetStatBuf.st_ino != 0)) {
if ((sourceStatBuf.st_ino == targetStatBuf.st_ino) &&
(sourceStatBuf.st_dev == targetStatBuf.st_dev)) {
result = TCL_OK;
goto done;
}
}
if (S_ISDIR(sourceStatBuf.st_mode)
&& !S_ISDIR(targetStatBuf.st_mode)) {
errno = EISDIR;
Tcl_AppendResult(interp, "can't overwrite file \"", target,
"\" with directory \"", source, "\"", (char *) NULL);
goto done;
}
if (!S_ISDIR(sourceStatBuf.st_mode)
&& S_ISDIR(targetStatBuf.st_mode)) {
errno = EISDIR;
Tcl_AppendResult(interp, "can't overwrite directory \"", target,
"\" with file \"", source, "\"", (char *) NULL);
goto done;
}
}
if (copyFlag == 0) {
result = TclpRenameFile(sourceName, targetName);
if (result == TCL_OK) {
goto done;
}
if (errno == EINVAL) {
Tcl_AppendResult(interp, "error renaming \"", source, "\" to \"",
target, "\": trying to rename a volume or ",
"move a directory into itself", (char *) NULL);
goto done;
} else if (errno != EXDEV) {
errfile = target;
goto done;
}
}
if (S_ISDIR(sourceStatBuf.st_mode)) {
result = TclpCopyDirectory(sourceName, targetName, &errorBuffer);
if (result != TCL_OK) {
errfile = Tcl_DStringValue(&errorBuffer);
if (strcmp(errfile, sourceName) == 0) {
errfile = source;
} else if (strcmp(errfile, targetName) == 0) {
errfile = target;
}
}
} else {
result = TclpCopyFile(sourceName, targetName);
if (result != TCL_OK) {
errfile = target;
}
}
if ((copyFlag == 0) && (result == TCL_OK)) {
if (S_ISDIR(sourceStatBuf.st_mode)) {
result = TclpRemoveDirectory(sourceName, 1, &errorBuffer);
if (result != TCL_OK) {
errfile = Tcl_DStringValue(&errorBuffer);
if (strcmp(errfile, sourceName) == 0) {
errfile = source;
}
}
} else {
result = TclpDeleteFile(sourceName);
if (result != TCL_OK) {
errfile = source;
}
}
if (result != TCL_OK) {
Tcl_AppendResult(interp, "can't unlink \"", errfile, "\": ",
Tcl_PosixError(interp), (char *) NULL);
errfile = NULL;
}
}
done:
if (errfile != NULL) {
Tcl_AppendResult(interp,
((copyFlag) ? "error copying \"" : "error renaming \""),
source, (char *) NULL);
if (errfile != source) {
Tcl_AppendResult(interp, "\" to \"", target, (char *) NULL);
if (errfile != target) {
Tcl_AppendResult(interp, "\": \"", errfile, (char *) NULL);
}
}
Tcl_AppendResult(interp, "\": ", Tcl_PosixError(interp),
(char *) NULL);
}
Tcl_DStringFree(&errorBuffer);
Tcl_DStringFree(&sourcePath);
Tcl_DStringFree(&targetPath);
return result;
}
static int
FileForceOption(interp, argc, argv, forcePtr)
Tcl_Interp *interp;
int argc;
char **argv;
int *forcePtr;
{
int force, i;
force = 0;
for (i = 0; i < argc; i++) {
if (argv[i][0] != '-') {
break;
}
if (strcmp(argv[i], "-force") == 0) {
force = 1;
} else if (strcmp(argv[i], "--") == 0) {
i++;
break;
} else {
Tcl_AppendResult(interp, "bad option \"", argv[i],
"\": should be -force or --", (char *)NULL);
return -1;
}
}
*forcePtr = force;
return i;
}
static char *
FileBasename(interp, path, bufferPtr)
Tcl_Interp *interp;
char *path;
Tcl_DString *bufferPtr;
{
int argc;
char **argv;
Tcl_SplitPath(path, &argc, &argv);
if (argc == 0) {
Tcl_DStringInit(bufferPtr);
} else {
if ((argc == 1) && (*path == '~')) {
Tcl_DString buffer;
ckfree((char *) argv);
path = Tcl_TranslateFileName(interp, path, &buffer);
if (path == NULL) {
return NULL;
}
Tcl_SplitPath(path, &argc, &argv);
Tcl_DStringFree(&buffer);
}
Tcl_DStringInit(bufferPtr);
if (argc > 0) {
if ((argc > 1)
|| (Tcl_GetPathType(argv[0]) == TCL_PATH_RELATIVE)) {
Tcl_DStringAppend(bufferPtr, argv[argc - 1], -1);
}
}
}
ckfree((char *) argv);
return Tcl_DStringValue(bufferPtr);
}
int
TclFileAttrsCmd(interp, objc, objv)
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
{
Tcl_Obj *resultPtr = Tcl_GetObjResult(interp);
char *fileName;
int length, index;
Tcl_Obj *listObjPtr;
Tcl_Obj *elementObjPtr;
Tcl_DString buffer;
if ((objc > 2) && ((objc % 2) == 0)) {
Tcl_AppendStringsToObj(resultPtr,
"wrong # args: must be \"file attributes name ?option? ?value? ?option value? ...\"",
(char *) NULL);
return TCL_ERROR;
}
fileName = Tcl_GetStringFromObj(objv[0], &length);
if (Tcl_TranslateFileName(interp, fileName, &buffer) == NULL) {
return TCL_ERROR;
}
fileName = Tcl_DStringValue(&buffer);
if (objc == 1) {
listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
for (index = 0; tclpFileAttrStrings[index] != NULL; index++) {
elementObjPtr = Tcl_NewStringObj(tclpFileAttrStrings[index], -1);
Tcl_ListObjAppendElement(interp, listObjPtr, elementObjPtr);
if ((*tclpFileAttrProcs[index].getProc)(interp, index, fileName,
&elementObjPtr) != TCL_OK) {
Tcl_DecrRefCount(listObjPtr);
return TCL_ERROR;
}
Tcl_ListObjAppendElement(interp, listObjPtr, elementObjPtr);
}
Tcl_SetObjResult(interp, listObjPtr);
} else if (objc == 2) {
if (Tcl_GetIndexFromObj(interp, objv[1], tclpFileAttrStrings, "option",
0, &index) != TCL_OK) {
return TCL_ERROR;
}
if ((*tclpFileAttrProcs[index].getProc)(interp, index, fileName,
&elementObjPtr) != TCL_OK) {
return TCL_ERROR;
}
Tcl_SetObjResult(interp, elementObjPtr);
} else {
int i;
for (i = 1; i < objc ; i += 2) {
if (Tcl_GetIndexFromObj(interp, objv[i], tclpFileAttrStrings, "option",
0, &index) != TCL_OK) {
return TCL_ERROR;
}
if ((*tclpFileAttrProcs[index].setProc)(interp, index, fileName,
objv[i + 1]) != TCL_OK) {
return TCL_ERROR;
}
}
}
Tcl_DStringFree(&buffer);
return TCL_OK;
}