C write file as executable ASCII - c

I'm making some program with a server sending a shell script to a client. the client gets the code and puts it in a file, and later exec it.
The adding function :
void addScript(char *name,char *content){
int fd;
char path[BUFSIZ];
strcpy(path,"scripts/");
strcat(path,name);
strcat(path,".sh");
printf("file : %s\n",path);
fd = open(path,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP);
if(fd == -1){
printf("Error openning file !!");
return;
}
write(fd,content,strlen(content));
write(fd,"\n",1);
close(fd);
}
I have the file, but when I try to run it exec gives me an error :
Exec failed : Exec format error
I ran file command on the script, it gives me :
mount.sh: Non-ISO extended-ASCII text
Whereas on a hand-written script (working this one) I have :
diskUse.sh: POSIX shell script, ASCII text executable
I can't find how to force file type when I create it with my function, would anyone have an idea ? Thanks !
Edit some more details :
Example of a non-working file :
#!/bin/sh
mount
"Funny" thing is that it works well if I run it by hand from my shell (zsh) with ./mount.sh, but I have the warning :
./mount.sh: line 1: #!/bin/sh: No such file or directory
But :
$ ll /bin/sh
lrwxrwxrwx 1 root root 4 30 déc. 23:08 /bin/sh -> bash
Var content is a char *, sent throught network. The user types it in a web interface, it's then stored in a SQLite3 database, before being sent through network.

Had non-printable chars which appeared with hexedit, works now.
thank you all !

Related

How to allocate an input file instead of the stdin in an Eclipse CDT application?

My application is a simple executable used from the command line and takes stdin as input and stdout as output, so it behaves like many GNU tools.
To test it, I want to set up an Eclipse CDT DEBUG Configuration to pass a file to stdin and another one to stdout.
I have tried unsuccessfully a few solutions, all inside the DEBUG Configuration GUI :
In Common / Standard Input and Output / Input File: I put inputfile.txt and in the same section Output file: I put outputfile.txt. As the GUI indicates that the working directory is ${workspace_loc:/myprogram}, it should be alright, but when the debugger is started, it warns :
[Invalid file specified for console output: test/WittenACM87ArithmCoding-1.txt.coded]
[Invalid file specified for stdin file: test/WittenACM87ArithmCoding-1.txt]
In Arguments I put < inputfile.txt > outputfile.txt which is obviously not designed for that
Of course, both files are in the working directory. All attempts fails on the ch = getc(stdin); code line with some strange message:
Can't find a source file at "/build/glibc-p3Km7c/glibc-2.24/io/../sysdeps/unix/syscall-template.S"
Locate the file or edit the source lookup path to include its location.
Here is the stack:
Thread #1 [myprogram] 31960 [core: 5] (Suspended : Signal : SIGINT:Interrupt)
__read_nocancel() at /build/glibc-p3Km7c/glibc-2.24/io/../sysdeps/unix/syscall-template.S:84 0x7ffff7811700
_IO_new_file_underflow() at /build/glibc-p3Km7c/glibc-2.24/libio/fileops.c:600 0x7ffff77a9a00
__GI__IO_default_uflow() at /build/glibc-p3Km7c/glibc-2.24/libio/genops.c:413 0x7ffff77aab02
_IO_getc() at /build/glibc-p3Km7c/glibc-2.24/libio/getc.c:38 0x7ffff77a54f0
main() at /xxxxxx/src/myprogram.c:20 0x555555554f01
When I run the application directly in the console, it works:
./myprogram < inputfile.txt > outputfile.txt
I assume from this that Eclipse does not manage to realise the files redirections to stdin and stdout, so obviously, I do it the wrong way. I have searched for it, but here and here don't provide solution for my use case.
So, in order to be able to use the debugger from Eclipse, what is the right way to set up the Eclipse DEBUG Configuration ?
In fact, solution 1 was using a relative path not related to the working directory. Using either button Workspace... or File System... in the GUI enables to select the files which shall already exist.
For example with Workspace definition, the field becomes :
${workspace_loc:/myprogram/inputfile.txt} (same for output)
And it works. Debuggers says :
[Console output redirected to file:/.../myprogram/outputfile.txt]

Can't read file generated by bash line

I have written a C code where the bash script lines are used inside this C code, and this is how I wrote it:
printf("wc -l < smallerthan > number_lines\n");
if( (fr=fopen(fname_rcut,"r"))==NULL ) { printf("error in rcut file: %s\n",fname_rcut); exit(1); }
I need to read the file "number_lines" which is generated from "smallerthan" file, the problem is when I source the C code to run automatically like:
$gcc myC_code.c -lm
$./a.out > run.sh
$source run.sh
Then if I view the run.sh
& vi run.sh
I get this inside run.sh:
wc -l < smallerthan > number_lines
ls
error in rcut file: /home/number_lines
which mean the code upto this point didn't find my "number_lines" file yet since the number_lines file is yet to appear, but if I copy the line and run it separately, instead of "automatically", then it works because the file is there now.
My question is, how to make my code run automatically and my C code to read the file which is generated by bash line or how to generate the file and read it properly?
Any idea please because I'm really new to programming and I have to use bash inside C for my work.
Note: the above is only small part of my C code but I used several bash lines inside my C code.
There are a number of observations in your code. I assume that char *fname_rcut indeed points to "/home/number_lines".
First observation: if you write commands to a file, they will not be executed.So the file number_lines is created only after you run run.sh. Therefore, the file will not exist during the execution of your C program. You might look into int system(const char *command) (man 3 system).
Second observation: /home/number_lines is probably not the correct filename. It would probably be /home/your_name/number_lines; try a pwd to see what the exact directory name is.
Third observation: Why do you want to source run.sh? Source executes the file in the current shell. There is usually no need for that.
I have solved it :
what we need actually is using system(command) after each shell command
for example :
printf("wc -l < smallerthan > number_lines\n");
will be after solving :
sprintf(command1,"wc -l < smallerthan > number_lines\n");
system(command1);

Opening image files through unix system command in C

I have a project where the user can open files by selecting them in a menu.
I have two near identical pieces of code, but one works, whereas the other doesn't:
the one that works is for opening text files through gedit ("chemin" contains the file path):
char buf[200];
snprintf(buf,sizeof(buf),"gedit %s",chemin);
system(buf);
And this one doesn't work when run in my code, but does work when run outside of it (opens .jpg files with eog - have also tried xdg with no improvement):
snprintf(buf,sizeof(buf),"eog %s",chemin);
system(buf);
Is there a surer way of opening .jpg files from the unix command line? Or did I forget something?
TIA
UPDATE
It seems the buffer only prints its first 7 characters to the command line, ie:
my file path: ./FICHIER_PROJET/basededonnee/basedeDonneefichier/IMG_RGB/21.jpg
what the command line prints: eog ./FI
This only happens with these .jpg files
The problem might come from the char '\0' that has the wrong place, try something like :
strncpy(buf, "eog ", 4);
strncat(buf, chemin, sizeof(chemin));
buf[4+sizeof(chemin)] = '\0';
if(system(buf) == -1){
perror("Error with the system call ");
exit(-1);
}
Solved it... I had been using one general buffer for all my system commands. I created a new buffer just for this one and it works.

How to work with C pipes on a Mac?

I'm experimenting with pipes in C. I want to connect the Standard Output of file1:
int main() {
printf("6");
}
...to the Standard Input of file2:
int main() {
int number;
scanf("%d", &number);
printf("The number is %d.", number);
}
The output should be: The number is 6. In Xcode, file1 is appended to the executable (the target). I open the Terminal app, change the directory to the Debug folder (which contains the executable file) and issue this line:
.\TestDrive | /usr/someone/somewhere/file2.c
TestDrive is the name of the executable file (that contains file1). Since file2 is not contained in the Debug folder, I have to specify the full path of it. If you wish, you can download the project here.
In the console, I get Permission denied. What am I doing wrong?
If you are piping into /usr/someone/somewhere/file2.c, it is expected that that file is executable; .c files generally are not, but the file that got generated by compiling it probably is.
You should make your TestDrive file executable, from the command line via chmod.

C - execl(): can't execute file located in another directory

I'm trying to execute a binary file named "helloworld" (compiled from a source.c) located in another directory with my program notify.c. Here's where my files are located:
/home/morts/Desktop/helloworld
/home/morts/Desktop/Homeworks/notify
I tried with:
const char *cmd = "/home/morts/Desktop/helloworld";
execl(cmd, "LOL", NULL);
perror("execl()");
exit(EXIT_FAILURE);
but I get:
execl(): Permission denied
If helloworld would be located in the same directory of notify, I'd simply put "./helloworld", but since they are in different directories, how can I fix this?
Thanks and regards.
if you want to execute an file hello under path /bin/test by execl without any parameters
try following:
execl("/bin/test/hello", "hello", (char*)NULL);
Check following otherwise you may get a permission denial error:
The first argument must be an /path/your_file without any space
The second argument must be "your_file" the same as in first argument
the last argument must be (char*)NULL
You should check that the path is correct and you have the right permissions to execute this file by trying to run the command directly in the terminal :
/home/morts/Desktop/helloworld
If it works then you have to check that the user running your program also have the permission to execute that file.

Resources