Reputation: 5015
My program receives an executable binary file through a TCP socket.
I need to save this file in to the harddisk as a executable program. File is successfully received but the problem is the default file attribute is being set to non executable.
How to change the file's attribute as executable in C in Ubuntu?
Thank you, Regards, Robo
Upvotes: 2
Views: 1127
Reputation: 204994
How are you creating and writing the file? If you know it's going to be executable, just make the file with the correct mode in the first place.
int fd = open("path/to/file", O_WRONLY | O_CREAT, 0777);
Unless umask
is stripping the executable bits (common values are 0022
and 0002
which leave the executable bits alone), path/to/file
will be created initially executable.
Upvotes: 3
Reputation: 4816
How about int chmod(const char *path, mode_t mode)
and int fchmod(int fd, mode_t mode) ?
apropos chmod
man 2 chmod
The most basic example:
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
char * fpath = "/path/to/binary";
int ret=0;
if(ret = chmod(fpath, S_IRUSR|S_IXUSR) < 0){
perror("chmod failed");
exit(1);
}
printf("chmod ok\n");
exit(0);
}
Upvotes: 5
Reputation: 13446
You can change the file mode with chmod
. Read the man page(man 2 chmod
) for detail(roughly the same as shell command chmod
).
Upvotes: 2