Deleted Added
sdiff udiff text old ( 8870 ) new ( 69656 )
full compact
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 15 unchanged lines hidden (view full) ---

24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: head/lib/libc/gen/telldir.c 69656 2000-12-06 03:15:49Z deischen $
34 */
35
36#if defined(LIBC_SCCS) && !defined(lint)
37static char sccsid[] = "@(#)telldir.c 8.1 (Berkeley) 6/4/93";
38#endif /* LIBC_SCCS and not lint */
39
40#include <sys/param.h>
41#include <dirent.h>

--- 8 unchanged lines hidden (view full) ---

50#define SINGLEUSE
51
52/*
53 * One of these structures is malloced to describe the current directory
54 * position each time telldir is called. It records the current magic
55 * cookie returned by getdirentries and the offset within the buffer
56 * associated with that return value.
57 */
58struct _ddloc {
59 LIST_ENTRY(_ddloc) loc_lqe; /* entry in list */
60 long loc_index; /* key associated with structure */
61 long loc_seek; /* magic cookie returned by getdirentries */
62 long loc_loc; /* offset of entry in buffer */
63};
64
65/*
66 * return a pointer into a directory
67 */
68long
69telldir(dirp)
70 DIR *dirp;
71{
72 struct _ddloc *lp;
73
74 if ((lp = (struct _ddloc *)malloc(sizeof(struct _ddloc))) == NULL)
75 return (-1);
76 lp->loc_index = dirp->dd_loccnt++;
77 lp->loc_seek = dirp->dd_seek;
78 lp->loc_loc = dirp->dd_loc;
79 LIST_INSERT_HEAD(&dirp->dd_locq, lp, loc_lqe);
80 return (lp->loc_index);
81}
82
83/*
84 * seek to an entry in a directory.
85 * Only values returned by "telldir" should be passed to seekdir.
86 */
87void
88_seekdir(dirp, loc)
89 DIR *dirp;
90 long loc;
91{
92 struct _ddloc *lp;
93 struct dirent *dp;
94
95 LIST_FOREACH(lp, &dirp->dd_locq, loc_lqe) {
96 if (lp->loc_index == loc)
97 break;
98 }
99 if (lp == NULL)
100 return;
101 if (lp->loc_loc == dirp->dd_loc && lp->loc_seek == dirp->dd_seek)
102 goto found;
103 (void) lseek(dirp->dd_fd, (off_t)lp->loc_seek, SEEK_SET);
104 dirp->dd_seek = lp->loc_seek;
105 dirp->dd_loc = 0;
106 while (dirp->dd_loc < lp->loc_loc) {
107 dp = readdir(dirp);
108 if (dp == NULL)
109 break;
110 }
111found:
112#ifdef SINGLEUSE
113 LIST_REMOVE(lp, loc_lqe);
114 free((caddr_t)lp);
115#endif
116}
117
118/*
119 * Reclaim memory for telldir cookies which weren't used.
120 */
121void
122_reclaim_telldir(dirp)
123 DIR *dirp;
124{
125 struct _ddloc *lp;
126 struct _ddloc *templp;
127
128 lp = LIST_FIRST(&dirp->dd_locq);
129 while (lp != NULL) {
130 templp = lp;
131 lp = LIST_NEXT(lp, loc_lqe);
132 free(templp);
133 }
134 LIST_INIT(&dirp->dd_locq);
135}