Skip to content

Commit 920d51e

Browse files
committed
Parse arguments
1 parent 40d217c commit 920d51e

File tree

8 files changed

+95
-2
lines changed

8 files changed

+95
-2
lines changed

Diff for: .gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
objs
2-
hw1
31
.vscode

Diff for: HW1/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
objs
2+
hw1

Diff for: HW2/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
logger_objs
2+
so_objs
3+
logger
4+
logger.so
5+
.vscode

Diff for: HW2/Makefile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
CXX = g++
2+
3+
LOGGER_DIR = logger_objs/
4+
LOGGER_FLAGS = -O3 -std=c++17 -Wall -I.
5+
LOGGER_FILES = hw2.cpp
6+
LOGGER_OBJS = $(addprefix $(LOGGER_DIR), $(LOGGER_FILES:.cpp=.o))
7+
LOGGER = logger
8+
9+
SO_DIR = so_objs/
10+
SO_FLAGS = -O3 -std=c++17 -shared -fPIC -ldl -Wall -I.
11+
SO_FILES = shared_object.cpp
12+
SO_OBJS = $(addprefix $(SO_DIR), $(SO_FILES:.cpp=.o))
13+
SHARED_OBJECT = logger.so
14+
15+
.PHONY: all create compile_logger compile_so clean
16+
17+
all: create compile_logger compile_so
18+
19+
create:
20+
mkdir -p $(LOGGER_DIR)
21+
mkdir -p $(SO_DIR)
22+
23+
compile_logger: $(LOGGER_OBJS)
24+
$(CXX) $^ -o $(LOGGER) $(LOGGER_FLAGS)
25+
26+
$(LOGGER_DIR)%.o: %.cpp
27+
$(CXX) -c $< -o $@ $(LOGGER_FLAGS)
28+
29+
compile_so: $(SO_OBJS)
30+
$(CXX) $^ -o $(SHARED_OBJECT) $(SO_FLAGS)
31+
32+
$(SO_DIR)%.o: %.cpp
33+
$(CXX) -c $< -o $@ $(SO_FLAGS)
34+
35+
clean:
36+
rm -rf $(LOGGER) $(SHARED_OBJECT) $(LOGGER_DIR) $(SO_DIR)

Diff for: HW2/hw2.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "hw2.hpp"
2+
3+
int main(int argc, char* argv[]) {
4+
// Parse arguments
5+
char ch{};
6+
std::string output_file{};
7+
std::string logger_path{"./logger.so"};
8+
while ((ch = getopt(argc, argv, "o:p:")) != -1) {
9+
switch (ch) {
10+
case 'o': {
11+
output_file = std::string(optarg);
12+
break;
13+
}
14+
case 'p': {
15+
logger_path = std::string(optarg);
16+
break;
17+
}
18+
default: {
19+
std::cout << "usage: ./logger [-o file] [-p sopath] [--] cmd "
20+
"[cmd args ...]\n"
21+
<< " -p: set the path to logger.so, default = "
22+
"./logger.so\n"
23+
<< " -o: print output to file, print to "
24+
"\"stderr\" if no file specified\n"
25+
<< " --: separate the arguments for logger "
26+
"and for the command\n"
27+
<< std::endl;
28+
return 1;
29+
}
30+
}
31+
}
32+
33+
if (optind == argc) {
34+
std::cout << "no command given\n" << std::endl;
35+
return 1;
36+
}
37+
38+
// Parse command with its arguments
39+
std::string command{argv[optind]};
40+
std::vector<std::string> arguments{};
41+
for (int idx{optind + 1}; idx < argc; idx++)
42+
arguments.emplace_back(argv[idx]);
43+
44+
return 0;
45+
}

Diff for: HW2/hw2.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <unistd.h>
2+
3+
#include <iostream>
4+
#include <string>
5+
#include <vector>

Diff for: HW2/shared_object.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <shared_object.hpp>

Diff for: HW2/shared_object.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <dlfcn.h>

0 commit comments

Comments
 (0)