How to run shell commands in a C program [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am really new to advanced programming (at least this is advanced for me)
I want to learn how to run shell commands through C program on windows
I did search for it and I know it has got something to do with system() and exec() but I didn't get a definite answer.
To begin with,i would like to execute cd command and also md command
So if someone can break this down to really basic level,it will be much appreciated. Thank you
P.S. I succeeded in doing so and I know now one shouldn't run system commands through C but this was just an assignment.Thank you

Here's a short program that runs dir from inside a C program.
#include <stdlib.h>
int main() {
system("dir");
return 0;
}
Basically, whatever command you pass as a string inside the parameter for system() is run using the shell on your system. In your case, since you are working on Windows, it is equivalent to running the string inside your command prompt. This is equivalent to the "DOS Commands" you talk about. However, these are actually shell commands.
Note: In general, you do NOT want to be running system() since there are almost always a better way of doing things. Also, if your code is just basically what is above, then you're better off writing a batch file (i.e. a .bat file).

Related

.exe doesn't close properly in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am a beginner in C, I wrote a code to calculate the average of grades, it's fairly simple, but it won't close properly when I run it in the .exe. I'll attach a video showing how it works, first I run it from the code, then from the .exe.
Video
Whatever the IDE is you're using, it is using a runner.exe to wrap the execution of your program and show you that "program stopped, hit any key" sort of prompt.
That doesn't happen for a compiled program, and the console window is closed right away at your exit(-1) call.
If you want to see the final output of your program, run it via the command prompt.
You can use exit(0) instead of exit(-1).

C programming language,how to convert input string in code and run it [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Is there a way to transform what the user inputs in code?
like
if
char k[50];
printf("insert the code to insert");
scanf("%s", &k);
function =>
converts string and execute the string if the code is correct
for example
if the user insert "int a = 0;"
as input , the program run the input string?
What you're looking for is essentially something like the eval function that some languages has.
Nope, there is not a way to do that. At least not in a practical way. The reason is quite simple. In C, you need to compile the complete program before you run it. If there is even a single syntax error in you 10k+ lines of code, you will not get an executable binary from the compiler.
Python on the other hand can execute line by line. That's why you can execute Python code that has syntax errors. The program will not crash until you encounter the error.
Besides, you should always think twice before using something like this irregardless of the language. The risks for vulnerabilities are extremely high.
Contrary to some other languages, the C language doesn't have an eval function to do what you want. The only option is to write the source file into a file and then execute the compiler available on the host machine.

Getting started with coding unix commands [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have been learning c and data structures for quite some time now and I wanted to see whether I could apply what I have learnt. I searched a bit and found out that I could start with util linux but, before I could do so, I thought I'd check and perhaps dabble a bit with the code for basic unix commands like "cat". I was able to understand what a part of the code might have been trying to do, but I was not able to understand the entire code as a unit.
For example, in the "cat" code, a pointer to the output buffer and input buffer is declared and is appropriately used, which I could understand. What i could not understand, are parts of code like io_blksize (stat_buf) which has no description whatsoever, on what it does. Or how two pointers declared as pointers to the input and output buffers, actually correspond to the input and output buffers ?
So my question being, how do I approach these type of code, how can I understand something that has no description to what it does (in the example given above) and how can I make and implement changes in the code, so that I can see the changes when i run a command ?
(Would really appreciate references or topics I should start with, so that I can relate what I have learnt to how command code's can be modified. I also apologize if the question is to abstract.)
This is a bit of a subjective question so my answers will just be my opinion of course.
A good place to start when you run into something you don't recognise while reading source code is the manpages. Each function will generally have a manpage, e.g. man 2 read or man 3 printf. Beyond that, I feel perhaps you should get more of a foundation in Unix before attempting to read the straight source code, a good book is Advanced Programming in the Unix Environment. I've been working through it myself and am finding my Unix knowledge improving considerably.
Just my two cents.

Are Linux commands really C object files? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was reading "The C Programming Language" by Kernighan & Ritchie and came across some programs which mimic some Unix commands (also implemented in Linux) such as the cat command. The program took command line arguments just like the original cat command.
I am just curious to know whether they are the same thing or not.
Correct me if I'm wrong, any help would be appreciated.
In a command-line environment (such as, of course, Unix/Linux), a principle unit of abstraction is the command. A command has a well-defined interface: the command-line arguments it expects, the input it reads (if any), and the output it generates. You can reimplement a command any time you like, either using a different internal algorithm, or a different language, or just because you want to write your own version. Yes, cat was originally written in C, but we could rewrite it in C++, or Perl, or Python, or sh, or other languages. As long as our reimplementation meets the same interface requirements, we can accurately say that it "is" cat.

How to extract C code from base R and play with it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to try some C code from base R. I may need to modify the
code and compile it to understand how it works.
For example, I am trying to understand the function C_modelmatrix.
One thing can be done is that I directly modify the source code
src/library/stats/src/model.c where modelmatrix() is defined. But I
will need to compile the whole R source and load this newly modified
R, each time when I modify C_modelmatrix. This process is not
efficient.
Another way is to only extract the related C code and compile it as an
.so file. Then load the .so file with an existing R. But this approach
seems to be difficult. I'm yet to figure out an exact procedure on how
to do this.
Does anybody have any suggestions on what might be the best way to play with
the C code in base R?
I have my source directory (from svn) in ~/src/R-devel
~/src$ svn co https://svn.r-project.org/R/trunk R-devel
... (additional commands, e.g., R-devel/tools/rsync-recommended
I build in ~/bin/R-devel/
~/bin/R-devel$ CFLAGS="-g -O0" CXXFLAGS="-g -O0" ~/src/R-devel/configure
~/bin/R-devel$ make -j
I would make a change in ~/src/R-devel/src/library/stats/src/, then inside the directory ~/bin/R-devel/src/library/stats I can run
~/bin/R-devel/src/library/stats$ make
and only the stats package will be compiled / installed.
For exploring C code I actually prefer to use gdb to step through and interact with the code, with some hints available.

Resources