Debian Codegraph @

pTk/mTk/tclGeneric/tclFCmd.c

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