diff --git a/.gitignore b/.gitignore index e7388b9..4433abd 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ # Debug files *.dSYM/ +.idea/ \ No newline at end of file diff --git a/env_vars/Makefile b/env_vars/Makefile new file mode 100644 index 0000000..8130219 --- /dev/null +++ b/env_vars/Makefile @@ -0,0 +1,7 @@ +P= +OBJECTS= +CFLAGS = -g -Wall -O3 +LDLIBS= +CC=gcc + +$(P): $(OBJECTS) diff --git a/env_vars/README.md b/env_vars/README.md new file mode 100644 index 0000000..3deb5c6 --- /dev/null +++ b/env_vars/README.md @@ -0,0 +1,20 @@ +# Environment Variables + +Ok this is kind of getting interesting. + +## Building + +This makefile is kind of interesting. To build and name our program we just need to: + +``` +make P="env" +``` +Ok I don't really know the magic that is going on behind that one. Notice though that you are passing that variable after calling the program. +Technically, I think it's supposed to work like this: + +``` +make CFLAGS="-g -Wall" Set a makefile variable. +CFLAGS="-g -Wall" make Set an environment variable that only make and its children see. +``` + +But on my mac I am noticing only the set makefile variable method works. diff --git a/env_vars/env b/env_vars/env new file mode 100755 index 0000000..015c364 Binary files /dev/null and b/env_vars/env differ diff --git a/env_vars/env.c b/env_vars/env.c new file mode 100644 index 0000000..ba94c51 --- /dev/null +++ b/env_vars/env.c @@ -0,0 +1,14 @@ +#include //getenv, atoi +#include //printf + +int main(){ + // pointer to a char + char *repstext=getenv("reps"); + int reps = repstext ? atoi(repstext) : 10; + + char *msg = getenv("msg"); + if (!msg) msg = "Hello."; + + for (int i=0; i< reps; i++) + printf("%s\n", msg); +} diff --git a/file_io/Makefile b/file_io/Makefile new file mode 100644 index 0000000..7362e1a --- /dev/null +++ b/file_io/Makefile @@ -0,0 +1,7 @@ + + +csv_writer: + gcc -o csv_writer csv_writer.c + +clean: + rm -f csv_writer diff --git a/file_io/csv_writer b/file_io/csv_writer new file mode 100755 index 0000000..b6e6961 Binary files /dev/null and b/file_io/csv_writer differ diff --git a/file_io/csv_writer.c b/file_io/csv_writer.c new file mode 100644 index 0000000..34378b4 --- /dev/null +++ b/file_io/csv_writer.c @@ -0,0 +1,56 @@ +#include +#include +#include + +#define FILE_EXTENSION ".csv" + + +int main(int argc, char const *argv[]){ + + if (argc <= 1){ + fprintf(stderr, "Failed to provide a csv file\n"); + exit(1); + } + + size_t file_len = strlen(argv[1]); + + /*char pointer of*/ + char * FILE_NAME = malloc(file_len + 1); + if (FILE_NAME == NULL){ + fprintf(stderr, "Failed to allocate memory for file name!"); + exit(1); + } + + /* Copy file name from executable's first arguments.*/ + strcpy(FILE_NAME, argv[1]); + + /*Returns a pointer to the location the substring if found*/ + if (strstr(FILE_NAME, FILE_EXTENSION) == NULL ){ + fprintf(stderr, "What the fuck are you doing? That %s isn't a csv file!\n", FILE_NAME); + free(FILE_NAME); + FILE_NAME = 0; + exit(1); + } + + char name[128], phone_number[13]; + // puts(FILE_NAME); + FILE *_target_file = fopen(FILE_NAME, "a"); + if (_target_file == NULL){ + exit(1); + + } + + puts("Name: "); + scanf("%s", name); + puts("Number: "); + scanf("%s", phone_number); + + printf("Appending %s and %s to %s", name, phone_number, FILE_NAME); + fprintf(_target_file,"%s,%s\n", name, phone_number); + fclose(_target_file); + /*clean up*/ + free(FILE_NAME); + FILE_NAME = 0; + + return 0; +} diff --git a/file_io/dirp.csv b/file_io/dirp.csv new file mode 100644 index 0000000..75cadf7 --- /dev/null +++ b/file_io/dirp.csv @@ -0,0 +1 @@ +Margot,517-775-7040 diff --git a/headers/Makefile b/headers/Makefile new file mode 100644 index 0000000..63a9b12 --- /dev/null +++ b/headers/Makefile @@ -0,0 +1,11 @@ +.PHONY: clean +P=temp_count +OBJECTS= temp.o temp_count.o +CFLAGS = -g -Wall -O3 +LDLIBS='' +CC=gcc + +$(P): $(OBJECTS) + +clean: + rm -f *.o $(P) diff --git a/headers/temp_count b/headers/temp_count new file mode 100755 index 0000000..f6560b5 Binary files /dev/null and b/headers/temp_count differ diff --git a/hello/Makefile b/hello/Makefile new file mode 100644 index 0000000..876d9a2 --- /dev/null +++ b/hello/Makefile @@ -0,0 +1,7 @@ +.PHONY: clean + +hello: + gcc hello.c -o hello -g -Wall -O3 + +clean: + rm -f *.o hello diff --git a/pointers/Makefile b/pointers/Makefile new file mode 100644 index 0000000..1e3c385 --- /dev/null +++ b/pointers/Makefile @@ -0,0 +1,17 @@ +CFLAGS=-Wall -O3 +PROGRAMS=simple_pointer adv_pointer + +all: $(PROGRAMS) + +simple_pointer: + gcc -o simple_pointer $(CFLAGS) simple_pointer.c + +adv_pointer: + gcc -o adv_pointer $(CFLAGS) adv_pointer.c + +# NOT IMPLEMENTED +#array_pointer: +# gcc -o array_pointer -Wall -O1 array_pointer.c + +clean: + rm -f $(PROGRAMS) diff --git a/pointers/README.md b/pointers/README.md new file mode 100644 index 0000000..1ef1086 --- /dev/null +++ b/pointers/README.md @@ -0,0 +1,5 @@ +#Pointers + +Following this [C Pointers Tutorial]. + +[C Pointers Tutorial]: https://www.cprogramming.com/tutorial/c/lesson6.html diff --git a/pointers/adv_pointer b/pointers/adv_pointer new file mode 100755 index 0000000..60ace4b Binary files /dev/null and b/pointers/adv_pointer differ diff --git a/pointers/adv_pointer.c b/pointers/adv_pointer.c new file mode 100644 index 0000000..e87e43e --- /dev/null +++ b/pointers/adv_pointer.c @@ -0,0 +1,30 @@ +#include +#include + + +int main(void){ + int o = 1337; + + printf("Let's learn to use malloc!\n"); + int *p = malloc(sizeof(*p)); + /* We need to make sure we got the memory block*/ + if (p){ + fprintf(stdout, "Ok we got a good value from malloc we can assign a value.\n"); + } + else { + fprintf(stderr, "Well fuck this didn't work. Malloc failed"); + return 1; + } + + printf("Ok the address of o is on the stack at: %p\n", &o); + printf("*p is on the heap at: %p\n", p); + printf("Let's assign a value to p: \n"); + scanf("%d", p); + printf("We got %d\n", *p); + + printf("Freeing up the memory\nClosing program...\n"); + free(p); + p = 0; + + return 0; +} diff --git a/pointers/simple_pointer b/pointers/simple_pointer new file mode 100755 index 0000000..d09d3f3 Binary files /dev/null and b/pointers/simple_pointer differ diff --git a/pointers/simple_pointer.c b/pointers/simple_pointer.c new file mode 100644 index 0000000..c736bd1 --- /dev/null +++ b/pointers/simple_pointer.c @@ -0,0 +1,18 @@ +#include + +int main(void){ + + int x; + int *p; + + /* Read it, "assign the address of x to p" */ + p = &x; + printf("This should be random: %d\n", *p); + printf("Ok the address of x is %p\n", &x); + printf("Check that p has the same value: %p\n\n", p); + + printf("Ok let's put a value in the pointer. Give me an int: \n"); + scanf("%d", p); + printf("The value you provided was %d\n", *p); + printf("Let's check x since p was a pointer to x's memory address: %d\n", x); +} diff --git a/static/Makefile b/static/Makefile new file mode 100644 index 0000000..dcc95a1 --- /dev/null +++ b/static/Makefile @@ -0,0 +1,5 @@ +all: + gcc -o eggs -Wall -O3 eggs.c + +clean: + rm -f eggs diff --git a/static/eggs b/static/eggs new file mode 100755 index 0000000..c04439b Binary files /dev/null and b/static/eggs differ diff --git a/structs/Makefile b/structs/Makefile new file mode 100644 index 0000000..f1cb8f8 --- /dev/null +++ b/structs/Makefile @@ -0,0 +1,16 @@ +CC=gcc +CFLAGS=-Wall -O1 +PROGRAMS=pixel struct2 + +all: $(PROGRAMS) + +pixel: + $(CC) -o pixel $(CFLAGS) pixel.c + +struct2: + $(CC) -o struct2 $(CFLAGS) struct2.c + +clean: + rm -f $(PROGRAMS) + +.PHONY=clean diff --git a/structs/pixel b/structs/pixel new file mode 100755 index 0000000..f7341f1 Binary files /dev/null and b/structs/pixel differ diff --git a/structs/struct2 b/structs/struct2 new file mode 100755 index 0000000..453e56b Binary files /dev/null and b/structs/struct2 differ