signame.c 958 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* signame.c -- get the name of a signal
  2. Copyright 2012 The Go Authors. All rights reserved.
  3. Use of this source code is governed by a BSD-style
  4. license that can be found in the LICENSE file. */
  5. #include <string.h>
  6. #include "runtime.h"
  7. #include "arch.h"
  8. #include "malloc.h"
  9. String Signame (intgo sig) __asm__ (GOSYM_PREFIX "syscall.Signame");
  10. String
  11. Signame (intgo sig)
  12. {
  13. const char* s = NULL;
  14. char buf[100];
  15. size_t len;
  16. byte *data;
  17. String ret;
  18. #if defined(HAVE_STRSIGNAL)
  19. s = strsignal (sig);
  20. #endif
  21. if (s == NULL)
  22. {
  23. snprintf(buf, sizeof buf, "signal %ld", (long) sig);
  24. s = buf;
  25. }
  26. len = __builtin_strlen (s);
  27. data = runtime_mallocgc (len, nil, false);
  28. __builtin_memcpy (data, s, len);
  29. // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
  30. if ('A' <= data[0] && data[0] <= 'Z' && 'a' <= data[1] && data[1] <= 'z')
  31. data[0] += 'a' - 'A';
  32. ret.str = data;
  33. ret.len = len;
  34. return ret;
  35. }