/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1991, 1993, 19944* The Regents of the University of California. All rights reserved.5* Copyright (c) 2026 Dag-Erling Smørgrav6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15* 3. Neither the name of the University nor the names of its contributors16* may be used to endorse or promote products derived from this software17* without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*/3132#include <sys/param.h>33#include <sys/stat.h>3435#include <err.h>36#include <stdbool.h>37#include <stdio.h>38#include <stdlib.h>39#include <unistd.h>4041static char *42getcwd_logical(void)43{44struct stat log, phy;45char *pwd, *p, *q;4647/* $PWD is set and absolute */48if ((pwd = getenv("PWD")) == NULL || *pwd != '/')49return (NULL);50/* $PWD does not contain /./ or /../ */51for (p = pwd; *p; p = q) {52for (q = ++p; *q && *q != '/'; q++)53/* nothing */;54if ((*p == '.' && q == ++p) ||55(*p == '.' && q == ++p))56return (NULL);57}58/* $PWD refers to the current directory */59if (stat(pwd, &log) != 0 || stat(".", &phy) != 0 ||60log.st_dev != phy.st_dev || log.st_ino != phy.st_ino)61return (NULL);62return (pwd);63}6465static char *66getcwd_physical(void)67{68static char pwd[MAXPATHLEN];6970return (getcwd(pwd, sizeof(pwd)));71}7273static void74usage(void)75{76(void)fprintf(stderr, "usage: pwd [-L | -P]\n");77exit(1);78}7980int81main(int argc, char *argv[])82{83char *pwd;84int opt;85bool logical;8687logical = true;88while ((opt = getopt(argc, argv, "LP")) != -1) {89switch (opt) {90case 'L':91logical = true;92break;93case 'P':94logical = false;95break;96default:97usage();98}99}100argc -= optind;101argv += optind;102if (argc != 0)103usage();104105/*106* If we're trying to find the logical current directory and that107* fails, behave as if -P was specified.108*/109if ((logical && (pwd = getcwd_logical()) != NULL) ||110(pwd = getcwd_physical()) != NULL)111printf("%s\n", pwd);112else113err(1, ".");114if (fflush(stdout) != 0)115err(1, "stdout");116exit(0);117}118119120