目錄表

Overview and Tools

0x00 Outline


0x01 Prepare your UNIX environment

使用 Ubuntu Linux (Debian-based) 為例,建議最少需要以下套件

$ sudo apt-get install gcc g++ gdb make manpages-dev manpages-posix manpages-posix-dev

0x02 A Brief Introduction to the UNIX environment

Booting Process

開機後依序會經過下面流程

File System Architecture

Everything starts from the “root directory” (/)

UNIX Commands

Common Notations

UNIX 文件中的提示符號

Man page

$ man [section] page
  1. User commands
  2. System calls
  3. C library functions
  4. Devices and special files
  5. File formats and conventions
  6. Games et al.
  7. Miscellaneous
  8. System administration tools and daemons

0x03 Fundamental UNIX Programming Practices

#include <stdio.h>
 
int main() {
    printf("Hello, World.\n");
    return 0;
}

Return value of the main() function

Read return values from your program

Shell’s short cut branch

Boolean OR (||) – Evaluate until a condition is true

$ ./return 0 || echo 'A'
$ ./return 1 || echo 'B'

Boolean AND (&&) – Evaluate until a condition is not false

$ ./return 0 && echo 'C'
$ ./return 1 && echo 'D'

Handle program options

參數包含程式名稱本身

#include <unistd.h>
 
int getopt(int argc, char * const argv[], const char *optstring);
 
extern char *optarg;
extern int optind, opterr, optopt;
 
/*Return: option now if OK, -1 if no more options, Colon (:) or question mark (?) if Invalid option encountered*/
 
#include <getopt.h>
 
int getopt_long(int argc, char * const argv[],
           const char *optstring,
           const struct option *longopts, int *longindex);
 
int getopt_long_only(int argc, char * const argv[],
           const char *optstring,
           const struct option *longopts, int *longindex);
 
struct option {
    const char *name;
    int         has_arg;
    int        *flag;
    int         val;
};

UNIX time representations

struct timeval {
    long tv_sec; // seconds
    long tv_usec; // microseconds
};

相關函式

#include<time.h>
 
time_t time(time_t *t);
int gettimeofday(struct timeval *tv, struct timezone *tz);
clock_t clock(void);

0x04 Tools


0x05 參考資料