(regex_compile) <normal_char>: Pay attention to multibyteness.
[gnulib.git] / lib / save-cwd.c
1 /* save-cwd.c -- Save and restore current working directory.
2    Copyright (C) 1995, 1997, 1998 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by Jim Meyering <meyering@na-net.ornl.gov>.  */
19
20 #if HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include <stdio.h>
25
26 #ifdef STDC_HEADERS
27 # include <stdlib.h>
28 #endif
29
30 #if HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33
34 #if HAVE_FCNTL_H
35 # include <fcntl.h>
36 #else
37 # include <sys/file.h>
38 #endif
39
40 #include <errno.h>
41 #ifndef errno
42 extern int errno;
43 #endif
44
45 #include "save-cwd.h"
46 #include "error.h"
47
48 char *xgetcwd PARAMS ((void));
49
50 /* Record the location of the current working directory in CWD so that
51    the program may change to other directories and later use restore_cwd
52    to return to the recorded location.  This function may allocate
53    space using malloc (via xgetcwd) or leave a file descriptor open;
54    use free_cwd to perform the necessary free or close.  Upon failure,
55    no memory is allocated, any locally opened file descriptors are
56    closed;  return non-zero -- in that case, free_cwd need not be
57    called, but doing so is ok.  Otherwise, return zero.  */
58
59 int
60 save_cwd (struct saved_cwd *cwd)
61 {
62   static int have_working_fchdir = 1;
63
64   cwd->desc = -1;
65   cwd->name = NULL;
66
67   if (have_working_fchdir)
68     {
69 #if HAVE_FCHDIR
70       cwd->desc = open (".", O_RDONLY);
71       if (cwd->desc < 0)
72         {
73           error (0, errno, "cannot open current directory");
74           return 1;
75         }
76
77 # if __sun__ || sun
78       /* On SunOS 4, fchdir returns EINVAL if accounting is enabled,
79          so we have to fall back to chdir.  */
80       if (fchdir (cwd->desc))
81         {
82           if (errno == EINVAL)
83             {
84               close (cwd->desc);
85               cwd->desc = -1;
86               have_working_fchdir = 0;
87             }
88           else
89             {
90               error (0, errno, "current directory");
91               close (cwd->desc);
92               cwd->desc = -1;
93               return 1;
94             }
95         }
96 # endif /* __sun__ || sun */
97 #else
98 # define fchdir(x) (abort (), 0)
99       have_working_fchdir = 0;
100 #endif
101     }
102
103   if (!have_working_fchdir)
104     {
105       cwd->name = xgetcwd ();
106       if (cwd->name == NULL)
107         {
108           error (0, errno, "cannot get current directory");
109           return 1;
110         }
111     }
112   return 0;
113 }
114
115 /* Change to recorded location, CWD, in directory hierarchy.
116    If "saved working directory", NULL))
117    */
118
119 int
120 restore_cwd (const struct saved_cwd *cwd, const char *dest, const char *from)
121 {
122   int fail = 0;
123   if (cwd->desc >= 0)
124     {
125       if (fchdir (cwd->desc))
126         {
127           error (0, errno, "cannot return to %s%s%s",
128                  (dest ? dest : "saved working directory"),
129                  (from ? " from " : ""),
130                  (from ? from : ""));
131           fail = 1;
132         }
133     }
134   else if (chdir (cwd->name) < 0)
135     {
136       error (0, errno, "%s", cwd->name);
137       fail = 1;
138     }
139   return fail;
140 }
141
142 void
143 free_cwd (struct saved_cwd *cwd)
144 {
145   if (cwd->desc >= 0)
146     close (cwd->desc);
147   if (cwd->name)
148     free (cwd->name);
149 }