execlp system call in C - c

Hi have this line of code in a C program that I must understand as a whole, I understand everything except for the line below. I have been looking into the documentation at it seems to me that it is just as if user introduced the ls command in Ubuntu. However I am not sure about it and I do not know why there is a second ls, the -l option and the NULL. Any help is very much appreciated!
execlp ("ls","ls","-l", NULL);

Related

using GDB with arguments

For a class assignment we needed to write a compiler. This includes an optimizer portion. In other words, we take in a file with some "code". An output file is generated. In the second step we take in the outputted code and remove any "dead" code and re-output to a second file. I have some problems with the optimizer portion and would like to use gdb. But I can't get gdb to operate properly with the input and output files arguments. The way we would normally run the optimizer is:
./optimize <tinyL.out> optimized.out
where tinyL.out is the file outputted in the first step and optimized.out is the file I want to output with the new optimized and compiled code.
I have searched Google for the solution and the tips I have found do not seem to work for my situation. Most people seem to want to only accept an input file and not output a separate file as I need to do.
Any help is appreciated (of course)
I'm not exactly sure what you're asking. But since I'm not yet able to comment everywhere, I write this answer with a guess and edit/delete if necessary.
When GDB is started and before you start the program you wish to debug, set the arguments you want to use with set args.
A reference to the documentation.
You just need to do the file redirection within gdb.
gdb ./optimize
(gdb) run < tinyL.out > optimized.out
https://stackoverflow.com/a/2388594/5657035

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.

how to remove a file with using execl function in UBUNTU

hi there i have to remove a file at end of a C programme but i couldnt make it. i tried to use
execl("/usr/bin/rm","rm","example.txt",NULL);
but it isnt working. i will be appreciate if you can help and thanks anyway.
As other people pointed out, there are more efficient ways to remove a file.
If you want to know why your program is failing, just run it under strace:
strace your-binary
You'll see all the system calls that your program does, with the corresponding return values. In this case, I strongly suspect that rm is not in /usr/bin, but in /bin. You can check it by executing which rm.
why not try remove(const char *filename) function in stdio.h?

I need some tips on implementing the chroot command in C

I am tasked with writing a simple shell along with these three commands: "nl", "head", "chroot"(with no parameters). I've written nl.c and head.c but I don't know where to start with the chroot.c. I've read what chroot does, googled some documentation and to me as a beginner this is complicated.
Any advice on this matter?
chroot without an argument just prints an error message. You can use printf for that.
Otherwise, chroot calls chroot, chdir("/") and then executes a shell with one of the exec* functions.

Beginner Doing K&R

I'm just starting programming and going through K&R to try and learn C. I've gotten to the section on command line arguments (5.10) but now I'm stumped. Every time I try and open a program I've written with command line arguments I'm told that file X, X being the argument, doesn't exist.
`gcc -o find find.c
open find test
The file /Documents/Learning_C/test does not exist.`
Any suggestions? Thanks
What system are you on? In Unix/Linux you compile & run your executable via:
gcc -o find find.c
./find test
As others have noted, when you prefix your binary with "./", there wont be any naming conflicts. However, if you made find accessible in your $PATH, you might have some conflicts with find and test--standard programs with most *nix distributions... Maybe you could choose more specific names (i.e. ./myFind testArg)
Try giving your output executable a different name.
I suspect your executing the system find command which is looking for a directory called 'test'.
Or try forcing it by executing
./find toto
Edit: Prepending the ./ to the command is important because it tells the shell to execute the find in the current directory as opposed to the first 'find' that exists in your PATH. It is normally recommended that you don't have . (the current directory) in your PATH for security reasons.
HTH
P.S. Forgot to say good one for working through K&R. I just finished doing the same after working in C for thirty years and it was good to get back and refresh the mind!
Instead of making us all individually guess what exactly you're doing wrong, perhaps you should paste the program you're using for the illustration mentioned ?

Resources