4 * Copyright (c) 1997-2009 Erez Zadok
5 * Copyright (c) 1990 Jan-Simon Pendry
6 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7 * Copyright (c) 1990 The Regents of the University of California.
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgment:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * File: am-utils/amq/pawd.c
47 * pawd is similar to pwd, except that it returns more "natural" versions of
48 * pathnames for directories automounted with the amd automounter. If any
49 * arguments are given, the "more natural" form of the given pathnames are
52 * Paul Anderson (paul@ed.lfcs)
58 #endif /* HAVE_CONFIG_H */
64 static char *localhost
= "localhost";
65 static char transform
[MAXPATHLEN
];
72 # ifdef HAVE_EXTERN_GETCCENT
74 # endif /* HAVE_EXTERN_GETCCENT */
79 # ifdef HAVE_EXTERN_GETCCENT
80 while ((cp
= getccent()))
81 if (cp
->cnode_type
== 'r')
82 return cp
->cnode_name
;
83 # endif /* HAVE_EXTERN_GETCCENT */
87 #endif /* HAVE_CNODEID */
90 /* DISK_HOME_HACK added by gdmr */
95 char partition
[MAXPATHLEN
];
96 char username
[MAXPATHLEN
];
97 char hesiod_lookup
[MAXPATHLEN
];
98 char *to
, *ch
, *hes_name
, *dot
;
102 fprintf(stderr
, "hack_name(%s)\n", dir
);
105 if (dir
[0] == '/' && dir
[1] == 'a' && dir
[2] == '/') {
106 /* Could be /a/server/disk/home/partition/user... */
108 while (*ch
&& *ch
!= '/') ch
++; /* Skip server */
109 if (!NSTREQ(ch
, "/disk/home/", 11))
110 return NULL
; /* Nope */
111 /* Looking promising, next should be the partition name */
114 while (*ch
&& *ch
!= '/') *to
++ = *ch
++;
117 return NULL
; /* Off the end */
118 /* Now the username */
121 while (*ch
&& *ch
!= '/') *to
++ = *ch
++;
124 fprintf(stderr
, "partition %s, username %s\n", partition
, username
);
127 xsnprintf(hesiod_lookup
, sizeof(hesiod_lookup
),
128 "%s.homes-remote", username
);
129 hes
= hes_resolve(hesiod_lookup
, "amd");
133 fprintf(stderr
, "hesiod -> <%s>\n", *hes
);
135 hes_name
= strstr(*hes
, "/homes/remote/");
136 if (!hes_name
) return NULL
;
139 fprintf(stderr
, "hesiod -> <%s>\n", hes_name
);
142 while (*dot
&& *dot
!= '.') dot
++;
145 fprintf(stderr
, "hesiod -> <%s>\n", hes_name
);
148 if (strcmp(partition
, hes_name
)) return NULL
;
150 fprintf(stderr
, "A match, munging....\n");
152 xstrlcpy(transform
, "/home/", sizeof(transform
));
153 xstrlcat(transform
, username
, sizeof(transform
));
155 xstrlcat(transform
, ch
, sizeof(transform
));
157 fprintf(stderr
, "Munged to <%s>\n", transform
);
163 #endif /* DISK_HOME_HACK */
167 * The routine transform_dir(path) transforms pathnames of directories
168 * mounted with the amd automounter to produce a more "natural" version.
169 * The automount table is obtained from the local amd via the rpc interface
170 * and reverse lookups are repeatedly performed on the directory name
171 * substituting the name of the automount link for the value of the link
172 * whenever it occurs as a prefix of the directory name.
175 transform_dir(char *dir
)
177 #ifdef DISK_HOME_HACK
179 #endif /* DISK_HOME_HACK */
181 struct sockaddr_in server_addr
;
185 struct timeval tmo
= {10, 0};
189 #ifdef DISK_HOME_HACK
190 if (ch
= hack_name(dir
))
192 #endif /* DISK_HOME_HACK */
195 server
= cluster_server();
196 #else /* not HAVE_CNODEID */
198 #endif /* not HAVE_CNODEID */
200 if ((hp
= gethostbyname(server
)) == NULL
)
202 memset(&server_addr
, 0, sizeof(server_addr
));
203 /* as per POSIX, sin_len need not be set (used internally by kernel) */
204 server_addr
.sin_family
= AF_INET
;
205 server_addr
.sin_addr
= *(struct in_addr
*) hp
->h_addr
;
207 clnt
= clntudp_create(&server_addr
, AMQ_PROGRAM
, AMQ_VERSION
, tmo
, &s
);
209 clnt
= clnttcp_create(&server_addr
, AMQ_PROGRAM
, AMQ_VERSION
, &s
, 0, 0);
213 xstrlcpy(transform
, dir
, sizeof(transform
));
214 dummystr
= transform
;
215 spp
= amqproc_pawd_1((amq_string
*) &dummystr
, clnt
);
216 if (spp
&& *spp
&& **spp
) {
217 xstrlcpy(transform
, *spp
, sizeof(transform
));
225 /* getawd() is a substitute for getwd() which transforms the path */
227 getawd(char *path
, size_t l
)
230 char *wd
= getcwd(path
, MAXPATHLEN
);
231 #else /* not HAVE_GETCWD */
232 char *wd
= getwd(path
);
233 #endif /* not HAVE_GETCWD */
238 xstrlcpy(path
, transform_dir(wd
), l
);
244 main(int argc
, char *argv
[])
246 char tmp_buf
[MAXPATHLEN
], *wd
;
249 wd
= getawd(tmp_buf
, sizeof(tmp_buf
));
251 fprintf(stderr
, "pawd: %s\n", tmp_buf
);
254 fprintf(stdout
, "%s\n", wd
);
258 wd
= transform_dir(*++argv
);
259 fprintf(stdout
, "%s\n", wd
);