How can I to write into a file owned by root, for example in /etc ? In particular I need to have the executable owned by a normal user therefore I can't run it as root nor call it with sudo.
I know this is not a specific C problem, however I'd like to know a possible solution in C. The same solution in another language may help as well.
Example using a C programming language
/*
How can I write into a file owned by root user?
-rwxrwxr-x 1 user user 8480 ott 17 11:48 test
-rw-rw-r-- 1 user user 432 ott 17 11:48 test.c
-rw-rw-r-- 1 root root 20 ott 17 11:48 textfile.txt
$ ./test
Test how to write a file owned by root
File can't be opened
Segmentation fault (core dumped)
*/
#include <stdio.h>
int main(int argc, char **argv)
{
printf("Test how to write a file owned by root\n");
FILE *fp;
fp = fopen("textfile.txt", "w");
if (fp != NULL) {
fprintf(fp, "Wite success!\n");
printf("\n Details successfully written to the file\n\n");
}
else
printf("File can't be opened\n");
fclose(fp);
return 0;
}
Thank you