Fixes: https://todo.sr.ht/~laumann/ani/2
ZPNA2D42RFGGC45PLDYDGQUDPC7D4NBEH2HD4Y2L5P2CK3R52FAQC
2U7P5SFQG3AVALKMPJF4WMZE6PXIXXZYOMZ3RZKILBUJ4UMFXVIAC
XJ2PEH74CLJUELZBR47QHGUSKXB4Z5T7EKEF6Y4CYY2VBHZXUTDAC
Y26WT3ZFN7KSVXOZ26B5Y2OR4M4VQYQLPMAHPC4O5VIT3ENBISXAC
XTKRT6OQYN4LARQRDLI2KBSAJTOFZNT4AMBZ46CPUDZXD7IDYYWQC
FB67XX5EGNF45JNAQISW7CBLSLS36F6JEUOBTTPUMVBMOVLGA3VQC
PEUS54XQ5KJQYAVUYBG5MWLEHIOVPMZ3ANVC7HPQP6JUWWPRDW5AC
Q7TKZCJP2Z75EICZYKCEZDHKGERSOKZGMTSU3UXETBHTF663T66AC
B3XLVPNC4COLLC3FUE34Y7HIKTMF6CJZUASZOU3YM2YGPZKJZP7QC
AHIXA5ZESN6QJXV2LKRSC4H5ZRFMJFHFPY2RQRARTH3XJLLCMCGQC
char *
xstrdup(const char *str)
{
char *ret = strdup(str);
if (!ret)
die("out of memory, strdup failed");
return ret;
}
#ifndef ANI_DIR_H
#define ANI_DIR_H
/* Routines for working with directories */
int has_dotpijul(const char *); /* check if given directory has a .pijul */
/**
* Search for a .pijul folder. Starts from the current working
* directory, and walks up the parent directories until a directory is
* found. If found, returns the absolute path (without "/.pijul" as an
* allocated string. Otherwise, returns NULL
*/
char *find_dotpijul(void);
#endif
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include "scaffold.h"
#include "dir.h"
int
has_dotpijul(const char *path)
{
struct dirent *d;
DIR *dir;
int ret;
ret = 0;
if (!(dir = opendir(path)))
return ret;
while ((d = readdir(dir)) != NULL) {
if (!strncmp(d->d_name, ".pijul", 6)) {
ret = 1;
break;
}
}
closedir(dir);
return ret;
}
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
char *
find_dotpijul()
{
/**
* idea is this: set a string to current directory, check if
* ".pijul" is present - if not, cd("..") and look again.
*/
char path[PATH_MAX] = { 0 };
char *s = NULL;
int found = 0;
if (!getcwd(path, PATH_MAX))
die("unable to get current working directory");
while (!(found = has_dotpijul(path)) && path != s) {
s = strrchr(path, '/');
*s = '\0';
}
if (!found)
return NULL;
s = xstrdup(path);
return s;
}