Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 48 lines (36 sloc) 1.062 kb
#!/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"
Jump to Line
Something went wrong with that request. Please try again.