~ubuntu-branches/ubuntu/vivid/gzip/vivid

« back to all changes in this revision

Viewing changes to lib/save-cwd.c

  • Committer: Steve Langasek
  • Date: 2012-06-29 02:07:40 UTC
  • mfrom: (4.1.9 sid)
  • Revision ID: steve.langasek@canonical.com-20120629020740-qqikrblzana08v2y
Merge version 1.5-1.1 from Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* save-cwd.c -- Save and restore current working directory.
2
2
 
3
 
   Copyright (C) 1995, 1997-1998, 2003-2006, 2009-2010 Free Software
 
3
   Copyright (C) 1995, 1997-1998, 2003-2006, 2009-2012 Free Software
4
4
   Foundation, Inc.
5
5
 
6
6
   This program is free software: you can redistribute it and/or modify
30
30
 
31
31
#include "chdir-long.h"
32
32
#include "unistd--.h"
33
 
#include "xgetcwd.h"
 
33
#include "cloexec.h"
34
34
 
35
35
#if GNULIB_FCNTL_SAFER
36
36
# include "fcntl--.h"
38
38
# define GNULIB_FCNTL_SAFER 0
39
39
#endif
40
40
 
41
 
/* On systems without the fchdir function (WOE), pretend that open
42
 
   always returns -1 so that save_cwd resorts to using xgetcwd.
43
 
   Since chdir_long requires fchdir, use chdir instead.  */
44
 
#if !HAVE_FCHDIR
45
 
# undef open
46
 
# define open(File, Flags) (-1)
47
 
# undef fchdir
48
 
# define fchdir(Fd) (abort (), -1)
49
 
# undef chdir_long
50
 
# define chdir_long(Dir) chdir (Dir)
51
 
#endif
52
 
 
53
41
/* Record the location of the current working directory in CWD so that
54
42
   the program may change to other directories and later use restore_cwd
55
43
   to return to the recorded location.  This function may allocate
56
 
   space using malloc (via xgetcwd) or leave a file descriptor open;
 
44
   space using malloc (via getcwd) or leave a file descriptor open;
57
45
   use free_cwd to perform the necessary free or close.  Upon failure,
58
46
   no memory is allocated, any locally opened file descriptors are
59
47
   closed;  return non-zero -- in that case, free_cwd need not be
60
48
   called, but doing so is ok.  Otherwise, return zero.
61
49
 
62
 
   The `raison d'etre' for this interface is that the working directory
 
50
   The _raison d'etre_ for this interface is that the working directory
63
51
   is sometimes inaccessible, and getcwd is not robust or as efficient.
64
52
   So, we prefer to use the open/fchdir approach, but fall back on
65
 
   getcwd if necessary.
 
53
   getcwd if necessary.  This module works for most cases with just
 
54
   the getcwd-lgpl module, but to be truly robust, use the getcwd module.
66
55
 
67
56
   Some systems lack fchdir altogether: e.g., OS/2, pre-2001 Cygwin,
68
57
   SCO Xenix.  Also, SunOS 4 and Irix 5.3 provide the function, yet it
75
64
{
76
65
  cwd->name = NULL;
77
66
 
78
 
  cwd->desc = open (".", O_RDONLY);
 
67
  cwd->desc = open (".", O_SEARCH);
79
68
  if (!GNULIB_FCNTL_SAFER)
80
69
    cwd->desc = fd_safer (cwd->desc);
81
70
  if (cwd->desc < 0)
82
71
    {
83
 
      cwd->name = xgetcwd ();
 
72
      cwd->name = getcwd (NULL, 0);
84
73
      return cwd->name ? 0 : -1;
85
74
    }
86
75
 
 
76
  set_cloexec_flag (cwd->desc, true);
87
77
  return 0;
88
78
}
89
79