Add string_compare_guy

This commit is contained in:
Christine Dodrill 2015-06-19 15:23:28 -07:00
parent a2fd9b8725
commit 0663f56757
4 changed files with 51 additions and 3 deletions

View File

@ -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 |>

2
src/.gitignore vendored
View File

@ -3,3 +3,5 @@
##### Do not edit.
/.gitignore
/reaper.o
/string_compare_guy.log
/string_compare_guy.o

View File

@ -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

42
src/string_compare_guy.c Normal file
View File

@ -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;
}