This is the knowledge summary of June 2021.
Coding summary
C / C++
Use sscanf to check for uncertain input parameters:
char name[8] = {0};
int type = 0;
int threadnum = 0;
int total = - 1;
// Note: Check the more first, then check the less
int num = sscanf(cmd, "%s %d %d %d", name, &type, &threadnum, &total);
if(num ! =4)
{
num = sscanf(cmd, "%s %d %d", name, &type, &threadnum);
if(num ! =3)
{
return - 1; }}Copy the code
Note: In practice, it is recommended to use the form main(argc, argv[]) to check parameters for easy modification.
docker
Change the root password of the container
Scenario: Download the centos image and run it as a common user by default. You cannot switch to root because you do not know the password.
Sudo docker exec-id -u root < container name or id > bashCopy the code
In this case, the container is the root permission. Run the passwd root command to set the password. In this case, run the su root command to switch the permission of the container. Of course, you can also directly use the above commands to perform root permission operations.
The Python script creates the Docker image
Execute command, execute command and get return value, string is empty.
SRC_PATH = '/home/latelee/docker-test/demo' DOCKER_ADDRESS = "registry.cn-hangzhou.aliyuncs.com/latleee" DOCKER_IMAGE_NAME = "busybox" DOCKER_TAG = "latest" def buidDocker(): #os.chdir(SRC_PATH) Use popen get back mirror ID CMD = "docker images | grep" + DOCKER_IMAGE_NAME + "| awk '{print $3}'" imageid = OS. Popen (CMD). The read () #print("cmd %s %s" % (cmd, imageid)) if len(imageid) ! = 0: print("found docker image, Remove it") CMD = 'docker rmi '+ imageid os.system(CMD) print("start build docker in %s" % (SRC_PATH)) # "/" + DOCKER_IMAGE_NAME + "" + SRC_PATH # # CMD = "build -t "+ DOCKER_ADDRESS + "/" + DOCKER_IMAGE_NAME +"." #print("build CMD: %s " % (cmd)) ret = os.system(cmd) if ret == 0: print('build docker ok') else: print('build docker failed')Copy the code
SQL
Select * from table_name where table_name = ‘sqlite3’;
SELECT name FROM sqlite_master where type='table' order by name
Copy the code
Other encoding
Makefile
To continue with makefiles:
A project needs to be compiled into 32-bit and 64-bit programs because there are two target systems. The original project uses Eclipse to edit and compile. After taking over, we changed it to Makefile compilation. For the convenience of personal use, we studied how to adapt two different bit systems in Makefile.
In the shell script, you can use the following method to determine the system bits:
BITS=`getconf LONG_BIT`
BITS=" --$BITS"bit
Copy the code
The output is 32-bit or 64-bit, depending on the operating system. This string can be used to generate version header files.
Use the following in a Makefile:
ARCH = $(shell getconf LONG_BIT)
DEFS += -DARCH=$(ARCH)
ifeq ($(ARCH), 32)
CFLAGS += -I/usr/local/sqlite/sqlite-x86/include/
LDFLAGS += -L/usr/local/sqlite/sqlite-x86/lib/ -L/usr/local/uuid/uuid-x86/
endif
Copy the code
This allows you to use different library directories for different systems.
In addition, the target system, 32-bit debian system, 64-bit centos system, I wanted to use Docker to achieve the compilation of the same VIRTUAL machine, but because the version is not easy to find, give up. Furthermore, the project has a large amount of code that is no longer used but not deleted. Most of the variables and functions are similar, with only a few characters or dates different, such as foo, foo_I, foo_2020, leading to certain reading disabilities — the code snippet you just saw, switch files, see the same code again, and later, see the same code again. I’m not moving yet.
copyright
There was a project on Github a few years ago where the code didn’t have a copyright notice — now most of the code doesn’t have a copyright notice. After consideration, or to add their own warehouse statement. Be nice to other people and be nice to follow-up maintenance. At present, it is tentatively BSP protocol with few restrictions.
BSD: allowed for commercial and private use. With the original copyright file, the modified code need not be explained. Do not use the author’s name for promotion.
General knowledge
Research and development of thinking
After writing code for a long time, I found that I dare not break through. Instead of pursuing programming skills, I pursue maintainability and readability. Sometimes, after finding some problems, we will naturally go to the reconstruction, but after the reconstruction, we find that it does not meet the expectation, because there are some hidden business points that we have not mastered, and it will only waste time to revise them hastily. Therefore, as long as there is no problem, we will not take the initiative to change, but code style naming, which affects the efficiency of programming, is not included. During the modification process, ** is partially refactored according to the actual situation.
Domain design: first have a certain understanding of a certain domain, summarize, clear logic, and then design. Personal opinion: It should not be appropriate. Although I have been coding for many years and contacted industries in different fields, my method of learning knowledge and code written can be applied more or less. — Because they will summarize and accumulate, so as to form their own knowledge base to deal with different fields/businesses. And a more important reason is that I have not been deeply engaged in a field. May own business research is not deep, precipitation is not enough, just draw the above conclusion.
Study and plan this month
There is no
Other intravenous drip
Since the result of physical examination, after 2 shots of vaccine, the outsourcing staff cancelled the meal supplement, the family asked when to buy a house, and had a small quarrel with mom and dad, work and life are not motivated. However, I did not delay the company. I worked extra shifts several times in the evening, and I worked extra shifts all day last weekend (arriving at the company at 8 am and leaving at 6 PM). I also made a negligible contribution to the centennial gift.