Executing windows commands in C - c

I have a batch file which changes the direction to a specific toolchain and executes one command like this:
cd C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin
avr-objcopy -O binary C:\Users\cinar\Desktop\hextobin\GccApplication.elf C:\Users\cinar\Desktop\hextobin\GccApplication.bin
I want to do this with my C application. I found this topic, tried the system(); command and it works partially. I can call this:
system("cd");
and get the direction back. But I can not change it with this command:
system("cd C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin");
This caused a compile warning about unknown escapes, so i added \ to escapes and tried this:
system("cd C:\\Program Files (x86)\\Atmel\\Studio\\7.0\\toolchain\\avr8\\avr8-gnu-toolchain\\bin");
I was able to compile and run this but that didn't change the direction.
Is there any possibility to execute my commands with system()? As I just want to change the direction and execute one command, I wanted to keep it simple.
Update: I found this topic afterwards:
system("cd <path>") in a C program
Then solved my query with this:
chdir("C:\\Program Files (x86)\\Atmel\\Studio\\7.0\\toolchain\\avr8\\avr8-gnu-toolchain\\bin");
system("avr-objcopy -O binary C:\\Users\\cinar\\Desktop\\ff.elf C:\\Users\\cinar\\Desktop\\ff.bin");

Your program has some incorrect assumptions. First of all, "cd" and "dir" are not programs, but commands built into the shell, cmd.exe. Second, I suspect you don't need to change the current directory at all.
Either way, since this is a Windows system, I would look at an example on how to start a program with CreateProcess().
For changing the current directory, check out the lpCurrentDirectory parameter of the CreateProcess() call.
Also
system("dir Users\\whatEverNextFolder > test.txt");

Related

Exact same command line produces error in Make yet succeeds if run from shell

I have a problem that I just can't wrap my head around. I have a minimal example makefile that is supposed to compile a very simple .c file into an executable program.
When I run make, the compiler starts compiling and then produces an error message
"T:\printOffsets.c:10:21: error: bootIfc.h: No such file or
directory"
Then I copy the exact same command line make is using to build the target and run it directly in the same Windows command shell instance, and suddenly compilation succeeds without errors!! The command line is (path names simplified):
T:\perl\c\bin\gcc.exe T:\printOffsets.c -IT:\include\ -o
D:\printOffsets.exe
How do I know? Well, make prints the command line before it executes it, so I simply copy&paste from the shell.
I don't get it! How is this possible?? How can the exact same command work on the shell and fail if launched from within a Makefile??
I'm using GNU Make 3.82 on Windows 7, by the way.
When command in makefile is giving different result from shell, just make sure it is using the shell you want.
Add a phony target in your make file:
.PHONY:testshell
testshell:
echo $(SHELL)
And run:
gmake testshell
If the result is not your favorite shell you can force it by adding a line such as this one at the beginning of your makefile:
SHELL=C:\Windows\System32\cmd.exe
If you are not sure of full path of your shell, just open a DOS console and launch:
where cmd
Edit: alternative solution
When using sh shell instead of cmd shell, you can also replaces all backslashes in commands with slashes and keep using sh.
Edit 2: change shell for a single target

Run Unix Commands to DOS equivalent in C Language

In Operating System concept i want to write a C program to make Unix command works as DOS commands.
It Means when ever i press Unix Command like ls -which is used for Display list of Files- it works like Dir command in DOS.Could you please help me out with this?
Starting with the ls command as example, take input from user for the command. If the command is ls call a windows function that will display the content of the current working directory(). For ls,
you need to get first the current working directory. GetCurrentDir() for windows will be your first step. This will help How do I get the directory that a program is running from?
Then you can list the files in directory like this https://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx. But you do need the output of first step.
Other commands can also be implemented like this

Can't find how to select path to run a C program

I am using mac OS x and have written C program using GCC compiler.
But while running the program on terminal I am being shown that "No such file or directory found"
Please help me how to select the path?
run it with $./yourProgramFile command the ./ in the beginning is important. It means the program resides in the current directory.
Example:
/path/to/your/cFile $ gcc myfile.c -o myfile
/path/to/your/cFile $ ./myfile
You can do one of two things
1) Add the program's folder to the system's PATH; that way, you can call the program from any location. If it's a program you plan to use constantly, this is the best option.
Here's a way to do that:
Open up the .profile file in your home directory using any text editor.
Paste the following code anywhere in the file, preferably around the bottom of the file.
#make sure there's no space in the pasted code
export PATH=$PATH:path_to_the_program
Save it and restart your computer. That should put the program in the system's PATH.
2) Navigate to the folder of the program; then type
./program_name
Hope the explanation is clear and the answer helps.
Make sure that when you compile the program, you should use -o program_name to make the program name whatever you want it to be; otherwise, the program's name will be a.out, which would be very confusing.

How to execute a program in linux after compiling it?

I am compiling a C code in linux with the following command:
gcc -o myprogram myprogram.c
If I hadn't given a name to it, I could have simply written the command ./a.out to execute it. But now, to execute the program I just write "myprogram" to the command line, but it says "command not found". What can I do to execute it?
It's possible that the current directory (".") isn't on your PATH. (You can check this by typing echo $PATH, this is a list of directories delimited with" :". "." should be in the list if you want to run something in the current directory.)
If the current directory isn't on your PATH, you'll need to type ./myprogram (or whatever the correct path is).
./myprogram
should do the trick.
(But really... have you looked at the contents of the directory after compiling the program "without name"? Or do you think ./a.out is a magic sequence Bash recognizes?)

GNU style configure batch file issues

I'm trying to create a GNU like configure script for Windows in a batch file. It seems to work ok so far except depending on the order options get ignored and in particular with the '--with-smtube' option it sometimes gets the path but again depending on the order causes it to result in:
test.cmd --enable-portable --with-smtube=C:\svn\smtube
configure: error: unrecognized option: `C:\svn\smtube'
Try `--with-smtube --help' for more information
(supposed to also say Try `test.cmd --help)
I copied the structure from another script I found but not having success. The order of arguments shouldn't matter and arguments can be omitted (all or some). Can someone steer me in the right direction?
The script is for a Qt program, it uses command prompt as the shell and not msys or cygwin or anything like that.
This is what I have so far: http://redxii.users.sourceforge.net/test.cmd

Resources