The following code is used in a native c++ library to create directory under android shell,
JNIEXPORT void JNICALL Java_com_xprea_lib_STB_mkdir(JNIEnv* env, jobject javaThis, jstring jdir) {
const char* dir = (env)->GetStringUTFChars(jdir, 0);
string d=dir;
string cmd= "su -c 'mkdir -p "+d+"'";
const char* c=cmd.c_str();
LOGE("s%s",c);
system(c);
}
it's not working because the command is built from concatenated strings. I tested it without concatenation and it's working
What is the correct way to concat the strings all together and send them to system()
Your concatenation"su -c 'mkdir -p "+d+"'"
results in the stringsu -c 'mkdir -p BLA'
(If the dir name is BLA)So the command
su
searches for a command namedmkdir -p BLA
which it won't find. Better make:"su -c mkdir -p '"+d+"'"
your concatenation. This way it will search for a command namedmkdir
which it hopefully will find, and the directory name may even contain white space (although you would have to escape the'
and\
character with\
.