/usr/lib/cgi-bin/c/index.c

[Ritorna]

/* index.c
/*
 *
 * Copyright (C) 2016 Ing. Stefano Salvi<stefano@salvi.mn.it>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXBUF  30
#define FALSE   0
#define TRUE    (!FALSE)

int getParam(const char *params, const char *name, char *buf, char maxlen);
void showFile (const char *title, const char *name);

char *head =
"Content-Type: text/html;charset=utf-8\n\
\n\
<!DOCTYPE html>\n\
<html lang=\"it\">\n\
  <head>\n\
    <title>Raspberry-My Programmazione in C</title>\n\
    <meta  name=\"author\" content=\"Ing. Stefano Salvi<stefano@salvi.mn.it>\">\n\
    <meta charset=\"utf-8\">\n\
    <link rel=\"stylesheet\" type=\"text/css\" href=\"../../css/index.css\">\n\
    <script type=\"text/javascript\" src=\"../../js/index.js\"></script>\n\
  </head>\n\
  <body>\n\
    <!-- Dialog -->\n\
    <div id=\"isf_overlay\">\n\
      <div id=\"isf_overlay_frame\">\n\
        <div id=\"isf_overlay_content\">\n\
              <h2>\n\
                  Dialog modale visualizzato dal Javascript\n\
              </h2><p>\n\
                  &Egrave; possibile fare comparire un dialog sopra la pagina, che scompaia con un bottone.\n\
              </p>\n\
        </div>\n\
        <div id=\"isf_overlay_bottom\">\n\
          <input id=\"isf_overlay_close_button\" type=\"button\" onClick=\"closeDialog()\" value=\"Chiudi\">\n\
        </div>\n\
      </div>\n\
    </div>\n\
    <!-- Fine Dialog -->\n\
    <!-- Slideshow -->\n\
    <div id=\"testa-carousel\">\n\
      <div id=\"testa-slideshow\">\n\
            <img src=\"../../img/little_pi.png\" alt=\"Raspberry Pi 3\" width=\"400\" height=\"400\" />\n\
            <img src=\"../../img/raspberry-pi-2-angle-100569133-orig.png\" alt=\"Raspberry Pi 3\" width=\"400\" height=\"400\" />\n\
            <img src=\"../../img/Raspberry_Pi_-_Model_A.jpg\" alt=\"Raspberry Pi Model A\" width=\"400\" height=\"400\" />\n\
            <img src=\"../../img/rpi1bplus.png\" alt=\"Raspberry Pi 2\" width=\"400\" height=\"400\" />\n\
      </div>\n\
    </div>\n\
    <!-- Fine Slideshow -->\n\
    <script type=\"text/javascript\">var slides = new Slideshow('testa-slideshow', 1000, 400);</script>\n\
    <h1>Esempi di programmazione in C</h1>\n\
    <p>[<a href=\"../../index.html#c\">Ritorna</a>]</p>\n\
    <p><input type=\"button\" onClick=\"openDialog()\" value=\"Apri Dialog\"></p>\n\
    <form method=\"get\" action=\"index.cgi\">\n\
      <p>Primo numero: <input type=\"text\" name=\"primo\"><br>\n\
      Secondo numero: <input type=\"text\" name=\"secondo\"><br>\n\
      <input type=\"submit\" value=\"Somma\"></p>\n\
    </form>\n\
";


char *tail =
"    <div id=\"footer\">\n\
      <span class=\"author\">Autore Stefano Salvi</span> <a href=\"mailto:stefano@salvi.mn.it\" title=\"Indirizzo Mail di Salvi\">stefano@salvi.mn.it</a>\n\
      Rilasciato su licenza <a href=\"https://creativecommons.org/licenses/by-sa/3.0/it/legalcode\" title=\"Attribuzione CC-by-sa\">CC-BY-SA</a><br>\n\
      <a href=\"https://creativecommons.org/licenses/by-sa/3.0/it/legalcode\" title=\"Attribuzione CC-by-sa\"><img src=\"../../img/cc_by_sa_88x31.png\" alt=\"Logo Creative Commons\" title=\"Logo Creative Commons\"></a>\n\
    </div>\n\
  </body>\n\
</html>\n\
";

int main () {
  char *query = getenv("QUERY_STRING");

  puts(head);
  if (query && query[0]) {
    char buf1[MAXBUF];
    char buf2[MAXBUF];
    int a,b;
    if (!getParam(query, "primo", buf1, MAXBUF) || !buf1[0]) {
      puts ("    <div class=\"error\">\n");
      puts ("      <h2>Il Primo numero non pu&ograve; essere vuoto</h2>\n");
      puts ("    </div>\n");
    } else if (buf1[0] != '-' && (buf1[0] < '0' || buf1[0] > '9')) {
      puts ("    <div class=\"error\">\n");
      puts ("      <h2>Il Primo numero  deve essere un numero</h2>\n");
      puts ("    </div>\n");
    } else if (!getParam(query, "secondo", buf2, MAXBUF) || !buf2[0]) {
      puts ("    <div class=\"error\">\n");
      puts ("      <h2>Il Secondo numero  non pu&ograve; essere vuoto e deve essere un numero</h2>\n");
      puts ("    </div>\n");
    } else if (buf2[0] != '-' && (buf2[0] < '0' || buf2[0] > '9')) {
      puts ("    <div class=\"error\">\n");
      puts ("      <h2>Il Secondo numero  deve essere un numero</h2>\n");
      puts ("    </div>\n");
    } else {
      a = strtol(buf1, (char**)0, 10);
      b = strtol(buf2, (char**)0, 10);
      puts ("    <div class=\"display\">\n");
      printf ("      <h2>%d  +  %d = %d</h2>\n", a, b, a+b);
      puts ("    </div>\n");
    }
  }
  showFile ("Memoria installata:", "/proc/meminfo");
  showFile ("CPU presenti:", "/proc/cpuinfo");
  puts (tail);
  return 0;
}

int getParam(const char *params, const char *name, char *buf, char maxlen) {
  params = strstr(params, name);
  if (params) {
    params += strlen(name);
    if (*params++ != '=') {
      return FALSE;
    }
    while (*params && *params!= '&' && maxlen-- > 1) {
      *buf++ = *params++;
    }
    *buf = 0;
    return TRUE;
  }
  return FALSE;
}

void showFile (const char *title, const char *name) {
  FILE *f = fopen(name, "r");
  printf ("    <div class=\"display\">\n      <h2>%s</h2>\n      <p>\n", title);
  while (f && !feof(f) && !ferror(f)) {
    char buf[MAXBUF];
    if (fgets(buf, MAXBUF, f)) {
      printf("%s<br>", buf);
    }
  }
  puts ("      </p>\n    </div>\n");
}