Debian Codegraph @

libidu/idfile.c

raw symbols SCIP
1 /* idfile.c -- read & write mkid database file header
2 Copyright (C) 1986-2020 Free Software Foundation, Inc.
3 Written by Greg McGary <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <obstack.h>
25 #include <error.h>
26 #include <sys/stat.h>
27
28 #include "idfile.h"
29 #include "xalloc.h"
30 #include "xnls.h"
31
32 static int io_size (FILE *, void *, unsigned int size, int);
33
34 /****************************************************************************/
35
36 /* Discover the name of the ID file. If ARG is NULL, consult $IDPATH.
37 If $IDPATH is undefined, default to "ID". If the candidate file
38 name is relative, search successive ancestor directories until the
39 file is found or we reach the root. If we find it, return the
40 relative file name, otherwise return NULL. */
41
42 char const *
43 locate_id_file_name (char const *arg)
44 {
45 static char file_name_buffer[BUFSIZ];
46 char *buf = file_name_buffer;
47 char *id_path = 0;
48 struct stat rootb;
49 struct stat statb;
50
51 if (arg == 0)
52 {
53 id_path = getenv ("IDPATH");
54 if (id_path)
55 {
56 id_path = xstrdup (id_path);
57 arg = strsep (&id_path, ":");
58 /* FIXME: handle multiple ID file names */
59 }
60 }
61 if (arg == 0)
62 arg = DEFAULT_ID_FILE_NAME;
63
64 /* if we got absolute name, just use it. */
65 if (arg[0] == '/')
66 return arg;
67 /* if the name we were give exists, don't bother searching */
68 if (stat (arg, &statb) == 0)
69 return arg;
70 /* search up the tree until we find a directory where this
71 * relative file name is visible.
72 * (or we run out of tree to search by hitting root).
73 */
74
75 if (stat ("/", &rootb) != 0)
76 return NULL;
77 do
78 {
79 strcpy (buf, "../");
80 buf += 3;
81 strcpy (buf, arg);
82 if (stat (file_name_buffer, &statb) == 0)
83 return file_name_buffer;
84 *buf = '\0';
85 if (stat (file_name_buffer, &statb) != 0)
86 return NULL;
87 }
88 while (statb.st_ino != rootb.st_ino || statb.st_dev != rootb.st_dev);
89 return NULL;
90 }
91
92
93 /****************************************************************************/
94
95 int
96 sizeof_idhead (void)
97 {
98 return io_idhead (0, io_size, 0);
99 }
100
101 static int
102 io_size (FILE *ignore_FILE, void *ignore_addr, unsigned int size, int io_type)
103 {
104 if (io_type == IO_TYPE_STR)
105 error (0, 0, _("can't determine the io_size of a string!"));
106 return size;
107 }
108
109 /* The sizes of the fields must be hard-coded. They aren't
110 necessarily the sizes of the struct members, because some
111 architectures don't have any way to declare 4-byte integers
112 (e.g., Cray) */
113
114 int
115 io_idhead (FILE *fp, io_func_t iof, struct idhead *idhp)
116 {
117 unsigned int size = 0;
118 unsigned char pad = 0;
119 if (fp)
120 fseek (fp, 0L, 0);
121 size += iof (fp, idhp->idh_magic, 2, IO_TYPE_FIX);
122 size += iof (fp, &pad, 1, IO_TYPE_FIX);
123 size += iof (fp, &idhp->idh_version, 1, IO_TYPE_FIX);
124 size += iof (fp, &idhp->idh_flags, 2, IO_TYPE_INT);
125 size += iof (fp, &idhp->idh_file_links, 4, IO_TYPE_INT);
126 size += iof (fp, &idhp->idh_files, 4, IO_TYPE_INT);
127 size += iof (fp, &idhp->idh_tokens, 4, IO_TYPE_INT);
128 size += iof (fp, &idhp->idh_buf_size, 4, IO_TYPE_INT);
129 size += iof (fp, &idhp->idh_vec_size, 4, IO_TYPE_INT);
130 size += iof (fp, &idhp->idh_tokens_offset, 4, IO_TYPE_INT);
131 size += iof (fp, &idhp->idh_flinks_offset, 4, IO_TYPE_INT);
132 size += iof (fp, &idhp->idh_end_offset, 4, IO_TYPE_INT);
133 size += iof (fp, &idhp->idh_max_link, 2, IO_TYPE_INT);
134 size += iof (fp, &idhp->idh_max_path, 2, IO_TYPE_INT);
135 return size;
136 }