/contrib/cvs/src/myndbm.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 331 lines · 253 code · 32 blank · 46 comment · 50 complexity · 0cd538fd9e1d312abf0fe6e1e2a493ca MD5 · raw file

  1. /*
  2. * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
  3. *
  4. * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
  5. * and others.
  6. *
  7. * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
  8. * Portions Copyright (C) 1989-1992, Brian Berliner
  9. *
  10. * You may distribute under the terms of the GNU General Public License as
  11. * specified in the README file that comes with the CVS source distribution.
  12. *
  13. * A simple ndbm-emulator for CVS. It parses a text file of the format:
  14. *
  15. * key value
  16. *
  17. * at dbm_open time, and loads the entire file into memory. As such, it is
  18. * probably only good for fairly small modules files. Ours is about 30K in
  19. * size, and this code works fine.
  20. */
  21. #include <assert.h>
  22. #include "cvs.h"
  23. #include "getline.h"
  24. #ifdef MY_NDBM
  25. # ifndef O_ACCMODE
  26. # define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
  27. # endif /* defined O_ACCMODE */
  28. static void mydbm_load_file PROTO ((FILE *, List *, char *));
  29. /* Returns NULL on error in which case errno has been set to indicate
  30. the error. Can also call error() itself. */
  31. /* ARGSUSED */
  32. DBM *
  33. mydbm_open (file, flags, mode)
  34. char *file;
  35. int flags;
  36. int mode;
  37. {
  38. FILE *fp;
  39. DBM *db;
  40. fp = CVS_FOPEN (file, (flags & O_ACCMODE) != O_RDONLY ?
  41. FOPEN_BINARY_READWRITE : FOPEN_BINARY_READ);
  42. if (fp == NULL && !(existence_error (errno) && (flags & O_CREAT)))
  43. return ((DBM *) 0);
  44. db = (DBM *) xmalloc (sizeof (*db));
  45. db->dbm_list = getlist ();
  46. db->modified = 0;
  47. db->name = xstrdup (file);
  48. if (fp != NULL)
  49. {
  50. mydbm_load_file (fp, db->dbm_list, file);
  51. if (fclose (fp) < 0)
  52. error (0, errno, "cannot close %s", file);
  53. }
  54. return (db);
  55. }
  56. static int write_item PROTO ((Node *, void *));
  57. static int
  58. write_item (node, data)
  59. Node *node;
  60. void *data;
  61. {
  62. FILE *fp = (FILE *)data;
  63. fputs (node->key, fp);
  64. fputs (" ", fp);
  65. fputs (node->data, fp);
  66. fputs ("\012", fp);
  67. return 0;
  68. }
  69. void
  70. mydbm_close (db)
  71. DBM *db;
  72. {
  73. if (db->modified)
  74. {
  75. FILE *fp;
  76. fp = CVS_FOPEN (db->name, FOPEN_BINARY_WRITE);
  77. if (fp == NULL)
  78. error (1, errno, "cannot write %s", db->name);
  79. walklist (db->dbm_list, write_item, (void *)fp);
  80. if (fclose (fp) < 0)
  81. error (0, errno, "cannot close %s", db->name);
  82. }
  83. free (db->name);
  84. dellist (&db->dbm_list);
  85. free ((char *) db);
  86. }
  87. datum
  88. mydbm_fetch (db, key)
  89. DBM *db;
  90. datum key;
  91. {
  92. Node *p;
  93. char *s;
  94. datum val;
  95. /* make sure it's null-terminated */
  96. s = xmalloc (key.dsize + 1);
  97. (void) strncpy (s, key.dptr, key.dsize);
  98. s[key.dsize] = '\0';
  99. p = findnode (db->dbm_list, s);
  100. if (p)
  101. {
  102. val.dptr = p->data;
  103. val.dsize = strlen (p->data);
  104. }
  105. else
  106. {
  107. val.dptr = (char *) NULL;
  108. val.dsize = 0;
  109. }
  110. free (s);
  111. return (val);
  112. }
  113. datum
  114. mydbm_firstkey (db)
  115. DBM *db;
  116. {
  117. Node *head, *p;
  118. datum key;
  119. head = db->dbm_list->list;
  120. p = head->next;
  121. if (p != head)
  122. {
  123. key.dptr = p->key;
  124. key.dsize = strlen (p->key);
  125. }
  126. else
  127. {
  128. key.dptr = (char *) NULL;
  129. key.dsize = 0;
  130. }
  131. db->dbm_next = p->next;
  132. return (key);
  133. }
  134. datum
  135. mydbm_nextkey (db)
  136. DBM *db;
  137. {
  138. Node *head, *p;
  139. datum key;
  140. head = db->dbm_list->list;
  141. p = db->dbm_next;
  142. if (p != head)
  143. {
  144. key.dptr = p->key;
  145. key.dsize = strlen (p->key);
  146. }
  147. else
  148. {
  149. key.dptr = (char *) NULL;
  150. key.dsize = 0;
  151. }
  152. db->dbm_next = p->next;
  153. return (key);
  154. }
  155. /* Note: only updates the in-memory copy, which is written out at
  156. mydbm_close time. Note: Also differs from DBM in that on duplication,
  157. it gives a warning, rather than either DBM_INSERT or DBM_REPLACE
  158. behavior. */
  159. int
  160. mydbm_store (db, key, value, flags)
  161. DBM *db;
  162. datum key;
  163. datum value;
  164. int flags;
  165. {
  166. Node *node;
  167. node = getnode ();
  168. node->type = NDBMNODE;
  169. node->key = xmalloc (key.dsize + 1);
  170. *node->key = '\0';
  171. strncat (node->key, key.dptr, key.dsize);
  172. node->data = xmalloc (value.dsize + 1);
  173. *(char *)node->data = '\0';
  174. strncat (node->data, value.dptr, value.dsize);
  175. db->modified = 1;
  176. if (addnode (db->dbm_list, node) == -1)
  177. {
  178. error (0, 0, "attempt to insert duplicate key `%s'", node->key);
  179. freenode (node);
  180. return 0;
  181. }
  182. return 0;
  183. }
  184. static void
  185. mydbm_load_file (fp, list, filename)
  186. FILE *fp;
  187. List *list;
  188. char *filename; /* Used in error messages. */
  189. {
  190. char *line = NULL;
  191. size_t line_size;
  192. char *value;
  193. size_t value_allocated;
  194. char *cp, *vp;
  195. int cont;
  196. int line_length;
  197. int line_num;
  198. value_allocated = 1;
  199. value = xmalloc (value_allocated);
  200. cont = 0;
  201. line_num=0;
  202. while ((line_length =
  203. getstr (&line, &line_size, fp, '\012', 0, GETLINE_NO_LIMIT)) >= 0)
  204. {
  205. line_num++;
  206. if (line_length > 0 && line[line_length - 1] == '\012')
  207. {
  208. /* Strip the newline. */
  209. --line_length;
  210. line[line_length] = '\0';
  211. }
  212. if (line_length > 0 && line[line_length - 1] == '\015')
  213. {
  214. /* If the file (e.g. modules) was written on an NT box, it will
  215. contain CRLF at the ends of lines. Strip them (we can't do
  216. this by opening the file in text mode because we might be
  217. running on unix). */
  218. --line_length;
  219. line[line_length] = '\0';
  220. }
  221. /*
  222. * Add the line to the value, at the end if this is a continuation
  223. * line; otherwise at the beginning, but only after any trailing
  224. * backslash is removed.
  225. */
  226. if (!cont)
  227. value[0] = '\0';
  228. /*
  229. * See if the line we read is a continuation line, and strip the
  230. * backslash if so.
  231. */
  232. if (line_length > 0)
  233. cp = &line[line_length - 1];
  234. else
  235. cp = line;
  236. if (*cp == '\\')
  237. {
  238. cont = 1;
  239. *cp = '\0';
  240. --line_length;
  241. }
  242. else
  243. {
  244. cont = 0;
  245. }
  246. expand_string (&value,
  247. &value_allocated,
  248. strlen (value) + line_length + 5);
  249. strcat (value, line);
  250. if (value[0] == '#')
  251. continue; /* comment line */
  252. vp = value;
  253. while (*vp && isspace ((unsigned char) *vp))
  254. vp++;
  255. if (*vp == '\0')
  256. continue; /* empty line */
  257. /*
  258. * If this was not a continuation line, add the entry to the database
  259. */
  260. if (!cont)
  261. {
  262. Node *p = getnode ();
  263. char *kp;
  264. kp = vp;
  265. while (*vp && !isspace ((unsigned char) *vp))
  266. vp++;
  267. if (*vp)
  268. *vp++ = '\0'; /* NULL terminate the key */
  269. p->type = NDBMNODE;
  270. p->key = xstrdup (kp);
  271. while (*vp && isspace ((unsigned char) *vp))
  272. vp++; /* skip whitespace to value */
  273. if (*vp == '\0')
  274. {
  275. if (!really_quiet)
  276. error (0, 0,
  277. "warning: NULL value for key `%s' at line %d of `%s'",
  278. p->key, line_num, filename);
  279. freenode (p);
  280. continue;
  281. }
  282. p->data = xstrdup (vp);
  283. if (addnode (list, p) == -1)
  284. {
  285. if (!really_quiet)
  286. error (0, 0,
  287. "duplicate key found for `%s' at line %d of `%s'",
  288. p->key, line_num, filename);
  289. freenode (p);
  290. }
  291. }
  292. }
  293. if (line_length < 0 && !feof (fp))
  294. /* FIXME: should give the name of the file. */
  295. error (0, errno, "cannot read file in mydbm_load_file");
  296. free (line);
  297. free (value);
  298. }
  299. #endif /* MY_NDBM */