History log of /freebsd-current/bin/sh/cd.c
Revision Date Author Comments
# e043f372 24-Nov-2023 Warner Losh <imp@FreeBSD.org>

bin: Automated cleanup of cdefs and other formatting

Apply the following automated changes to try to eliminate
no-longer-needed sys/cdefs.h includes as well as now-empty
blank lines in a row.

Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/
Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/
Remove /\n+#if.*\n#endif.*\n+/
Remove /^#if.*\n#endif.*\n/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/

Sponsored by: Netflix


# 90aea514 23-Nov-2023 Warner Losh <imp@FreeBSD.org>

bin: Remove ancient SCCS tags.

Remove ancient SCCS tags from the tree, automated scripting, with two
minor fixup to keep things compiling. All the common forms in the tree
were removed with a perl script.

Sponsored by: Netflix


# 1d386b48 16-Aug-2023 Warner Losh <imp@FreeBSD.org>

Remove $FreeBSD$: one-line .c pattern

Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/


# d3eae2a6 31-Dec-2019 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Fix rare memory leak with SIGINT

If getcwd() failed earlier on but later succeeded in the pwd builtin,
there was no INTOFF protection between calling savestr() and storing its
result.

It is quite rare for getcwd() to fail, and rarer for it to succeed later in
the same directory.

