2019-10-04

GCC 32bit compile on Mac

  • The i386 architecture is deprecated for macOS
    $ gcc -m32 fib.c
    ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)
    ld: warning: ignoring file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd, missing required architecture i386 in file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd
    Undefined symbols for architecture i386:
      "_printf", referenced from:
          _main in fib-68f6b9.o
    ld: symbol(s) not found for architecture i386
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    
  • Install i386 support
    $ sudo rm -rf /Library/Developer/CommandLineTools
    $ xcode-select --install
    $ open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
    
  • Compile
    $ gcc -m32 fib.c
    ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)
    
    $ make CFLAGS="-m32" fib
    cc -m32    fib.c   -o fib
    ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)
    

2019-08-10

Paper Size in millimeter

A1 594X841 A2 420X594 A3 297X420 A4 210X297 A5 148X210 2ì ˆ 545X788 4ì ˆ 394X545 8ì ˆ 272X394 16ì ˆ 197X272 32ì ˆ 136X197 A1 594X841 A2 420X594 A3 297X420 A4 210X297 A5 148X210 2ì ˆ 545X788 4ì ˆ 394X545 8ì ˆ 272X394 16ì ˆ 197X272 32ì ˆ 136X197

2019-07-20

Broken attachment filename with Apple mail

With Apple mail, Korean attachment filename is broken.
Attachment filename is encoded to UTF-8
Run the following command within Terminal, then restart Apple mail.
$ defaults write com.apple.mail NSPreferredMailCharset "UTF-8"

2019-07-02

Rotate 7" touchscreen of Raspberry PI


  • Add parameters to /boot/config.txt
    display_rotate=1
    dtoverlay=rpi-ft5406,touchscreen-swapped-x-y=1,touchscreen-inverted-x=1
    
  • Parameters
    Rotate display
    
    display_rotate=0 # normal
    display_rotate=1 # rotate 90°
    display_rotate=2 # rotate 180°
    display_rotate=3 # rotate 270°
    
    Touchscreen
    
    touchscreen-size-x      Touchscreen X resolution, default 800
    touchscreen-size-y      Touchscreen Y resolution, default 600
    touchscreen-inverted-x  Invert touchscreen X coordinates, default 0
    touchscreen-inverted-y  Invert touchscreen Y coordinates, default 0
    touchscreen-swapped-x-y Swap X and Y coordinates, default 0
    

2019-06-27

Language performance test

Lower value shows better performance

  • C
    #include <stdio.h>
    
    int fib(int n) {
        if(n == 1) return 0;
        if(n == 2) return 1;
        return fib(n - 2) + fib(n - 1);
    }
    
    int main() {
        int i = 0;
        for(i = 1; i <= 40; i++) {
            printf("%d %d\n", i, fib(i));
        }
        printf("\nC\n");
        return 0;
    }
    
  • Java
    public class Fib {
     
        public static int calc(int n) {
            if(n <= 1) return 0;
            if(n == 2) return 1;
            return Fib.calc(n - 2) + Fib.calc(n - 1);
        }
        public static void main(String[] args) {
            for(int i = 1; i <= 40; i++) {
                System.out.println(i + " " + Fib.calc(i));
            }
            System.out.println("\nJAVA");
        }
     
    }
    
  • JavaScript
    #!/usr/bin/env node
    
    let range = Array.from({length: 40}, (x, i) => i+1);
    let fib = n => n === 1 ? 0 : n === 2 ? 1 : fib(n - 2) + fib(n - 1);
    
    range.forEach( i => console.log(i, fib(i)) );
    console.log("\nJavaScript");
    
  • PHP
    #!/usr/bin/env php
    <?php
    function fib(int $n) {
        if ($n === 1) return 0;
        if ($n === 2) return 1;
        return fib($n - 2) + fib($n - 1);
    }
    
    foreach (range(1, 40) as $i) {
        echo $i, ' ', fib($i), PHP_EOL;
    }
    echo "\nPHP\n";
    
  • Python
    #!/usr/bin/env python
    
    def fib(n):
      if n == 1:
        return 0
      elif n == 2:
        return 1
      return fib(n - 2) + fib(n - 1)
    
    if __name__ == "__main__":
      for i in range(1, 40 + 1):
        print(i, fib(i))
      print("\nPYTHON")
    
  • Ruby
    #!/usr/bin/env ruby
    
    def fib(n)
      return 0 if n == 1
      return 1 if n == 2
      return fib(n - 2) + fib(n - 1)
    end
    
    (1..40).each do |i|
      puts "#{i.to_s} #{fib(i).to_s}"
    end
    puts "\nRUBY"
    

