0%

Linux File Magic Number

  • file <file_name>
  • magic numbers are usually stored in (linux) /usr/share/file/magic
  • also File Signatures, Magic Bytes
  • A magic number is a numeric or string constant that indicates the file type.
  • This number is in the first 512 bytes of the file.
    • Originally, this term was used for a specific set of 2-byte identifiers at the beginnings of files, but since any binary sequence can be regarded as a number, any feature of a file format which uniquely distinguishes it can be used for identification
  • By default the localized magic file /usr/lib/locale/locale/LC_MESSAGES/magic is used to identify files that have a magic number.
    • If a localized magic file does not exist, the /etc/magic file is utilized.
      閱讀全文 »

C/C++ Compiler

  • gcc and g++分別是gnu的c & c++編譯器 gcc/g++在執行編譯工作的時候,總共需要4步
    1. 預處理,生成.i的文件 [預處理器cpp]
    2. 將預處理後的文件不轉換成彙編語言,生成文件.s [編譯器egcs]
    3. 有彙編變為目標代碼(機器代碼)生成.o的文件 [彙編器as]
    4. 連接目標代碼,生成可執行程序 [鏈接器ld]
      閱讀全文 »

C/C++ optind, getopt

  • getopt, getopt_long, getopt_long_only, optarg, optind, opterr, optopt
  • #include <getopt.h>
  • Parse command-line options
  • An element of argv that starts with ‘-‘ (and is not exactly “-“ or “–”) is an option element
    閱讀全文 »

Linux Kernel Modules

  • 系統啟動後,正常工作的模組都在/proc/modules檔案中列出。使用lsmod命令也可顯示相同內容。
  • Linux 會將所有核心模組放在 /lib/modules/uname -r/kernel/drivers/ 目錄下
    閱讀全文 »

ntpq

  • standard NTP query program

introduction

  • ntpq [-46dinp] [-c command] [host] [...]
  • ntpq uses NTP mode 6 packets to communicate with the NTP server, and hence can be used to query any compatible server on the network which permits it.
    • Note that since NTP is a UDP protocol this communication will be somewhat unreliable, especially over large distances in terms of network topology. ntpq makes one attempt to retransmit requests, and will time requests out if the remote host is not heard from within a suitable timeout time.
      閱讀全文 »

ntpd

introduction

1
2
systemctl start ntpd
systemctl status ntpd
  • 編輯 /etc/ntp.conf
    • e.g.
      1
      2
      3
      4
      5
      6
      # 自己指定 NTP 伺服器
      server tock.stdtime.gov.tw
      server watch.stdtime.gov.tw
      server time.stdtime.gov.tw
      server clock.stdtime.gov.tw
      server tick.stdtime.gov.tw
      閱讀全文 »

Vim Plugin

management

vim-plug

  • Download plug.vim and put it in the “autoload” directory.

    • curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  • .vimrc

    1
    2
    3
    call plug#begin('~/.vim/plugged')
    Plug 'plugin'
    call plug#end()
  • install

    1
    2
    3
    // 先退出 vim 再進去執行 :PluginInstall
    // or
    vim +PluginInstall +qall
    閱讀全文 »

ntpdate

  • NTP 服務(ntpd)本身就有自動校時的功能,若啟用 NTP 服務後,就不可以使用 ntpdate 的方式校時,兩者僅能擇一使用。
  • 以手動更新系統時間
  • ntpdate time.stdtime.gov.tw
    :::info
  • 台灣常見 NTP 伺服器
    • tock.stdtime.gov.tw
    • watch.stdtime.gov.tw
    • time.stdtime.gov.tw
    • clock.stdtime.gov.tw
    • tick.stdtime.gov.tw
      :::
      閱讀全文 »

C++ fstream, ifstream, ofstream

ifstream

  • constructor
  1. default constructor
    • Constructs an ifstream object that is not associated with any file.
    • Internally, its istream base constructor is passed a pointer to a newly constructed filebuf object (the internal file stream buffer).
  2. initialization constructor
    • Constructs an ifstream object, initially associated with the file identified by its first argument (filename), open with the mode specified by mode.
    • Internally, its istream base constructor is passed a pointer to a newly constructed filebuf object (the internal file stream buffer). Then, filebuf::open is called with filename and mode as arguments.
    • If the file cannot be opened, the stream’s failbit flag is set.
  3. copy constructor (deleted)
    • Deleted (no copy constructor).
  4. move constructor
    • Acquires the contents of x.
    • First, the function move-constructs both its base istream class from x and a filebuf object from x’s internal filebuf object, and then associates them by calling member set_rdbuf.
    • x is left in an unspecified but valid state.
      閱讀全文 »

C/C++ Check File Exists

  • 主要有幾種方式
    • ifstream (C++)
    • FILE (C)
    • access()
    • std::filesystem::exists() (C++17)
    • boost::filesystem::exists() (Boost)
      閱讀全文 »