misc.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "all.h"
  2. extern int cmdfd;
  3. Float
  4. famd(Float a, int b, int c, int d)
  5. {
  6. ulong x, m;
  7. x = (a + b) * c;
  8. m = x % d;
  9. x /= d;
  10. if(m >= d / 2)
  11. x++;
  12. return x;
  13. }
  14. ulong
  15. fdf(Float a, int d)
  16. {
  17. ulong x, m;
  18. m = a % d;
  19. x = a / d;
  20. if(m >= d / 2)
  21. x++;
  22. return x;
  23. }
  24. long
  25. belong(char *s)
  26. {
  27. uchar *x;
  28. x = (uchar *)s;
  29. return (x[0] << 24) + (x[1] << 16) + (x[2] << 8) + x[3];
  30. }
  31. void
  32. panic(char *fmt, ...)
  33. {
  34. char buf[8192], *s;
  35. va_list arg;
  36. s = buf;
  37. s += sprint(s, "%s %s %d: ", progname, procname, getpid());
  38. va_start(arg, fmt);
  39. s = vseprint(s, buf + sizeof(buf) / sizeof(*buf), fmt, arg);
  40. va_end(arg);
  41. *s++ = '\n';
  42. write(2, buf, s - buf);
  43. abort();
  44. exits(buf);
  45. }
  46. #define SIZE 4096
  47. void
  48. cprint(char *fmt, ...)
  49. {
  50. char buf[SIZE], *out;
  51. va_list arg;
  52. va_start(arg, fmt);
  53. out = vseprint(buf, buf+SIZE, fmt, arg);
  54. va_end(arg);
  55. write(cmdfd, buf, (long)(out-buf));
  56. }
  57. /*
  58. * print goes to fd 2 [sic] because fd 1 might be
  59. * otherwise preoccupied when the -s flag is given to kfs.
  60. */
  61. int
  62. print(char *fmt, ...)
  63. {
  64. va_list arg;
  65. int n;
  66. va_start(arg, fmt);
  67. n = vfprint(2, fmt, arg);
  68. va_end(arg);
  69. return n;
  70. }