2 (C) 1998 AROS - The Amiga Research OS
9 /*****************************************************************************
25 Creates identical copies of one or more files.
29 SOURCE - The files that are to be copied. You can specify a pattern
32 DEST - The directory, where the copies of the files are to be
33 stored. If only one file was specified as SOURCE, this
34 can be a different filename. The new file will named DEST.
38 Standard DOS return codes.
46 Pattern-Matching does not work, yet.
48 Error messages could be more comprehensive.
52 Delete(), Rename(), MakeDir()
58 ******************************************************************************/
62 #include <proto/exec.h>
63 #include <exec/memory.h>
64 #include <exec/types.h>
65 #include <proto/dos.h>
67 #include <dos/rdargs.h>
68 #include <utility/tagitem.h>
71 static const char version
[] = "$VER: Copy 41.0 (29.08.1998)\n";
74 #define ARG_TEMPLATE "SOURCE/M/A,DEST/A"
80 #define ERROR_HEADER "Copy"
84 int copy_file(STRPTR source
, STRPTR dest
)
86 int result
= RETURN_OK
;
89 sf
= Open(source
, MODE_OLDFILE
);
91 df
= Open(dest
, MODE_NEWFILE
);
95 while((read
= Read(sf
, buffer
, sizeof(buffer
))) > 0)
96 Write(df
, buffer
, read
);
98 PrintFault(IoErr(), ERROR_HEADER
);
99 result
= RETURN_ERROR
;
103 PrintFault(IoErr(), ERROR_HEADER
);
104 result
= RETURN_FAIL
;
108 PrintFault(IoErr(), ERROR_HEADER
);
109 result
= RETURN_FAIL
;
116 STRPTR
make_dest_name(STRPTR source
, STRPTR destdir
)
119 ULONG slen
= (ULONG
)strlen(source
);
120 ULONG dlen
= (ULONG
)strlen(destdir
);
122 dest
= AllocVec(slen
+ dlen
+ 2, MEMF_ANY
);
124 PrintFault(ERROR_NO_FREE_STORE
, ERROR_HEADER
);
128 strcpy(dest
, destdir
);
129 AddPart(dest
, source
, slen
+ dlen
+ 2);
135 int copy_files(STRPTR
*source
, STRPTR dest
)
137 int result
= RETURN_OK
;
138 BOOL dest_is_dir
= FALSE
;
139 BOOL multiple_sources
= FALSE
;
143 /* find out, if dest is a directory */
144 lock
= Lock(dest
, SHARED_LOCK
);
146 /* if IoErr() == ERROR_OBJECT_NOT_FOUND, dest is a plain file */
147 if(IoErr() != ERROR_OBJECT_NOT_FOUND
) {
148 PrintFault(IoErr(), ERROR_HEADER
);
149 result
= RETURN_FAIL
;
152 struct FileInfoBlock
*fib
;
153 fib
= AllocDosObject(DOS_FIB
, NULL
);
155 if(Examine(lock
, fib
) == DOSTRUE
) {
156 if(fib
->fib_DirEntryType
>= 0)
159 PrintFault(IoErr(), ERROR_HEADER
);
160 result
= RETURN_FAIL
;
162 FreeDosObject(DOS_FIB
, fib
);
164 PrintFault(IoErr(), ERROR_HEADER
);
165 result
= RETURN_FAIL
;
172 /* find out, if we have multiple source files */
174 multiple_sources
= TRUE
;
176 #warning FIXME: pattern matching
179 if(multiple_sources
&& !dest_is_dir
) {
180 PrintFault(ERROR_OBJECT_WRONG_TYPE
, ERROR_HEADER
);
181 result
= RETURN_FAIL
;
184 if(multiple_sources
) {
185 unsigned int pos
= 0;
186 STRPTR dest_name
= NULL
;
187 while(source
[pos
] && result
== RETURN_OK
) {
188 #warning FIXME: pattern matching
189 dest_name
= make_dest_name(source
[pos
], dest
);
191 result
= copy_file(source
[pos
], dest_name
);
194 result
= RETURN_FAIL
;
198 } else { /* !multiple_sources */
200 STRPTR dest_name
= make_dest_name(*source
, dest
);
202 result
= copy_file(*source
, dest_name
);
205 result
= RETURN_FAIL
;
207 result
= copy_file(*source
, dest
);
214 int main(int argc
, char *argv
[])
216 int result
= RETURN_OK
;
218 IPTR argarray
[ARG_NUM
] = { NULL
, NULL
};
222 rda
= ReadArgs(ARG_TEMPLATE
, argarray
, NULL
);
224 result
= copy_files((STRPTR
*)argarray
[ARG_SOURCE
], (STRPTR
)argarray
[ARG_DEST
]);
227 PrintFault(IoErr(), ERROR_HEADER
);
228 result
= RETURN_FAIL
;