config

OpenBSD system configuration
git clone git://jacobedwards.org/config
Log | Files | Refs | README

commit 7bdd1bc9e4c8e1fe62754c961b6d9a360a937830
parent 0f047b44b0972948e5111e239de384ee997fc729
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date:   Sun,  8 Oct 2023 08:50:15 -0700

Add rfind program

This program reverse walks the current path looking for the first
name matching the given argument. For instance 'rfind .git' will
determine whether you're in a git repository.

Diffstat:
Mlocal/src/src/singles/Makefile | 3++-
Alocal/src/src/singles/rfind.c | 46++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/local/src/src/singles/Makefile b/local/src/src/singles/Makefile @@ -1,7 +1,8 @@ names =\ datediff\ - timedec\ numsep\ + rfind\ + timedec\ urldecode\ urlencode\ diff --git a/local/src/src/singles/rfind.c b/local/src/src/singles/rfind.c @@ -0,0 +1,46 @@ +/* + * Copyright 2022 Jacob R. Edwards + * This program walks the directory hierarchy backwards looking for + * the given name. + */ + +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +void +die(char *msg) +{ + perror(msg); + exit(1); +} + +int +main(int argc, char *argv[]) +{ + char *name; + char path[PATH_MAX]; + int last; + + if (argc != 2) { + fprintf(stderr, "usage: rfind name\n"); + exit(1); + } + name = argv[1]; + + last = 0; + for (last = 0; getwd(path);) { + last = !path[1]; + if (access(name, F_OK) == 0) { + if (last) + exit(printf("/%s\n", name) < 0); + else + exit(printf("%s/%s\n", path, name) < 0); + } + if (chdir("..")) + die("chdir .."); + } + + return 1; +}