lib/tt/mini_isam: remove register keyword
[oweals/cde.git] / cde / lib / tt / mini_isam / isrename.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                   */
24 /*%%  (c) Copyright 1993, 1994 International Business Machines Corp.     */
25 /*%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                    */
26 /*%%  (c) Copyright 1993, 1994 Novell, Inc.                              */
27 /*%%  $XConsortium: isrename.c /main/3 1995/10/23 11:43:48 rswiston $                                                    */
28 #ifndef lint
29 static char sccsid[] = "@(#)isrename.c 1.10 89/07/17 Copyr 1988 Sun Micro";
30 #endif
31 /*
32  * Copyright (c) 1988 by Sun Microsystems, Inc.
33  */
34
35 /*
36  * isrename.c
37  *
38  * Description:
39  *      Rename an ISAM file. 
40  */
41
42
43 #include "isam_impl.h"
44 #include <sys/time.h>
45
46 void _removelast();
47 static void _removelast2();
48 char *_lastelement();
49 static void _rename_datfile(), _rename_indfile(), _rename_varfile();
50 static int _amrename();
51
52 /*
53  * isfd = isrename(oldname, newname)
54  *
55  *
56  * Errors:
57  *      EBADFILE ISAM file is corrupted or it is not an NetISAM file
58  *      EFLOCKED The file is exclusively locked by other process.
59  *      EFNAME  Invalid ISAM file name 
60  *      EFNAME  ISAM file does not exist
61  *      ETOOMANY Too many ISAM file descriptors are in use (128 is the limit)
62  *
63  * The following error code is "borrowed" from UNIX:
64  *      EACCES  UNIX file system protection denies access to the file:
65  *               - mode is INOUT or OUTPUT and ISAM file is on 
66  *                 a Read-Only mounted file system
67  *               - UNIX file permissions don't allow access to the file
68  */
69
70 int 
71 isrename(char *oldname, char *newname)
72 {
73     Isfd                isfd, isfd2;
74     Fab                 *fab;
75     char                olddir [MAXPATHLEN];
76     char                newdir [MAXPATHLEN];
77     char                datfname[MAXPATHLEN];
78
79     /*
80      * Check that the odl and new filename are in the same directory.
81      */
82     snprintf(olddir, sizeof(olddir), "%s", oldname);
83     _removelast2(olddir);
84     snprintf(newdir, sizeof(newdir), "%s", newname);
85     _removelast2(newdir);
86
87     if (strcmp(newdir, olddir) != 0) {
88         _setiserrno2(EFNAME, 9, 0);
89         return (ISERROR);
90     }
91
92     /*
93      * Open the file
94      */
95     if ((isfd = isopen(oldname, ISINOUT)) == -1)
96         return (ISERROR);                    /* iserrno is set */
97
98     /*
99      * Reject rename if 'newfile' exists.
100      */
101     if ((isfd2 = isopen(newname, ISINOUT)) != -1 || iserrno != ENOENT) {
102
103         _setiserrno2(EEXIST, 9, 0);
104         if (isfd2 >= 0) (void)isclose(isfd2);
105         return (ISERROR);                    /* iserrno is set */
106     }
107
108     /*
109      * Get File Access Block.
110      */
111     if ((fab = _isfd_find(isfd)) == NULL) {
112         _isfatal_error("isrename() cannot find FAB");
113         _setiserrno2(EFATAL,'9','0');
114         return (ISERROR);
115     }
116
117     if (_amrename(&fab->isfhandle, _lastelement(newname), &fab->errcode)) {
118         _seterr_errcode(&fab->errcode);
119         (void)isclose(isfd);
120         return (ISERROR);
121     }
122
123     _fab_destroy(fab);                       /* Deallocate Fab object */
124     _isfd_delete(isfd);
125
126     /*
127      * We must unlink() the .rec file, or isbuild() with the same 
128      * following immediatly would fail because for NFS files the client
129      * still thinks that the file exists for a few seconds.
130      */
131
132     snprintf(datfname, sizeof(datfname), "%s", oldname);
133     _makedat_isfname(datfname);
134     (void)unlink(datfname);
135
136     return (ISOK);                           /* Successful isrename() */
137 }
138
139 /*
140  * _removelast2(path)
141  *
142  * Remove last element of path. E.g. /usr/db/part yields /usr/db.
143  * Unlike _removelast() that path does not have to start with '/'.
144  */
145
146 Static void
147 _removelast2(char *path)
148 {
149     char        *p;
150
151     for (p = path + strlen(path); *--p != '/' && p >= path; ) 
152         *p = '\0';
153 }
154
155 /*
156  * _amrename(isfhandle, newname)
157  *
158  * _amrename() renames ISAM file
159  *
160  * Input params:
161  *      isfhandle       Handle of ISAM file
162  *
163  * Output params:
164  *      errcode         Error code
165  *
166  */
167
168
169 static int
170 _amrename(Bytearray *isfhandle, char *newname, struct errcode *errcode)
171 {
172     Fcb                 *fcb;
173     char                *isfname = _getisfname(isfhandle);
174
175     _isam_entryhook();
176
177     /*
178      * Get FCB corresponding to the isfhandle handle.
179      */
180     if ((fcb = _openfcb(isfhandle, errcode)) == NULL) {
181         goto ERROR;
182     }
183
184     /*
185      * Delete FCB and remove it from FCB cache.
186      */
187     (void) _watchfd_decr(_isfcb_nfds(fcb));
188     _isfcb_close(fcb);
189     _mngfcb_delete(isfhandle);
190
191     /*
192      * Rename all UNIX files.
193      */
194     _rename_datfile(isfname, newname);
195     _rename_indfile(isfname, newname);
196     _rename_varfile(isfname, newname);
197
198     _isam_exithook();
199     return (ISOK);
200
201  ERROR:
202
203     _isam_exithook();
204     return (ISERROR);
205 }
206
207
208 /* newname, with no prefix */
209 Static void
210 _rename_datfile(char *isfname, char *newname)     
211 {
212     char        namebuf[MAXPATHLEN];
213     char        newbuf[MAXPATHLEN];
214     char        newbuftemp[sizeof(newbuf)];
215
216     snprintf(namebuf, sizeof(namebuf), "%s", isfname);
217     snprintf(newbuf, sizeof(newbuf), "%s", isfname);
218
219     /*
220      * Replace the last element of the old path with newname.
221      */
222     _removelast(newbuf);
223     if (strcmp(newbuf, "/") != 0) {
224         snprintf(newbuftemp, sizeof(newbuftemp),  "%s/", newbuf);
225         strcpy(newbuf, newbuftemp);
226     }
227     snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
228     strcpy(newbuf, newbuftemp);
229
230     _makedat_isfname(namebuf);
231     _makedat_isfname(newbuf);
232
233     (void)rename(namebuf, newbuf);
234 }
235
236 /* newname, with no prefix */
237 Static void
238 _rename_indfile(char *isfname, char *newname)     
239 {
240     char        namebuf[MAXPATHLEN];
241     char        newbuf[MAXPATHLEN];
242     char        newbuftemp[MAXPATHLEN];
243
244     snprintf(namebuf, sizeof(namebuf), "%s", isfname);
245     snprintf(newbuf, sizeof(newbuf), "%s", isfname);
246
247     /*
248      * Replace the last element of the old path with newname.
249      */
250     _removelast(newbuf);
251     if (strcmp(newbuf, "/") != 0) {
252         snprintf(newbuftemp, sizeof(newbuftemp), "%s/", newbuf);
253         strcpy(newbuf, newbuftemp);
254     }
255     snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
256     strcpy(newbuf, newbuftemp);
257
258     _makeind_isfname(namebuf);
259     _makeind_isfname(newbuf);
260
261     (void)rename(namebuf, newbuf);
262 }
263
264 /* newname, with no prefix */
265 Static void
266 _rename_varfile(char *isfname, char *newname) 
267 {
268     char        namebuf[MAXPATHLEN];
269     char        newbuf[MAXPATHLEN];
270     char        newbuftemp[MAXPATHLEN];
271
272     snprintf(namebuf, sizeof(namebuf), "%s", isfname);
273     snprintf(newbuf, sizeof(newbuf), "%s", isfname);
274
275     /*
276      * Replace the last element of the old path with newname.
277      */
278     _removelast(newbuf);
279     if (strcmp(newbuf, "/") != 0) {
280         snprintf(newbuftemp, sizeof(newbuftemp), "%s/", newbuf);
281         strcpy(newbuf, newbuftemp);
282     }
283     snprintf(newbuftemp, sizeof(newbuftemp), "%s%s", newbuf, newname);
284     strcpy(newbuf, newbuftemp);
285
286     _makevar_isfname(namebuf);
287     _makevar_isfname(newbuf);
288
289     (void)rename(namebuf, newbuf);
290 }
291