home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.unix.shell:5262 comp.unix.programmer:5834
- Nntp-Posting-Host: solva.ifi.uio.no
- Newsgroups: comp.unix.shell,comp.unix.programmer
- Path: sparky!uunet!mcsun!sunic!aun.uninett.no!nuug!ifi.uio.no!nntp.ifi.uio.no!jar
- From: jar@solva.ifi.uio.no (Jo Are Rosland)
- Subject: Re: A quoting problem.
- In-Reply-To: richb@stard.Eng.Sun.COM's message of 5 Jan 1993 18:55:44 GMT
- Message-ID: <JAR.93Jan5234121@solva.ifi.uio.no>
- X-Md4-Signature: 7865957a18c847f93c565a20a7cbbbed
- Sender: jar@ifi.uio.no (Jo Are Rosland)
- Organization: Dept. of Informatics, University of Oslo, Norway
- References: <lkh5trINNvf@exodus.Eng.Sun.COM> <1ialduINNe7o@chnews.intel.com>
- <lkjmdgINN3t2@exodus.Eng.Sun.COM>
- Date: Tue, 5 Jan 1993 22:41:21 GMT
- Lines: 74
- Originator: jar@solva.ifi.uio.no
-
- In article <lkjmdgINN3t2@exodus.Eng.Sun.COM> Rich Burridge writes:
- > Let's turn this into a more specific example. Solving this will hopefully
- > show me how to solve the generic case.
- > ------
- >
- > I have three files I want to set the environment variable FILES too:
- >
- > file a
- > file'a
- > file"a
- >
- > What should the string cmd be equal too below in order to get this to work?
- >
- > char cmd[1024] ;
- >
- > /* Incorrect version - no quoting. */
- >
- > strcpy(cmd, "FILES='file a file'a file"a';export FILES;rm $FILES") ;
- >
- > ...
- >
- > execl("/bin/sh", "sh", "-c", cmd, (char *) 0) ;
- >
- > -----
- > No solutions suggest unlink and removing the files one at a time please.
- > I need a generic solution. I need the correct quoted interpretation of:
- >
- > FILES='file a file'a file"a'
- >
- > that correctly works in the string above. Note that people have suggested
- > using putenv(3). I'd still have the same quoting problem there.
- >
- > Thanks again for any help.
-
- Quoting is not all you have to do to achieve the above, the following
- works:
-
- strcpy(cmd, "FILES='file\\ a file'\\\\\\''a file\\\"a'; eval touch $FILES");
-
- The shell doesn't do any further quote processing on the value of an
- environment variable, so you have to use eval to get that done.
-
- This is how you'd program if you know the value to pass on to execl()
- already at compile time. But this doesn't seem to fit with what
- you're saying about a "generic routine". I guess your function is
- getting these file names, and the command to run, as arguments, and
- that the command argument can include the use of the environment
- variable FILES, and possibly some other variables as well.
-
- In this case your code could look like this:
-
- char cmd[8192];
- char files[8192];
-
- for each file name {
- for each character {
- if (character == single-quote)
- add '\\\'' to files (as a literal string in c
- this would be "'\\\\\\''")
- else
- add a backquote followed by the character to files
- }
- add a space to files
- }
-
- sprintf(cmd, "FILES=\'%s\'; eval %s", files, command);
-
- execl("/bin/sh", "sh", "-c", cmd, (char *) 0) ;
-
-
- Hope this helps,
- --
- Jo Are Rosland
- jar@ifi.uio.no
-