In Operating System concept i want to write a C program to make Unix command works as DOS commands.
It Means when ever i press Unix Command like ls -which is used for Display list of Files- it works like Dir command in DOS.Could you please help me out with this?
Starting with the ls command as example, take input from user for the command. If the command is ls call a windows function that will display the content of the current working directory(). For ls,
you need to get first the current working directory. GetCurrentDir() for windows will be your first step. This will help How do I get the directory that a program is running from?
Then you can list the files in directory like this https://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx. But you do need the output of first step.
Other commands can also be implemented like this
Related
I have a batch file which changes the direction to a specific toolchain and executes one command like this:
cd C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin
avr-objcopy -O binary C:\Users\cinar\Desktop\hextobin\GccApplication.elf C:\Users\cinar\Desktop\hextobin\GccApplication.bin
I want to do this with my C application. I found this topic, tried the system(); command and it works partially. I can call this:
system("cd");
and get the direction back. But I can not change it with this command:
system("cd C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin");
This caused a compile warning about unknown escapes, so i added \ to escapes and tried this:
system("cd C:\\Program Files (x86)\\Atmel\\Studio\\7.0\\toolchain\\avr8\\avr8-gnu-toolchain\\bin");
I was able to compile and run this but that didn't change the direction.
Is there any possibility to execute my commands with system()? As I just want to change the direction and execute one command, I wanted to keep it simple.
Update: I found this topic afterwards:
system("cd <path>") in a C program
Then solved my query with this:
chdir("C:\\Program Files (x86)\\Atmel\\Studio\\7.0\\toolchain\\avr8\\avr8-gnu-toolchain\\bin");
system("avr-objcopy -O binary C:\\Users\\cinar\\Desktop\\ff.elf C:\\Users\\cinar\\Desktop\\ff.bin");
Your program has some incorrect assumptions. First of all, "cd" and "dir" are not programs, but commands built into the shell, cmd.exe. Second, I suspect you don't need to change the current directory at all.
Either way, since this is a Windows system, I would look at an example on how to start a program with CreateProcess().
For changing the current directory, check out the lpCurrentDirectory parameter of the CreateProcess() call.
Also
system("dir Users\\whatEverNextFolder > test.txt");
I want to run an executable found in ../folder1/folder2 while inside of a C script. Right now I'm trying to do:
char command[50];
strcpy(command, "cd ../folder1/folder2");
system(command);
memset(command,0,sizeof(command));
strcpy(command, "./executable_name");
system(command);
but it's not working. Should I be using chdir(), or is there another way to do this? Is it even possible?
It's not working because when you execute:
system("cd ../folder1/folder2");
it does not have an effect on the current directory of the executable. Hence, when you execute:
system("./executable_name");
it doesn't find it.
You can solve it using any of the following methods:
Change the command given to system:
system("../folder1/folder2/executable_name");
Change the command given to system:
This is going to work if your default shell is bash, and many of the UNIX shells.
system("cd ../folder1/folder2; ./executable_name");
Create a shell script that has:
#/bin/bash
cd ../folder1/folder2
./executable_name
and then run the shell script from C using system
system("myscript.sh");
Add a line to chdir in C before you call system:
chdir("../folder1/folder2");
system("./executable_name");
Update, Thanks due to #Jongware
All of the above assume that your program is executed in a directory from where ../folder1/folder2 is a valid path. If your program is executed from a different directory and you want to account for that scenario, you have to parse argv[0] and adjust the way you handle the calls to execute executable_name.
The simplest way is normally:
system("../folder1/folder2/executable_name");
Failing that, you have to either do the cd via chdir() in your program, or arrange to do the cd and execute the command in a single shell (single call to system()), as in:
system("cd ../folder1/folder2; ./executable_name");
The problem is that system, does a fork, executes the cd in a sub-process, which ends and does not effect the working directory of the program process. Shells, need to make cd & dirs builtins for them to have an effect.
Rather than use a shell command, you can use getwd and chdir to change directory then system with "." as you tried, add a "cd ../folder1/folder2; ./executable_name", use fork and change directory and run the command in the child with exec yourself, or alternatively just run the program using the relative path "../folder1/folder2/executable_name".
#include <unistd.h>
int chdir(const char *path);
char *getcwd(char *buf, size_t size);
You need to execute a script that changes the directory then runs the application.
You can simply execute a program using a relative or absolute path. If you actually want to execute it in another working directory, read on.
Currently your program does not change its working directory before executing the child program.
Simply explained, system starts a new program which changes its working directory and then exits, so your program is not affected.
What you tried works in actual scripts because the interpreter does not spawn a new shell for each command you give it, but executes them itself.
Use chdir() or some other API for changing the directory of the currently executing program.
I want to make a launcher for a c program I made and I want it to run in terminal. How would I do this? I don't even have the slightest idea
Just create a text file, save it to the Desktop as e.g. my_C_program.command (note the .command suffix), then in the text file you can put whatever terminal commands you like, e.g.
# run my_C_program
my_C_program arg1 arg2
Note: after saving the .command file you need to make sure it is executable:
$ chmod +x ~/Desktop/my_C_program.command
This will give you a double-clickable icon on the desktop called my_C_program that runs my_C_program via the command line. (This assumes my_C_program is somewhere in your PATH).
Double-click your programm
You will asked for a programm to open your programm (sounds strage, I know)
Search for "Terminal" & Choose it
That's it! Now your programm should open when you double-click it.
I've a executable named say "sortx". Now I want to write a C program which transforms this executable into a shell command.
ex:
./sortx numbers.txt
After running the C program on "sortx" what I want is :
sortx numbers.txt
Add the directory in which sortx is present to $PATH. This way you could execute your program locally, like,
sortx numbers.txt
To add directory ~/my_bin to the beginning of the $PATH environment variable, add or update this in your .bash_profile:
PATH=~/my_bin:$PATH
On Linux to make any script or program globally executable (e.g "sortx" rather than "./sortx") you can put the script in wither /usr/bin or /bin -- I prefer /usr/bin :)
I/O Redirection
Advanced Shell Topics: stdin, stdout and redirection
I am programming a small unix shell written in c. I want it to do only some basic commands for now. e.g. ls, pwd, ch
My problem is how do I set the Home directory and Path directory? I want to read the configuration from a text file so it can be easily changed whenever.
I am going to be using execv() to call unix functions such as ls. For example PATH
should determine the directories my shell should use to search for executable programs
when the user types a command
Thanks
They are all simply environment variables that you manipulate e. g. through setenv(3) (run man 3 setenv for details). The variables are HOME and PATH. See also man 7 environ.
Note that setting/changing an environment variable only influences the current process and all processes forked from it after the setting/changing (unlike in Windows, AFAIK).
Check out the function setenv. See man 3 setenv for information about it.
Unix already offers you an environmental variable that contains all the paths where system executables are stored. Retrieve the variable in your code with getenv("PATH"); Each path is separated with a ':' so all you need to do is tokenize and begin searching those paths for the executable your command wants to run. In this function you should also be able to search any path of your choosing for an executable.
You can decide what directory you want to start in ("home directory" as you say) by manipulating the current working directory before the shell prompts with chdir(). You can also use that unix function for implementing a cd command that can be used throughout the run time of the shell.