#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;
}