misc.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "all.h"
  10. extern int cmdfd;
  11. Float
  12. famd(Float a, int b, int c, int d)
  13. {
  14. uint32_t x, m;
  15. x = (a + b) * c;
  16. m = x % d;
  17. x /= d;
  18. if(m >= d / 2)
  19. x++;
  20. return x;
  21. }
  22. uint32_t
  23. fdf(Float a, int d)
  24. {
  25. uint32_t x, m;
  26. m = a % d;
  27. x = a / d;
  28. if(m >= d / 2)
  29. x++;
  30. return x;
  31. }
  32. int32_t
  33. beint32(char *s)
  34. {
  35. uint8_t *x;
  36. x = (uint8_t *)s;
  37. return (x[0] << 24) + (x[1] << 16) + (x[2] << 8) + x[3];
  38. }
  39. void
  40. panic(char *fmt, ...)
  41. {
  42. char buf[8192], *s;
  43. va_list arg;
  44. s = buf;
  45. s += sprint(s, "%s %s %d: ", progname, procname, getpid());
  46. va_start(arg, fmt);
  47. s = vseprint(s, buf + sizeof(buf) / sizeof(*buf), fmt, arg);
  48. va_end(arg);
  49. *s++ = '\n';
  50. write(2, buf, s - buf);
  51. abort();
  52. exits(buf);
  53. }
  54. #define SIZE 4096
  55. void
  56. cprint(char *fmt, ...)
  57. {
  58. char buf[SIZE], *out;
  59. va_list arg;
  60. va_start(arg, fmt);
  61. out = vseprint(buf, buf+SIZE, fmt, arg);
  62. va_end(arg);
  63. write(cmdfd, buf, (int32_t)(out-buf));
  64. }
  65. /*
  66. * print goes to fd 2 [sic] because fd 1 might be
  67. * otherwise preoccupied when the -s flag is given to kfs.
  68. */
  69. int
  70. print(char *fmt, ...)
  71. {
  72. va_list arg;
  73. int n;
  74. va_start(arg, fmt);
  75. n = vfprint(2, fmt, arg);
  76. va_end(arg);
  77. return n;
  78. }