Running command line from C
Asked Answered
R

2

5

I am running Windows 8, and I'm using cygwin to compile my code. I'm trying to run command line commands using the system() command. This seems like it should be simple, but amazingly I couldn;t turn anything up on it. Below is my code:

#include <stdio.h>
#include <string.h>

int main ()
{
    char command[50];
    int error = 0;

    strcpy( command, "cd c:/");
    error = system(command);
    printf("%s\n", command);
    printf("%d\n", error);
    while(1)
    {
        ;
    }

    return(0);
} 

However, the above program just returns the variable error as "127" and the variable command as "cd c:/". Some google searching on exit codes showed that this meant that the "127" means the command was not found. I'm totally lost. I've searched for a while, but I can only find questions on this issue relating to C#. How do I run command line commands from a c program?

EDIT: I tried running the program from the cygwin command line and it runs fine. It just runs incorrectly from the normal cmd.exe and when I double-click on the .exe file.

Ranite answered 14/9, 2014 at 6:47 Comment(2)
possible duplicate of changing the directory from inside a c program under windows using system commandLocalism
Thanks for the link! However, it still doesn't address why none of the commands seem to be working.Ranite
A
6

Under Windows 'cd' is a built-in command in the shell. You need to run a shell to execute it. Please check this answer too.

EDIT: You can use the system command to run a shell like this:

system("<fullpath to cmd.exe>\\cmd.exe /c \"your command here\"");

It can get quite tricky with the escaped quotes unless you run a single executable after /c. If your executable, or internal shell command needs parameters you may need to double and triple escape the quotes. For some special characters like | && (pipes) > (redirection) you'll need to use the special windows ^ escape character Microsoft added for this purpose.

Here is an example of such a command. This one reboots Windows after a small delay:

system("cmd.exe /c \"start /b shutdown -t 1 -r\"");

Here is a more complicated one with special escapes:

system("cmd.exe /c \"ipconfig ^| find \"IPv4\" ^> C:\Users\someUser\a.txt ^&^& for /f \"tokens=13 delims=: \" %%i in (C:\Users\someUser\a.txt) do echo %%i ^> C:\Users\someUser\ip.txt ^&^& del C:\Users\someUser\a.txt\"");

This last one gets the ip of the machine using only simple commands and saves to ip.txt in a roundabout way under the home directory of a user called 'someUser'. Notice the ^ escapes.

Anthony answered 14/9, 2014 at 6:58 Comment(6)
That makes sense. The link doesn't really answer how to accomplish my goal though. I WIll go search some more on how to open a shell in c. Thank you!Ranite
Thank you so much for the help, but it's still not working. I can run it from a command prompt and it will work fine, but I want to be able to run it as an .exe file. Is there a specific library I have to include?Ranite
I compiled the above code using "\"c:/Windows/System32/cmd.exe\"" in place of "cd c:/" in my original code. If I run the .exe from the cywin command prompt, it compiles and runs fine. However, if I run it from the regular cmd.exe or just by double clicking on the .exe, it will not open cmd.exe. Why is this?Ranite
That is, running the .exe from cygwin opens the command prompt in cygwin. However, running the .exe from the command prompt prints the "127" error message and freezes up the command prompt (I can;t even escape with ctrl+c). Running the .exe just prints the "127" error and waits for me to close it.Ranite
I dont use cygwin so much for builds, but I assume that without the cygwin shell and environment settings some library needed is not foundAnthony
I ended up just using the Unity C# editor to compile and run. It's for game development anyway, so using the Unity engine is probably better in the long run. Unity works fine for this. Thank you so much for the help!Ranite
H
2

In Kali, system needs stdlib library too to execute. For people looking for full code to copy paste:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ()
{
    char command[50];
    int error = 0;

    strcpy(command, "cd c:/");
    error = system(command);
    printf("%s\n", command);
    printf("%d\n", error);
    return(0);
} 
Hern answered 24/12, 2021 at 5:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.