2019-06-26

Allow SSH access based on Country using GeoIP

  1. Install packages
    $ sudo apt install geoip-bin geoip-database
    
  2. Create script
    $ sudo vi /usr/local/bin/sshfilter.sh
    
    
    #!/bin/bash
    
    ALLOWED_COUNTRIES="KR US GB NZ"
    ALLOWED_IP="192.168 127.0.0"
    
    [[ "`echo $1 | grep ':'`" != "" ]] && SUFFIX="6"
    if [[ $ALLOWED_IP =~ ${1:0:7} ]]; then
     logger -p authpriv.notice "SSH ALLOWED $1 LOCALDOMAIN"
     exit 0
    fi
    
    GEORESULT=`/usr/bin/geoiplookup${SUFFIX} "$1"`
    COUNTRY=`echo $GEORESULT | cut -d : -f 2 | xargs | head -1`
    COUNTRYCODE=`echo $COUNTRY | cut -d , -f 1 | xargs`
    [[ $ALLOWED_COUNTRIES =~ $COUNTRYCODE ]] && RESPONSE="ALLOWED" || RESPONSE="DENIED"
    logger -p authpriv.notice "SSH $RESPONSE $1 $COUNTRY"
    [[ $RESPONSE == "ALLOWED" ]] && exit 0 || exit 1
    
  3. edit /etc/hosts.allow and /etc/hosts.deny
    # /etc/hosts.allow
    sshd: ALL: /usr/local/bin/sshfilter.sh %a
    
    # /etc/hosts.deny
    sshd: ALL
    

SSH lockout 60min after 5 brute force attempts in 10min

  1. install fail2ban
    $ sudo apt update
    $ sudo apt install fail2ban
    
  2. edit configuration
    $ sudo vi /etc/fail2ban/jail.conf
    
    [sshd]
    enabled  = true
    port     = ssh
    maxretry = 5    # 5 attempts not permitted
    findtime = 600  # within 10 minutes
    bantime  = 3600 # ban 1 hour
    logpath  = %(sshd_log)s
    backend  = %(sshd_backend)s
    
  3. unban an ip address
    $ sudo fail2ban-client set sshd unbanip IP_ADDRESS
    

PIP upgrade all, python package

$ for package in `pip list --outdated | cut -d ' ' -f 1`
> do
> pip install --upgrade $package
> done

2019-06-12

Serial port communication on Mac with SCREEN command


Use screen command to communicate with Serial port

  1. Open Terminal or iTerm2
  2. See if the serial port exists
    $ ls -l /dev/cu.*
    crw-rw-rw-  1 root  wheel   20,   1 Jun 10 21:07 /dev/cu.Bluetooth-Incoming-Port
    crw-rw-rw-  1 root  wheel   20,   3 Jun 12 18:13 /dev/cu.usbmodem1421401
    
  3. Connect to the port
    $ screen -L /dev/cu.usbmodem1421401 115200
    -L : show log of the command from the serial port
  4. Other commands
    # in a running session
    Ctrl-A, K : kill the session
    Ctrl-A, D : detach from the session
    
    # out of session
    $ screen -ls : list sessions
    
    There is a screen on:
    9244.ttys001.Dee15S (Attached)
    
    Format : [PID].[TTYID].[HOSTNAME]
    
    $ screen -r : attach the default session
    $ screen -r [NAME] : attach to a session named [NAME]
    $ screen -S [NAME] : attach to a session named [NAME]
    $ screen -d [ID] : detach from the session with screen id [ID]