Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 48 lines (36 sloc) 1.062 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#!/bin/bash
#
# tool to convert symbols like EPERM to their numeric values and vice versa
#

shopt -s extglob

base=/usr/include/asm-generic/errno-base.h
upper=/usr/include/asm-generic/errno.h

exec 0< <(cat "$base" "$upper")

numeric_to_symbol() {
  awk -v "num=$1" '/^#define/ && $3 == num {
printf "%s = '$1'\n", $2
exit 1
}'
}

symbol_to_numeric() {
  awk -v "sym=$1" '/^#define/ && $2 == sym {
printf "'$1' = %s\n", $3
exit 1
}'
}

perror() {
  printf '#include <errno.h>
#include <string.h>
int main(void) { errno = %s; perror("message"); return 0; }
' "$1" | gcc -x c - -o /tmp/a.out; [[ -x /tmp/a.out ]] && /tmp/a.out; rm -f /tmp/a.out
}

[[ $1 ]] || { printf 'usage: %s [symbol|errno]\n' "${0##*/}"; exit 1; }>&2

declare -u arg=$1

# if its numeric, try to get the symbol
case "$arg" in
  +([[:digit:]])) numeric_to_symbol "$arg" ;;
  +([[:upper:]])) symbol_to_numeric "$arg" ;;
  *) printf 'unknown input: %s\n'>&2; exit 1 ;;
esac

# if successful (awk returns error) print the error message
(( $? )) && perror "$arg"

Something went wrong with that request. Please try again.