makedatprog.c
上传用户:s81996212
上传日期:2007-01-04
资源大小:722k
文件大小:2k
源码类别:

WEB邮件程序

开发平台:

C/C++

  1. /*
  2. ** Copyright 1998 - 1999 Double Precision, Inc.
  3. ** See COPYING for distribution information.
  4. */
  5. #if HAVE_CONFIG_H
  6. #include "config.h"
  7. #endif
  8. #if HAVE_UNISTD_H
  9. #include <unistd.h>
  10. #endif
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include "dbobj.h"
  15. static const char rcsid[]="$Id: makedatprog.c,v 1.4 1999/12/06 13:22:17 mrsam Exp $";
  16. static int addgdbm(char *p, struct dbobj *o)
  17. {
  18. char *key, *val;
  19. if (!*p || *p == '#')
  20. return (0);
  21. key=p;
  22. if ( (val=strchr(p, 't')) == 0) val="";
  23. else *val++=0;
  24. if (*key)
  25. {
  26. if (!*val) val="1";
  27. if (dbobj_store(o, key, strlen(key), val, strlen(val), "I"))
  28. {
  29. fprintf(stderr, "Cannot store record for %s - duplicate or out of disk space.n", key);
  30. return (-1);
  31. }
  32. }
  33. return (0);
  34. }
  35. static int buildgdbm(FILE *i, struct dbobj *o)
  36. {
  37. char *buf=0;
  38. size_t bufsize, buflen;
  39. bufsize=0;
  40. buflen=0;
  41. for (;;)
  42. {
  43. int c;
  44. buflen=0;
  45. for (;;)
  46. {
  47. if (buflen >= bufsize)
  48. {
  49. bufsize += 256;
  50. buf= buf ? realloc(buf, bufsize):
  51. malloc(bufsize);
  52. if (!buf)
  53. {
  54. perror("malloc");
  55. return (-1);
  56. }
  57. }
  58. c=getc(i);
  59. if (c == 'n' || c == EOF) break;
  60. buf[buflen++]=c;
  61. }
  62. buf[buflen]=0;
  63. if (c == EOF) return (-1);
  64. if (strcmp(buf, ".") == 0) return (0);
  65. if (addgdbm(buf, o)) return (-1);
  66. }
  67. }
  68. int main(int argc, char **argv)
  69. {
  70. FILE *i;
  71. struct dbobj obj;
  72. if (argc < 4)
  73. {
  74. fprintf(stderr, "Usage: %s textfile tmpfile gdbmfilen",
  75. argv[0]);
  76. exit(1);
  77. }
  78. if (strcmp(argv[1], "-") == 0)
  79. i= stdin;
  80. else
  81. {
  82. if ((i=fopen(argv[1], "r")) == 0)
  83. {
  84. perror(argv[1]);
  85. exit(1);
  86. }
  87. }
  88. dbobj_init(&obj);
  89. if (dbobj_open(&obj, argv[2], "N"))
  90. {
  91. fprintf(stderr, "Cannot create %sn", argv[2]);
  92. exit (1);
  93. }
  94. if (buildgdbm(i, &obj))
  95. {
  96. dbobj_close(&obj);
  97. unlink(argv[2]);
  98. exit (1);
  99. }
  100. dbobj_close(&obj);
  101. if (rename(argv[2], argv[3]))
  102. {
  103. perror("rename");
  104. unlink(argv[2]);
  105. exit(1);
  106. }
  107. exit(0);
  108. return (0);
  109. }