| 1 | /* |
|---|
| 2 | This file is part of MutekP. |
|---|
| 3 | |
|---|
| 4 | MutekP is free software; you can redistribute it and/or modify it |
|---|
| 5 | under the terms of the GNU General Public License as published by |
|---|
| 6 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 | (at your option) any later version. |
|---|
| 8 | |
|---|
| 9 | MutekP is distributed in the hope that it will be useful, but |
|---|
| 10 | WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 12 | General Public License for more details. |
|---|
| 13 | |
|---|
| 14 | You should have received a copy of the GNU General Public License |
|---|
| 15 | along with MutekP; if not, write to the Free Software Foundation, |
|---|
| 16 | Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 17 | |
|---|
| 18 | UPMC / LIP6 / SOC (c) 2008 |
|---|
| 19 | Copyright Ghassan Almaless <ghassan.almaless@gmail.com> |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | #include <string.h> |
|---|
| 23 | #include <stdint.h> |
|---|
| 24 | #include <vfs.h> |
|---|
| 25 | #include <kminiShell.h> |
|---|
| 26 | |
|---|
| 27 | error_t cp_func(void *param) |
|---|
| 28 | { |
|---|
| 29 | char *src_name; |
|---|
| 30 | char *dest_name; |
|---|
| 31 | error_t err; |
|---|
| 32 | uint32_t argc; |
|---|
| 33 | uint32_t i; |
|---|
| 34 | ssize_t size; |
|---|
| 35 | struct ku_obj kuo; |
|---|
| 36 | struct vfs_file_s src_file; |
|---|
| 37 | struct vfs_file_s dest_file; |
|---|
| 38 | ms_args_t *args; |
|---|
| 39 | uint_t count; /* FIXME to be correctly used */ |
|---|
| 40 | |
|---|
| 41 | args = (ms_args_t *) param; |
|---|
| 42 | argc = args->argc; |
|---|
| 43 | dest_name = args->argv[argc - 1]; |
|---|
| 44 | err = 0; |
|---|
| 45 | |
|---|
| 46 | KK_BUFF(kuo, dest_name); |
|---|
| 47 | if((err=vfs_open(ms_n_cwd, &kuo, VFS_O_WRONLY | VFS_O_CREATE,0, &dest_file))) |
|---|
| 48 | return err; |
|---|
| 49 | |
|---|
| 50 | argc --; |
|---|
| 51 | |
|---|
| 52 | for(i=1; i< argc; i++) |
|---|
| 53 | { |
|---|
| 54 | src_name = args->argv[i]; |
|---|
| 55 | |
|---|
| 56 | KK_BUFF(kuo, src_name); |
|---|
| 57 | if((err=vfs_open(ms_n_cwd, &kuo,VFS_O_RDONLY,0, &src_file))){ |
|---|
| 58 | vfs_close(&dest_file, &count); |
|---|
| 59 | ksh_print("error while opinig %s\n",src_name); |
|---|
| 60 | return err; |
|---|
| 61 | } |
|---|
| 62 | size = 0; |
|---|
| 63 | KK_SZ_BUFF(kuo, buffer, BUFFER_SIZE); |
|---|
| 64 | while((size = vfs_read(&src_file, &kuo)) > 0) |
|---|
| 65 | { |
|---|
| 66 | //KK_SZ_BUFF(kuo, buffer, BUFFER_SIZE); |
|---|
| 67 | if((size=vfs_write(&dest_file, &kuo)) < 0) |
|---|
| 68 | goto CP_FUNC_ERROR; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | if(size < 0) goto CP_FUNC_ERROR; |
|---|
| 72 | vfs_close(&src_file, &count); |
|---|
| 73 | } |
|---|
| 74 | vfs_close(&dest_file,&count); |
|---|
| 75 | return 0; |
|---|
| 76 | |
|---|
| 77 | CP_FUNC_ERROR: |
|---|
| 78 | vfs_close(&src_file,&count); |
|---|
| 79 | vfs_close(&dest_file,&count); |
|---|
| 80 | |
|---|
| 81 | return size; |
|---|
| 82 | } |
|---|