How to use *.* parameter in the exec family functions?

A

andylcx

Hi all:
I have one question about the system call under Unix system. I would
like to copy all the files in the current directory to a destination
directory using the following code, but it does not work. Any idea?
Thanks in advance!
if (fork() == 0)
{
execl("/usr/bin/cp", "cp", "*.*", "destination", (char*)0);
}
 
J

Joona I Palaste

(e-mail address removed) scribbled the following:
Hi all:
I have one question about the system call under Unix system. I would
like to copy all the files in the current directory to a destination
directory using the following code, but it does not work. Any idea?
Thanks in advance!
if (fork() == 0)
{
execl("/usr/bin/cp", "cp", "*.*", "destination", (char*)0);
}

comp.lang.c reply: Ask in comp.unix.programmer.
Off-topic reply: *.* is a feature of the shell, not the Unix system
itself. The way you're doing it, cp attempts to copy a file that is
actually named "*.*". You need to execl() a shell and give "cp *.*
destination" as its parameter.
 
?

=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=

I have one question about the system call under Unix system. I would
like to copy all the files in the current directory to a destination
directory using the following code, but it does not work. Any idea?

This newsgroup is for questions about C, not about Unix, but I will
answer nonetheless.
if (fork() == 0)
{
execl("/usr/bin/cp", "cp", "*.*", "destination", (char*)0);
}

First of all, Unix programs don't expand glob patterns in command-line
arguments. They expect their parent (normally the shell) to do it for
them. When you run the above code, cp tries to copy a file named,
literally, "*.*" (without the quotes).

Second, the glob pattern "*.*" will only match files whose name
contains a period. In DOS, there is always a period, even in file
names without extensions ("C:\README" and "C:\README." are the same
file), so "*.*" will match all files, but it won't in Unix.

Third, execl() may fail, and bad things will probably happen if you
don't exit immediately (preferrably by calling _exit(), not exit())
when it does.

DES
 
S

SM Ryan

(e-mail address removed) wrote:
# Hi all:
# I have one question about the system call under Unix system. I would
# like to copy all the files in the current directory to a destination
# directory using the following code, but it does not work. Any idea?

Unless you need to do something fancy like redirect stdin or stderr,
you can do with the ANSI C function system(). It will take care of
expanding *.* for you which the exec* functions will not.

Just to remember to 'quote' file names in the system() call that you would
need to quote if typing it in at the command prompt.

# if (fork() == 0)
# {
# execl("/usr/bin/cp", "cp", "*.*", "destination", (char*)0);
# }

int exitcode = system("/usr/bin/cp *.* destination");
if (exitcode!=0) {awk! an error occurred!}

or perhaps

char *destination = ....;
....
static char F[] = "/usr/bin/cp *.* '%s'";
char *f = malloc((sizeof F)+strlen(destination));
sprintf(f,F,destination);
int exitcode = system(f);
free(f);
if (exitcode!=0) {awk! an error occurred!}
 
K

Keith Thompson

SM Ryan said:
Unless you need to do something fancy like redirect stdin or stderr,
you can do with the ANSI C function system(). It will take care of
expanding *.* for you which the exec* functions will not.

Just to remember to 'quote' file names in the system() call that you would
need to quote if typing it in at the command prompt.

['#' remapped to '>' yet again]

More precisely, system-specific entity that processes the command
string will presumably take care of expanding wildcards in some
system-specific manner.

<OT>
On Unix-like systems, the system() function invokes the shell,
/bin/sh, with the given command string as an argument. If you don't
go through the shell (e.g., if you use one of the system-specific
exec*() function), your wildcards aren't going to be expanded, and the
literal string is going to be passed directly to the /usr/bin/cp
command, which won't know what to do with it unless there happens to
be a file called "*.*".

And of course, given the usual Unix semantics, this will copy only
files whose names whose names contain a '.' character *and* don't
start with '.' -- which is great if that happens to be what you want.

For more information, try comp.unix.programmer.
</OT>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top