Programmatically Recursively Make Directory - c

I want my program to create a file located at the path of the command parameter, i.e. program /home/user/directory/that/doesnt/exist/file. If the directory doesn't exist, fopen will fail to open the file, so the program would have to create the directories. Would I have to program my own loop to detect each slash and mkdir each directory or is there a function to do this automatically?

Someone coded it for you: mkpath()
Or if you're satisfied using system(), then you can use system("mkdir -p /your/path/here");

Related

why my Xcode doesn't find the .txt files that I create?

my program seems to "see" only the .txt files which he creates with the fopen("file.txt","w") function. I changed the working directory and put added the file to the project, but if I create the file.txt the program can't see it. the only way is to edit the one he creates with the open function
You just have a problem concerning the current directory, it is not what you expect.
When you create "file.txt" it is created in the current directory because the path is not specified, so you can open it after because it is where the program look at by default.
{edit add}
You are under MacOS probably, if you start the program by hand from a shell like /Applications/......./prog the current directory is the current directory where you are in the shell, but if you start it through its icon etc it will depends on the installation directory

How to make my Linux C program accessible from bash

Say I made and compiled a small program in C to count the bytes of a file, called filebyte. To run it I would use ./filebyte
Now I want to make it universal on bash, like for example to run a php file, I would use bash command php file.php, same way I would like to run my program, filebyte filename.
How do I do this?
Thanks!
I often create a bin/ directory in my home directory, for small custom applications.
You then need to add that directory to your PATH, which is a list of colon-separated paths that your shell searches for executables when you type a name on thr command line.
This is usually accomplished by putting this in your ~/.bashrc file:
PATH="$PATH:~/bin"
Check the environment variable PATH and put the executable in one of the directories listed. You can also put it in a custom directory and then append it to PATH. You can check it by executing printenv PATH
If you want it for your current active shell alone, do
export PATH=$PATH:</path/to/file>
For permanently making the file available add the above line to ~/.bashrc
Why add it in PATH variable, man bash says why,
PATH The search path for commands. It is a colon-separated list of
directories in which the shell looks for commands (see COMMAND
EXECUTION below). A zero-length (null) directory name in the
value of PATH indicates the current directory. A null directory
name may appear as two adjacent colons, or as an initial or
trailing colon. The default path is system-dependent, and is set
by the administrator who installs bash. A common value is
''/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin''.

Set paths in C program for executing external programs

I wrote my program in C which includes calling a bash script and another external program from it (all of them located in the same directory as my C program). I set the path of the executing files strict so like:
char *path_for_the_script = "location/of/script.sh";
and compile my program.c without any special arguments..
Now I wonder how can I manage that this path gets set by the program, so that someone else could use it from his computer without changing the paths manually?
Load the path from a environment variable (also add a fallback or failure path if the variable is not set) and have your program being launched through a wrapper script, which the user can adjust.
Example
in yourprogram.c
char const * const path_for_the_script = gentenv("YOURPROGRAM_SCRIPT_PATH");
the programlauncher.sh
#!/bin/sh
export YOURPROGRAM_SCRIPT_PATH="..."
exec yourprogram $#
Instead of setting the path in your code, create a text file in the same directory and fetch the path information from the txt file. Thus anyone can modify the text file with required path and execute the program, with out any code modification.
create a file in same directory as source.txt
vi source.txt
location/of/script.sh
In your program, use file open operation and access the file contents and assign to path_for_the_script char buffer. This method works only when all the users share the same computer.

How do I run an executable in a different directory in C?

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.

Making a executable into a shell command using a C program

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

Resources