2 * Copyright (c) 1997-2006 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * File: am-utils/amq/pawd.c
45 * pawd is similar to pwd, except that it returns more "natural" versions of
46 * pathnames for directories automounted with the amd automounter. If any
47 * arguments are given, the "more natural" form of the given pathnames are
50 * Paul Anderson (paul@ed.lfcs)
56 #endif /* HAVE_CONFIG_H */
62 static char *localhost = "localhost";
63 static char transform[MAXPATHLEN];
70 # ifdef HAVE_EXTERN_GETCCENT
72 # endif /* HAVE_EXTERN_GETCCENT */
77 # ifdef HAVE_EXTERN_GETCCENT
78 while ((cp = getccent()))
79 if (cp->cnode_type == 'r')
80 return cp->cnode_name;
81 # endif /* HAVE_EXTERN_GETCCENT */
85 #endif /* HAVE_CNODEID */
88 /* DISK_HOME_HACK added by gdmr */
93 char partition[MAXPATHLEN];
94 char username[MAXPATHLEN];
95 char hesiod_lookup[MAXPATHLEN];
96 char *to, *ch, *hes_name, *dot;
100 fprintf(stderr, "hack_name(%s)\n", dir);
103 if (dir[0] == '/' && dir[1] == 'a' && dir[2] == '/') {
104 /* Could be /a/server/disk/home/partition/user... */
106 while (*ch && *ch != '/') ch++; /* Skip server */
107 if (!NSTREQ(ch, "/disk/home/", 11))
108 return NULL; /* Nope */
109 /* Looking promising, next should be the partition name */
112 while (*ch && *ch != '/') *to++ = *ch++;
115 return NULL; /* Off the end */
116 /* Now the username */
119 while (*ch && *ch != '/') *to++ = *ch++;
122 fprintf(stderr, "partition %s, username %s\n", partition, username);
125 xsnprintf(hesiod_lookup, sizeof(hesiod_lookup),
126 "%s.homes-remote", username);
127 hes = hes_resolve(hesiod_lookup, "amd");
131 fprintf(stderr, "hesiod -> <%s>\n", *hes);
133 hes_name = strstr(*hes, "/homes/remote/");
134 if (!hes_name) return NULL;
137 fprintf(stderr, "hesiod -> <%s>\n", hes_name);
140 while (*dot && *dot != '.') dot++;
143 fprintf(stderr, "hesiod -> <%s>\n", hes_name);
146 if (strcmp(partition, hes_name)) return NULL;
148 fprintf(stderr, "A match, munging....\n");
150 xstrlcpy(transform, "/home/", sizeof(transform));
151 xstrlcat(transform, username, sizeof(transform));
153 xstrlcat(transform, ch, sizeof(transform));
155 fprintf(stderr, "Munged to <%s>\n", transform);
161 #endif /* DISK_HOME_HACK */
165 * The routine transform_dir(path) transforms pathnames of directories
166 * mounted with the amd automounter to produce a more "natural" version.
167 * The automount table is obtained from the local amd via the rpc interface
168 * and reverse lookups are repeatedly performed on the directory name
169 * substituting the name of the automount link for the value of the link
170 * whenever it occurs as a prefix of the directory name.
173 transform_dir(char *dir)
175 #ifdef DISK_HOME_HACK
177 #endif /* DISK_HOME_HACK */
179 struct sockaddr_in server_addr;
183 struct timeval tmo = {10, 0};
187 #ifdef DISK_HOME_HACK
188 if (ch = hack_name(dir))
190 #endif /* DISK_HOME_HACK */
193 server = cluster_server();
194 #else /* not HAVE_CNODEID */
196 #endif /* not HAVE_CNODEID */
198 if ((hp = gethostbyname(server)) == NULL)
200 memset(&server_addr, 0, sizeof(server_addr));
201 /* as per POSIX, sin_len need not be set (used internally by kernel) */
202 server_addr.sin_family = AF_INET;
203 server_addr.sin_addr = *(struct in_addr *) hp->h_addr;
205 clnt = clntudp_create(&server_addr, AMQ_PROGRAM, AMQ_VERSION, tmo, &s);
207 clnt = clnttcp_create(&server_addr, AMQ_PROGRAM, AMQ_VERSION, &s, 0, 0);
211 xstrlcpy(transform, dir, sizeof(transform));
212 dummystr = transform;
213 spp = amqproc_pawd_1((amq_string *) &dummystr, clnt);
214 if (spp && *spp && **spp) {
215 xstrlcpy(transform, *spp, sizeof(transform));
223 /* getawd() is a substitute for getwd() which transforms the path */
225 getawd(char *path, size_t l)
228 char *wd = getcwd(path, MAXPATHLEN);
229 #else /* not HAVE_GETCWD */
230 char *wd = getwd(path);
231 #endif /* not HAVE_GETCWD */
236 xstrlcpy(path, transform_dir(wd), l);
242 main(int argc, char *argv[])
244 char tmp_buf[MAXPATHLEN], *wd;
247 wd = getawd(tmp_buf, sizeof(tmp_buf));
249 fprintf(stderr, "pawd: %s\n", tmp_buf);
252 fprintf(stdout, "%s\n", wd);
256 wd = transform_dir(*++argv);
257 fprintf(stdout, "%s\n", wd);