Remove trailing whitespace.
[dragonfly.git] / contrib / tar / 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 #ifndef O_DIRECTORY
46 # define O_DIRECTORY 0
47 #endif
48
49 #include "save-cwd.h"
50 #include "error.h"
51
52 char *xgetcwd PARAMS ((void));
53
54 /* Record the location of the current working directory in CWD so that
55    the program may change to other directories and later use restore_cwd
56    to return to the recorded location.  This function may allocate
57    space using malloc (via xgetcwd) or leave a file descriptor open;
58    use free_cwd to perform the necessary free or close.  Upon failure,
59    no memory is allocated, any locally opened file descriptors are
60    closed;  return non-zero -- in that case, free_cwd need not be
61    called, but doing so is ok.  Otherwise, return zero.  */
62
63 int
64 save_cwd (struct saved_cwd *cwd)
65 {
66   static int have_working_fchdir = 1;
67
68   cwd->desc = -1;
69   cwd->name = NULL;
70
71   if (have_working_fchdir)
72     {
73 #if HAVE_FCHDIR
74       cwd->desc = open (".", O_RDONLY | O_DIRECTORY);
75       if (cwd->desc < 0)
76         {
77           error (0, errno, "cannot open current directory");
78           return 1;
79         }
80
81 # if __sun__ || sun
82       /* On SunOS 4, fchdir returns EINVAL if accounting is enabled,
83          so we have to fall back to chdir.  */
84       if (fchdir (cwd->desc))
85         {
86           if (errno == EINVAL)
87             {
88               close (cwd->desc);
89               cwd->desc = -1;
90               have_working_fchdir = 0;
91             }
92           else
93             {
94               error (0, errno, "current directory");
95               close (cwd->desc);
96               cwd->desc = -1;
97               return 1;
98             }
99         }
100 # endif /* __sun__ || sun */
101 #else
102 # define fchdir(x) (abort (), 0)
103       have_working_fchdir = 0;
104 #endif
105     }
106
107   if (!have_working_fchdir)
108     {
109       cwd->name = xgetcwd ();
110       if (cwd->name == NULL)
111         {
112           error (0, errno, "cannot get current directory");
113           return 1;
114         }
115     }
116   return 0;
117 }
118
119 /* Change to recorded location, CWD, in directory hierarchy.
120    If "saved working directory", NULL))
121    */
122
123 int
124 restore_cwd (const struct saved_cwd *cwd, const char *dest, const char *from)
125 {
126   int fail = 0;
127   if (cwd->desc >= 0)
128     {
129       if (fchdir (cwd->desc))
130         {
131           error (0, errno, "cannot return to %s%s%s",
132                  (dest ? dest : "saved working directory"),
133                  (from ? " from " : ""),
134                  (from ? from : ""));
135           fail = 1;
136         }
137     }
138   else if (chdir (cwd->name) < 0)
139     {
140       error (0, errno, "%s", cwd->name);
141       fail = 1;
142     }
143   return fail;
144 }
145
146 void
147 free_cwd (struct saved_cwd *cwd)
148 {
149   if (cwd->desc >= 0)
150     close (cwd->desc);
151   if (cwd->name)
152     free (cwd->name);
153 }