u_signal_names.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Signal name/number conversion routines.
  4. *
  5. * Copyright 2006 Rob Landley <rob@landley.net>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. static const struct signal_name {
  11. int number;
  12. char name[5];
  13. } signals[] = {
  14. // SUSv3 says kill must support these, and specifies the numerical values,
  15. // http://www.opengroup.org/onlinepubs/009695399/utilities/kill.html
  16. {0, "0"}, {1, "HUP"}, {2, "INT"}, {3, "QUIT"}, {6, "ABRT"}, {9, "KILL"},
  17. {14, "ALRM"}, {15, "TERM"},
  18. // And Posix adds the following:
  19. {SIGILL, "ILL"}, {SIGTRAP, "TRAP"}, {SIGFPE, "FPE"}, {SIGUSR1, "USR1"},
  20. {SIGSEGV, "SEGV"}, {SIGUSR2, "USR2"}, {SIGPIPE, "PIPE"}, {SIGCHLD, "CHLD"},
  21. {SIGCONT, "CONT"}, {SIGSTOP, "STOP"}, {SIGTSTP, "TSTP"}, {SIGTTIN, "TTIN"},
  22. {SIGTTOU, "TTOU"}
  23. };
  24. // Convert signal name to number.
  25. int get_signum(const char *name)
  26. {
  27. int i;
  28. i = atoi(name);
  29. if (i) return i;
  30. for (i = 0; i < sizeof(signals) / sizeof(struct signal_name); i++)
  31. if (!strcasecmp(signals[i].name, name) ||
  32. (!strncasecmp(signals[i].name, "SIG", 3)
  33. && !strcasecmp(signals[i].name+3, signals[i].name)))
  34. return signals[i].number;
  35. return -1;
  36. }
  37. // Convert signal number to name
  38. const char *get_signame(int number)
  39. {
  40. int i;
  41. static char buf[8];
  42. for (i=0; i < sizeof(signals) / sizeof(struct signal_name); i++) {
  43. if (number == signals[i].number) {
  44. return signals[i].name;
  45. }
  46. }
  47. itoa_to_buf(number, buf, 8);
  48. return buf;
  49. }