home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / shell / 5262 < prev    next >
Encoding:
Internet Message Format  |  1993-01-05  |  2.9 KB

  1. Xref: sparky comp.unix.shell:5262 comp.unix.programmer:5834
  2. Nntp-Posting-Host: solva.ifi.uio.no
  3. Newsgroups: comp.unix.shell,comp.unix.programmer
  4. Path: sparky!uunet!mcsun!sunic!aun.uninett.no!nuug!ifi.uio.no!nntp.ifi.uio.no!jar
  5. From: jar@solva.ifi.uio.no (Jo Are Rosland)
  6. Subject: Re: A quoting problem.
  7. In-Reply-To: richb@stard.Eng.Sun.COM's message of 5 Jan 1993 18:55:44 GMT
  8. Message-ID: <JAR.93Jan5234121@solva.ifi.uio.no>
  9. X-Md4-Signature: 7865957a18c847f93c565a20a7cbbbed
  10. Sender: jar@ifi.uio.no (Jo Are Rosland)
  11. Organization: Dept. of Informatics, University of Oslo, Norway
  12. References: <lkh5trINNvf@exodus.Eng.Sun.COM> <1ialduINNe7o@chnews.intel.com>
  13.     <lkjmdgINN3t2@exodus.Eng.Sun.COM>
  14. Date: Tue, 5 Jan 1993 22:41:21 GMT
  15. Lines: 74
  16. Originator: jar@solva.ifi.uio.no
  17.  
  18. In article <lkjmdgINN3t2@exodus.Eng.Sun.COM> Rich Burridge writes:
  19. > Let's turn this into a more specific example. Solving this will hopefully
  20. > show me how to solve the generic case.
  21. > ------
  22. > I have three files I want to set the environment variable FILES too:
  23. > file a
  24. > file'a
  25. > file"a
  26. > What should the string cmd be equal too below in order to get this to work?
  27. > char cmd[1024] ;
  28. > /*  Incorrect version - no quoting. */
  29. > strcpy(cmd, "FILES='file a file'a file"a';export FILES;rm $FILES") ;
  30. > ...
  31. > execl("/bin/sh", "sh", "-c", cmd, (char *) 0) ;
  32. > -----
  33. > No solutions suggest unlink and removing the files one at a time please.
  34. > I need a generic solution. I need the correct quoted interpretation of:
  35. > FILES='file a file'a file"a'
  36. > that correctly works in the string above. Note that people have suggested
  37. > using putenv(3). I'd still have the same quoting problem there.
  38. > Thanks again for any help.
  39.  
  40. Quoting is not all you have to do to achieve the above, the following
  41. works:
  42.  
  43.   strcpy(cmd, "FILES='file\\ a file'\\\\\\''a file\\\"a'; eval touch $FILES");
  44.  
  45. The shell doesn't do any further quote processing on the value of an
  46. environment variable, so you have to use eval to get that done.
  47.  
  48. This is how you'd program if you know the value to pass on to execl()
  49. already at compile time.  But this doesn't seem to fit with what
  50. you're saying about a "generic routine".  I guess your function is
  51. getting these file names, and the command to run, as arguments, and
  52. that the command argument can include the use of the environment
  53. variable FILES, and possibly some other variables as well.
  54.  
  55. In this case your code could look like this:
  56.  
  57.   char cmd[8192];
  58.   char files[8192];
  59.  
  60.   for each file name {
  61.       for each character {
  62.           if (character == single-quote)
  63.               add '\\\'' to files (as a literal string in c
  64.               this would be "'\\\\\\''")
  65.           else
  66.               add a backquote followed by the character to files
  67.       }
  68.       add a space to files
  69.   }
  70.  
  71.   sprintf(cmd, "FILES=\'%s\'; eval %s", files, command);
  72.  
  73.   execl("/bin/sh", "sh", "-c", cmd, (char *) 0) ;
  74.  
  75.  
  76. Hope this helps,
  77. --
  78. Jo Are Rosland
  79. jar@ifi.uio.no
  80.