util.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ndb.h>
  5. #include <fcall.h>
  6. #include <thread.h>
  7. #include <9p.h>
  8. #include <ctype.h>
  9. #include "dat.h"
  10. #include "fns.h"
  11. void*
  12. erealloc(void *a, uint n)
  13. {
  14. a = realloc(a, n);
  15. if(a == nil)
  16. sysfatal("realloc %d: out of memory", n);
  17. setrealloctag(a, getcallerpc(&a));
  18. return a;
  19. }
  20. void*
  21. emalloc(uint n)
  22. {
  23. void *a;
  24. a = mallocz(n, 1);
  25. if(a == nil)
  26. sysfatal("malloc %d: out of memory", n);
  27. setmalloctag(a, getcallerpc(&n));
  28. return a;
  29. }
  30. char*
  31. estrdup(char *s)
  32. {
  33. s = strdup(s);
  34. if(s == nil)
  35. sysfatal("strdup: out of memory");
  36. setmalloctag(s, getcallerpc(&s));
  37. return s;
  38. }
  39. char*
  40. estredup(char *s, char *e)
  41. {
  42. char *t;
  43. t = emalloc(e-s+1);
  44. memmove(t, s, e-s);
  45. t[e-s] = '\0';
  46. setmalloctag(t, getcallerpc(&s));
  47. return t;
  48. }
  49. char*
  50. estrmanydup(char *s, ...)
  51. {
  52. char *p, *t;
  53. int len;
  54. va_list arg;
  55. len = strlen(s);
  56. va_start(arg, s);
  57. while((p = va_arg(arg, char*)) != nil)
  58. len += strlen(p);
  59. len++;
  60. t = emalloc(len);
  61. strcpy(t, s);
  62. va_start(arg, s);
  63. while((p = va_arg(arg, char*)) != nil)
  64. strcat(t, p);
  65. return t;
  66. }
  67. char*
  68. strlower(char *s)
  69. {
  70. char *t;
  71. for(t=s; *t; t++)
  72. if('A' <= *t && *t <= 'Z')
  73. *t += 'a'-'A';
  74. return s;
  75. }