Exemplo n.º 1
0
int main(void)
{
    char s[MAXSIZE] = "abc";
    char t[MAXSIZE] = "abracadabra";
    mystrncpy(s, t, 6);
    printf("%s\n", s);

    strncpy(t, "", MAXSIZE);
    mystrncpy(s, t, 6);
    printf("%s\n", s);

    strncpy(s, "foo", MAXSIZE);
    strncpy(t, "bar", MAXSIZE);
    mystrncat(s, t, 5);
    printf("%s\n", s);

    strncpy(s, "foobar", MAXSIZE);
    strncpy(t, "foobar", MAXSIZE);
    printf("%d\n", mystrncmp(s, t, 10));
    printf("%d\n", mystrncmp(s, t, 4));

    strncpy(t, "fooby", MAXSIZE);
    printf("%d\n", mystrncmp(s, t, 10));
    printf("%d\n", mystrncmp(s, t, 4));
    printf("%d\n", mystrncmp(s, t, 5));
    return 0;
}
Exemplo n.º 2
0
int picolCondition(picolInterp *i, char* str)
{
    if(str) {
        char buf[MAXSTR], buf2[MAXSTR];
        int rc;
        rc = picolSubst(i,str);
        if(rc != PICOL_OK) return rc;
        //mysnprintf(buf, MAXSTR, "Condi: (%s) ->(%s)\n",str,i->result);
        //dbg_send_str3(buf, 1);
        mystrncpy(buf2,i->result, MAXSTR);
        /* ------- try whether the format suits [expr]... */
        mystrncpy(buf,"llength ", MAXSTR); LAPPEND(buf,i->result);
        //dbg_send_str3(buf, 1);
        rc = picolEval(i,buf);
        if(rc != PICOL_OK) return rc;
#if 0
        if(EQ(i->result,"3")) {
            FOREACH(buf,cp,buf2) argv[a++] = mystrdup(buf);
            if(picolGetCmd(i,argv[1])) { /* defined operator in center */
                mystrncpy(buf,argv[1], MAXSTR);       /* translate to Polish :) */
                LAPPEND(buf,argv[0]);      /* e.g. {1 > 2} -> {> 1 2} */
                LAPPEND(buf,argv[2]);
                rc = picolEval(i, buf);
                return rc;
            }
        } /* .. otherwise, check for inequality to zero */
#endif
        if(*str == '!') {mystrncpy(buf, "== 0 ", MAXSTR); str++;} /* allow !$x */
        else             mystrncpy(buf, "!= 0 ", MAXSTR);
        mystrncat(buf, str, MAXSTR);
        return picolEval(i, buf); // todo: compare without eval
    }
    else
        return picolErr(i, "NULL condition");
}
Exemplo n.º 3
0
int main() {
    char s1[MAX_LEN] = "abcdefg";
    char *t1 = "xxx";
    mystrncpy(s1, t1, 2);
    printf("%s\n", s1);
    printf("----\n");

    char s3[MAX_LEN] = "abcd";
    char *t3 = "efg";
    mystrncat(s3, t3, 3);
    printf("%s\n", s3);
    char s4[MAX_LEN] = "abcd";
    char *t4 = "efg";
    mystrncat(s4, t4, 1);
    printf("%s\n", s4);
    printf("----\n");

    char s5[MAX_LEN] = "abcdef";
    char *t5 = "abca";
    printf("%d\n", mystrncmp(s5, t5, 4));
}
Exemplo n.º 4
0
int main(void)
{
    printf("\n\n");
    char string[17];
    char *ptr,c='r';
    char d[20]="GoldenGlobal";
    char *s="View";
    char *s1 = "Hello,Programmer";
    char *s2 = "Hello,Programmer!";
    int r;
    strcpy(string,"Thisisastring");
    ptr=mystrrchr(string,c);
    if(ptr)
        printf("The character %c is at position:%s\n" , c, ptr);
    else
        printf("The character was notfound\n");

    printf("\nfinish\n");


    mystrcat(d,s);
    printf("%s\n", d);

    mystrncat(d,s,2);
    printf("%s",d);
    printf("\n");

    r = memcmp(s1,s2,strlen(s1));
    print(r);
    r = mymemcmp(s1,s2,strlen(s1));
    print(r);

    printf("finish \n");
    r = strcmp(s1,s2);
    print(r);
//    r = mystrcmp(s1,s2);
    print(r);
    r = linuxstrcmp(s1,s2);
    print(r);

    return 0;
}
Exemplo n.º 5
0
int main (void)
{
   /* strcpy */
   char string[20] = "Hello";

   printf("%s\n", mystrcpy(string, "World"));
   strcpy(string, "Hello");  /* restoring old value */

   /* strncpy */
   printf("%s\n", mystrncpy(string, "Qwerty", 3));
   strcpy(string, "Hello");  /* restoring old value */

   /* strcat */
   printf("%s\n", mystrcat(string, "World"));
   strcpy(string, "Hello");  /* restoring old value */

   /* strncat is buggy somehow ...*/
   printf("%s\n", mystrncat(string, "World", 3));
   
   return 0;
}
Exemplo n.º 6
0
long int data_buffer(mytext *thisdata,char *data,int type,int dsize){
     mytext *texttmp,*texttmp1;
     long int size,i;
     char *tmp;
     
     if (type==1){//add
      size=thisdata->size+dsize;
     	
     	if(thisdata->size+1>=_mytextlen)
        while(thisdata->next){
            if(thisdata->size+1>=_mytextlen)
                thisdata=thisdata->next;
            else
                break;
        }
        
        if(size<=_mytextlen-1){
            size=dsize;
            mystrncat(thisdata,data,size);
        }else{
            
      size=_mytextlen-thisdata->size;
	    texttmp=thisdata;
	    

	    if(thisdata->next==0){
	        thisdata=(mytext *)malloc(sizeof(mytext));
	        thisdata->last=texttmp;
	        thisdata->next=0;
	        thisdata->size=0;
	        texttmp->next=thisdata;
	    }else{
	        thisdata=thisdata->next;
	        thisdata->size=0;
	    }
	    thisdata->size=0;
      mystrncat(texttmp,data,size-1);
      tmp=&data[size-1];
      mystrncat(thisdata,tmp,dsize-(size-1));
      }
     	return 0;
     }
     
     if (type==2){//delete
        while(thisdata->next){
            thisdata=thisdata->next;
        }
        thisdata->size=0;
        while(thisdata->last){
            thisdata=thisdata->last;
            thisdata->size=0;
        }

     }

     if (type==3){//get sum
     	size=0;
        while(thisdata->last){
            thisdata=thisdata->last;
        }
     	
     	size=thisdata->size;
        while(thisdata->next){
            thisdata=thisdata->next;
            size=size+thisdata->size;
        }
     	return size;
     }
     
     if (type==4){//get data
     	size=0;
        while(thisdata->last){
            thisdata=thisdata->last;
        }

     	tmp=&data[0];
     	memcpy(tmp,thisdata->mydata,thisdata->size);
     	size=size+thisdata->size;
        while(thisdata->next){
            thisdata=thisdata->next;
            //tmp=&data[size];
            memcpy(&data[size],thisdata->mydata,thisdata->size);
            size=size+thisdata->size;
        }
        data[size]=0;
     }
}
Exemplo n.º 7
0
int main(){

  printf("\n\n");

  char test[]="testing_the_length_of_this_string";

  printf("Test for strlen:\n string: %s\n length: %d\n",test,mystrlen(test));

  printf("\n");

  char src[]="source",
    dest[]="destination";

  printf("Test for strcpy:\n before copying:\n  src: %s\n  dest: %s\n",src,dest);

  printf(" after copying:\n  src: %s\n  dest: %s\n",src,mystrcpy(dest,src));

  printf("\n");

  char src0[]="source",
    dest0[]="destination";
  int n=3;

  printf("Test for strncpy:\n before copying:\n  src: %s\n  dest: %s\n  n: %d\n",src0,dest0,n);

  printf(" after copying:\n  src: %s\n  dest: %s\n",src,mystrncpy(dest0,src0,n));

  printf("\n");

  char src1[]="source",
    dest1[]="destination";

  printf("Test for strcat:\n before concatenation:\n  src: %s\n  dest: %s\n",src1,dest1);

  printf(" after concatenation:\n  src: %s\n  dest: %s\n",src1,mystrcat(dest1,src1));

  printf("\n");

  char src2[]="source",
    dest2[]="destination";
  int m=3;

  printf("Test for strncat:\n before concatenation:\n  src: %s\n  dest: %s\n  n: %d\n",src2,dest2,m);

  printf(" after concatenation:\n  src: %s\n  dest: %s\n",src2,mystrncat(dest2,src2,m));

  printf("\n");

  char stra[]="apple",
    strb[]="pie";

  printf("Test for strcmp:\n a: %s\n b: %s\n a compared to b: %d\n",stra,strb,mystrcmp(stra,strb));

  printf("\n");

  char strc[]="pie",
    strd[]="bpple";

  printf("Test 2 for strcmp:\n a: %s\n b: %s\n a compared to b: %d\n",strc,strd,mystrcmp(strc,strd));

  printf("\n");

  char stre[]="x",
    strf[]="x";

  printf("Test for 3 strcmp:\n a: %s\n b: %s\n a compared to b: %d\n",stre,strf,mystrcmp(stre,strf));

  printf("\n");

  char s[]="string",
    c1='r',
    c2='a';

  printf("Test for strchr:\n s: %s\n c1: %c\n c2: %c\n char c1 as found in in s: [%c] at location %p\n char c2 as found in s: [%c] at location %p\n",s,c1,c2,*mystrchr(s,c1),mystrchr(s,c1),*mystrchr(s,c2),mystrchr(s,c2));

  printf("\n");

  char haystack[]="haystack",
    needle1[]="needle",
    needle2[]="sta";

  printf("Test for strstr:\n haystack: %s\n needle1: %s\n needle2: %s\n substring beginning with needle1 in haystack: [%s] at location %p (the terminating null)\n substring beginning with needle2 in haystack: [%s] at location %p\n",haystack,needle1,needle2,mystrstr(haystack,needle1),mystrstr(haystack,needle1),mystrstr(haystack,needle2),mystrstr(haystack,needle2));

  printf("\n");

  char haystack2[]="ababa",
    needle1a[]="b",
    needle2a[]="ab";

  printf("Test 2 for strstr:\n haystack: %s\n needle1: %s\n needle2: %s\n substring beginning with needle1 in haystack: [%s]\n substring beginning with needle2 in haystack: [%s]\n",haystack2,needle1a,needle2a,mystrstr(haystack2,needle1a),mystrstr(haystack2,needle2a));
  printf("(demonstrates that the function looks for the first occurence of the substring (needle), not the last or an arbitrary one)\n");

  printf("\n");

  return 0;
}