This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: sprintf question.


The problem does not lie in sprintf(), but with system().
System invokes a shell which re-interprets the backslashes
\w -> w, \s -> s and \n -> n 

The cygwin shell, ash -- I'm guessing here --, likes slashes, not back slashes.
You might try, system("/cygdrive/c/winnt/system32/notepad.exe");

Heck, system("notepad"); is probably good enough, if you're not paranoid,
and /cygdrive/c/WINNT/system32 is in your $PATH.

BTW, why copy backslashes at all?

And, sprintf(result,"%s",backslashes);
is the same as strcpy(result,backslashes), but works too hard ;-)

> -----Original Message-----
> From: cygwin-owner@cygwin.com [mailto:cygwin-owner@cygwin.com]On Behalf
> Of Pitts, Christopher (Inland)
> Sent: Thursday, June 20, 2002 12:10 PM
> To: 'cygwin@cygwin.com'
> Subject: sprintf question.
> 
> 
> I have, what I hope, is a easy question about sprintf under cygwin.
> 
> In a nutshell, it looks like system or sprintf is chopping off any
> backslashes that get passed in.  Example below.
> 
> C:\work\COMPILER\ee>gcc -v
> Reading specs from /usr/lib/gcc-lib/i686-pc-cygwin/2.95.3-5/specs
> gcc version 2.95.3-5 (cygwin special)
> 
> 
> sprintfExample.c
> ----CUT HERE----
> #include <stdio.h>
> #include <stdlib.h>
> 
> int main(void)
> {
>     char backslashes[] = "C:\\winnt\\system32\\notepad.exe";
>     char result[128];
>     int retval;
> 
>     printf("printf says \"%s\"\n",backslashes);
>     sprintf(result,"%s",backslashes);
> 
>     retval = system(result);
> 
>     return(0);
> }
> ----END CUT----
> 
> compile it...
> C:\work\COMPILER\ee>gcc sprintfRevision.c -o sprintf.exe
> 
> run it...
> C:\work\COMPILER\ee>sprintf.exe
> printf says "C:\winnt\system32\notepad.exe"
> C:winntsystem32notepad.exe: not found
> 
> 
> I've looked through the GNU LibC manual (PDF), the man pages, and the cygwin
> docs, and I don't see any mention of this.
> Am I just missing something easy?  Is this a bug?
> 
> If I change the 
> char backslashes[] = "C:\\winnt\\system32\\notepad.exe";
> to be 
> char backslashes[] = "C:'\\'winnt'\\'system32'\\'notepad.exe";
> it works as expected (fires up notepad).
> 
> What am I missing here guys?
> 
> 
> C. Pitts
> 
> 
> --
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
> Bug reporting:         http://cygwin.com/bugs.html
> Documentation:         http://cygwin.com/docs.html
> FAQ:                   http://cygwin.com/faq/

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]