Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a file as executable program in C in Ubuntu

Tags:

c++

c

linux

ubuntu

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

like image 289
RoboAlex Avatar asked Aug 09 '26 05:08

RoboAlex


1 Answers

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);
}
like image 174
Diego Schulz Avatar answered Aug 11 '26 19:08

Diego Schulz