0% found this document useful (0 votes)
16 views

Lucrarea de Laborator Nr. 3: Programarea Sistemelor Distribuite

This document describes a CGI program for recording student grades. It includes three files: notare.html (the form), interCGI.c (a library for handling form data), and notare.c (the CGI program). The form allows selecting a course and entering a student name, exam grade, and coursework grade. Notare.c uses interCGI.c to retrieve the form data and write it to the note.txt file. The lab assignment is to: 1) Create a CGI program to display the grades from note.txt, 2) Modify the form to dynamically populate the student name from the studenti.txt file, and 3) Create a CGI program to add student names to studenti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lucrarea de Laborator Nr. 3: Programarea Sistemelor Distribuite

This document describes a CGI program for recording student grades. It includes three files: notare.html (the form), interCGI.c (a library for handling form data), and notare.c (the CGI program). The form allows selecting a course and entering a student name, exam grade, and coursework grade. Notare.c uses interCGI.c to retrieve the form data and write it to the note.txt file. The lab assignment is to: 1) Create a CGI program to display the grades from note.txt, 2) Modify the form to dynamically populate the student name from the studenti.txt file, and 3) Create a CGI program to add student names to studenti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAMAREA SISTEMELOR DISTRIBUITE

Lucrarea de laborator nr. 3


Obiective

Dezvoltarea programelor CGI;


Prelucrarea formularelor interactive.

1. Programe CGI. Prelucrarea formularelor interactive

Detalii privind CGI gsii n cursul web;

Studiai librria intergCGI;

Studiai i testai aplicaia de mai jos. Salvai


toate programele C n directorul cgi-bin
corespunztor contului dumneavoastr de pe
serverul apollo.eed.usv.ro.

nregistrarea notelor obinute de studeni la diferite discipline.


Aplicaia a fost conceput sub forma a trei fiiere:
notare.html conine formularul interactiv;
interCGI.c librria utilizat pentru preluarea la server a datelor trimise
de ctre client;
notare.c programul CGI utilizat pentru nregistrarea notelor;
Fiierul notare.html
<HTML>
<HEAD><TITLE>Notare studenti</TITLE></HEAD>
<BODY>
<FORM ACTION="http://apollo.eed.usv.ro/~tudor_c/cgibin/notare.cgi" ACTION="POST" >
Disciplina:
<SELECT NAME="disciplina">
<OPTION VALUE="PCLP1">PCLP1</OPTION>
<OPTION VALUE="PCLP2">PCLP2</OPTION>
<OPTION VALUE="PSD">PSD</OPTION>
<OPTION VALUE="APA">APA</OPTION>

<OPTION VALUE="SDTP">SDTP</OPTION>
</SELECT>
<BR>
Nume student: <INPUT TYPE="TEXT" NAME="numestudent"><BR>
Nota examen: <INPUT TYPE="TEXT" NAME="notaex"><BR>
Nota pe parcurs: <INPUT TYPE="" NAME="notaparcurs"><BR>
<INPUT TYPE="SUBMIT" NAME="Trimite" VALUE="Trimite">
<INPUT TYPE="RESET" NAME="Reset" VALUE="Reset">
</FORM>
</BODY>
</HTML>

Fiierul interCGI.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define NUMAX 50
#define VALMAX 50
#define NPARAM_MAX 20

#define isdig(x) ((x)>='0' && (x)<='9')


#define ishexa(x) ((x>='A' && x<='F') || ( x>='a' && x<='f'))

struct { char nume[NUMAX+1];


char valoare[VALMAX+1];
} param_CGI [NPARAM_MAX];
int nr_param_CGI;

void DecodificaSir_CGI(char t[])


{

char c; int i,j;


for(i=j=0;(c=t[i])!=0;i++,j++){
if(c=='+') c=' ';
else if(c=='%'){
c=0;
if(isdig(t[i+1])) c=t[++i]-'0';
else if(ishexa(t[i+1])) c=toupper(t[++i])-'A'+10;
if(isdig(t[i+1])) c=c*16+t[++i]-'0';
else
if(ishexa(t[i+1])) c=c*16+toupper(t[++i])'A'+10;
}
t[j]=c;
}
t[j]=0;
}

