From b462e74945d68dacfd5fde50603665435694e1b4 Mon Sep 17 00:00:00 2001 From: rangersly <2281598291@qq.com> Date: Sat, 31 May 2025 09:16:27 +0800 Subject: [PATCH] MERGE: fix ADD: ./program/c_cpp/coding-style.md ./linux/command -> timeshift FIX: ./linux/arch/auto.sh ./linux/config/.bashrc --- linux/arch/auto.sh | 26 +++- linux/arch/install.md | 22 +-- linux/command.md | 48 +++++- linux/config/.bashrc | 6 +- program/c_cpp/coding-style.md | 143 ++++++++++++++++++ program/c_cpp/{mylib => libs}/bit.h | 0 program/c_cpp/{mylib => libs}/dbtools.h | 0 program/c_cpp/{mylib => libs}/log/log.c | 0 program/c_cpp/{mylib => libs}/log/log.h | 0 program/c_cpp/{mylib => libs}/offsetof.h | 0 program/c_cpp/{mylib => libs}/string_hash.c | 0 program/c_cpp/{mylib => libs}/thread-pool.cpp | 0 program/c_cpp/universal/libs.md | 15 +- tool/vim/.vimrc | 2 +- 14 files changed, 222 insertions(+), 40 deletions(-) create mode 100644 program/c_cpp/coding-style.md rename program/c_cpp/{mylib => libs}/bit.h (100%) rename program/c_cpp/{mylib => libs}/dbtools.h (100%) rename program/c_cpp/{mylib => libs}/log/log.c (100%) rename program/c_cpp/{mylib => libs}/log/log.h (100%) rename program/c_cpp/{mylib => libs}/offsetof.h (100%) rename program/c_cpp/{mylib => libs}/string_hash.c (100%) rename program/c_cpp/{mylib => libs}/thread-pool.cpp (100%) diff --git a/linux/arch/auto.sh b/linux/arch/auto.sh index bcfb9a1..abf6252 100644 --- a/linux/arch/auto.sh +++ b/linux/arch/auto.sh @@ -1,13 +1,25 @@ #!/bin/bash -pacman -S man man-pages bash-completion -pacman -S noto-fonts noto-fonts-cjk noto-fonts-emoji # 安装字体 -pacman -S plasma -pacman -S fcitx5-im fcitx5-chinese-addons fcitx5-rime -pacman -S btop firefox konsole gcc g++ gdb sdcc +pacman -Syyu # 更新源 +pacman-key --lsign-key "farseerfc@archlinux.org" +pacman -Sy archlinuxcn-keyring +pacman -Syyu # 更新源 + +pacman -S --noconfirm --needed man man-pages base-devel bash-completion noto-fonts noto-fonts-cjk noto-fonts-emoji fcitx5-im fcitx5-chinese-addons fcitx5-rime plasma yay btop firefox konsole gcc g++ gdb sdcc timeshift v2raya openssh perf sudo usermod -a -G uucp $USER # 允许用户使用串口 -systemctl enable sddm -sudo systemctl enable bluetooth.service # 设置开机自启 +git clone http://git@e0x1a.cn:23000/rangersly/material.git +cp -rf ./material /home/rangersly +cp /home/rangersly/material/tool/vim/.vimrc /home/rangersly/ +mkdir -p /home/rangersly/.vim/dict/ +cp /home/rangersly/material/tool/vim/cpp.dict /home/rangersly/.vim/dict/ +cp /home/rangersly/material/linux/config/.bashrc /home/rangersly/ + +systemctl enable v2ray +systemctl enable v2raya +systemctl enable sddm +systemctl enable bluetooth.service # 设置开机自启 + +reboot diff --git a/linux/arch/install.md b/linux/arch/install.md index 71a699a..ba8fdcd 100644 --- a/linux/arch/install.md +++ b/linux/arch/install.md @@ -12,7 +12,6 @@ - [新系统](#新系统) - [换源](#mirrors) - [普通用户](#普通用户) - - [KDE](#kde) ## 基础安装 @@ -55,23 +54,10 @@ mount --mkdir /dev/sdX1 /mnt/boot `pacstrap -K /mnt xxx` + base linux linux-firmware + grub efibootmgr -+ vim base-devel networkmanager ++ vim networkmanager --- -+ yay - 1. /etc/pacman.conf - ``` - [archlinuxcn] - Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch - ``` - 2. archlinuxcn - ```bash - sudo pacman-key --lsign-key "farseerfc@archlinux.org" - sudo pacman -Sy archlinuxcn-keyring - sudo pacman -Syyu # 更新源 - sudo pacman -S yay # 直接安装 Yay - ``` ### fstab @@ -116,6 +102,12 @@ Server = http://mirrors.aliyun.com/archlinux/$repo/os/$arch Server = https://mirrors.ustc.edu.cn/archlinux/$repo/os/$arch ``` +- `/etc/pacman.conf` +``` +[archlinuxcn] +Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch +``` + ### 普通用户 `useradd -m -G wheel username` diff --git a/linux/command.md b/linux/command.md index eadf9d3..04f3e92 100644 --- a/linux/command.md +++ b/linux/command.md @@ -136,6 +136,7 @@ - [dmidecode](#dmidecode) - [dpkg](#dpkg) - [apt](#apt) +- [timeshift](#timeshift) : 备份工具 - [index](#index) @@ -686,8 +687,8 @@ curl -# -O https://example.com/largefile.zip `bc`支持高精度数学运算、变量、函数、条件语句等编程功能 - **基本用法** - - **交互模式**:终端输入 `bc` 后回车,进入交互界面。 - - **非交互模式**:直接计算表达式: + - **交互模式**:终端输入 `bc` 后回车 + - **非交互模式**:直接计算表达式 ```bash echo "5 + 3" | bc ``` @@ -701,6 +702,7 @@ curl -# -O https://example.com/largefile.zip - **进制转换** - **输入进制**:用 `ibase` 设置输入数字的进制。 - **输出进制**:用 `obase` 设置输出结果的进制。 + - **调整输入进制一定要注意先后顺序** ```bash echo "ibase=2; 1101" | bc # 二进制 1101 → 十进制 13 echo "obase=16; 255" | bc # 十进制 255 → 十六进制 FF @@ -711,7 +713,6 @@ curl -# -O https://example.com/largefile.zip a=5 b=a*2 # b=10 ``` - ```bash define square(x) { return x*x @@ -734,8 +735,45 @@ square(4) # 输出 16 - **example** - 计算Pi `echo "scale=1000; a=1; b=1/sqrt(2); t=1/4; p=1; for (i=0; i<10; i++) { a1=(a+b)/2; b1=sqrt(a*b); t1=t - p*(a - a1)^2; p1=2*p; a=a1; b=b1; t=t1; p=p1 }; (a+b)^2/(4*t)" | bc -l` - `echo "scale=100; 4*a(1)" | bc -l` - + `echo "scale=1000; 4*a(1)" | bc -l` - **退出** 输入 `quit` 或按 `Ctrl+D` 退出交互模式。 + +--- + +### **timeshift** +- 系统恢复步骤 + +1. 挂载原系统根目录与重要分区到`/mnt` + +2. 挂载timeshift存储位置(如果在独立分区) +``` +mkdir /mnt/timeshift +mount /dev/sda1 /mnt/timeshift # 挂载快照分区到临时目录 +``` + +3. chroot到原系统 + +4. 执行恢复 + +`timeshift --restore --snapshot '2024-05-20_12-00-00' --target / --yes` + +- `--snapshot` 指定快照名称 +- `--target /` 恢复到根分区 +- `--yes` 跳过确认 + +5. 修复引导 +```bash +# 针对 Arch Linux +mkinitcpio -P +grub-install /dev/nvme0n1 # 安装 GRUB 到磁盘(非分区) +grub-mkconfig -o /boot/grub/grub.cfg +``` + +6. 退出重启 +```bash +exit # 退出 chroot +umount -R /mnt # 卸载所有分区 +reboot +``` diff --git a/linux/config/.bashrc b/linux/config/.bashrc index 2699725..512a01d 100644 --- a/linux/config/.bashrc +++ b/linux/config/.bashrc @@ -13,10 +13,6 @@ shopt -s histappend # 追加历史而不是覆盖 export EDITOR=vim export VISUAL=vim -# 设置语言(解决可能的中文乱码) -export LANG=en_US.UTF-8 -export LC_ALL=en_US.UTF-8 - # 自定义PATH export PATH=$PATH:$HOME/.local/bin:$HOME/bin @@ -89,7 +85,7 @@ alias fgrep='fgrep --color=auto' alias egrep='egrep --color=auto' # ls别称 -alias l='ls -alFh --group-directories-first' +alias l='ls -aslFh --group-directories-first' alias ll='ls -A' # alias l='ls -CF' diff --git a/program/c_cpp/coding-style.md b/program/c_cpp/coding-style.md new file mode 100644 index 0000000..8301017 --- /dev/null +++ b/program/c_cpp/coding-style.md @@ -0,0 +1,143 @@ +# Linux 内核代码风格(修改版) + +## **缩进** +缩进的全部意义就在于清楚的定义一个控制块起止于何处,好的缩进是会当你的函数嵌套太深的时候可以给你警告,如果你需要3级以上的缩进,不管用何种方式你的代码已经有问题了,应该修正你的程序 + +也不要在一行里放多个赋值语句,内核代码风格超级简单,就是避免可能导致别人误读的表达式 + +除了注释-文档和 Kconfig 之外,不要使用空格来缩进,选用一个好的编辑器,不要在行尾留空格 + +--- + +## **把长的行和字符串打散** +代码风格的意义就在于使用平常使用的工具来维持代码的可读性和可维护性 + +每一行的长度的限制是 80 列,我们强烈建议您遵守这个惯例 + +这同样适用于有着很长参数列表的函数头 + +然而,绝对不要打散对用户可见的字符串 + +--- + +## **大括号和空格的放置** +把起始大括号放在行尾,而把结束大括号放在行首,这适用于所有的非函数语句块 + +```c +if (x is true) { + we do y +} +switch (action) { +case KOBJ_ADD: + return "add"; +case KOBJ_REMOVE: + return "remove"; +case KOBJ_CHANGE: + return "change"; +default: + return NULL; +} +``` + +函数的起始大括号放置于下一行的开头 + +```c +int function(int x) +{ + body of function +} +``` + +(大多数)关键字后要加一个空格,值得注意的例外是 `sizeof, typeof, alignof, __attribute__`,这些关键字某些程度上看起来更像函数,所以在这些关键字之后放一个空格: +`if, switch, case, for, do, while` + +不要在小括号里的表达式两侧加空格 + +在大多数二元和三元操作符两侧使用一个空格,例如下面所有这些操作符 +`= + - < > * / % | & ^ <= >= == != ? :` + +当声明指针类型或者返回指针类型的函数时, * 的首选使用方式是使之靠近变量名 或者函数名,而不是靠近类型名 + +但是一元操作符后不要加空格 +`& * + - ~ ! ++ --` + +--- + +## **命名** +C 是一个简朴的语言,你的命名也应该这样 + +全局变量(只有当你*真正*需要它们的时候再用它)需要有一个具描述性的名字,就像全局函数 + +本地变量名应该简短,而且能够表达相关的含义 + +- 变量/函数:小写 + 下划线 +- 宏/枚举:全大写 + 下划线 +- 结构体/类型 + - 结构体名:struct `my_struct_s` + - Typedef 类型名:`my_type_t` + +总的来说,如果一个指针或者一个结构体里的元素可以合理的被直接访问到,那么它们 就不应该是一个 typedef + +--- + +## **函数** +函数应该简短而漂亮,并且只完成一件事情。函数应该可以一屏或者两屏显示完 (我们 都知道 ISO/ANSI 屏幕大小是 80x24),只做一件事情,而且把它做好 + +函数的另外一个衡量标准是本地变量的数量。此数量不应超过 5-10 个,否则你的函数 就有问题了。重新考虑一下你的函数,把它分拆成更小的函数。人的大脑一般可以轻松的同时跟踪 7 个不同的事物,如果再增多的话,就会糊涂了 + +在函数原型中包含参数名和它们的数据类型 + +--- + +## **集中的函数退出途径** +当一个函数从多个位置退出,并且需要做一些类似清理的常见操作时,goto 语句就很方便了。如果并不需要清理操作,那么直接 return 即可 + +使用 goto 的理由是: +- 无条件语句容易理解和跟踪 +- 嵌套程度减小 +- 可以避免由于修改时忘记更新个别的退出点而导致错误 +- 让编译器省去删除冗余代码的工作 + +--- + +## **注释** +注释是好的,不过有过度注释的危险。永远不要在注释里解释你的代码是如何运作的,更好的做法是让别人一看你的代码就可以明白,一般来说你用注释告诉别人你的代码做了什么,而不是怎么做的 + +长 (多行) 注释的首选风格是: +```c +/* + * This is the preferred style for multi-line + * comments in the Linux kernel source code. + * Please use it consistently. + * + * Description: A column of asterisks on the left side, + * with beginning and ending almost-blank lines. + */ +``` + +注释数据也是很重要的,不管是基本类型还是衍生类型。为了方便实现这一点,每一行应只声明一个数据(不要使用逗号来一次声明多个数据)。这样你就有空间来为每个数据写一段小注释来解释它们的用途了 + +--- + +## **数据结构** +如果一个数据结构,在创建和销毁它的单线执行环境之外可见,那么它必须要有一个引用计数器 + +引用计数意味着你能够避免上锁,并且允许多个用户并行访问这个数据结构 + +--- + +## **宏,枚举和RTL** +用于定义常量的宏的名字及枚举里的标签需要大写 + +在定义几个相关的常量时,最好用枚举 + +宏的名字请用大写字母,不过形如函数的宏的名字可以用小写字母 + +含有多个语句的宏应该被包含在一个 do-while 代码块里 +```c +#define macrofun(a, b, c) \ + do { \ + if (a == 5) \ + do_this(b, c); \ + } while (0) +``` diff --git a/program/c_cpp/mylib/bit.h b/program/c_cpp/libs/bit.h similarity index 100% rename from program/c_cpp/mylib/bit.h rename to program/c_cpp/libs/bit.h diff --git a/program/c_cpp/mylib/dbtools.h b/program/c_cpp/libs/dbtools.h similarity index 100% rename from program/c_cpp/mylib/dbtools.h rename to program/c_cpp/libs/dbtools.h diff --git a/program/c_cpp/mylib/log/log.c b/program/c_cpp/libs/log/log.c similarity index 100% rename from program/c_cpp/mylib/log/log.c rename to program/c_cpp/libs/log/log.c diff --git a/program/c_cpp/mylib/log/log.h b/program/c_cpp/libs/log/log.h similarity index 100% rename from program/c_cpp/mylib/log/log.h rename to program/c_cpp/libs/log/log.h diff --git a/program/c_cpp/mylib/offsetof.h b/program/c_cpp/libs/offsetof.h similarity index 100% rename from program/c_cpp/mylib/offsetof.h rename to program/c_cpp/libs/offsetof.h diff --git a/program/c_cpp/mylib/string_hash.c b/program/c_cpp/libs/string_hash.c similarity index 100% rename from program/c_cpp/mylib/string_hash.c rename to program/c_cpp/libs/string_hash.c diff --git a/program/c_cpp/mylib/thread-pool.cpp b/program/c_cpp/libs/thread-pool.cpp similarity index 100% rename from program/c_cpp/mylib/thread-pool.cpp rename to program/c_cpp/libs/thread-pool.cpp diff --git a/program/c_cpp/universal/libs.md b/program/c_cpp/universal/libs.md index 0a1062b..dec6d82 100644 --- a/program/c_cpp/universal/libs.md +++ b/program/c_cpp/universal/libs.md @@ -29,13 +29,14 @@ - [CPP](#cpp) - [STL](../STL/) - [POSIX](#unistd) -- [SELF](#self) - - [offsetof](../mylib/offsetof.h) : 计算结构体成员相对结构体首地址的偏移度 - - [string hash](../mylib/string_hash.c) - - [thread pool](../mylib/thread-pool.cpp) - - [BIT](../mylib/bit.h) - - [LOG](../mylib/log/log.h) - - [dbtools](../mylib/dbtools.h) +- [OTHER]() +- [SELF]() + - [offsetof](../libs/offsetof.h) : 计算结构体成员相对结构体首地址的偏移度 + - [string hash](../libs/string_hash.c) + - [thread pool](../libs/thread-pool.cpp) : 简易线程池 + - [BIT](../libs/bit.h) : 有关位运算的函数封装 + - [LOG](../libs/log/log.h) : 简单日志系统 + - [dbtools](../libs/dbtools.h) : DEBUG工具集 --- diff --git a/tool/vim/.vimrc b/tool/vim/.vimrc index 16c1f43..05d81a7 100644 --- a/tool/vim/.vimrc +++ b/tool/vim/.vimrc @@ -8,7 +8,7 @@ set fileencoding=utf-8 set nocompatible "? "set guifont=consolas:h14 set wrap "自动折行 -set cursorline "光标行高亮 +"set cursorline "光标行高亮 set hlsearch "高亮显示搜索结果 set incsearch "搜索模式下,每输入一个字符,就跳到对应结果 set ignorecase "忽略搜索大小写