user1089679
user1089679

Reputation: 2368

How to use cp (Copy) command in system function in native C code on Android

I want to copy file from on directory to another in my native C program. I tried using system function but it's not working.

I tried this code in native code of Android

int result = system("cp /mnt/test /Download/"); // It's not working

This system function returns 256 (error code) integer value. So we can say system function is working in Android. I also installed BusyBox so I can use the cp command also.

If I execute directly cp /mnt/test /Download/ command then it's working fine.

So what is problem here in system function. Have I missed something?

Upvotes: 1

Views: 6004

Answers (3)

Klaas van Gend
Klaas van Gend

Reputation: 1128

I guess your question stems from the fact that there is no "cp" function call in glibc.

If you only need to copy files, just open two files and start copying ;-) The textbook examples for implementing cp usually start by byte-by-byte copy, then move to block copy and finally implement a file copy using mmap().

If you need other functionality like preserving symlinks and such, the code will quickly increase in complexity.

In that case, it might be tempting to want to use system(). However, on Android, there are only a few systems that ship with busybox. Most systems still ship with the original Android 'toolbox'. This one is very very limited in what it provides and can do.

By the way, if 'cp' doesn't work and you are sure you have busybox, try '/bin/busybox cp' instead.

Upvotes: 0

hopia
hopia

Reputation: 5006

Could be a path issue. Try specifying the full path to the busybox "cp" command, like so:

int result = system("/path/to/bbdir/cp /mnt/test /Download/"); 

I'm not sure where busybox keeps its files, so just change "/path/to/bbdir" to reflect your actual busybox path.

Upvotes: 0

well-wisher
well-wisher

Reputation: 74

system() function returns non-zero to indicate that a command processor is available, or zero if none is available. You can use macros defined in sys/wait.h to analize the return value. Also system() in fact calls fork() syscall and execl() with default (sh) command interperter. So if your programm exits immidiately after calling system() consider using waitpid() function.

Upvotes: 1

Related Questions