1
  1. Hi there, I am trying to make a C program that will print herself source code.

  2. I am assuming that the source code file and the execute file are in the same directory without any other .c files.

  3. Well I have tried many codes, and it didn't worked out.

  4. 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).

  5. I am trying to ignore fopen and other functions and to use only simple OS commands.

  6. 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

CrazyTux
  • 204
  • 2
  • 12
  • 1
    Use standard file IO functions to make the program portable (`fopen`, `fread` and friends) – Eugene Sh. Jun 07 '21 at 14:27
  • 1
    Good luck: https://stackoverflow.com/questions/142508/how-do-i-check-os-with-a-preprocessor-directive – Dominique Jun 07 '21 at 14:27
  • 1
    The term for the type of program described in #1 is "quine. " – Bacon Bits Jun 07 '21 at 14:31
  • @EugeneSh. i am trying to ignore that commands to use the OS basic commands. – CrazyTux Jun 07 '21 at 14:36
  • @Dominique well I tried that but when I am trying to compile the program in UNIX the compiler does not recognize the _WIN32 variable, it does works on windows that way, but the target from the start was to run and compile the program in any OS. – CrazyTux Jun 07 '21 at 14:38
  • Generally you cannot compile a C program into an executable that runs on "_any OS_". – the busybee Jun 07 '21 at 15:18
  • hi everybody and thanks for help, I made a code that works and compile in one of this two OS's I will edit the post. – CrazyTux Jun 07 '21 at 15:56
  • 1
    You should post _code that works_ as an answer, not edit into the question post. – Armali Jun 07 '21 at 16:09
  • "The Working Code I Made Compile And Run In One of this Two OS:"is that the solution or another version which you are not satisfied yet? If solution see comment by Armali. If not satisfied please explain what is missing/wrong. – Yunnosch Jun 07 '21 at 16:11
  • 1
    @Armali sorry I will fix that out, I am new in this forum. – CrazyTux Jun 07 '21 at 16:18

2 Answers2

2

I Made This Code Working, Can Be Compiled and Run in Both OS

#include <stdlib.h>

/* defining variables to check if what OS we are running in */

#if defined (_WIN32) || defined (_WIN64) /* if we are running win 32bit or win 64 bit */
    #define HAVEWIN 1 /* defined we are running at windows OS */
    #define HAVEUNIX 0 /* defined we are not running at unix OS */
#elif defined (__unix__) /* if we are running a unix OS */
    #define HAVEUNIX 1 /* defined we are running at unix OS */
    #define HAVEWIN 0 /* defined we are not running at unix OS */
#endif /* end of the OS checks */

/* main program */

int main()
{/* start of main */

    if(HAVEWIN == 1) /* if we are running on windows OS */
        system("type *.c"); /* use windows command to print file text */
    else if(HAVEUNIX == 1) /* if we are running on unix OS */
        system("cat *.c"); /* use unix command to print file text */
    return 0;

}/* end of main */
CrazyTux
  • 204
  • 2
  • 12
0

The symbol __FILE__ always expands to the name of the current file. The following code will print the source code location:

#include <stdio.h>

int main()
{
  printf("%s\n", __FILE__);
  return 0;
}

So you should just then just add code to dump its contents. But don't use system() calls, as this is not portable and very inefficient.

Brecht Sanders
  • 6,215
  • 1
  • 16
  • 40