[Linux-programlama] Re: gcc altında strcmp - gets gibi string fonksiyonlarının kullanımı.

---------

[Linux-programlama] Re: gcc altında strcmp - gets gibi string fonksiyonlarının kullanımı.

From: Ustun ERGENOGLU <ustun.ergenoglu_at_gmail.com>
Date: Thu, 10 Jan 2008 00:57:55 +0200
Message-ID: <681812960801091457k6cb944f7pf25fb104e9088d4e@mail.gmail.com>

              while(comand[i]!=32){
                cmd[i]=comand[i];
                i++;
              }

programýnýz yukarýda yazdýðým kýsýmda segmentation fault veriyor.
yukarýdaki döngüde string'in uzunluðunu kontrol etmediðiniz için,
stringde de boþluk karakteri olmadýðý için döngü stringin dýþýna
çýkýyor, sonrasýnda da segmentation fault. gdb kullanýrsanýz bu gibi
hatalarýn nasýl oluþtuðunu görebilirsiniz. m. ali vardar'ýn
www.linuxprogramlama.com/documents/gdb.pdf adresinde güzel bir türkçe
gdb kýlavuzu var.

üstün

09.01.2008 tarihinde YILDIRIM <linux_at_isadamlari.org> yazmýþ:
>
> Ustun ERGENOGLU wrote:
> nasýl kullandýðýnýzý belirtirseniz, yardýmcý olacak birileri çýkabilir.
>
> üstün
>
> 09.01.2008 tarihinde YILDIRIM <linux_at_isadamlari.org> yazmýþ:
>
>
> Linux altýnda gcc kullanarak derleme yaptýðýmda string.h kütüphanesinde
> bulunan gets(), strcmp() gibi string fonksiyonlarýndan kaynaklanan
> "segmentation fault" hatalarý alýyorum. Bu problemi çözmenin bir yolu
> var mýdýr?
> Þimdiden yardýmcý olan arkadaþlara teþekkür ederim.
>
> Mehmet Salih YILDIRIM
> _______________________________________________
> Linux-programlama mailing list
> Linux-programlama_at_liste.linux.org.tr
> http://liste.linux.org.tr/mailman/listinfo/linux-programlama
>
>
> _______________________________________________
> Linux-programlama mailing list
> Linux-programlama_at_liste.linux.org.tr
> http://liste.linux.org.tr/mailman/listinfo/linux-programlama
>
>
> Aþaðýda bi programýn þablonu olarak hazýrladýðým kodlar bulunmakta.
> Arkadaþým windows altýnda bir derleyiciyle (dev c) derlediðinde sorunsuz
> çalýþýyor. Fakat Linux altýnda gcc ile derlediðimde derleme baþarýlý oluyor
> fakat "help" komutu gibi komutlarý verdiðimde "segmentation fault" verip
> programý sonlandýrýyor. Sorunun string.h kütüphanesindeki fonksiyonlardan
> kaynaklandýðýný düþünüyorum. Ýlgilenen herkese teþekkür ederim.
>
> Mehmet Salih YILDIRIM
>
>
> <code>
>
> // prevent program crash when using commands with no
> parameters or with not expected value of parameters //
> // for whoami and logout it is not necacery to define them as
> a "program" //
>
>
>
> // ANA PROGRAM //
>
> #include <stdio.h>
> //#include <stdlib.h>
> #include <string.h>
>
>
>
> static char actvusr[20]="testuser";
>
>
> int main () {
>
> char pathp[20];
> char param2[20];
> char param1[20];
> char cmd[20];
> char comand[20];
> int i,j,k;
>
> printf("\n\n\n Welcome FS-1a \n");
> printf("\n by A.Egitmen & M.S.Yildirim\n\n\n");
> printf("\n For instructions type 'help'\n\n\n");
>
>
>
> do {
> printf("/%s>",actvusr);
> gets(comand);
> i=0;
> j=0;
> k=0;
> while(comand[i]!=32){
> cmd[i]=comand[i];
> i++;
> }
> cmd[i]='\0';
> i++;
>
>
> //______________________________________________________________________________________________________________
>
>
> /*help*/ if(!strcmp(cmd,"help")){
>
> printf("\n Login Usage : login
> 'username'\n ");
> printf("\n Dir Usage : dir
> 'pathname'\n");
> printf("\n Copy Usage : cp
> 'source''FILENAME' 'destination''FILENAME'\n");
>
> printf("\n Delete Usage : del
> 'source2''FILENAME'\n");
> printf("\n ReaderList Usage : rl\n" );
> printf("\n Receive Usage : receive
> 'FILENAME'\n");
> printf("\n Send Usage : send 'FILENAME'
> 'USERNAME'\n");
> printf("\n Whoami Usage : whoami\n");
> printf("\n Setpass Usage : setp\n");
> printf("\n Logout Usage : logout\n");
> printf("\n Exit Usage : exit\n");
>
>
> }
>
> //_____________________________________________________________________________________________________________
>
>
> /*login*/ else if(!strcmp(cmd,"login")){
> if(strcmp(actvusr,"\0")){
> printf("\n You Have Already Logged \n");
> }
> else{
>
> while(comand[i]!=32){
> param1[j]=comand[i];
> i++;
> j++;
> }
> param1[j]='\0';
>
> printf("\n command : %s\n",cmd );
> printf("\n param1 : %s\n",param1);
> // - login call - if pass match return =>
> actvuser=username
> // write system date to lastlogin.txt
> // find username from actvuser.dat and write 1 to there
> // you will rechange it to 0 when logout
> }
> }
>
> //___________________________________________________________________________________________________________
>
> /*dir*/ else if(!strcmp(cmd,"dir")){
> if(!strcmp(actvusr,"\0")){
> printf("\n Please Login Before Using Commands\n");
> }
> else{
> while(comand[i]!=32){
> param1[j]=comand[i];
> i++;
> j++;
> }
> param1[j]='\0';
> printf("\n command : %s\n",cmd );
> printf("\n param1 : %s\n",param1);
>
> // call dir , print files while in dir; return
> void
> }
>
> }
>
> //_____________________________________________________________________________________________________________
>
>
> /*COPY*/ else if(!strcmp(cmd,"cp")){
> if(!strcmp(actvusr,"\0")){
> printf("\n Please Login Before Using
> Commands\n");
> }
> else{
>
> while(comand[i]!=32){
> param1[j]=comand[i];
> i++;
> j++;
> }
> param1[j]='\0';
>
> i++;
> while(comand[i]!=32){
> param2[k]=comand[i];
> i++;
> k++;
> }
> param2[k]='\0';
> printf("\n command : %s\n",cmd );
> printf("\n param1 : %s\n",param1);
> printf("\n param2 : %s\n",param2);
> i=0;
> while(param1[i]!=47){
> pathp[i]=param1[i];
> i++;
> }
> pathp[i]='\0';
>
> printf("\n path : %s\n",pathp);
>
> //get user id from uid ; with uid get supervisor
> information from users.dat
> //if ok then call cp
> //if not get read permision for pathp from permis.txt
> //if ok call cp ;
> //if not cancel it
> //--call cp-- return nothing//
>
> }
> }
>
> //_____________________________________________________________________________________________________________
>
>
> /*DELETE*/ else if(!strcmp(cmd,"del")){
> if(!strcmp(actvusr,"\0")){
> printf("\n Please Login Before Using
> Commands\n");
> }
> else{
> while(comand[i]!=32){
> param1[j]=comand[i];
> i++;
> j++;
> }
> param1[j]='\0';
> printf("\n command : %s\n",cmd );
> printf("\n param1 : %s\n",param1);
> while(param1[i]!=47){
> pathp[i]=param1[i];
> i++;
> }
> pathp[i]='\0';
>
> printf("\n path : %s\n",pathp);
> //get user id from uid ; with uid get supervisor
> information from users.dat
> //if ok then call del
> //if not get read permision for pathp from permis.txt
> //if ok call del ;
> //if not cancel it
> //--call del-- return nothing//
>
> }
> }
>
> //_____________________________________________________________________________________________________________
>
>
> /*ReaderList*/ else if(!strcmp(cmd,"rl")){
> if(!strcmp(actvusr,"\0")) {
> printf("\n Please Login Before Using Commands\n");
> }
> else{
>
> //call rl ; in rl open spool/username/files.txt
> //read it line by line write it and return void
>
> }
> }
>
> //_______________________________________________________________________________________________________________
>
> /*Whoami*/ else if(!strcmp(cmd,"whoami")){
> if(!strcmp(actvusr,"\0")){
> printf("\nUnidentified Person\n");
> }
> else{
> printf("\n %s \n",actvusr);
> }
> }
>
> //_______________________________________________________________________________________________________________
>
> /*Logout*/ else if(!strcmp(cmd,"logout")){
> if(!strcmp(actvusr,"\0")){
> printf("\n You Are Not Seems to be Logged\n");
> }
> else{
> printf("\n Thank You For Using FS-1a Please Contact
> alper_egitmen_at_hotmail.com For Bugs \n" );
> actvusr[0]='\0';
>
> }
>
> }
>
> //________________________________________________________________________________________________________________
>
>
> /*SEND*/ else if(!strcmp(cmd,"send")){
> if(!strcmp(actvusr,"\0")) {
> printf("\n Please Login Before Using Commands\n");
> }
> else{
> while(comand[i]!=32){
> param1[j]=comand[i];
> i++;
> j++;
> }
> param1[j]='\0';
>
> i++;
> while(comand[i]!=32){
> param2[k]=comand[i];
> i++;
> k++;
> }
> param2[k]='\0';
> printf("\n command : %s\n",cmd );
> printf("\n param1 : %s\n",param1);
> printf("\n param2 : %s\n",param2);
>
> }
>
>
> // call send without any permission check
>
> }
>
>
>
> //_________________________________________________________________________________________________________________
>
>
>
> /*Receive*/ else if(!strcmp(cmd,"receive")) {
> if(!strcmp(actvusr,"\0")) {
> printf("\n Please Login Before Using Commands\n");
> }
> else{
> while(comand[i]!=32){
> param1[j]=comand[i];
> i++;
> j++;
> }
> param1[j]='\0';
> strcpy(param2,actvusr);
>
> printf("\n command : %s\n",cmd );
> printf("\n param1 : %s\n",param1);
> printf("\n param2 : %s\n",param2);
>
> //call receive without any permission chech
>
>
> }
>
> }
>
>
> //________________________________________________________________________________________________________________
>
>
>
>
> /* SETPASS*/ else if(!strcmp(cmd,"setp")){
> if(!strcmp(actvusr,"\0")) {
> printf("\n Please Login Before Using Commands\n");
> }
> else{
>
> //get id from uid , using id get information
> "setpass" from users.dat
> //if ok ; than as for old pass
> //if ok ask new pass
> //change pass with using id from passwd.dat
> }
>
> }
>
> //_______________________________________________________________________________________________________________
>
> // commands ends //
>
> else if(!strcmp(cmd,"\0"));
>
> else
> printf("\n\n Unknown Command\n\n");
>
>
> }
>
>
>
>
>
>
>
> while(strcmp(cmd,"exit"));
>
>
>
>
> return 0;
>
> }
>
> </code>
>
> _______________________________________________
> Linux-programlama mailing list
> Linux-programlama_at_liste.linux.org.tr
> http://liste.linux.org.tr/mailman/listinfo/linux-programlama
>
>
_______________________________________________
Linux-programlama mailing list
Linux-programlama_at_liste.linux.org.tr
http://liste.linux.org.tr/mailman/listinfo/linux-programlama
Received on Thu 10 Jan 2008 - 00:25:39 EET

---------

Bu arsiv hypermail 2.2.0 tarafindan uretilmistir.