Cygwin- Trying to run DOS command in C language - c

I have tried both system() and popen(). When I run the program on cygwin terminal it works perfectly fine but when I try to run it on windows platform by double clicking on .exe file I get this error:
exception::handle: Exception: STATUS_ACCESS_VIOLATION
I get this error on popen() however I do not get any error for system().
I am running simple commands that work on both unix and windows OSs such as:
system("echo foo>foo.txt");
What am I doing wrong here?

I think that's because both system() and popen() pass their arguments to the /bin/sh which you don't have in the path, you could try adding it to the path first. According to this you need the following in your path:
cyggcc_s-1.dll
cygiconv-2.dll
cygintl-8.dll
cygncursesw-10.dll
cygreadline7.dll
cygwin1.dll
ls.exe
sh.exe

Related

Daemon on embedded Linux device using Busybox be written in C or as a script

Should a daemon on an embedded device using Busybox be written in C or as a script?
All the examples I have seen use #! /bin/ash at the top of the file and that is for scripting? But in the device I'm writing to has only complied C files (I think) and symbolic links in /usr/bin.
Every way I try to compile a C file with #include </bin/ash> (e.g. gcc -Wall -o daemon_busybox daemon_busybox.c) I get error after error report in /bin/ash:
/bin/ash:174:1: error: stray ‘\213’ in program
/bin/ash:174:1: error: stray ‘\10’ in program
/bin/ash:174:1: error: stray ‘\273’ in program
/bin/ash:174:1: error: stray ‘\204’ in program
/bin/ash:174:1: error: stray ‘\342’ in program
Note I have set this: /bin/ash -> busybox
Any ideas which way I should go?
Update:
I've been given the task trying to see if a daemon can be run on a small device that runs Linux (2.6.35-at-alpha4) and Java (SE Embedded Runtime Environment) with very limited memory (i.e. a 10 second wait to get java -version to report back).
Two weeks ago I didn't know much about daemons — only knew the word. So, this is all new to me.
On my development machine I have built two different daemon files, one in C and one as a script. Both run very nicely on my Linux machine.
But because of the very small size of the target device there is only busybox (no /lib/lsb/init-functions). So I'm trying to build a 3rd daemon file. I believe it should be written in C for this device, but all examples for busybox point to scripting.
Once your question is edited so that the file name you're trying to #include is visible, the problem becomes self-evident:
#include </bin/ash>
This tries to make the C compiler include the binary of busybox (via the symlink /bin/ash) into the code to be compiled. The average binary is not a valid C source file; this is doomed to failure.
Perhaps you simply need to drop that line — the C compiler stands a better chance of working if it is given header files and source files to compile. Maybe there's more work needed; we don't have enough information to help there.
Many daemons are written as C programs, but a carefully written shell script could be used instead.
Personally, I would like to do this as a script (I've never liked C). But on the device everything in the /usr/sbin folder looks like a C file. So, the conservative coder in me says C is the way to go. I know: ask the guys developed the device — but they're long gone. Right now my daemon is just a test (i.e. printf("Hello World\n"); ). I'm trying to get printf passed to Busybox. But so far I cannot get this file to compile. I just need a simple daemon in C to start.
OK; your C code for that should be just:
#include <stdio.h>
int main(void)
{
printf("Hello World\n");
return 0;
}
Save it in hw_daemon.c. Compile it using:
gcc -o hw_daemon hw_daemon.c
If that won't compile, then you've not got a workable C development environment for the target machine. If that will compile, you should be able to run it with:
./hw_daemon
and you should see the infamous 'Hello World' message appear.
If that does not work, then you can go with the script version instead, in a file hw_script.sh:
#!/bin/ash
printf "Hello World\n"
You should be able to run that with:
Predicted output — not output observed on a machine.
$ ash hw_script.sh
Hello World
$ chmod +x hw_script.sh
$ ./hw_script.sh
Hello World
$
If neither of those works at all, then you've got major problems on the system (maybe Busybox doesn't provide a printf command workalike, for example, and you need to use echo "Hello World" instead of the printf).

How to execute a C file through cmd

In my current example, I have a C file called 'Main.c' which converts and prints US pounds into UK stones/UK pounds/kilos, and the variable US pounds is currently defined by the user's input (scanf). The file itself is executed through Visual Express 2013; however, I want to change this so that I can navigate to the C file and execute the file directly through the commmand prompt (cmd) whiles passing a value to define US pounds.
I understand that this is required in main:
void main(int argc, char *argv[])
And I know how to navigate to the C file through the command prompt:
>cd Directory/To/The/File
However this is where I get stuck; I don't know how to execute the C file. I have researched into this and found several examples, such as using 'gcc' and 'cc', but the system doesn't recognise these commands. None of the materials that I have found fully explains what exactly I have to do in order to accomplish what I am trying to do; do I have to install something, or am I using the wrong commands, is it possible what I am trying to do, what excactly do I have to do?
Broadly speaking, you need to do the following:
Compile the program using a compiler to generate an executable.
Run the executable file.
When you "run the program in Visual Studio", it is doing all of those steps for you.
I don't know much about command line compiling in Windows, but I believe msbuild is the name of the compiler which Visual Studio uses. Look into that, and you'll see how to compile. Compilation will then generate the executable, which you just type into the command line to run.
EDIT: I found an article which suggests that cl is a C compiler command available on the command line.
Here is the relevant excerpt:
At the command prompt, specify the cl command together with the name of your source file—for example, cl simple.c—and press Enter to compile the program. The cl.exe compiler generates an .obj file that contains the compiled code, and then runs the linker to build an executable program that has the name of your source file, but has an .exe file name extension—for example, Simple.exe.
You cannot cd to the file, only to the directory. The cd command means "change directory".
You cannot execute a C file, it's just a text file and your computer doesn't know how to run it. You need to compile it first (this is what Visual Studio does). There should be an EXE file somewhere close, you need to find it. It is the binary, executable, form of your C program which you can run directly from the command line.

Send Linux command from C program

I'm writing a C program to run in Linux shell.
Now I got a problem with such command.
#include <stdio.h>
void main()
{
char* command="history>>history";
system(command);
}
I want it to write the result of command "history" into a document, but it failed with a blank one.
If I change it to "date>>history", current system time will be written into the document.
Is there any problem with "history>>history"?
What should I do if I want to get that work?
Thanks!
The problem is that history is not a real command but a shell builtin. Thus you can't call it from a C program[1].
Depending on the shell the user is using, you can instead get the history from ~/.bash_history, ~/.zsh_history and so on. Note however that zsh only write to this file at the end of a session.
[1] Well, you could theorically try system("bash -c history"), but you won't get the actual history because the builtin isn't run in the context of the current session.

Error:unresolved external symbol _popen

Why does using popen result in Error: Failed to close command stream under windows?
You actually have to use: _popen and _pclose (yes WITH the silly underscore) under Windows.
See MSDN Entry on it; it has a nice example
Now as for _popening "ps" you know whether or not you have a ps on your system.

compile program with set runtime environment?

I am trying to compile a program which, when run, will be run in a predefined environment in c.
More specifically, I am looking to somehow include LD_PRELOAD=./lib.so so that when I run ./program it will run as if I had used the command LD_PRELOAD=./lib.so ./program
Does anyone know if this is possible?
Make the program set LS_PRELOAD and then run itself if LS_PRELOAD isn't set. Use execv() for that.

Resources