int CitesteParam_CGI()
{
char citit[(NUMAX+VALMAX)*NPARAM_MAX],*p,k,i;
//Citirea argumentelor
if(strcmp(getenv("REQUEST_METHOD"),"POST")==0)
fgets(citit,atoi(getenv("CONTENT_LENGTH"))+1,stdin);
else
if(strcmp(getenv("REQUEST_METHOD"),"GET")==0)
strcpy(citit,getenv("QUERY_STRING"));
else return 0;
//Decodificare perechi nume=valoare
for(k=0,p=citit;*p;k++,p++){
for(i=0;*p && *p!='=';p++) param_CGI[k].nume[i++]=*p;
param_CGI[k].nume[i]='\0';
if(*p++==0){ //fara valoare
param_CGI[k].valoare[i]='\0';
break;
}
for(i=0;*p && *p!='&';p++) param_CGI[k].valoare[i++]=*p;
param_CGI[k].valoare[i]='\0';
DecodificaSir_CGI(param_CGI[k].valoare);
if(*p==0)
break;
}
return nr_param_CGI=k+1;

//nr.parametrilor CGI

char * ValoareParametru_CGI(char *nume)


{
int k;
for(k=0;k<nr_param_CGI;k++)
if(strcmp(nume,param_CGI[k].nume)==0)
return param_CGI[k].valoare;
return "";

}
void DeschideDocDinamic(char *titlu)
{
printf("Content-type: text/html\n\n");
printf("\n<HTML><HEAD><TITLE>%s</TITLE></HEAD><BODY>",titlu);
}
void InchideDocDinamic()
{
printf("</BODY></HTML>");
}

Fiierul notare.c
#include "interCGI.c"
int main()
{
int n;
FILE *f;
DeschideDocDinamic("Inregistrare note");
if(strcmp(getenv("REQUEST_METHOD"), "POST") != 0)
return 0;
if( (n = CitesteParam_CGI())<1 || n>5)
return 0;
printf("Date primite<BR>");
printf("Nume: %s<BR>", ValoareParametru_CGI("numestudent"));
printf("Disciplina: %s<BR>", ValoareParametru_CGI("disciplina"));
printf("Nota examen: %s<BR>", ValoareParametru_CGI("notaex"));
printf("Nota pe parcurs %s<BR>",
ValoareParametru_CGI("notaparcurs"));
if ( (f = fopen("note.txt", "at")) == NULL)
return 0;
fprintf(f,"%s\n",
fprintf(f,"%s\n",
fprintf(f,"%s\n",
fprintf(f,"%s\n",

ValoareParametru_CGI("numestudent"));
ValoareParametru_CGI("disciplina"));
ValoareParametru_CGI("notaex"));
ValoareParametru_CGI("notaparcurs"));

fclose(f);
printf("<HR>Datele au fost scrise in fisier!<BR>");
InchideDocDinamic();
return 0;
}

Compilarea programului
gcc -o notare.cgi notare.c

Testarea programului
Creai un fiier note.txt (n directorul cgi-bin) i acordai drepturi de
scriere tuturor utilizatorilor;
ncrcai formularul HTML;
Introducei valori pentru toate cmpurile formularului;
Efectuai click de mouse pe butonul Trimite.

Fiierul note.txt
Ion Ionescu
PCLP1
9
8
Vasilescu Vasile
PCLP1
6
9
Ion Ionescu
PCLP2
5
7

Tem de laborator:
1. Scriei un program CGI pentru afiarea notelor
nregistrate n fiierul note.txt.
2. Modificai exemplul prezentat n lucrarea de
laborator astfel nct, numele studentului
cruia i se nregistreaz nota s nu mai fie
introdus ntr-un cmp text (ceea ce ar putea
provoca erori), ci s fie ales dintr-o list de
selecie. Lista de selecie va fi populat n
mod dinamic, pe baza informaiilor coninute
ntr-un fiier text, denumit studenti.txt. Pentru
realizarea acestui obiectiv, formularul de
introducere a notelor va fi creat, n mod
dinamic, de ctre un program CGI.
3. Scriei o aplicaie CGI care va permite
introducerea
numelor
studenilor
i
nregistrarea acestor informaii n fiierul
studenti.txt.

You might also like