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

  1. Xref: sparky comp.unix.shell:5263 comp.unix.programmer:5835
  2. Newsgroups: comp.unix.shell,comp.unix.programmer
  3. Path: sparky!uunet!cs.utexas.edu!convex!seas.smu.edu!mustafa
  4. From: mustafa@seas.smu.edu (Mustafa Kocaturk)
  5. Subject: Re: A quoting problem.
  6. Message-ID: <1993Jan5.223544.18750@seas.smu.edu>
  7. Summary: Using escape sequences in Unix and literals in C
  8. Keywords: SVR4, Bourne shell, quoting filenames.
  9. Sender: news@seas.smu.edu (USENET News System)
  10. Nntp-Posting-Host: turbo_f.seas.smu.edu
  11. Followups-To: comp.lang.c
  12. Organization: SMU - School of Engineering & Applied Science - Dallas
  13. References: <lkh5trINNvf@exodus.Eng.Sun.COM> <1ialduINNe7o@chnews.intel.com>
  14. Date: Tue, 5 Jan 1993 22:35:44 GMT
  15. Lines: 48
  16.  
  17. In article <1ialduINNe7o@chnews.intel.com> bhoughto@sedona.intel.com (Blair P. Houghton) writes:
  18. >In article <lkh5trINNvf@exodus.Eng.Sun.COM> richb@stard.Eng.Sun.COM (Rich Burridge) writes:
  19. >>strcpy(cmd, "FILES='file file1 file2';export FILES;rm $FILES") ;
  20. >>...
  21. >>execl("/bin/sh", "sh", "-c", cmd, (char *) 0) ;
  22. >>...
  23. >>Now the problem is how to correct quote the "cmd" string to handle all
  24. >>possible filenames. Can this be done?
  25. >
  26. >Not trivially.
  27. >
  28. >Since you're sending the output to the shell your program
  29. >has to be as smart as you would have been had you typed the
  30. >command-line to the shell yourself.
  31. >
  32. >That is, the program would have to put a backslash in all
  33. >the proper places in filenames that require escaped characters.
  34. >
  35. >The simplest way to do this would be to put a backslash in
  36. >front of every character, but you then conflict with C's
  37. >interpretation of certain escape sequences within strings
  38. >(e.g., "\n" and "\"").
  39. >
  40. >That is, sometimes it takes two backslashes, sometimes one,
  41. >but most of the time none.
  42. >
  43. >                --Blair
  44. >                  "That depends..."
  45.  
  46. The idea of preceding each character with a  backslash  does  not
  47. pose  a  problem  as far as the C string interpretation rules are
  48. concerned: C will interpret escape  sequences  in  literals,  and
  49. only during compilation.  The string under consideration is not a
  50. literal; it is an array filled in by the  program  while  it exe-
  51. tes.  C  will never have to interpret that string, except for the
  52. terminating null character that is interpreted by the execl func-
  53. tion.  (Followups to comp.lang.c)
  54.  
  55. This leaves you with one less reason for extra  backslashes  than
  56. suggested above.
  57.  
  58.  
  59. Best regards,
  60.  
  61. Mustafa
  62. -- 
  63. Mustafa Kocaturk   mustafa@seas.smu.edu  EE Dept., Room 305A, Caruth Bldg.
  64. Home: 214-706-5954  Office: 214-768-1475  SMU Box 753190, Dallas, TX 75275
  65.