#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int InsertIntoStr(char *target,char value[100],int start,int n);
int StrCpyFromToPtrs(char *str,char *targetstr,int start,int end);//returns the index right after the substring found in the
string e.g "hello world","ello" would return 4, position right after o,
int GetSubstrPos(int startingPos,char str[100],char substr[50]);//returns the index right after the end of the substring
int StrCpyFromTo(char str[100],char *targetstr[100],int start,int end);
int FormatData(char *formatedData,char name[100],char val[100],char capsule,char delimiter);
int AddIntoMemory(char *mem,char name[100],char value[100]);
float GetFromMemory(char *mem,char name[100]);
int SolveString(char *str,char sign);
int CountCharsInStr(char str[400],char symbol);//counts how many of the given characters tehre are in a str
int CutFromStr(char *str,int start,int end);
int Validate(char str[400],char validChars[20]);//checks if a string contains ONLY the given characters
int GetBetweenSymbols(char *str,char *result,char symbol1,char symbol2);//copys the inner most characters between symbol1 and
symbol2
int main(int argc, char *argv[])
{
system("TITLE Calculator by Rainer Plumer");
int i;
char teststr[100],targetstr[100];
char Memory[500];
Memory[0]='\0';
char chartestStr[100];
char BetweenBraces[100];
char *memPtr,*testStrPtr;
float arv;
int BracPos = 0;
int solveReturn=0;
int isValid = 0;//boolean for validation
memPtr = &Memory;
char *testptr,*betweenptr;
int cycles = 1;
int BracLen = 0;
int BracStart = 0;
char choise = 'x';
float resultAsNr = 0.0;
char function[10];
char *functionPtr;
int nrOfSigns=0;
functionPtr = &function;
testptr = &teststr;
betweenptr = &BetweenBraces;
//InsertIntoStr(testptr," world",5,5);||
while (1+1==2)
{
//------------------------------------
choise='x';
printf("Do you wish to use functions like [sin(),cos(),tan(),average()]\nOptions [y,n,e],e=exit\n");
scanf("%c",&choise);
if( (choise=='e')||(choise=='E') ){break;}
if( (choise=='y')||(choise=='Y') )
{
printf("Enter function name and a value between the braces \n Valid functions are[sin()sin(),cos(),tan(),average()] \n");
scanf("%s",testptr);getchar();
BracStart=0;
BracStart = GetBetweenSymbols(testptr,betweenptr,'(',')');//leiab sulgude vahelise osa
BracStart--;
StrCpyFromToPtrs(testptr,functionPtr,0,BracStart);//gets the function name part
printf("sub is:%i \n",GetSubstrPos(0,function,"sin"));
if(GetSubstrPos(0,function,"average") == 7)
{
nrOfSigns = CountCharsInStr(BetweenBraces,'+')+1;
solveReturn=1;
while(solveReturn !=0)
{
solveReturn=SolveString(betweenptr,'+');
}
printf("Average is:%f \n",atof(BetweenBraces)/nrOfSigns);
}
else if(GetSubstrPos(0,function,"sin") == 3)
{
printf("Answer Is:%f",sin( (atof(BetweenBraces)*(3.14159/180)) ));
}
else if(GetSubstrPos(0,function,"cos") == 3)
{
printf("Answer Is:%f",cos( (atof(BetweenBraces)*(3.14159/180)) ));
}
else if(GetSubstrPos(0,function,"tan") == 3)
{
printf("Answer Is:%f",tan( (atof(BetweenBraces)*(3.14159/180)) ));
}
else if(GetSubstrPos(0,function,"sqrt") == 4)
{
printf("Answer Is:%f",sqrt( atof(BetweenBraces) ));
}
// printf("StrCpyFromTo:%s \n",function);
printf("functionPtr is:%s \n",function);
continue;
}
printf("Enter your Equation \n");
printf("Valid Characters Are[1,2,3,4,5,6,7,8,9,0,(,),+,-,*,/,.] \n");
scanf("%s",testptr);
printf("is %i",CountCharsInStr(teststr,'('));
isValid=Validate(teststr,"1234567890()+-*/.");
if(isValid == 0){printf("ERROR:YOU ENTERED AN INVALID CHARACTER \n");continue;}
if( (CountCharsInStr(teststr,'(')-CountCharsInStr(teststr,')') )!=0 ){printf("ERROR:MISSING ONE BRACE \n");continue;}
BracPos = GetSubstrPos(0,teststr,"(");
while(BracPos != (-1))
{
BracStart = GetBetweenSymbols(testptr,betweenptr,'(',')');//leiab sulgude vahelise osa
//printf("between:%s: \n",BetweenBraces);
BracLen = strlen(BetweenBraces);
CutFromStr(testptr,BracStart,BracStart+BracLen+1);//lõikab sulgude osa võrrandist välja
//----------lahendamine
cycles = 1;
while (cycles < 5)
{
if(cycles == 1){solveReturn=SolveString(betweenptr,'*');}
else if(cycles == 2){solveReturn=SolveString(betweenptr,'/');}
else if(cycles == 3){solveReturn=SolveString(betweenptr,'+');}
else if(cycles == 4){solveReturn=SolveString(betweenptr,'-');}
else if(GetSubstrPos(0,teststr,"-")==1){break;}
if(solveReturn==0){cycles++;}
//solveReturn returns 0 if sign was not found in the equation.
}
//printf("Befor3e insertion:%s: \n",testptr);
InsertIntoStr(testptr,betweenptr,BracStart,0);
//printf("after insertion:%s: \n",testptr);
//----------eo lahendamine
BracStart = 0;
BracPos = GetSubstrPos(0,teststr,"(");
}
cycles = 1;
//arv = GetFromMemory(memPtr,"vastus");
while (cycles < 5)
{
if(cycles == 1){solveReturn=SolveString(testptr,'*');}
else if(cycles == 2){solveReturn=SolveString(testptr,'/');}
else if(cycles == 3){solveReturn=SolveString(testptr,'+');}
else if(cycles == 4){solveReturn=SolveString(testptr,'-');}
else if(GetSubstrPos(0,teststr,"-")==1){break;}
if(solveReturn==0){cycles++;}
//solveReturn returns 0 if sign was not found in the equation.
}
printf("Vastus On:%s: \n",testptr);
printf("Do you wish to continue [y,n] \n");
getchar();
if(getchar()=='n'){break;}
//------------------------------------
}
//AddIntoMemory(memPtr,"tehe","1001");
//AddIntoMemory(memPtr,"vas*tus","3993");
//AddIntoMemory(memPtr,"arv1","0.32");
//AddIntoMemory(memPtr,"arv3","89.98");
//printf("Memory(%s) \n",Memory);
//printf("Memory query(%f) \n",arv);
//i = GetSubstrPos(0,"Katorlaator v0.1","tor");
//system("PAUSE");
return 0;
}
int GetSubstrPos(int startingPos,char str[100],char substr[50])//returns the index right after the substring found in the string e.g
"hello world","ello" would return 4, position right after o,
{
int i = 0;
int matches=0;//once mathes gets bigger that 0, all other symbols must match too
if (startingPos> strlen(str)){return (-1);}
for (i=startingPos;i < strlen(str);i++)
{
if(str[i]==substr[matches]){matches++;}
else if(matches != 0){matches == 0;}
if (matches == strlen(substr)){return (i+1);}//printf("match at pos: %i :",i+1);
}
return (-1);
}
int StrCpyFromTo(char str[100],char *targetstr[100],int start,int end)//returns the index right after the substring found in the
string e.g "hello world","ello" would return 4, position right after o,
{
int i = 0;
int resptr = start;
char temp[100];
//*targetstr = temp;
if (start> strlen(str)){return (-1);}
for (i=start;( (i < strlen(str))&&(i <= end) );i++)
{
//printf("pre added %i:",i);
*targetstr[resptr] = str[i];
//printf("aft added %i:",i);
resptr++;
}
return (-1);
}
int StrCpyFromToPtrs(char *str,char *targetstr,int start,int end)//returns the index right after the substring found in the string
e.g "hello world","ello" would return 4, position right after o,
{
int i = 0;
int resptr =start;
char temp[100];
//*targetstr = temp;
if (start> strlen(str)){return (-1);}
for (i=start;( (i < strlen(str))&&(i <= end) );i++)
{
//printf("pre added %i:",i);
targetstr[resptr] = str[i];
//printf("aft added %i:",i);
resptr++;
}
return (-1);
}
int InsertIntoStr(char *target,char value[100],int start,int n)
{
//printf("Started FN InsertIntoStr:\n");
char eos[100];//end part of string
int eosPtr=0;
int i;
int cptr=0;
int targetLen=strlen(target);
int valueLen =strlen(value);
//printf("target len is:%i: \n",targetLen);
int valueptr = 0;
if (n==0){n=valueLen-1;}//if 0 is entered, all the string is copied into 1 param
//must keep copyng from target str until found end of strhing char \0
//n=1;
//for (i=start;(i <= start+n);i++)
for (i=start;i <= (targetLen-start)+(start+n);i++)
{
if(i < targetLen)
{
//printf("eos[%i] = :%c: at pos %i\n",cptr,target[i],i);
eos[cptr] = target[i];//copis the char that is gona be shifted to the right
}
if(i <= start+n)
{
// printf("target[%i] = :%c: \n",i,value[cptr]);
target[i] = value[valueptr];valueptr++;
}
cptr++;
}
for (i=0;(i <= cptr-1);i++)
{
//printf("conpying end target[%i] = :%c: \n",(1+start+n)+i,eos[i]);
target[(1+start+n)+i] = eos[i];
}
target[targetLen+n+1] = '\0';
//printf("str copied :%s: \n",target);
//printf("Finished FN InsertIntoStr:\n");
return 1;
}
int FormatData(char *formatedData,char name[100],char val[100],char capsule,char delimiter)//capsule, a character which is added
around each value,delimiter separates name-value pairs
{
int i=0;
//char formatedData[203];//:name;value;:
int memLen = strlen(formatedData);
int nameLen = strlen(name);
int valLen = strlen(val);
int formatedLen = 0;
//char *fpt;
//fpt = &formatedData;
formatedData[0]=delimiter;
int valptr=0;
for (i=1;i <= nameLen;i++)//first add the name part
{
formatedData[i]=name[i-1];
if(i+1==nameLen){formatedData[i+2]=capsule;formatedData[i+3]='\0';}//adds the capsule´s left part
}
formatedLen = strlen(formatedData);
for (i=formatedLen;i < formatedLen+valLen;i++)//first add the name part
{
formatedData[i]=val[valptr];valptr++;
if(i+1==formatedLen+valLen){formatedData[i+1]=capsule;formatedData[i+2]='\0';}//adds the capsule´s left part
}
formatedLen = strlen(formatedData);
formatedData[formatedLen]= delimiter;
formatedData[formatedLen+1]= '\0';
//printf("formated data %s \n",formatedData);
}
int AddIntoMemory(char *mem,char name[100],char value[100])
{
int i=0;
int memLen = strlen(mem);
int nameLen = strlen(name);
int valueLen = strlen(value);
int fdataInd = 0;
char fdata[203];//formated tata, e.g ;name:value:;
char *fdataPtr;
//fdataPtr = &fdata;
fdata[0]='\0';
//FormatData(fdataPtr,name,value,';',':');
for (i=0;i < nameLen;i++)//first add the name part
{
fdata[i]=name[i];
}
for (i=nameLen;i < (nameLen+valueLen);i++)//first add the name part
{
fdata[i]=value[i-nameLen];
}
fdata[nameLen+valueLen]=';';
fdata[(nameLen+valueLen)+1]='\0';
int fdataLen = strlen(fdata);
for (i=memLen;i < (memLen+fdataLen);i++)//first add the name part
{
//printf(" mem[%i]=fdata[%i] \n",i,fdataInd);
mem[i]=fdata[fdataInd];fdataInd++;
if(i+1==(memLen+fdataLen)){mem[i+1]='\0';}//terminates the string
}
}
float GetFromMemory(char *mem,char name[100])
{
int i=0;
int memLen = strlen(mem);
int nameLen = strlen(name);
char res[100];
char *resptr;
resptr = &res;
int fdataInd = 0;
int startInd,endInd;
//printf("len %s \n",name);
//name[nameLen+1]='\0';
nameLen = strlen(name);
//adds the ; at the end of the lookup word
startInd=GetSubstrPos(0,mem,name);//get substr pos returns pos right after the lookup value
endInd=GetSubstrPos(startInd,mem,";");
endInd-=2;
for (i=startInd;i <=endInd;i++)//first add the name part
{
res[i-startInd]=mem[i];
if (i==endInd){res[i-startInd+1]='\0'; }//terminate string
}
if(strlen(res)==0){return (0);}//if not set yet
return atof(res);
}
//4----1--2--3
//10+(5-3)*3/3
//9-3+4/2
int SolveString(char *str,char sign)
{
int i= 0;
int strLen = strlen(str);
char nr1[100],nr2[100],resultAsString[100];
int singFound = 0;
int startPt,finishPt;
int sliceStart,sliceEnd,signPos;
sliceStart=0;
sliceEnd=0;
startPt = 0;
finishPt = 0;
float result = 0.0;
for (i=0;i <strLen;i++)
{
if((str[i]==sign)&&(singFound == 0)&&(sign=='-')&&(startPt == 0) )//special case:if a minus sign is solved and a number
starts with minus
{
nr1[0]='-';startPt=1;
printf("solving - sign and first number starts with -");
}
else if((str[i]==sign)&&(singFound == 0))
{
singFound=1;
signPos=i;
finishPt=0;
}//if a sign Is found
else if((str[i]=='+')||(str[i]=='-')||(str[i]=='*')||(str[i]=='/'))//if some OTHER a sign is found
{
//printf("Found some other sign:%c: \n",str[i]);
if((singFound == 0)&&(str[i]!=sign))
{
if(str[i]=='-')//if a number starts with a -
{
nr1[0]='-';startPt=1;
}
else{startPt=0;}
}
else //we reached finish
{
if( (finishPt == 0)&&(str[i]=='-') ){nr2[0]='-';finishPt=1;}
else{break;}
//break;
}
}
//(5+-10)*10
//else if((str[i]=='\0')){printf("FINISH \n");break;}//if reached finish
else//if a character must be read into either starting part or finishing part
{
if(singFound == 0){nr1[startPt]=str[i];startPt++;}
else //reading input for second part
{
//printf("Added:%c:to nr 2 \n",str[i]);
nr2[finishPt]=str[i];finishPt++;
}
}
}
sliceStart = (signPos-startPt);
sliceEnd = (signPos+finishPt);
//printf("sliceStart=%i \n",sliceStart);
//printf("sliceEnd =%i \n",sliceEnd);
nr1[startPt]='\0';nr2[finishPt]='\0';//terminates both strings
printf("str solved is=%s:and sign solved is %c\n",str,sign);
printf("nr1=%s \n",nr1);
printf("nr2=%s \n",nr2);
if(sign == '+'){result = atof(nr1)+atof(nr2);}
else if(sign == '-'){result = atof(nr1)-atof(nr2);}
else if(sign == '*'){result = atof(nr1)*atof(nr2);}
else if(sign == '/'){result = atof(nr1)/atof(nr2);}
if(singFound !=0)
{
CutFromStr(str,sliceStart,sliceEnd);//removes the part that was calculated from the big string
snprintf(resultAsString,100,"%f",result);//converts the float result into string
printf("bef insertion:%s: \n",str);
if(result==0){InsertIntoStr(str,"-0",sliceStart,0);}
else{InsertIntoStr(str,resultAsString,sliceStart,0);}//adds the new value back into the big string
printf("after insertion:%s: \n",str);
}
//printf("StringSolvedTO=%s \n",str);
if(startPt==0){return 0;}
return singFound;//returns 0 if the given sign was not found in the string
}
int CutFromStr(char *str,int start,int end)
{
int i= 0;
char endPart[100];
int strLen = strlen(str);
int endPartCounter=0;
int endPtLen = 0;
//int replaceLen = str(replace);
//123456789
//copy the end part of the string into temp memory
if((strLen-1)>=(end+1))//if str is gut to the end, there is no need to copy anything
{
for ( (i=end+1);i <strLen;i++)
{
endPart[endPartCounter]=str[i];endPartCounter++;
}
endPart[endPartCounter]='\0';
}
else{str[start]='\0';return 1;}//printf("cut to end str IS=%s \n",str);
//replaces the deltable part with the end part, that collapses in. terminates with \0
endPtLen = endPartCounter;
//printf("endPtLen IS=%i \n",endPtLen);
endPartCounter=0;
for (i=start;i <start+endPtLen;i++)
{
//printf("str[%i]=endPart[%c]\n",i,endPart[endPartCounter]);
str[i]=endPart[endPartCounter];endPartCounter++;
}
str[(start+endPtLen)]='\0';
//printf("cut str IS=%s \n",str);
}
int GetBetweenSymbols(char *str,char *result,char symbol1,char symbol2)
{
int i = 0;
int strLen = strlen(str);
int foundS1=0;
int foundS2=0;
int resCounter = 0;
int startingPt = 0;
int finishingPt = 0;
for (i=0;i <strLen;i++)
{
//printf("str[%i]=endPart[%c]\n",i,endPart[endPartCounter]);
if( (str[i]==symbol2)&&(foundS1 == 1) )
{
foundS2 = 1;
finishingPt = i;
return startingPt;
}
if( (foundS1 == 1)&&(foundS2 == 0) )
{
result[resCounter]=str[i];result[resCounter+1]='\0';
resCounter++;
//printf("result[%i]=str[%c] \n",resCounter,str[i]);
}
if(str[i]==symbol1){foundS1 = 1;resCounter=0;startingPt=i;}
}
return 0;
}
int Validate(char str[400],char validChars[20])
{
int j,i = 0;
int strLen = strlen(str);
int validCharsLen = strlen(validChars);
int error = 0;
for (i=0;i <strLen;i++)
{
error = 1;
for (j=0;j <validCharsLen;j++)
{
if(str[i]==validChars[j]){error = 0;}
}
if(error == 1){return 0;}
}
return 1;
}
int CountCharsInStr(char str[400],char symbol)//counts how many of the given characters tehre are in a str
{
int i=0;
int symbols=0;
int strLen = strlen(str);
for (i=0;i <strLen;i++)
{
if(str[i]==symbol){symbols++;}
}
return symbols;
}
Thursday, September 17, 2009
Monday, September 14, 2009
SSH-Secure Shell
SSH on programmide kogumik, mis kasutavad avaliku ja privaat võtme tehnoloogiat andmete edastamisel üle võrgu.
SSH kasutamine:
1) SSH kasutamiseks tuleb see kõigepealt paigaldada nii kliendi, kui ka server arvutisse.
2) Igas arvutis on üks avaliku ja privaatse võtme paar.
3) Kliendi avaliku võtme võib panna /etc/ssh_known_hosts faili. Server kasutab seda avalikku võtit kliendi hilisemaks tuvastamiseks
4)Kui kliendi avalik võti(mis asub kliendi arvutis, kaustas /etc/ssh_host_key.pub)
leidub ka serveris,failis /etc/ssh_known_hosts, Ning kui sa paned Host´i nime faili .rhosts või .shosts, avatakse paroolivaba ühendus
5) Kui kliendi avalikku võtit ei leita serverist, küsitakse kasutajal sisselogimiseks parooli. Parooli sisestamisel see krüpteeritakse ning edastatakse serverisse, kus toimub kasutaja parooli põhjal autentimine.
-------------------------------------------------------------------------------
Igal kasutaja kontol on ka oma võtmepaar, mis võimaldab seda konkreetsed kontod autentida(ilma paroolita sisse logida). Võtme saamiseks peab kasutaja käivitama prgrammi ssh-keygen,mis tekitab kausta $HOME/.ssh ning genereerib sinna avaliku/privaat võtme paari.
Seejärel tuleb see selle võtmepaari avalik võti($HOME/.ssh/identity.pub) kopeerida serverisse, faili nimega $HOME/.ssh/authorized_keys
rohkem infot SSH kohta
http://www.openssh.com/
ftp://ftp.lysator.liu.se/pub/security/lsh/
windowsi masinatele on soovitatav kas OpenSSH või
http://staff.washington.edu/dittrich/misc/ssh/
SSH kasutamine:
1) SSH kasutamiseks tuleb see kõigepealt paigaldada nii kliendi, kui ka server arvutisse.
2) Igas arvutis on üks avaliku ja privaatse võtme paar.
3) Kliendi avaliku võtme võib panna /etc/ssh_known_hosts faili. Server kasutab seda avalikku võtit kliendi hilisemaks tuvastamiseks
4)Kui kliendi avalik võti(mis asub kliendi arvutis, kaustas /etc/ssh_host_key.pub)
leidub ka serveris,failis /etc/ssh_known_hosts, Ning kui sa paned Host´i nime faili .rhosts või .shosts, avatakse paroolivaba ühendus
5) Kui kliendi avalikku võtit ei leita serverist, küsitakse kasutajal sisselogimiseks parooli. Parooli sisestamisel see krüpteeritakse ning edastatakse serverisse, kus toimub kasutaja parooli põhjal autentimine.
-------------------------------------------------------------------------------
Igal kasutaja kontol on ka oma võtmepaar, mis võimaldab seda konkreetsed kontod autentida(ilma paroolita sisse logida). Võtme saamiseks peab kasutaja käivitama prgrammi ssh-keygen,mis tekitab kausta $HOME/.ssh ning genereerib sinna avaliku/privaat võtme paari.
Seejärel tuleb see selle võtmepaari avalik võti($HOME/.ssh/identity.pub) kopeerida serverisse, faili nimega $HOME/.ssh/authorized_keys
rohkem infot SSH kohta
http://www.openssh.com/
ftp://ftp.lysator.liu.se/pub/security/lsh/
windowsi masinatele on soovitatav kas OpenSSH või
http://staff.washington.edu/dittrich/misc/ssh/
CHROOT
chroot on linux´i käsklus mis muudab juurkataloogi asukohta.
juurkataloogi muutmine on kasulik turvalisuse eesmärkidel. Kasutajad/rakendused ei saa muuta faile mis asuvad nende juurkataloogist kõrgemal.
chroot /home/kasutaja
pärast sellise käsu andmist kõik viited "/" tähendavad /home/kasutaja kataloogi.
nt käsklus vi /tekstifail, tekitab faili kausta /home/kasutaja/tekstifail
juurkataloogi muutmine on kasulik turvalisuse eesmärkidel. Kasutajad/rakendused ei saa muuta faile mis asuvad nende juurkataloogist kõrgemal.
chroot /home/kasutaja
pärast sellise käsu andmist kõik viited "/" tähendavad /home/kasutaja kataloogi.
nt käsklus vi /tekstifail, tekitab faili kausta /home/kasutaja/tekstifail
Monday, September 7, 2009
linuxi kataloogid
/etc asuvad kõikide programmide häälestusfailid. nt LAMP
/root root kasutaja kodu kataloog
/bin operatsiooni süsteemi käsufailid nagu rm,ls,
/sbin operatsiooni süsteemi välised käsufalid.
/usr/src lähtetekstid. siia on kasulik tõmmata lähtekoodid
/usr/local on kasutaja poolt installitud programmid.
/usr/local/etc installitud progede confid
/tmp ´lon ära määratud sticky bit
http://www.openbsd.org/faq/faq4.html#Install
view installed
ps(1); kill(1); killall(1); pkill(1); pgrep(1)
PS1---post script. teha käsuviiba prompt
/root root kasutaja kodu kataloog
/bin operatsiooni süsteemi käsufailid nagu rm,ls,
/sbin operatsiooni süsteemi välised käsufalid.
/usr/src lähtetekstid. siia on kasulik tõmmata lähtekoodid
/usr/local on kasutaja poolt installitud programmid.
/usr/local/etc installitud progede confid
/tmp ´lon ära määratud sticky bit
http://www.openbsd.org/faq/faq4.html#Install
view installed
ps(1); kill(1); killall(1); pkill(1); pgrep(1)
PS1---post script. teha käsuviiba prompt
Change DHCP ip to StaticIP
http://www.howtogeek.com/howto/ubuntu/change-ubuntu-server-from-dhcp-to-a-static-ip-address/
Saturday, September 5, 2009
ISO-OSI Seansi Kiht.
Seansikiht
Seansikiht on ISO-OSI mudeli 5´s kiht mis tegeleb seansside alustamise, jälgimise ning lõpetamisega. Seansse kasutavad paljud lõpp-kasutaja programmid üle võrgu suhtlemisel.
Seanss koosneb rakenduste poolt saadetavatest ning vastu võetavatest päringutest.
Seansikiht kasutab võrgus suhtlemiseks erinevaid protokolle. Üheks selliseks protokolliks on „Session Layer Protocol” ,(ISO 8327), mis interneti ühenduse katkedes võib üritada ühenduse taasloomist. Kui ühendust pole pikka aega kasutatud võib see protokoll ühenduse sulgeda. Seansi protokoll vastutab ka ühenduse sünkroniseerimise eest.
Seansikihi protokollid:
Seansikiht on ISO-OSI mudeli 5´s kiht mis tegeleb seansside alustamise, jälgimise ning lõpetamisega. Seansse kasutavad paljud lõpp-kasutaja programmid üle võrgu suhtlemisel.
Seanss koosneb rakenduste poolt saadetavatest ning vastu võetavatest päringutest.
Seansikiht kasutab võrgus suhtlemiseks erinevaid protokolle. Üheks selliseks protokolliks on „Session Layer Protocol” ,(ISO 8327), mis interneti ühenduse katkedes võib üritada ühenduse taasloomist. Kui ühendust pole pikka aega kasutatud võib see protokoll ühenduse sulgeda. Seansi protokoll vastutab ka ühenduse sünkroniseerimise eest.
Seansikihi protokollid:
• ADSP, AppleTalk Data Stream Protocol
• ASP, AppleTalk Session Protocol
• H.245, Call Control Protocol for Multimedia Communication
• ISO-SP, OSI Session Layer Protocol (X.225, ISO 8327)
• iSNS, Internet Storage Name Service
• L2F, Layer 2 Forwarding Protocol
• L2TP, Layer 2 Tunneling Protocol
• NetBIOS, Network Basic Input Output System
• PAP, Password Authentication Protocol
• PPTP, Point-to-Point Tunneling Protocol
• RPC, Remote Procedure Call Protocol
• RTCP, Real-time Transport Control Protocol
• SMPP, Short Message Peer-to-Peer
• SCP, Secure Copy Protocol
• SSH, Secure Shell
• ZIP, Zone Information Protocol
• SDP, Sockets Direct Protocol
*http://en.wikipedia.org/wiki/Session_Layer
Friday, September 4, 2009
Kalkulaator v0.02
#include <>
#include <>
#include <>
#include <>
//binary nr is negative if it starts with 0
int getEquation(char str[100],float *t1,float *t2,char *sng,int *inv);
//int86(int intrptnum, struct REGS *regs, struct REGS *regs);
//int86x(int intrptnum, struct REGS *inregs, struct REGS *outregs, struct SSREGS *sregs);
int main(int argc, char *argv[])
{
float a1,a2,res;
char tehe[5];
char sign='x';
char *signpt;
float *aipt,*aipt2,*invpt;
int loc;
int Finished = 0;
int ScriptRunLimit=1000;
char function[4];
function[0]='x';//sets default function to x, to check it later
char tempc;
int invalid=0;//if the equation contains some illegal letters
aipt = &a1;
aipt2 = &a2;
signpt = &sign;
invpt = &invalid;
printf("C Kalkulaator:Rainer Plumer v0.02 \n");
while ( (Finished == 0)||(ScriptRunLimit ==0) )
{
a1=0;
a2=0;
res = 0;
if(invalid == 0)
{
sign='x';signpt = &sign;
printf("enter the equation \n");
printf("operators supported: +,-,*,/,sin(),cos(),tan(),sqrt(), \n");
scanf("%s",&tehe);
//loc = strlen(tehe);
getEquation(tehe,aipt,aipt2,signpt,invpt);
printf("sign is:%f \n",*aipt);
if(sign == '+'){res = a1+a2;}
else if(sign == '-'){res = (a1-(a2));}
else if(sign == '*'){res = a1*a2;}
else if(sign == '/'){res = a1/a2;}
else {res = a1;printf("USED FN \n");}
printf("running a1 %f \n",a1);printf("running a2 %f \n",a2);
printf("%f \n",res);
printf("Press q to exit or any other key to continue \n");
getchar();//this one gets rid of the enter key pressed
tempc = getchar();
}
else
{
invalid = 0;
}
if (tempc == 'q'){Finished = 1;}
;
ScriptRunLimit--;
}
getchar();
return 0;
}
int getEquation(char str[100],float *t1,float *t2,char *sng,int *inv)//returns the position of the first occurance of the given character param.
{
int i=0;
float a;
char static ValindNumbers[17] = {'0','1','2','3','4','5','6','7','8','9','.','-','(',')','+','/','*'};
// char static InvalidChars[]= {'0','1','2','3','4','5','6','7','8','9','.','-','(',')','+','/','*'};
int signPos = 0;
int nr1,nr2,n1pt,n2pt;
int funPt,nrPt;
char temp1[100],temp2[100];
char sign='x';
int isSign = 0;
char *funLoc = 'x';
char fun[4];
int neg=0;
int stage=0;//0 looks chars until the '(' sign, 1 looks chars until ')'
n1pt = 0;
n2pt = 0;
funLoc = strpbrk(str,"+-*/");
printf("strcspn(str,ValindNumbers):%i: \n",strspn(str,ValindNumbers));
if( ( strspn(str,ValindNumbers) != (strlen(str)))&&( (strstr(str,"cos(")==NULL)&&(strstr(str,"sin(")==NULL)&&(strstr(str,"tan(")==NULL) &&(strstr(str,"sqrt(")==NULL) ) )
{printf("ERROR:INVALID CHARACTER ENTERED: \n");*inv=1;return 0;}
else if (funLoc!=NULL)
{
printf("str is :%s: \n",str);
printf("tehe:%f %c %f \n",*t1,*sng,*t2);
for (i=0;i <= strlen(str);i++)
{
if((str[i]=='-')&&(i==0))//check if number starts with - sign
{
neg=1;//negative is true
temp1[0]=str[0];n1pt++;
printf("first number is negative");
}
else
{
if(isSign==0)
{
if(str[i]=='+'){sign='+';signPos = i;}
else if(str[i]=='-'){sign='-';signPos = i;}
else if(str[i]=='*'){sign='*';signPos = i;}
else if(str[i]=='/'){sign='/';signPos = i;}
}
if(sign == 'x'){temp1[n1pt]=str[i];n1pt++;}
else
{
if(isSign==1){temp2[n2pt]=str[i];n2pt++;}
isSign=1;//gets did of the sign mark
}
}
}
printf("str is:%s: to temp2:%f:\n",temp2,atof(temp2));
*t1 = atof(temp1);
*t2 = atof(temp2);
*sng = sign;
// if(neg==1){*t1=*t1*(-1);}
}
else
{
printf("Making function");
funPt=0;
nrPt =-1;
for (i=0;i < strlen(str);i++)
{
//look for first non valid number, thats the
if(str[i]=='('){stage=1;}
if(stage==0){fun[funPt]=str[i];funPt++;}//gets the function part, e.g cos sin sqrt tan
else if (stage == 1)
{
if(nrPt!=(-1)){temp1[nrPt]=str[i];}
nrPt++;
}
if(str[i]==')')
{
stage=2;
*t1 = atof(temp1);
if((fun[0]=='c')&&(fun[1]=='o')&&(fun[2]=='s')){*sng='x';*t1=cos((*t1*3.14159)/180);}
else if((fun[0]=='s')&&(fun[1]=='i')&&(fun[2]=='n')){*sng='x';*t1=sin((*t1*3.14159)/180);}
else if((fun[0]=='t')&&(fun[1]=='a')&&(fun[2]=='n')){*sng='x';*t1=tan((*t1*3.14159)/180);}
else if((fun[0]=='s')&&(fun[1]=='q')&&(fun[2]=='r')&&(fun[3]=='t')){*sng='x';*t1=sqrt(*t1);}
}
}
}
return 1;
}
#include <>
#include <>
#include <>
//binary nr is negative if it starts with 0
int getEquation(char str[100],float *t1,float *t2,char *sng,int *inv);
//int86(int intrptnum, struct REGS *regs, struct REGS *regs);
//int86x(int intrptnum, struct REGS *inregs, struct REGS *outregs, struct SSREGS *sregs);
int main(int argc, char *argv[])
{
float a1,a2,res;
char tehe[5];
char sign='x';
char *signpt;
float *aipt,*aipt2,*invpt;
int loc;
int Finished = 0;
int ScriptRunLimit=1000;
char function[4];
function[0]='x';//sets default function to x, to check it later
char tempc;
int invalid=0;//if the equation contains some illegal letters
aipt = &a1;
aipt2 = &a2;
signpt = &sign;
invpt = &invalid;
printf("C Kalkulaator:Rainer Plumer v0.02 \n");
while ( (Finished == 0)||(ScriptRunLimit ==0) )
{
a1=0;
a2=0;
res = 0;
if(invalid == 0)
{
sign='x';signpt = &sign;
printf("enter the equation \n");
printf("operators supported: +,-,*,/,sin(),cos(),tan(),sqrt(), \n");
scanf("%s",&tehe);
//loc = strlen(tehe);
getEquation(tehe,aipt,aipt2,signpt,invpt);
printf("sign is:%f \n",*aipt);
if(sign == '+'){res = a1+a2;}
else if(sign == '-'){res = (a1-(a2));}
else if(sign == '*'){res = a1*a2;}
else if(sign == '/'){res = a1/a2;}
else {res = a1;printf("USED FN \n");}
printf("running a1 %f \n",a1);printf("running a2 %f \n",a2);
printf("%f \n",res);
printf("Press q to exit or any other key to continue \n");
getchar();//this one gets rid of the enter key pressed
tempc = getchar();
}
else
{
invalid = 0;
}
if (tempc == 'q'){Finished = 1;}
;
ScriptRunLimit--;
}
getchar();
return 0;
}
int getEquation(char str[100],float *t1,float *t2,char *sng,int *inv)//returns the position of the first occurance of the given character param.
{
int i=0;
float a;
char static ValindNumbers[17] = {'0','1','2','3','4','5','6','7','8','9','.','-','(',')','+','/','*'};
// char static InvalidChars[]= {'0','1','2','3','4','5','6','7','8','9','.','-','(',')','+','/','*'};
int signPos = 0;
int nr1,nr2,n1pt,n2pt;
int funPt,nrPt;
char temp1[100],temp2[100];
char sign='x';
int isSign = 0;
char *funLoc = 'x';
char fun[4];
int neg=0;
int stage=0;//0 looks chars until the '(' sign, 1 looks chars until ')'
n1pt = 0;
n2pt = 0;
funLoc = strpbrk(str,"+-*/");
printf("strcspn(str,ValindNumbers):%i: \n",strspn(str,ValindNumbers));
if( ( strspn(str,ValindNumbers) != (strlen(str)))&&( (strstr(str,"cos(")==NULL)&&(strstr(str,"sin(")==NULL)&&(strstr(str,"tan(")==NULL) &&(strstr(str,"sqrt(")==NULL) ) )
{printf("ERROR:INVALID CHARACTER ENTERED: \n");*inv=1;return 0;}
else if (funLoc!=NULL)
{
printf("str is :%s: \n",str);
printf("tehe:%f %c %f \n",*t1,*sng,*t2);
for (i=0;i <= strlen(str);i++)
{
if((str[i]=='-')&&(i==0))//check if number starts with - sign
{
neg=1;//negative is true
temp1[0]=str[0];n1pt++;
printf("first number is negative");
}
else
{
if(isSign==0)
{
if(str[i]=='+'){sign='+';signPos = i;}
else if(str[i]=='-'){sign='-';signPos = i;}
else if(str[i]=='*'){sign='*';signPos = i;}
else if(str[i]=='/'){sign='/';signPos = i;}
}
if(sign == 'x'){temp1[n1pt]=str[i];n1pt++;}
else
{
if(isSign==1){temp2[n2pt]=str[i];n2pt++;}
isSign=1;//gets did of the sign mark
}
}
}
printf("str is:%s: to temp2:%f:\n",temp2,atof(temp2));
*t1 = atof(temp1);
*t2 = atof(temp2);
*sng = sign;
// if(neg==1){*t1=*t1*(-1);}
}
else
{
printf("Making function");
funPt=0;
nrPt =-1;
for (i=0;i < strlen(str);i++)
{
//look for first non valid number, thats the
if(str[i]=='('){stage=1;}
if(stage==0){fun[funPt]=str[i];funPt++;}//gets the function part, e.g cos sin sqrt tan
else if (stage == 1)
{
if(nrPt!=(-1)){temp1[nrPt]=str[i];}
nrPt++;
}
if(str[i]==')')
{
stage=2;
*t1 = atof(temp1);
if((fun[0]=='c')&&(fun[1]=='o')&&(fun[2]=='s')){*sng='x';*t1=cos((*t1*3.14159)/180);}
else if((fun[0]=='s')&&(fun[1]=='i')&&(fun[2]=='n')){*sng='x';*t1=sin((*t1*3.14159)/180);}
else if((fun[0]=='t')&&(fun[1]=='a')&&(fun[2]=='n')){*sng='x';*t1=tan((*t1*3.14159)/180);}
else if((fun[0]=='s')&&(fun[1]=='q')&&(fun[2]=='r')&&(fun[3]=='t')){*sng='x';*t1=sqrt(*t1);}
}
}
}
return 1;
}
Converting Strings
converting string to int
to use the atoi function you must include the stdlib.h
#include
int num;
char text[5]= {'1','9','8','2','3'};
num = atoi(text);
------------------------------------------------------
converting string to float
char test[5]={'2','3','4','.','2'};
float number;
number= atof(test);
printf("number value is: %f\n",number);//output is "234.199997"
to use the atoi function you must include the stdlib.h
#include
int num;
char text[5]= {'1','9','8','2','3'};
num = atoi(text);
------------------------------------------------------
converting string to float
char test[5]={'2','3','4','.','2'};
float number;
number= atof(test);
printf("number value is: %f\n",number);//output is "234.199997"
Thursday, September 3, 2009
C programming.
reference link
http://www.elook.org/programming/c/stdio.html
string functions
http://www.edcc.edu/faculty/paul.bladek/c_string_functions.htm
scanf() parameters
-------------------
-------------------
reference link
http://www.elook.org/programming/c/stdio.html
string functions
http://www.edcc.edu/faculty/paul.bladek/c_string_functions.htm
scanf() parameters
-------------------
%c a single character
%d a decimal integer
%i an integer
%e, %f, %g a floating-point number
%o an octal number
%s a string
%x a hexadecimal number
%p a pointer
%n an integer equal to the number of characters read so far
%u an unsigned integer
%[] a set of characters
%% a percent sign
-------------------
Wednesday, September 2, 2009
Tuesday, September 1, 2009
Vkhk Kodutööde blogi
Mida tean süsteem progemisest?
Süsteemprogrammeerimine arvuti riistvara kasutamiseks programmi kirjutamine.
Näiteks arvuti draiverite programmeerimine, printeri tarkvara jms programmid mis mis suhtlevad arvuti riistvaraga.
Süsteemprogrammeerimiseks kasutatakse peamiselt madala taseme programmeerimis keeli.
Võimaldab programmeerida osa programmist assembleris.
Võrreldes kõrgema taseme programmeerimis keeltega on madala taseme programmeerimis keeled kiiremad.
Süsteemprogrammeerimine arvuti riistvara kasutamiseks programmi kirjutamine.
Näiteks arvuti draiverite programmeerimine, printeri tarkvara jms programmid mis mis suhtlevad arvuti riistvaraga.
Süsteemprogrammeerimiseks kasutatakse peamiselt madala taseme programmeerimis keeli.
Võimaldab programmeerida osa programmist assembleris.
Võrreldes kõrgema taseme programmeerimis keeltega on madala taseme programmeerimis keeled kiiremad.
Subscribe to:
Posts (Atom)