utils

small programs, scripts and utils
git clone https://git.e1e0.net/utils.git
Log | Files | Refs

commit 4575b7d9bc5e91c8388c8f54e8eb2a292a33920b
parent a76ea7e6295e385fd208e7d4b315bb528ec245ea
Author: Paco Esteban <paco@e1e0.net>
Date:   Sat, 14 Mar 2020 19:20:45 +0100

add the getcwd utility by sthen and schwarze

Diffstat:
Agetcwd/.gitignore | 2++
Agetcwd/Makefile | 17+++++++++++++++++
Agetcwd/getcwd.c | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 111 insertions(+), 0 deletions(-)

diff --git a/getcwd/.gitignore b/getcwd/.gitignore @@ -0,0 +1,2 @@ +getcwd +*.o diff --git a/getcwd/Makefile b/getcwd/Makefile @@ -0,0 +1,17 @@ +CC = clang +PREFIX = /usr/local + +.PHONY: all clean +.SUFFIXES: .in.1 .1 + +all: getcwd + +clean: + rm -f getcwd + +install: all + install -d -m 0755 $(PREFIX)/bin + install -m 0755 getcwd $(PREFIX)/bin + +getcwd: getcwd.o + $(CC) -o getcwd getcwd.o diff --git a/getcwd/getcwd.c b/getcwd/getcwd.c @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org> + * Copyright (c) 2020 Stuart Henderson <stu@spacehopper.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * witH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/param.h> +#include <sys/sysctl.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <vis.h> +#include <wchar.h> + +void +mbswprint(const char *mbs) +{ + char buf[5]; + wchar_t wc; + int len; /* length in bytes of UTF-8 encoded string */ + int width; /* display width of a single Unicode char */ + + while (*mbs != '\0') { + len = mbtowc(&wc, mbs, MB_CUR_MAX); + if (len == -1) { + (void)mbtowc(NULL, NULL, MB_CUR_MAX); + len = 1; + } + if (len == 1) + width = vis(buf, mbs[0], + VIS_TAB | VIS_NL | VIS_CSTYLE, mbs[1]) - buf; + else if ((width = wcwidth(wc)) == -1) { + /* U+FFFD replacement character */ + memcpy(buf, "\357\277\275\0", 4); + width = 1; + } else { + memcpy(buf, mbs, len); + buf[len] = '\0'; + } + fputs(buf, stdout); + mbs += len; + } + fputs("\n", stdout); +} + +void +curwd(pid_t pid) +{ + int name[] = { CTL_KERN, KERN_PROC_CWD, pid }; + char path[PATH_MAX]; + size_t pathlen = sizeof path; + + if (sysctl(name, 3, path, &pathlen, NULL, 0) != 0) + *path = '\0'; + + mbswprint(path); +} + +int +main(int argc, char *argv[]) +{ + int pid; + const char *errstr; + + pledge("stdio ps", NULL); + + if (argc != 2) { + printf("usage: getcwd <pid>\n"); + return(1); + } + + pid = strtonum(argv[1], 1, UINT_MAX, &errstr); + if (errstr != NULL) { + printf("pid is %s: %s\n", errstr, argv[0]); + return(1); + } + + curwd(pid); + + return 0; +}