Found via code inspection for changing ckmalloc() and similar to assert
INTOFF protection instead of applying it directly (which protects against
corrupting malloc's internal state but allows memory leaks or double frees).

MFC after: 1 week


# 4600b569 15-Jul-2018 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Don't treat % specially in CDPATH


# 6f49cd26 25-Jun-2017 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Ignore error when cd writes the directory actually switched to.

If CDPATH is used non-trivially or the operand is "-", cd writes the
directory actually switched to. (We currently do this only in interactive
shells, but POSIX requires this in non-interactive shells as well.)

As mentioned in Austin group bug #1045, cd shall not return an error while
leaving the current directory changed. Therefore, ignore any write error.


# fbbd9655 28-Feb-2017 Warner Losh <imp@FreeBSD.org>

Renumber copyright clause 4

Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by: Jan Schaumann <jschauma@stevens.edu>
Pull Request: https://github.com/freebsd/freebsd/pull/96


# d5119a2a 24-Jan-2016 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Remove a global variable from cd.c.


# 7b40c6df 23-Jan-2016 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Use OLDPWD shell variable for 'cd -'.

Per POSIX, 'cd -' should use the OLDPWD shell variable, not internal state.
This variable is normally exported.

Also, if OLDPWD is not set, fail 'cd -' instead of changing to the current
directory.


# 781bfb5a 15-Feb-2015 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Prefer "" to nullstr where possible.


# a4652c28 15-Feb-2015 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Add stsavestr(), like savestr() but allocates using stalloc().


# 670dd3f0 30-Nov-2013 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Prefer memcpy() to strcpy() in most cases. Remove the scopy macro.


# 7cbda738 15-Sep-2012 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Prefer internal nextopt() to libc getopt().

This reduces code duplication and code size.

/usr/bin/printf is not affected.

Side effect: different error messages when certain builtins are passed
invalid options.


# e94e3511 13-Jan-2012 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Properly show "Not a directory" error in cd builtin.

The errno message display added in r222292 did not take attempting to
cd to a non-directory or something that cannot be stat()ed into account.

PR: bin/164070
MFC after: 10 days


# 454a02b3 13-Jun-2011 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Fix duplicate prototypes for builtins.

Have mkbuiltins write the prototypes for the *cmd functions to builtins.h
instead of builtins.c and include builtins.h in more .c files instead of
duplicating prototypes for *cmd functions in other headers.


# 64fa41f3 27-May-2011 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Correct criterion for using CDPATH in cd.

CDPATH should be ignored not only for pathnames starting with '/' but also
for pathnames whose first component is '.' or '..'.

The man page already describes this behaviour.


# 168b9dd1 25-May-2011 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Show errno messages in cd.


# d6ee26ad 20-May-2011 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Implement the cd -e flag proposed for the next POSIX issue.

This reflects failure to determine the pathname of the new directory in the
exit status (1). Normally, cd returns successfully if it did chdir() and the
call was successful.

In POSIX, -e only has meaning with -P; because our -L is not entirely
compliant and may fall back to -P mode, -e has some effect with -L as well.


# 5fe9123f 21-Dec-2010 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Add a function to print warnings (with command name and newline).
This is like error() but without raising an exception.
It is particularly useful as a replacement for the warnx macro in
bltin/bltin.h.


# 9d37e157 23-Nov-2010 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Code size optimizations to "stack string" memory allocation:
* Prefer one CHECKSTRSPACE with multiple USTPUTC to multiple STPUTC.
* Add STPUTS macro (based on function) and use it instead of loops that add
nul-terminated strings to the stack string.

No functional change is intended, but code size is about 1K less on i386.


# ae7f4735 22-Nov-2010 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Fix confusing behaviour if chdir succeeded but getcwd failed in cd -P.

If getcwd fails, do not treat this as an error, but print a warning and
unset PWD. This is similar to the behaviour when starting the shell in a
directory whose name cannot be determined.


# a7d5f7eb 19-Oct-2010 Jamie Gritton <jamie@FreeBSD.org>

A new jail(8) with a configuration file, to replace the work currently done
by /etc/rc.d/jail.


# 88328642 13-Oct-2010 David E. O'Brien <obrien@FreeBSD.org>

In the spirit of r90111, depend on c89 and remove the "STATIC" macro
and its usage.


# aa7b6f82 12-Oct-2010 David E. O'Brien <obrien@FreeBSD.org>

Consistently use "STATIC" for all functions in order to be able to set
breakpoints with in a debugger. And use naked "static" for variables.

Noticed by: bde


# 7baf1076 16-May-2010 Stefan Farfeleder <stefanf@FreeBSD.org>

Merge r199631: Handle current work directories of arbitrary length.


# 8eac1f94 17-Apr-2010 Jilles Tjoelker <jilles@FreeBSD.org>

sh: On startup of the shell, use PWD from the environment if it is valid.
Unset PWD if it is incorrect and no value for it can be determined.
This preserves the logical current directory across shell invocations.

Example (assuming /home is a symlink):
$ cd
$ pwd
/home/foo
$ sh
$ pwd
/home/foo

Formerly the second pwd would show the physical path (symlinks resolved).


# fe0506d7 09-Mar-2010 Marcel Moolenaar <marcel@FreeBSD.org>

Create the altix project branch. The altix project will add support
for the SGI Altix 350 to FreeBSD/ia64. The hardware used for porting
is a two-module system, consisting of a base compute module and a
CPU expansion module. SGI's NUMAFlex architecture can be an excellent
platform to test CPU affinity and NUMA-aware features in FreeBSD.


# 384aedab 27-Dec-2009 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Various warning fixes (from WARNS=6 NO_WERROR=1):
- const
- initializations to silence -Wuninitialized (it was safe anyway)
- remove nested extern declarations
- rename "index" locals to "idx"


# 2cac6e36 24-Dec-2009 Jilles Tjoelker <jilles@FreeBSD.org>

sh: Constify various strings.

Most of this is adding const keywords, but setvar() in var.c had to be
changed somewhat more.


# 1c645e0f 21-Nov-2009 Stefan Farfeleder <stefanf@FreeBSD.org>

Handle current work directories of arbitrary length. The argument to cd
continues to be limited by PATH_MAX (1024).

Obtained from: NetBSD
PR: 104456


# d7f03759 19-Oct-2008 Ulf Lilleengen <lulf@FreeBSD.org>

- Import the HEAD csup code which is the basis for the cvsmode work.


# 55d2c5b5 24-Feb-2008 Stefan Farfeleder <stefanf@FreeBSD.org>

Split updatepwd() into two smaller functions. The first one, findpwd(),
computes the new path and the second one, updatepwd(), updates the variables
PWD, OLDPWD and the path used for the pwd builtin according to the new
directory. For a logical directory change, chdir() is now called between
those two functions, no longer causing wrong values to be stored in PWD etc. if
it fails.

PR: 64990, 101316, 120571


# ed5c24e2 12-Jun-2006 Stefan Farfeleder <stefanf@FreeBSD.org>

Don't strip a leading ./ from the path for the cd builtin to avoid interpreting
.//dir as /dir. Rather strip it only for the purpose of checking if the
directory path should be printed.

PR: 88813
Submitted by: Josh Elsasser
Patch from: NetBSD (cd.c rev 1.38)
MFC after: 2 weeks


# 6195fb41 06-Apr-2004 Mark Murray <markm@FreeBSD.org>

Remove clause 3 from the UCB licenses.

OK'ed by: imp, core


# 2ba1b30b 05-Jul-2003 Diomidis Spinellis <dds@FreeBSD.org>

Changes following CScout analysis:

- Removed dead declarations
- Made objects that should have been declared as static, static.

The changes use STATIC instead of static, following the existing
convention in the rest of the code.

Approved by: schweikh (mentor)
MFC after: 2 weeks


# 7e1975c2 25-Jul-2002 Tim J. Robbins <tjr@FreeBSD.org>

Add a -P/-o physical option which behaves similarly to bash/ksh's options
by the same name. This option makes the cd and pwd builtins behave physically
(as opposed to logically) by default.

Submitted by: fanf


# 050f7913 25-Jul-2002 Tim J. Robbins <tjr@FreeBSD.org>

Set opterr to zero to avoid duplicate warnings from getopt(3) for unknown
options.


# 93c0dc5e 25-Jul-2002 Tim J. Robbins <tjr@FreeBSD.org>

Use PATH_MAX instead of a local guess at it, which happened to be incorrect.

Suggested by: fanf


# b5ae6928 25-Jul-2002 Tim J. Robbins <tjr@FreeBSD.org>

pwdcmd()'s argc and argv arguments are no longer unused, remove __unused.

Pointed out by: fanf


# 2628ebdb 19-Jul-2002 Tim J. Robbins <tjr@FreeBSD.org>

Remove broken and incomplete support for old releases of System V,
don't support system that implement getcwd(3) with a pipe to /bin/pwd.


# 2749b141 29-Jun-2002 David E. O'Brien <obrien@FreeBSD.org>

Consistently use FBSDID


# 09086b04 21-May-2002 Tim J. Robbins <tjr@FreeBSD.org>

Bring back the cd -L and -P options from revision 1.24, but try harder not
to fail when the logical current directory no longer exists. Allow changes
to absolute paths when logical cwd is invalid, fall back to physical cd
if logical cd fails.


# baf3e7c1 21-May-2002 Tim J. Robbins <tjr@FreeBSD.org>

Temporarily back out revision 1.24; it seems to handle the case where the
current directory no longer exists incorrectly and breaks `make cleandir'.


# 178897f1 20-May-2002 Tim J. Robbins <tjr@FreeBSD.org>

Add the SUSv3 -L and -P options to the cd and pwd builtin utilities. `Logical'
handling of .. is now the default.


# 9e3bfade 19-May-2002 Juli Mallett <jmallett@FreeBSD.org>

Remove a small, annoying, nit I ran in to editing this file, a lone tab
on a line by itself.


# 5134c3f7 01-Feb-2002 Warner Losh <imp@FreeBSD.org>

o __P has been reoved
o Old-style K&R declarations have been converted to new C89 style
o register has been removed
o prototype for main() has been removed (gcc3 makes it an error)
o int main(int argc, char *argv[]) is the preferred main definition.
o Attempt to not break style(9) conformance for declarations more than
they already are.
o Change
int
foo() {
...
to
int
foo(void)
{
...


# f14c1c47 07-Nov-2001 Tor Egge <tegge@FreeBSD.org>

Don't call setvar() with the VTEXTFIXED flag. A copy is created by
setvar() and passed to setvareq(). When the VTEXTFIXED flag is set,
that copy is never freed, causing a memory leak.

PR: 31533
Submitted by: maxim@macomnet.ru


# 6c48b6cf 29-Nov-1999 Martin Cracauer <cracauer@FreeBSD.org>

Include strerror(errno) in error messages after failed system calls.
Fix a warning.


# 2a456239 27-Aug-1999 Peter Wemm <peter@FreeBSD.org>

$Id$ -> $FreeBSD$


# 37f0a670 06-Sep-1998 Tor Egge <tegge@FreeBSD.org>

Don't blindly eliminate `..' and the previous pathname component.
PR: 2541
Obtained from: NetBSD


# 3d7b5b93 18-May-1998 Philippe Charnier <charnier@FreeBSD.org>

Add rcsid. Spelling.


# e7a0b024 18-May-1997 Steve Price <steve@FreeBSD.org>

Use the __unused attribute where warranted.


# afb033d5 27-Apr-1997 Steve Price <steve@FreeBSD.org>

Nuke register keyword usage and #if -> #ifdef.

Obtained from: NetBSD


# b97fa2ef 22-Feb-1997 Peter Wemm <peter@FreeBSD.org>

Revert $FreeBSD$ to $Id$


# 1130b656 14-Jan-1997 Jordan K. Hubbard <jkh@FreeBSD.org>

Make the long-awaited change from $Id$ to $FreeBSD$

This will make a number of things easier in the future, as well as (finally!)
avoiding the Id-smashing problem which has plagued developers for so long.

Boy, I'm glad we're not using sup anymore. This update would have been
insane otherwise.


# 5e46e019 04-Jan-1997 Steve Price <steve@FreeBSD.org>

Make sh(1) think and be in the same place at the same time. This closes
PR#2331: strange output of sh's pwd on symlinked directories.


# 8df71a5a 23-Dec-1996 Steve Price <steve@FreeBSD.org>

Oops, it needs little more caulk to get it right.


# 79e02527 23-Dec-1996 Steve Price <steve@FreeBSD.org>

Apply a little dab of bit caulk to keep those beggars
from leaking out.

Noticed by: bde


# a41f1d08 22-Dec-1996 Steve Price <steve@FreeBSD.org>

Don't use _POSIX_PATH_MAX to limit the size of the path, instead
use 'getcwd(NULL, 0)' just as pwd(1) does.

Suggested by: bde


# 958ba632 21-Dec-1996 Steve Price <steve@FreeBSD.org>

Make sh(1) a little braver in the face of adversity. sh(1)
now handles the getpwd() init problem the same way as bash
and ksh do. Also while I was in here, I cleaned up the format
a little, removed some unnnecessary #if SYMLINKS cruft, and
changed the pwd builtin to use getcwd(3) as Joerg suggested.


# ab0a2172 13-Dec-1996 Steve Price <steve@FreeBSD.org>

Merge in NetBSD mods and -Wall cleaning.

Obtained from: NetBSD, me


# aa9caaf6 01-Sep-1996 Peter Wemm <peter@FreeBSD.org>

Merge of 4.4-Lite2 sh source, plus some gcc -Wall cleaning. This is a
merge of parallel duplicate work by Steve Price and myself. :-]

There are some changes to the build that are my fault... mkinit.c was
trying (poorly) to duplicate some of the work that make(1) is designed to
do. The Makefile hackery is my fault too, the depend list was incomplete
because of some explicit OBJS+= entries, so mkdep wasn't picking up their
source file #includes.

This closes a pile of /bin/sh PR's, but not all of them..

Submitted by: Steve Price <steve@bonsai.hiwaay.net>, peter


# 069428af 26-May-1996 Peter Wemm <peter@FreeBSD.org>

Import the 4.4BSD-Lite2 /bin/sh sources

Requested by: joerg

(Note, this is mostly going to be conflicts, which is expected. Our entire
sh source has a mainline, so this should not change anything except for
a few new files appearing. I dont think they are a problem)


# 8d7d5cea 13-Nov-1995 Peter Wemm <peter@FreeBSD.org>

A fix for the "cd -" coredump on a brand new /bin/sh. The problem was
noticed on a NetBSD bugs mailing list but this is entirely my own work.

Inspired by: Scott Reynolds <scottr@plexus.com>, for NetBSD


# 61233bdc 26-Dec-1994 Bruce Evans <bde@FreeBSD.org>

Obtained from: partly from 1.1.5

Convert "" to "." for "cd" and "cd ''". chdir("") is required to fail
on POSIX systems.


# beeac5cd 05-Nov-1994 Jordan K. Hubbard <jkh@FreeBSD.org>

Get this braindead, mongoloid shell look in /stand for pwd if it can't
find it in /bin. This is something of a kludge, I know, but consider
my limited alternatives: I can't make this an execvp() without making
people scream that I introduced a failure point or slowed down pwd,
and I can't make it an optional macro since crunch doesn't let you pass
arbitrary command-line args to the build of one of its crunch-ees.
This is the simplest, if not the nicest looking, solution I could come up
with.


# 89730b29 23-Sep-1994 David Greenman <dg@FreeBSD.org>

Added $Id$


# 4b88c807 26-May-1994 Rodney W. Grimes <rgrimes@FreeBSD.org>

BSD 4.4 Lite bin Sources