Add string_compare_guy
This commit is contained in:
parent
a2fd9b8725
commit
0663f56757
|
@ -9,11 +9,11 @@ GOFLAGS += -g
|
|||
GOFLAGS += -O3
|
||||
|
||||
CFLAGS += -Wall
|
||||
CFLAGS += -Werror
|
||||
#CFLAGS += -Werror
|
||||
|
||||
!nim = |> ^ Nim: %f -> %o^ nim c $(NIMFLAGS) -o:%o %f && rm -rf .nimcache |>
|
||||
!gccgo = |> ^ GCCGo: %f -> %o^ gccgo $(GOFLAGS) -o %o %f |>
|
||||
!cc = |> ^ CC obj: %f -> %o^ gcc $(CFLAGS) -o %o %f |>
|
||||
!obj = |> ^ CC: %f -> %o^ gcc $(CFLAGS) -c -o %o %f |>
|
||||
!cc = |> gcc $(CFLAGS) -o %o %f |>
|
||||
!obj = |> gcc $(CFLAGS) -c -o %o %f |>
|
||||
!moon = |> ^ Moonscript: %f -> %o^ moonc -o %o %f |>
|
||||
!luascript = |> ^ Lua script: %f -> %o^ echo "#!/usr/bin/lua" >> %o && cat %f >> %o && chmod u+x %o |>
|
||||
|
|
|
@ -3,3 +3,5 @@
|
|||
##### Do not edit.
|
||||
/.gitignore
|
||||
/reaper.o
|
||||
/string_compare_guy.log
|
||||
/string_compare_guy.o
|
||||
|
|
|
@ -5,4 +5,8 @@ include_rules
|
|||
: foreach *.go |> !gccgo |> ../bin/%B
|
||||
|
||||
: foreach *.c |> !obj |> %B.o
|
||||
|
||||
: reaper.o |> !cc |> ../bin/reaper
|
||||
: string_compare_guy.o |> !cc |> ../bin/string_compare_guy
|
||||
|
||||
: ../bin/string_compare_guy |> ../bin/string_compare_guy | tee %o |> ./string_compare_guy.log
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int string_compare_guy(char* lhs, int llength, char* rhs, int rlength) {
|
||||
char* lnz = malloc(llength + 1);
|
||||
memcpy(lnz, lhs, llength);
|
||||
lnz[llength] = 0;
|
||||
|
||||
char* rnz = malloc(rlength + 1);
|
||||
memcpy(rnz, rhs, rlength);
|
||||
rnz[rlength] = 0;
|
||||
|
||||
int result = strstr(lnz, rhs);
|
||||
|
||||
free(lnz);
|
||||
free(rnz);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int retcode = 0;
|
||||
|
||||
printf("Comparing foo and bar\n");
|
||||
if(string_compare_guy("foo", 3, "bar", 3)) {
|
||||
retcode++;
|
||||
printf("failure\n");
|
||||
} else {
|
||||
printf("passed\n");
|
||||
}
|
||||
|
||||
printf("trying to find foo in foobar\n");
|
||||
if(string_compare_guy("foo", 3, "foobar", 6)) {
|
||||
retcode++;
|
||||
printf("failure\n");
|
||||
} else {
|
||||
printf("passed\n");
|
||||
}
|
||||
|
||||
return retcode;
|
||||
}
|
Loading…
Reference in New Issue