convbio.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <ctype.h>
  13. #include "authcmdlib.h"
  14. void
  15. clrbio(Acctbio *a)
  16. {
  17. int i;
  18. if(a->user)
  19. free(a->user);
  20. if(a->name)
  21. free(a->name);
  22. if(a->dept)
  23. free(a->dept);
  24. for(i = 0; i < Nemail; i++)
  25. if(a->email[i])
  26. free(a->email[i]);
  27. memset(a, 0, sizeof(Acctbio));
  28. }
  29. int
  30. ordbio(Biobuf *b, Acctbio *a)
  31. {
  32. char *p, *cp, *next;
  33. int ne;
  34. clrbio(a);
  35. while(p = Brdline(b, '\n')){
  36. if(*p == '\n')
  37. continue;
  38. p[Blinelen(b)-1] = 0;
  39. /* get user */
  40. for(cp = p; *cp && *cp != ' ' && *cp != '\t'; cp++)
  41. ;
  42. a->user = malloc(cp - p + 1);
  43. strncpy(a->user, p, cp - p);
  44. a->user[cp - p] = 0;
  45. p = cp;
  46. /* get name */
  47. while(*p == ' ' || *p == '\t')
  48. p++;
  49. for(cp = p; *cp; cp++){
  50. if(isdigit(*cp) || *cp == '<'){
  51. while(cp > p && *(cp-1) != ' ' && *(cp-1) != '\t')
  52. cp--;
  53. break;
  54. }
  55. }
  56. next = cp;
  57. while(cp > p && (*(cp-1) == ' ' || *(cp-1) == '\t'))
  58. cp--;
  59. a->name = malloc(cp - p + 1);
  60. strncpy(a->name, p, cp - p);
  61. a->name[cp - p] = 0;
  62. p = next;
  63. /* get dept */
  64. for(cp = p; *cp; cp++){
  65. if(*cp == '<')
  66. break;
  67. }
  68. next = cp;
  69. while(cp > p && (*(cp-1) == ' ' || *(cp-1) == '\t'))
  70. cp--;
  71. a->dept = malloc(cp - p + 1);
  72. strncpy(a->dept, p, cp - p);
  73. a->dept[cp - p] = 0;
  74. p = next;
  75. /* get emails */
  76. ne = 0;
  77. for(cp = p; *cp && ne < Nemail;){
  78. if(*cp != '<'){
  79. cp++;
  80. continue;
  81. }
  82. p = ++cp;
  83. while(*cp && *cp != '>')
  84. cp++;
  85. if(cp == p)
  86. break;
  87. a->email[ne] = malloc(cp - p + 1);
  88. strncpy(a->email[ne], p, cp - p);
  89. a->email[ne][cp-p] = 0;
  90. ne++;
  91. }
  92. return 0;
  93. }
  94. return -1;
  95. }
  96. void
  97. nwrbio(Biobuf *b, Acctbio *a)
  98. {
  99. int i;
  100. if(a->postid == 0)
  101. a->postid = "";
  102. if(a->name == 0)
  103. a->name = "";
  104. if(a->dept == 0)
  105. a->dept = "";
  106. if(a->email[0] == 0)
  107. a->email[0] = strdup(a->user);
  108. Bprint(b, "%s|%s|%s|%s|%s", a->user, a->user, a->name, a->dept, a->email[0]);
  109. for(i = 1; i < Nemail; i++){
  110. if(a->email[i] == 0)
  111. break;
  112. Bprint(b, "|%s", a->email[i]);
  113. }
  114. Bprint(b, "\n");
  115. }
  116. void
  117. main(void)
  118. {
  119. Biobuf in, out;
  120. Acctbio a;
  121. Binit(&in, 0, OREAD);
  122. Binit(&out, 1, OWRITE);
  123. while(ordbio(&in, &a) == 0)
  124. nwrbio(&out, &a);
  125. Bterm(&in);
  126. Bterm(&out);
  127. }