comparison mpffs/find.c @ 34:95f61c3b430a

mpffs-rdutils: pathname search implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 30 Jun 2013 16:20:28 +0000
parents
children
comparison
equal deleted inserted replaced
33:660b0ea739f3 34:95f61c3b430a
1 /*
2 * This module contains the code for searching the FFS tree structure
3 * for a specified absolute pathname.
4 */
5
6 #include <sys/types.h>
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <strings.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include "types.h"
14 #include "struct.h"
15
16 extern int root_node_no;
17
18 /*
19 * The following function iterates through the descendants of a directory
20 * object looking for a specific directory-member filename.
21 *
22 * Arguments:
23 * - index # of the first descendant (descendant pointer from the dir object)
24 * - filename to search for
25 *
26 * Returns: index # of the sought descendant object if found, 0 otherwise.
27 */
28 find_dir_member(first_descend, srchname)
29 char *srchname;
30 {
31 int ent;
32 struct objinfo obj;
33
34 for (ent = first_descend; ent != 0xFFFF; ent = obj.sibling) {
35 obj.entryno = ent;
36 get_index_entry(&obj);
37 if (!obj.type) /* skip deleted objects w/o further looking */
38 continue;
39 validate_chunk(&obj);
40 validate_obj_name(&obj);
41 if (!strcmp(obj.dataptr, srchname))
42 return(ent);
43 }
44 return(0);
45 }
46
47 /*
48 * The following function searches for a pathname from the root down.
49 * Returns the index # if found, otherwise exits with an error message
50 * indicating which step failed.
51 *
52 * Warning: the pathname in the argument buffer will be destroyed:
53 * 0s put in place of the slashes.
54 */
55 find_pathname(pathname)
56 char *pathname;
57 {
58 char *cur, *next;
59 int idx;
60 struct objinfo dir;
61
62 cur = pathname;
63 if (*cur == '/')
64 cur++;
65 for (idx = root_node_no; cur; cur = next) {
66 if (!*cur)
67 break;
68 next = index(cur, '/');
69 if (next == cur) {
70 fprintf(stderr,
71 "malformed pathname: multiple adjacent slashes\n");
72 exit(1);
73 }
74 if (next)
75 *next++ = '\0';
76 dir.entryno = idx;
77 get_index_entry(&dir);
78 if (dir.type != 0xF2) {
79 fprintf(stderr,
80 "pathname search error: encountered a non-directory\n");
81 exit(1);
82 }
83 idx = find_dir_member(dir.descend, cur);
84 if (!idx) {
85 fprintf(stderr,
86 "pathname search error: component name not found\n");
87 exit(1);
88 }
89 }
90 return(idx);
91 }