1

I've seen many many results for unix systems. I am using cygwin so I am using unistd.h library. I am trying to run this command but It does not run. What could I be missing here?

execl("C:\\WINDOWS\\SYSTEM32\\CMD.EXE", "/c echo foo>C:\\Users\\Sarp\\Desktop\\foo.txt");

Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103
  • You may want to escape the latter backslashes again: `C:\\\\Users\\\\Sarp\\\\Desktop\\\\foo.txt`. But keep in mind that forward slashes work fine on Windows too, even natively (i.e. without the Cygwin abstraction layer). – C2H5OH Feb 04 '13 at 00:52
  • What's the return value? Section 1.6.4 of this page (http://seit.unsw.adfa.edu.au/staff/sites/hrp/webDesignHelp/cygwin-ug-net-nochunks.html) appears to indicate that case sensitivity is not the issue. – enhzflep Feb 04 '13 at 00:54
  • @C2H5OH still, it doesn't change anything... – Sarp Kaya Feb 04 '13 at 01:25
  • @enhzflep it returns -1 – Sarp Kaya Feb 04 '13 at 01:29
  • @SarpKaya - well, to quote a man page for execl (http://linux.about.com/library/cmd/blcmdl3_execl.htm) RETURN VALUE If any of the exec functions returns, an error will have occurred. The return value is -1, and the global variable errno will be set to indicate the error. – enhzflep Feb 04 '13 at 01:32
  • I changed it into `execl("C:\\WINDOWS\\SYSTEM32\\CMD.EXE", "/c echo foo>C:\\\\Users\\\\Sarp\\\\Desktop\\\\foo.txt",(char *)NULL);` Now I get the output of `Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved.` but it does not run anything... – Sarp Kaya Feb 04 '13 at 01:38
  • I guess the problem here is not being able to pass parameters... But how can I fix it? – Sarp Kaya Feb 04 '13 at 01:56

1 Answers1

3

The execl function call does not split the arguments for you. This basically means that you need to separate each command line argument as a different string parameter when invoking the function. For example:

execl("C::\\WINDOWS\\SYSTEM32\\CMD.EXE", "cmd.exe", "/c", 
      "echo", "foo", ">C:\\Users\\Sarp\\Desktop\\foo.txt")

However, I'm under the impression that the output redirection may not work (depending on how the Windows shell interprets those), so I encourage you to try the system() function which resembles your usage case more closely.

C2H5OH
  • 5,452
  • 2
  • 27
  • 39
  • Thanks for that but instead of `"cmd.exe"` if i just put `""` it still works, so really what should be written into the second argument as it has no effect to the actual parameters whatsoever? – Sarp Kaya Feb 05 '13 at 04:48
  • 1
    The second parameter of `execl()` will become [the `argv[0]` string in the case of C programs](http://stackoverflow.com/questions/2050961/is-argv0-name-of-executable-an-accepted-standard-or-just-a-common-conventi). – C2H5OH Feb 05 '13 at 08:35
  • 1
    Note that in Windows the new process is passed a single argument string, so execl just appends the strings together. (That's why the details of how the output redirection should be split into tokens doesn't matter.) – Harry Johnston Feb 06 '13 at 05:45
  • you can use shlex's shell split here! https://docs.python.org/3/library/shlex.html – ti7 Nov 01 '21 at 19:11