Did a few exersises, most of the way through serial_logger.
This commit is contained in:
		
							parent
							
								
									1f4cc71b96
								
							
						
					
					
						commit
						97fb8de5da
					
				
					 8 changed files with 331 additions and 20 deletions
				
			
		
							
								
								
									
										65
									
								
								file_tee/Makefile
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								file_tee/Makefile
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,65 @@
 | 
			
		|||
CC := gcc
 | 
			
		||||
CXX := g++
 | 
			
		||||
OPTIMIZATION := -O3
 | 
			
		||||
CFLAGS = ${OPTIMIZATION} -Wextra -Wall -Wpedantic
 | 
			
		||||
# all: helloworld
 | 
			
		||||
#
 | 
			
		||||
# #automatically built from implicit rules.
 | 
			
		||||
# helloworld: helloworld.c
 | 
			
		||||
#
 | 
			
		||||
# clean:
 | 
			
		||||
# 	rm -f helloworld
 | 
			
		||||
#
 | 
			
		||||
TARGET_EXEC := filetee
 | 
			
		||||
 | 
			
		||||
BUILD_DIR := ./build
 | 
			
		||||
SRC_DIRS := ./src
 | 
			
		||||
 | 
			
		||||
# Find all the C and C++ files we want to compile
 | 
			
		||||
# Note the single quotes around the * expressions. The shell will incorrectly expand these otherwise, but we want to send the * directly to the find command.
 | 
			
		||||
SRCS := $(shell find $(SRC_DIRS) -name '*.cpp' -or -name '*.c' -or -name '*.s')
 | 
			
		||||
 | 
			
		||||
# Prepends BUILD_DIR and appends .o to every src file
 | 
			
		||||
# As an example, ./your_dir/hello.cpp turns into ./build/./your_dir/hello.cpp.o
 | 
			
		||||
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
 | 
			
		||||
 | 
			
		||||
# String substitution (suffix version without %).
 | 
			
		||||
# As an example, ./build/hello.cpp.o turns into ./build/hello.cpp.d
 | 
			
		||||
DEPS := $(OBJS:.o=.d)
 | 
			
		||||
 | 
			
		||||
# Every folder in ./src will need to be passed to GCC so that it can find header files
 | 
			
		||||
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
 | 
			
		||||
# Add a prefix to INC_DIRS. So moduleA would become -ImoduleA. GCC understands this -I flag
 | 
			
		||||
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
 | 
			
		||||
 | 
			
		||||
# The -MMD and -MP flags together generate Makefiles for us!
 | 
			
		||||
# These files will have .d instead of .o as the output.
 | 
			
		||||
DEPFLAGS := $(INC_FLAGS) -MMD -MP
 | 
			
		||||
 | 
			
		||||
# The final build step.
 | 
			
		||||
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
 | 
			
		||||
	$(CXX) $(OBJS) -o $@ $(LDFLAGS)
 | 
			
		||||
 | 
			
		||||
# Build step for C source
 | 
			
		||||
$(BUILD_DIR)/%.c.o: %.c
 | 
			
		||||
	mkdir -p $(dir $@)
 | 
			
		||||
	$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
 | 
			
		||||
 | 
			
		||||
# Build step for C++ source
 | 
			
		||||
$(BUILD_DIR)/%.cpp.o: %.cpp
 | 
			
		||||
	mkdir -p $(dir $@)
 | 
			
		||||
	$(CXX) $(DEPFLAGS) $(CXXFLAGS) -c $< -o $@
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
.PHONY: clean
 | 
			
		||||
clean:
 | 
			
		||||
	rm -r $(BUILD_DIR)
 | 
			
		||||
 | 
			
		||||
.PHONY: run
 | 
			
		||||
run: $(BUILD_DIR)/$(TARGET_EXEC)
 | 
			
		||||
	$(BUILD_DIR)/$(TARGET_EXEC)
 | 
			
		||||
 | 
			
		||||
# Include the .d makefiles. The - at the front suppresses the errors of missing
 | 
			
		||||
# Makefiles. Initially, all the .d files will be missing, and we don't want those
 | 
			
		||||
# errors to show up.
 | 
			
		||||
-include $(DEPS)
 | 
			
		||||
							
								
								
									
										69
									
								
								file_tee/src/file_tee.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								file_tee/src/file_tee.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,69 @@
 | 
			
		|||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
static const char Help[] = "Reads a file and outputs to a file while also printing contents.";
 | 
			
		||||
static const char Usage[] = "INPUT_FILE [OUTPUT_FILE]";
 | 
			
		||||
 | 
			
		||||
struct arguments {
 | 
			
		||||
	char* inFile;
 | 
			
		||||
	char* outFile;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int main(int argc, char* argv[]) {
 | 
			
		||||
	// for some reason, when doing this with a serial port, log.log never gets written to. I think this is becasue the
 | 
			
		||||
	// program is terminated with ctrl-c, so the file descriptors dont close?.
 | 
			
		||||
 | 
			
		||||
	struct arguments arguments = {NULL, NULL};
 | 
			
		||||
 | 
			
		||||
	switch (argc) {
 | 
			
		||||
	case 3:
 | 
			
		||||
		arguments.outFile = argv[2];
 | 
			
		||||
		// fallthrough
 | 
			
		||||
	case 2:
 | 
			
		||||
		arguments.inFile = argv[1];
 | 
			
		||||
		break;
 | 
			
		||||
	default:
 | 
			
		||||
		printf("%s\n%s\n", Usage, Help);
 | 
			
		||||
		return EXIT_FAILURE;
 | 
			
		||||
		break;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	FILE* inFile = fopen(arguments.inFile, "r");
 | 
			
		||||
	if (!inFile) {
 | 
			
		||||
		printf("error opening file %s\n", arguments.inFile);
 | 
			
		||||
		return EXIT_FAILURE;
 | 
			
		||||
	}
 | 
			
		||||
	FILE* outFile;
 | 
			
		||||
	if (arguments.outFile != NULL) {
 | 
			
		||||
		outFile = fopen(arguments.outFile, "w");
 | 
			
		||||
		if (!outFile) {
 | 
			
		||||
			printf("error opening file %s\n", arguments.outFile);
 | 
			
		||||
			return EXIT_FAILURE;
 | 
			
		||||
		}
 | 
			
		||||
		if (fputs("start of log\n", outFile) < 0) {
 | 
			
		||||
			printf("could not write to %s\n", arguments.outFile);
 | 
			
		||||
			return EXIT_FAILURE;
 | 
			
		||||
		}
 | 
			
		||||
		fflush(outFile);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	char* lineBuff = NULL;
 | 
			
		||||
	size_t lineBuffSize = 0;
 | 
			
		||||
	ssize_t line_size;
 | 
			
		||||
 | 
			
		||||
	do {
 | 
			
		||||
		line_size = getline(&lineBuff, &lineBuffSize, inFile);
 | 
			
		||||
		printf("%s", lineBuff);
 | 
			
		||||
		if (arguments.outFile != NULL) {
 | 
			
		||||
			fputs(lineBuff, outFile);
 | 
			
		||||
			fflush(outFile);
 | 
			
		||||
		}
 | 
			
		||||
	} while (line_size >= 0);
 | 
			
		||||
 | 
			
		||||
	// these things should be done by the OS when we exit.
 | 
			
		||||
	// free(lineBuff);
 | 
			
		||||
	// fclose(inFile);
 | 
			
		||||
	// fclose(outFile);
 | 
			
		||||
 | 
			
		||||
	return EXIT_SUCCESS;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue