We come at improving yesterday’s PHP Recompiles C Numerical Bubble Sort Tutorial two ways, today, they being …
- genericization … taking the form of changing a hardcoding in a dropdown and associated variable “remix” regarding the functionality of the “what we should now call just” Numerical Sort web application … and …
- simple defensive programming … where code can get to the operating system, as with serverside languages such as PHP and compiled programs like we’re doing here with C then care is needed
By “genericization” we’re taking the “bubble” out of “squeak” “bubble sort” to just leave “sort” with our thinking, though we’d like to restrict the sorting to “numerical sorting” ideas. In other words, we’ve channelled the excellent C code bases at W3schools, thanks, for more than just a “bubble sort” and left it up to the PHP to see what has been “plonked” C wise by us to develop a dropdown $selsorttype PHP variable containing a genericized outerHTML, rather than the previous “bubble_sort” hardcoding used …
<?php
$sorttype="bubble_sort";
$selsorttype="bubble_sort";
$ccont="";
$lastresult="";
if (isset($_POST['mysort'])) {
$sorttype=str_replace('+',' ',urldecode($_POST['mysort']));
$ccont=file_get_contents('./' . $sorttype . '.c');
} else if (isset($_GET['mysort'])) {
$sorttype=str_replace('+',' ',urldecode($_GET['mysort']));
$ccont=file_get_contents('./' . $sorttype . '.c');
} else {
$ccont=file_get_contents('./bubble_sort.c');
}
foreach (glob('*_sort.c') as $filename) {
if (strpos(basename($filename), 'new_') === false) {
if ($selsorttype == "bubble_sort") {
$selsorttype="<select onchange=\"location.href='./bubble_sort_c.php?mysort=' + this.value;\" style=display:inline-block; name=\"mysort\" id=\"mysort\"><option value='" . explode('.',basename($filename))[0] . "'>" . explode('.',basename($filename))[0] . "</option></select>";
} else if (basename($filename) == ($sorttype . ".c")) {
$selsorttype=str_replace(" id=\"mysort\">", " id=\"mysort\">" . "<option value='" . explode('.',basename($filename))[0] . "'>" . explode('.',basename($filename))[0] . "</option>", $selsorttype);
} else {
$selsorttype=str_replace("</select>", "<option value='" . explode('.',basename($filename))[0] . "'>" . explode('.',basename($filename))[0] . "</option></select>", $selsorttype);
}
}
}
?>
Why worry about “simple defensive programming”? Some users might be looking for loopholes in your code to exploit what the underlying programming language used is capable of, and that can be altering file systems. What if it was possible in that “what we hope” is a user supplied numerical comma separated list, somebody was able to embed some operating system call? Well, we figure that malicious idea would have to contain a quote or double quote, so now we have …
<?php
$prefix=explode(' = {', $ccont)[0] . ' = {';
$midbit=explode('"',explode("'",explode('}', explode($prefix, $ccont)[1])[0])[0])[0];
$suffix=explode($prefix . $midbit, $ccont)[1];
$altmidbit=$midbit;
$httpis=" http";
$endaf="youllveverfindthis";
$endat="youllveverfindthis";
$hbits=explode(' http', $prefix);
if (sizeof($hbits) > 1) {
$endaf=' http' . explode(' ', $hbits[1])[0];
$endat=' http' . explode(' ', $hbits[1])[0] . '</a>';
$httpis=$endaf;
}
if (isset($_POST['myarr'])) {
$altmidbit=str_replace(' ',' + ',str_replace('+',' ',urldecode($_POST['myarr'])));
file_put_contents('new_' . $sorttype . '.c', $prefix . explode('"',explode("'",$altmidbit)[0])[0] . $suffix);
exec('gcc new_' . $sorttype . '.c -o ' . $sorttype);
$lastresult='$ gcc new_' . $sorttype . '.c -o ' . $sorttype . ' <br>$ ./' . $sorttype . '<br>';
$lastresult.=shell_exec('./' . $sorttype);
$midbit=$altmidbit;
} else if (isset($_GET['myarr'])) {
$altmidbit=str_replace(' ',' + ',str_replace('+',' ',urldecode($_GET['myarr'])));
file_put_contents('new_' . $sorttype . '.c', $prefix . explode('"',explode("'",$altmidbit)[0])[0] . $suffix);
exec('gcc new_' . $sorttype . '.c -o ' . $sorttype);
$lastresult='$ gcc new_' . $sorttype . '.c -o ' . $sorttype . ' <br>$ ./' . $sorttype . '<br>';
$lastresult.=shell_exec('./' . $sorttype);
$midbit=$altmidbit;
}
?>
… used in fortified and genericized bubble_sort_c.php Numerical Sort web application.
Did you know?
(“Simple defensive programming” x 786) – ((Paranoid tendency factor) x 56) might get you to want to research Captive portals … and beyond!
Previous relevant PHP Recompiles C Numerical Bubble Sort Tutorial is shown below.
For years and years, before the Internet, there were (and still are) the publicly accessible desktop application worlds of …
- Windows with command line DOS
- Mac with command line (a lot like, but not exactly) Linux
… and in either of these woooorrrllldddsss, or within any Unix or Linux environment, for years and years the C programming language was a big programming language presence.
We’re hooking into this C programming today because up at our AlmaLinux web server we have the gcc (ie. GNU Compiler Collection) compiler available to compile C code into an executable program.
And we didn’t argue with the great Numerical Bubble Sort C code basis we visited at W3schools, thanks, and so we’re allowing users to …
- tweak that code … ie. we’d be mad not to control the bulk of what a user can do … so that …
- a user can add their own integer comma separated array list … our PHP …
- creates a new_bubble_sort.c … and as the PHP explains …
- use PHP exec and shell_exec to …
$ gcc new_bubble_sort.c -o bubble_sort
$ ./bubble_sort
- outputting a sorted array result
… further to that discussion back with CSH/KSH/PHP Numerical Bubble Sort Tutorial, some time ago now, going along the same lines.
See …
- PHP bubble_sort_c.php Numerical Bubble Sort … overseeing …
- C bubble_sort.c … as a C code basis
Previous relevant CSH/KSH/PHP Numerical Bubble Sort Tutorial is shown below.
Here is a tutorial that follows up on yesterday’s C++ Xcode Numerical Bubble Sort Tutorial as shown below, in an “oh, I forgot” moment to make the link between the programming language C (and then C++) and its relationship to the Linux or unix shell scripting environment C Shell (we’ll be saying CSH) (ie. today we go from a compiling language to an interpretive scripting set of languages), and CSH’s relationship with (the remarkable, the wonderful, the stupendous) awk, making functionality like numerical sorts quite easy just using shell scripting functionality. Well, that’s how it started with work on the Linux command line here on this Mac laptop using the Terminal application. But, and am so very very very sorry to start a sentence with but … but, well you see there’s this … but I digress … I thought this could become a webpage or web application pretty easily by involving our old favourite PHP/exec combination (with code rewritten for Korn Shell (we’ll be saying KSH)), because the rjmprogramming.com.au domain prefers KSH Korn shell … so we proceeded along those lines … and then lo and behold, it was apparent this could also be written for all the three PHP “modes of use” (latest “foot in the door” tutorial for this would be PHP Modes of Use File Traverse Tutorial) we’ve been talking about here lately at this blog … namely PHP used with curl and the command line usage and the web browser usage.
Attempting to write code with a generic eye has the advantage that you end up with more flexibility, and, usually, more readable code.
So in the CSH scripting code you’ll (need, maybe, to open up execute privileges via a command like chmod 755 ./bubble_sort.*sh and) see:
- The use of set via set variable=value syntax … will fit in with all those lovers of interpretive languages (who like to add $ … (who doesn’t?))
- The use of Command line arguments as with ./bubble_sort.csh 22 2 15 0 … adds to the flexibility of your code with the user being able to tailor each usage of the script
- The CSH (and very C like) way of incrementing numerical variables via @ variable++ syntax
- The use of the underlying Linux operating system command to (numerically) sort (ie. sort -n[r]) … whether it uses a “bubble sort” am not totally sure … but if not try the “buble sort” … chortle, chortle
- The deliberate pointing out of any important restriction should there be one … doh! … in that we have a stipulation for a numerical data sort
Found this link very useful for C shell (ie. CSH) research … thanks.
Link to some downloadable CSH programming code … rename to bubble_sort.csh … okay at Linux command line at this Mac, at least … but for rjmprogramming.com.au web server usage needed …
Link to some downloadable KSH programming code … rename to bubble_sort.ksh … the differences described by bubble_sort.csh
Link to some downloadable PHP programming code … rename to bubble_sort.php … which can supervise bubble_sort.ksh above using PHP exec command in the, aforementioned, “modes of use”:
- Via http transport layer (ie. web browsing, or “surfing the net”) in an Internet mode of use (eg: http://www.rjmprogramming.com.au/Linux/awk/csh/bubble_sort.php?jsize=12&jmin=37&jmax=82&imode=0) in address bar of a web browser
- Via curl in an Internet mode of use (eg. curl http://www.rjmprogramming.com.au/Linux/awk/csh/bubble_sort.php?jsize=12@37@82@0) at Linux or unix or Windows command line
- Via command line PHP in Linux or unix or Windows command line in a command or Intranet mode of use (eg. php ./bubble_sort.php 12 37 82 0) at a Linux or unix or Windows command line
Previous relevant C++ Xcode Numerical Bubble Sort Tutorial is shown below.
Here is a tutorial that uses the Xcode IDE on a Mac laptop to create a C++ desktop compiled application using a Bubble Sort method to numerically sort some numbers (ie. double).
Attempting to write code with a generic eye has the advantage that you end up with more flexibility, and, usually, more readable code.
Some ideas used in today’s code include:
- The use of Preprocessor directives via #define identifier replacement syntax … will fit in with all those lovers of interpretive languages
- The use of Command line arguments as with ./Test 22 2 15 0 … adds to the flexibility of your code with the user being able to tailor each usage of the executable
- The use of a pointer to an array within a function, meaning that the data can be changed (ie. sorted) in situ rather than returning a value (or resorting to a global variable array) … hence we use void numerically_sort_array(double* asort, int isize = ASIZE, bool ascending = (SMODE != 0)) … you could change the void return, to return an error code, for example
- The C++ use of default values in parameters of the function call, making use of the Preprocessor directives, so that the call could make sense as numerically_sort_array(psarray); as numerically_sort_array(psarray, 1000, true); … such thoughts come into play, in C++, also when considering Overloading
- The deliberate pointing out of any important restriction should there be one … doh! … in that we have a stipulation for a double data type array … and have not implemented any Template functionality, on this occasion
Link to some downloadable C++ programming code … rename to main.cpp for use.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.