I wrote a code below
#include<stdio.h>
int main(int argc, char *argv[]) {
char cmd[50]="dir";
if (argc == 2) {
sprintf(cmd,"dir %s",argv[1]);
}
if (argc == 3) {
sprintf(cmd,"dir %s %s", argv[1], argv[2]);
}
printf("%s\n",cmd);
system(cmd);
return 0;
}
when I executed like below
I think can't pass '*' by *argv[]
How can I pass something like "*.c" ?
update
code
#include<stdio.h>
int main(int argc, char *argv[]) {
char cmd[50]="dir";
if (argc == 2) {
sprintf(cmd,"dir %s",argv[1]);
}
if (argc == 3) {
sprintf(cmd,"dir %s %s", argv[1], argv[2]);
}
if (argc > 3) {
sprintf(cmd,"dir %s %s", argv[1], argv[2]);
}
printf("%s\n",cmd);
system(cmd);
return 0;
}
what..... @.@ ?
Updated code again
#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[]) {
int i;
char sp[2]=" ", cmd[250]="dir";
if (argc > 1) {
sprintf(cmd,"dir /d ");
for (i =1 ; i < argc; i ++) {
strcat(cmd,sp);
strcat(cmd,argv[i]);
}
}
printf("%s\n",cmd);
system(cmd);
return 0;
}
see what happen when I executed
kind of ugly.... any decent idea?
*.c
is passed correctly. – Borrellif (argc > 3)
case. Probably shell expansion is occurring – Sierrasiesserls "*.c"
– Jone"*.c"
? – Joneargc > 3
case is actually happening – Sierrasiesserecho *.c
. Does it display*.c
or the list of C files? – Jonels
. Especially since the stupiddir
command cannot handle multiple arguments gracefully. – Jonedir
. You cannot implement a decentls
as a wrapper on top of such a braindead CMD.EXE built-in command. You will need to write some substantial amount of non trivial code to implement thels
unix utility. If you just care about a single filespec, try to find a different toolchain that does not expand wildcards, such as Microsoft Visual Studio (I never thought I would ever give such advice!) – Jone