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