Hi there, I am trying to make a C program that will print herself source code.
I am assuming that the source code file and the execute file are in the same directory without any other .c files.
Well I have tried many codes, and it didn't worked out.
The error cause because the order of the commands (if it is windows command first it passed in windows OS and if it is a linux command first it passed in linux OS).
I am trying to ignore fopen and other functions and to use only simple OS commands.
Here is the code I have tried:
code working on windows:
/* C library statement */
#include <stdlib.h>
/* main program */
int main()
{/* start of main */
int windows = 0; /* a variable to check if we are running windows or unix operation system */
/* system function returns -1 value if it failed.
* so if unix value is -1 we are not running unix system.
* if we are not running unix system we will execute windows commands.
*/
windows = system("type *.c"); /* windows command to print file text */
if(windows == -1) /* if windows command failed (and the specific file is exists) then we are not running windows operation system */
system("cat *.c"); /* unix command to print file text */
return 0;
}/* end of main */
code working on linux:
/* this program will print herself source code by using simple linux or windows commands */
#include <stdlib.h>
/* main program */
int main()
{/* start of main */
int unix = 0; /* a variable to check if we are running windows or unix operation system */
/* system function returns -1 value if it failed.
* so if unix value is -1 we are not running unix system.
* if we are not running unix system we will execute windows commands.
*/
unix = system("cat *.c"); /* unix command to print file text */
if(unix == -1) /* if unix command failed (and the specific file is exists) then we are not running unix operation system */
system("type *.c"); /* windows command to print file text */
return 0;
}/* end of main */
well the problem is that linux recognize type as a bash command and windows is not compiling the cat command