Last active
November 27, 2015 09:44
-
-
Save coderodde/e24fcfb96e9349da9d71 to your computer and use it in GitHub Desktop.
A pseudoportable C program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# Below, XXXX asks for random characters in order to make a unique file name. | |
# Mac OSX seems to ignore XXXX, yet Ubuntu requires them. | |
# Create a temporary file name for the source code: | |
TMP_SOURCE_FILE="$(mktemp -t sourceXXXX).c" | |
# Create a temporary file name for the executable file: | |
TMP_PROGRAM_FILE="$(mktemp -t programXXXX)" | |
# Write the source code into the temporary source file: | |
cat > $TMP_SOURCE_FILE <<- END_OF_SOURCE | |
#include <stdio.h> | |
int main(int argc, char* argv[]) | |
{ | |
int i; | |
puts("Hello, world! I am a pseudoportable C program."); | |
for (i = 1; i < argc; ++i) | |
{ | |
printf("Argument %d: %s\n", i, argv[i]); | |
} | |
return argc - 1; | |
} | |
END_OF_SOURCE | |
# Compile and run: | |
gcc $TMP_SOURCE_FILE -o $TMP_PROGRAM_FILE | |
$TMP_PROGRAM_FILE $@ | |
EXIT_STATUS=$? | |
# Clean the source and the binary: | |
rm $TMP_SOURCE_FILE | |
rm $TMP_PROGRAM_FILE | |
# Delegate the exit status of the C program to calling bash: | |
exit $EXIT_STATUS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment