2 Copyright © 1995-2008, The AROS Development Team. All rights reserved.
8 /*****************************************************************************
16 Rename [{FROM}] <name> [TO|AS] <name> [QUIET]
18 FROM/A/M,TO=AS/A,QUIET/S
26 Renames a directory or file. Rename can also act like the UNIX mv
27 command, which moves a file or files to another location on disk.
31 FROM -- The name(s) of the file(s) to rename or move. There may
32 be many files specified, this is used when moving files
35 TO|AS -- The name which we wish to call the file.
37 QUIET -- Suppress any output from the command.
41 Standard DOS error codes.
47 Rename letter1.doc letter2.doc letters
49 Moves letter1.doc and letter2.doc to the directory letters.
51 Rename ram:a ram:b quiet
52 Rename from ram:a to ram:b quiet
53 Rename from=ram:a to=ram:b quiet
55 All versions, renames file "a" to "b" and does not output any
56 diagnostic information.
64 Rename() can only move a file to another directory, if and only if
65 the to path has the from filename appended to the end.
68 Rename("ram:a", "ram:clipboards/a");
70 Rename("ram:a", "ram:clipboards/");
72 ******************************************************************************/
75 #include <aros/debug.h>
77 #include <proto/dos.h>
78 #include <proto/exec.h>
81 #include <dos/dosasl.h>
82 #include <dos/rdargs.h>
83 #include <exec/memory.h>
84 #include <exec/types.h>
88 #define ARG_TEMPLATE "FROM/A/M,TO=AS/A,QUIET/S"
90 #define APPNAME "Rename"
101 #define MAX_PATH_LEN 2048
103 const TEXT version
[] = "$VER: Rename 41.2 (23.11.2000)\n";
106 int doRename(STRPTR
*from
, STRPTR to
, BOOL quiet
);
115 IPTR args
[NOOFARGS
] = { (IPTR
) NULL
, (IPTR
) NULL
, FALSE
};
117 int retval
= RETURN_FAIL
;
119 rda
= ReadArgs(ARG_TEMPLATE
, args
, NULL
);
123 STRPTR
*from
= (STRPTR
*)args
[ARG_FROM
];
124 STRPTR to
= (STRPTR
)args
[ARG_TO
];
125 BOOL quiet
= args
[ARG_QUIET
] != 0;
127 retval
= doRename(from
, to
, quiet
);
133 PrintFault(IoErr(), APPNAME
);
134 retval
= RETURN_ERROR
;
141 int doRename(STRPTR
*from
, STRPTR to
, BOOL quiet
)
143 #define ERROR(n) retval = (n); goto cleanup
145 struct AnchorPath
*ap
;
149 BOOL destIsDir
= FALSE
;
158 ap
= (struct AnchorPath
*)AllocVec(sizeof(struct AnchorPath
) + MAX_PATH_LEN
+ MAX_PATH_LEN
,
159 MEMF_ANY
| MEMF_CLEAR
);
166 pathName
= ((UBYTE
*) (ap
+ 1)) + MAX_PATH_LEN
;
168 ap
->ap_BreakBits
= SIGBREAKF_CTRL_C
;
169 ap
->ap_Flags
= APF_DOWILD
;
170 ap
->ap_Strlen
= MAX_PATH_LEN
;
171 if (MatchFirst(from
[0], ap
) != 0)
174 if (ioerr
== ERROR_OBJECT_NOT_FOUND
)
176 Printf("Can't rename %s as %s because ", from
[0], to
);
180 itsWild
= ap
->ap_Flags
& APF_ITSWILD
;
183 /* First we check if the destination is a directory */
184 tolock
= Lock(to
, SHARED_LOCK
);
187 struct FileInfoBlock
*fib
= AllocDosObject(DOS_FIB
, NULL
);
192 PrintFault(IoErr(), APPNAME
);
197 i
= Examine(tolock
, fib
);
198 entrytype
= fib
->fib_EntryType
;
199 FreeDosObject(DOS_FIB
, fib
);
210 PrintFault(IoErr(), APPNAME
);
217 /* Check if dest is not a dir and src is pattern or multi */
218 if (!destIsDir
&& (itsWild
|| from
[1]))
220 Printf("Destination \"%s\" is not a directory.\n", to
);
225 /* Handle single file name change */
226 isSingle
=!destIsDir
;
227 /* 15th Jan 2004 bugfix, (destIsDir && ...) not (!destisDir && ...) ! - Piru */
228 if (destIsDir
&& !from
[1])
232 fromlock
= Lock(from
[0], ACCESS_READ
);
235 isSingle
= SameLock(fromlock
, tolock
) == LOCK_SAME
;
240 /* 4th May 2003 bugfix: be quiet about single object renames - Piru */
245 if (ParsePattern(from
[0], pathName
, MAX_PATH_LEN
) > -1 &&
246 Rename(pathName
, to
))
252 Printf("Can't rename %s as %s because ", from
[0], to
);
258 if (!NameFromLock(tolock
, pathName
, MAX_PATH_LEN
))
260 if (IoErr() == ERROR_LINE_TOO_LONG
)
262 PrintFault(IoErr(), APPNAME
);
273 stccpy(pathName
, to
, MAX_PATH_LEN
);
277 /* Now either only one specific file should be renamed or the
278 destination is really a directory. We can use the same routine
281 fileStart
= pathName
+ strlen(pathName
);
283 ap
->ap_BreakBits
= SIGBREAKF_CTRL_C
;
284 ap
->ap_Strlen
= MAX_PATH_LEN
;
286 for (i
= 0; from
[i
]; i
++)
288 for (match
= MatchFirst(from
[i
], ap
); match
== 0; match
= MatchNext(ap
))
290 /* Check for identical 'from' and 'to'? */
294 /* Clear former filename */
296 if (!AddPart(pathName
, FilePart(ap
->ap_Buf
), MAX_PATH_LEN
))
300 PrintFault(ERROR_LINE_TOO_LONG
, APPNAME
);
301 SetIoErr(ERROR_LINE_TOO_LONG
);
309 Printf("Renaming %s as %s\n", ap
->ap_Buf
, pathName
);
312 if (!Rename(ap
->ap_Buf
, pathName
))
317 Printf("Can't rename %s as %s because ", ap
->ap_Buf
, pathName
);
325 if (ap
->ap_FoundBreak
& SIGBREAKF_CTRL_C
)
327 PrintFault(ERROR_BREAK
, NULL
);
329 retval
= RETURN_WARN
;
345 PrintFault(ioerr
, NULL
);
347 //retval = ERROR_FAIL;