A shell-like application in C on Ubuntu [closed] - c

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.

Related

How to create a program in C that compiles and runs another c program and saves the output in a txt file? [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 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.

how to write unix "time" like utility

I am new to unix and learning to write some c programs that we can execute using gcc compiler in ubuntu. question:I need to write something similar to this: "time ls" where time should be replaced by my program. I know how to write c program for this, however, I cannot understand how unix will figure out what to execute if I replace time with my utility lets say "mytime" for instance? Some background for this will really help
Read some good Linux programming book, perhaps ALP - a bit old, but freely downloadable.
Read also intro(2) & syscalls(2).
For time related stuff, start with time(7). It explains that there are several notions of time. Then consider time(2), gettimeofday(2), getrusage(2), clock_gettime(2), times(2), localtime(3), strftime(3) etc...
Notice also that time(1) is either a builtin command of your shell, or an external one in /usr/bin/time. So it is some free software, whose source code you could download and study.
I cannot understand how unix will figure out what to execute
Be aware of the PATH variable (see also environ(7)), used by shells and in execvp(3). You could set your PATH to suit your needs. You might also be interested by strace(1) to understand what system calls a command or a process is doing. Notice that shells are ordinary programs, and you can write your own one (and that is a very useful exercise). Most shells are free software whose source code you can study. sash is a very simple shell...

How to search for command line arguments in C? [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 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.

C executable returns immediately [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 6 years ago.
Improve this question
I installed the GCC compiler to write some C code, but when I navigate to the directory, and use the command gcc -o helloworld helloworld.c It makes an executable on my desktop like normal, but when I run it, the executable closes immediately
I don't think that the code is the problem, but it's a possibility.
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
The problem is that Windows has poor support for running non-GUI programs.
A common way to run a program under Windows is to double-click the executable from an explorer window. For a program like yours that just prints to standard output, this will open a new window for the program's output, the program will run and quickly finish, and Windows will immediately close the window, perhaps before you have a chance to see it.
A common workaround is to add something to the end of your program, such as a call to getchar(), to cause the program to wait for input.
Another solution is to run the program from a command prompt. Its output will then appear in the current window rather than in a temporary one, and you'll see the program's output followed by a new prompt. If you run it that way, and added getchar() is unnecessary, and will make the program wait for input before terminating.
The Windows OS emphasizes GUI programs rather than programs that use plain text input and output. C was developed in a different kind of environment (though of course implementations of C for Windows support graphical operations).
You have missed this line getchar() in your code.
#include <stdio.h>
int main()
{
printf("Hello world\n");
getchar();
return 0;
}
Note: Though, this is not the fix as #Keith Thompson explains in the other answer. Instead, this is a way where you can force the program from exiting until it waits for a keypress before the console window exits.
Another way (without using getchar())
Open the Command Prompt (cmd.exe), and navigate to the program's directory and run your program from there. You'll find that the window doesn't disappears anymore, rather it stays open.

Create a program from another program? [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 9 years ago.
Improve this question
I'm making an application that will create a working executable based on what the end-user inputs into the program.
For example:
if (make_annoying_sounds == true)
{
//Generates an executable that makes annoying beeping sounds
}
else
{
//Generates an executable that doesn't make annoying beeping sounds
}
Basically I want my program to generate/create another program. I've seen/used many programs that do this. I have searched all over the internet and can't find anything. All help is appreciated. (Create a program, from within my program).
Try using a basic system call to invoke a compiler after you've created the source file.
You can create the source file with just the utilities found in stdio.h
Security Note: The system function is known to be dangerous. When in doubt, call a function like exec to invoke the compiler. Although exec erases the currently running process, so you should use fork and then call exec if you want to keep doing stuff after the compilation has finished.
So you want to create a compiler? This question below contains a whole list of resources to help you get started.
Learning to write a compiler
You need to do the following:
Based on the user input, generate the code for the custom program.
Compile that code into an executable file.
Theorically, you could, depending on what the user inputs, make your C code generate C-code inside your if statements. However this would be quite difficult.
The best way I think is to make an independent C engine which will only implement functions that any of the generated program can execute (playing the sound given in parameter, for example). The program you are trying to code (not the engine, but the one with the if statements, let's call it the "master program") must generate code which implements the algorithm which will choose what function of the engine to call and when. This generated code should be written in a scripting language like lua, since in is easier to generate script code than C. Thus, the engine should be designed to be able to communicate with Lua scripts. When the user clicks on the final "generate program" button of the master program, the master program calls gcc to compile the engine and the Lua script to generate the program the user tried to create. This is long, but this is, I think, the right way to do it.

Resources