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 6 years ago.
Improve this question
I have compiled and run a C code (a lot of files) but I need to understand the physical meaning of the command line arguments.I run code like this
./testmt signal1 3 5 1 1
where
signal1 is the input file
How to search multiple .c files in order to find command line arguments(hopefully with commented lines)?
It is operating system specific. I guess you are on Linux or some other Posix system. Read first Advanced Linux Programming. Read also about globbing. Be aware that your Unix shell is expanding the arguments (and after expansion calling the execve(2) system call....). If you shell is bash (and actually that behavior is mandated by POSIX) read about Shell Operation then about Shell expansions. Read also the Program Arguments chapter of libc documentation. See also my answer on "good habits for designing command line arguments?".
The main function (of signature int main(int argc, char**argv);, and such a [possible] signature is defined by the C standards) - of the program started by execve - gets the positive argument count and a NULL terminated array of strings. So in your case (./testmt signal1 3 5 1 1) you get argc=6 and argv[0] is "./testmt", argv[1] is "signal", argv[2] is "3", etc... argv[5] is the last "1" and argv[6] is NULL.
So dive into your code, and look for main.
PS. AFAIK, on Windows the behavior is probably different. The program (not the shell) is expanding the arguments (actually, probably done in startup files, before they call main). But I know nothing about Windows. See also Operating Systems: Three easy pieces.
Related
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 3 years ago.
Improve this question
I'm working on Ubuntu 16.04 and I need to use those functions
int spawnv( mode, path, argv );
int spawnve( mode, path, argv, envp );
int spawnvp( mode, file, argv );
int spawnvpe( mode, file, argv, envp );
I know that they are compiler dependent, so how to find system calls supported by my compiler/system? or how to find my multi-tasking api for processes system calls?
I tried using man spawn + clicking on tab but nothing appears.
The Wikipedia page on Spawn (computing) indicates that the spawn*() functions you reference are from DOS/Windows. They don't have direct analogues in Unix — although they were originally derived from Unix (fork() and exec*()) and adapted to DOS/Windows.
There are no direct analogues to those functions in POSIX. Arguably the nearest approach is posix_spawn() and its multitude of support functions (see the 'SEE ALSO' section on that page for links to the other functions).
I didn't find any similar functions in Linux, even when looking at:
https://man7.org/linux/man-pages/man3
https://man7.org/linux/man-pages/man2
https://linux.die.net/man/2
https://linux.die.net/man/3
Similar functionality can probably be written using fork() (sometimes), exec*() and waitpid() or one of its relatives (sometimes), but it might not be as easy as all that. It depends in part on how exact and complete the emulation functionality has to be.
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 5 years ago.
Improve this question
I want to create a program in C such that it compiles another program of c and saves the output in a text file. For example, I want the output of my "input.c" file to be stored in a text file named "output.txt", using a C program. Please help.
I have chosen this project because it often becomes difficult to copy the entire output screen in Turbo C++, and sometimes turbo c doesn't show the entire output and only the current output screen is copied, leaving behind the previous output.
It's possible. You either run a shell command that compiles and runs your subprogram (man 3 system, easy), or you go with more advanced techniques such as on-the-fly compilation (http://blog.coldflake.com/posts/On-the-fly-C++/ or https://bellard.org/tcc/ if you're a C programmer)
You want to look at the Standard C functions named system() and POSIX popen().
On a POSIXly system with a C compiler, to compile a simple C program would be
system("cc -o input input.c");
And to run it and capture the output,
FILE *fp_in = popen("./input", "r"); /* to read */
FILE *fp_out = fopen("output.txt", "w"); /* to write */
Then read from fp_in and write to fp_out. That's the basic idea. I've left the details for you to figure out, so you gain deep insight into deep C secrets :-)
Don't forget the error handling for all library functions.
PS: If the system's shell supports redirection, you might even simplify the popen/fopen combo to a single system("./input > output.txt");
PPS: If the PS works, you might as well combine everything into system("cc -o input input.c && ./input > output.txt"); I trust you know how to wrap this in main().
The C11 standard (read n1570) does not define how to run a compiler. In many cases (think about cross-compiling for some Arduino target) you won't be able to run a compiler on the target machine (it could be "too small").
BTW, you could compile your code into some executable, remove every compiler from your system, and run that executable (in a situation where your system don't have any compiler)....
The C11 standard vaguely speaks in §7.22.4.8 of a system function.
On some implementations, that system function is able to start other programs (in processes) thru some unspecified command processor. But there is no guarantee that you'll be able to start a compiler (for example, you could run your executable on a Windows computer without any compiler).
(In practice, your computer is likely to practically have some command line C compiler, but you need to know which one and how to invoke it)
On POSIX, you could use system(3) (which uses /bin/sh -c), but also fork(2) & execve(2) -and other functions, e.g. popen(3)- to start other programs.
On Linux, you usually have some command line compiler, often GCC as gcc (or even cc). You could run it (e.g. using system).
I like to do the following trick on my Linux system: generate some temporary file containing C code, e.g. /tmp/temporaryc.c, then compile that temporary file into some temporary plugin /tmp/temporaryplugin.so by using system with a command (in a string built at runtime) like gcc -Wall -O -fPIC -shared /tmp/temporaryc.c -o /tmp/temporaryplugin.so, and at last dynamically load that plugin using dlopen(3).
Look also into JIT-compilation libraries like libgccjit.
BTW, you should consider giving up Turbo C (it is an obsolete compiler for obsolete variants of C or C++) and switch to Linux on your PC.
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 7 years ago.
Improve this question
Ok. So I'm trying to write a shell like application to run in the Ubuntu terminal but I don't know where to start. I need to write a C program in a text editor, run the program in the existing terminal (ioana#ioana-VirtualBox:~/Desktop$ gcc OS.c -o OS.c and than ./OS).
My C program should reset the terminal and let me remake and/or somehow import the basic function from the original shall(I should write an exit function that will close the terminal, but it should not be mistaken with the exit from the original shall of the terminal; make the buffer for key UP-DOWN history that won't be mistaken with the one already implemented). I read a bit about ncurses library and also installed it.
All I have so far is this:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "ncurses.h"
int main()
{
system("reset");
return 0;
}
It's a perfect running code and it clears the screen but if I press UP or DOWN, the previous commends are displayed and if I type exit the terminal closes. I don't want any of these.
I'll be pretty grateful if someone can explain what I can do. I read about those subjects but didn't manage to find something that I can clearly understand.
Several issues and hints:
read first Advanced Linux Programming (a book, also freely available online) to learn more about how to use system calls (listed in syscalls(2)...). Use strace(1) to discover what system calls are done by some programs (so try strace date then strace -f bash -c 'date; pwd'...). Read about credentials(7).
fork(2) & execve(2) & waitpid(2) are the basics of every shell; see my hints here. But they are difficult to understand. If you want to have command pipelines, you'll also need pipe(2), dup2(2), close(2); for redirection, you'll also need open(2)
understand what globbing is. See glob(7) and the references there.
terminals are in fact ttys, demystified here. For historical reasons, they are complex. See termios(3)
history and command line editing and auto-completion are well handled with the GNU readline library; full screen console-like editing (à la emacs or vi) would need the ncurses library; but it is not the essential part of a shell.
job control can be tricky. Read about process groups. See getpgid(2) & setsid(2)
all Linux shells are free software, so please study their source code. The sash shell has few features (and some bugs!), but its source code is small and easy to read. GNU bash, zsh, fish have more features so are more complex.
The system(3) library function is forking itself a /bin/sh shell, so using it in your own shell is somehow cheating.
PS. If you are new to all that, making a full-featured shell would take several months.
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 9 years ago.
Improve this question
Kind of a stupid question but seeing if its possible.
With the f commands you have for example, "w" which would be write to file, create new file if it doesn't exist.
With the O commands its a bit more complex, my research shows its: O_WRONLY|O_TRUNC|O_CREAT
the o commands seems a bit too much to remember oppose to the f command. Is it "good coding" and possible to use W as the flag in an open function
The fopen function is defined in the ISO C standards (C99 7.19.5.3 for example). It is implemented in the C runtime library. As such all compliant C implementations, across all platforms, have to implement it.
The open function is defined in POSIX. It is specific to unix-like platforms.
When you call fopen on a unix-like, POSIX compliant platform, the runtime library inspects the arguments you are supplying and translates them into the corresponding arguments to the POSIX open function.
If you called fopen on another system, such as Windows, the runtime library would be calling a windows specific function to open the file - perhaps OpenFile.
You can't just supply the fopen arguments directly to POSIX open, or to Windows OpenFile for that matter. Those functions don't understand them.
In terms of "good programming" and which of these layers to use: in general, you should avoid directly calling OS system calls (such as the POSIX ones or the windows ones) unless you specifically need to for some reason. The reason is that your program will be more portable if you don't. It will be possible to compile it for any platform which has a compliant C compiler.
On the other hand, if you need some capability or option which is not available in the C Runtime libraries, then you should use the OS system calls.
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 9 years ago.
Improve this question
On windows, the programmer could do something like: system("ls > outputFile.txt")
Is there a platform independent way to access the command line, or a least a way to determine which platform the program is being executed on (because calls for the same functionality vary quite a bit)?
The system(3) function is standard ANSI C, it's already platform-independent. Any conforming C implementation will allow you to call it to run the system default command line processor/shell application. Of course, the actual programs you can run will vary from system to system (e.g. dir only works on Windows, while ls usually works on Unix-like platforms).
system() itself is a standard C function defined in stdlib.h. The way it interprets its argument, though, is not standard (e.g. ls in UNIX, dir in Windows/DOS, etc.). If you're really asking whether there's a platform-independent way to list the files in a directory, the answer is (unfortunately) no. Some libraries do provide portable (to some degree) implementations, most notably Boost: How can I get the list of files in a directory using C or C++?