~sircmpwn/ctools

10cfd2d3578789369ec35ab6635121f943635742 — Pedro L. Ramos 6 months ago cc69324
Implemented pwd
8 files changed, 105 insertions(+), 1 deletions(-)

M STATUS
M doc/ctools.7.scd
M doc/meson.build
A doc/pwd.1.scd
M meson.build
A src/pwd.c
M test/meson.build
A test/pwd
M STATUS => STATUS +1 -1
@@ 101,7 101,7 @@ T       pathchk
T       printf
    W   prs
T       ps
T       pwd
 D      pwd
    W   qalter
    W   qdel
    W   qhold

M doc/ctools.7.scd => doc/ctools.7.scd +2 -0
@@ 53,6 53,8 @@ shell environment. These tools are used for tasks such as:
:  Invoke a command that ignores hangups
|  *nice*(1)
:  Run a process with an altered niceness value
|  *pwd*(1)
:  Prints current working directory
|  *rmdir*(1)
:  Remove directories
|  *sleep*(1)

M doc/meson.build => doc/meson.build +1 -0
@@ 21,6 21,7 @@ man_files = [
	'logname.1',
	'nice.1',
	'nohup.1',
	'pwd.1',
	'rmdir.1',
	'sleep.1',
	'tee.1',

A doc/pwd.1.scd => doc/pwd.1.scd +35 -0
@@ 0,0 1,35 @@
pwd(1) "ctools"

# NAME

pwd - prints current working directory.

# SYNOPSIS

*pwd* [ -L | -P ] 

# DESCRIPTION

Prints current working directory to stdout. 

# OPTIONS

*-L*
	This flag is implemented for POSIX compatibility and has no effect.	

*-P*
	This flag is implemented for POSIX compatibility and has no effect.	

# UNSPECIFIED BEHAVIOR

The POSIX standard specifies the -L and -P flags as having an effect on
the resolution of symlinks in preparing the pathname to print. However,
the ctools implementation of pwd always simply prints the value returned
from *getcwd*(3), regardless of any flags specified.

# DISCLAIMER

This command is part of ctools and is compatible with POSIX-1.2018, and may
optionally support XSI extensions. This man page is not intended to be a
complete reference, and where it disagrees with the specification, the
specification takes precedence.

M meson.build => meson.build +1 -0
@@ 27,6 27,7 @@ oneshots = [
	'logname',
	'nice', # Included in base but only effective under XSI
	'nohup',
	'pwd',
	'rmdir',
	'sleep',
	'tee',

A src/pwd.c => src/pwd.c +40 -0
@@ 0,0 1,40 @@
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static void
usage(void)
{
	fprintf(stderr, "usage: pwd [-L|-P]");
}

int
main(int argc, char *argv[])
{
	char *pwd = NULL;

	char opt;
	while ((opt = getopt(argc, argv, "LP")) != -1) {
		switch (opt) {
		case 'P':
			break;
		case 'L':
			break;
		default:
			usage();
			return 1;
		}
	}
	
	pwd = getcwd(pwd, 0);
	
	if (pwd == NULL) {
		return 1;
	}

	printf("%s\n", pwd);
	
	return 0;
}

M test/meson.build => test/meson.build +1 -0
@@ 16,6 16,7 @@ test_files = [
	'logname',
	'nice',
	'nohup',
	'pwd',
	'rmdir',
	'sleep',
	'tee',

A test/pwd => test/pwd +24 -0
@@ 0,0 1,24 @@
#!/bin/sh
tool="pwd"
. "$HARNESS"

mkdir "$TMPDIR"/dir-f
ln -s "$TMPDIR"/dir-f "$TMPDIR"/dir-s

should_work() (
    cd "$TMPDIR/dir-f"
    ct="$(pwd)"
    [ "$ct" = "$TMPDIR/dir-f" ]
)

should_handle_symlink() (
    cd "$TMPDIR/dir-f"
    ct="$(pwd -L)"
    [ "$ct" = "$TMPDIR/dir-f" ]
)

should_resolve_symlink() (
    cd "$TMPDIR/dir-f"
    ct="$(pwd -P)"
    [ "$ct" = "$TMPDIR/dir-f" ]
)