File: db_delete.c

package info (click to toggle)
man-db 2.3.10-65
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,440 kB
  • ctags: 1,826
  • sloc: ansic: 12,508; lex: 925; makefile: 642; sh: 328; sed: 93; perl: 62
file content (142 lines) | stat: -rw-r--r-- 3,270 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * db_delete.c: dbdelete(), database delete routine.
 *  
 * Copyright (C), 1994, 1995, Graeme W. Wilford. (Wilf.)
 *
 * You may distribute under the terms of the GNU Library General Public
 * License as specified in the file COPYING.LIB that comes with this
 * distribution.
 *
 * Mon Aug  8 20:35:30 BST 1994  Wilf. (G.Wilford@ee.surrey.ac.uk) 
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <assert.h>

#if defined(STDC_HEADERS)
#include <string.h>
#include <stdlib.h>
#elif defined(HAVE_STRING_H)
#include <string.h>
#elif defined(HAVE_STRINGS_H)
#include <strings.h>
#else /* no string(s) header file */
extern char *strtok();
extern char *strsep();
#endif /* STDC_HEADERS */

#if defined(HAVE_UNISTD_H)
#  include <unistd.h>
#endif /* HAVE_UNISTD_H */

#ifndef STDC_HEADERS
extern long atol();
extern char *strsep();
#endif /* not STDC_HEADERS */

#define NLS_SET	db_deleteSet
#include <libintl.h>
#define _(String) gettext (String)

#include "manconfig.h"
#include "lib/error.h"
#include "mydbm.h"
#include "db_storage.h"

/* Delete an entry for a page.
   Again, 3 possibilities:

   1) page is singular reference, just delete it :)
   2) page has 2+ companions. Delete page and alter multi entry to not 
      point to it anymore.
   3) page has 1 companion. Could do as (2), but we'd waste an entry in 
      the db. Should delete page, extract friend and reinsert as singular,
      overwriting the old multi entry.
*/

#define NO_ENTRY	1;

int dbdelete(char *name, struct mandata *info)
{
	datum key, cont;

	/* get entry for info */

	if (debug)
		fprintf(stderr, "Attempting delete of %s(%s) entry.\n", 
			name, info->ext);

	key.dptr = name;
	key.dsize = strlen(key.dptr) + 1;
	cont = MYDBM_FETCH(dbf, key);

	if (!cont.dptr) {			/* 0 entries */
		return NO_ENTRY;
	} else if (*cont.dptr != '\t') {	/* 1 entry */
		MYDBM_DELETE(dbf, key);
		MYDBM_FREE(cont.dptr);
	} else {				/* 2+ entries */
		char *ext[ENTRIES], **e;
		char *multi_content = NULL;
		datum multi_key;
		int refs;

		/* Extract all of the extensions associated with 
		   this key */

		refs = list_extensions(cont.dptr + 1, e = ext);
		
		while (*e && strcmp(*e, info->ext) != 0)
			e++;

		if (!*e) {
			MYDBM_FREE(cont.dptr);
			return NO_ENTRY;
		}
			
		multi_key = make_multi_key(name, *e);
		if (!MYDBM_EXISTS(dbf, multi_key)) {
			error (0, 0,
			       _( "multi key %s does not exist"),
			       multi_key.dptr);
			gripe_corrupt_data();
		}
		MYDBM_DELETE(dbf, multi_key);
		free(multi_key.dptr);
		**e = '\0';

		/* refs *may* be 1 if all manual pages with this name 
		   have been deleted. In this case, we'll have to remove 
		   the key too */
		   
		if (refs == 1) {
			MYDBM_FREE(cont.dptr);
			MYDBM_DELETE(dbf, key);
			return 0;
		}
			 
		/* create our new multi content */
		for (e = ext; *e; e++) {
			if (**e)
				multi_content = strappend(multi_content,
							  "\t", *e, NULL);
		}

		MYDBM_FREE(cont.dptr);

		/* if refs = 2 do something else. Doesn't really matter as
		   the gdbm db file does not shrink any after a deletion
		   anyway */

		cont.dptr = multi_content;
		cont.dsize = strlen(cont.dptr) + 1;

		if (MYDBM_REPLACE(dbf, key, cont))
			gripe_replace_key(key.dptr);
	}
	return 0;
}