Skip to content

Instantly share code, notes, and snippets.

@emekoi
Last active October 15, 2023 04:07
Show Gist options
  • Save emekoi/095e31453475f3cc937bce959a5a3da6 to your computer and use it in GitHub Desktop.
Save emekoi/095e31453475f3cc937bce959a5a3da6 to your computer and use it in GitHub Desktop.
search upwards for a `Makefile` to run, or until any file called `.git` is found
/* SPDX-License-Identifier: MIT
* Copyright (c) 2023 Emeka Nkurumeh
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
**/
#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
(void)argc;
bool found_root = false;
while (true) {
DIR *dir = opendir(".");
if (!dir) {
perror("opendir");
return 1;
}
for (struct dirent *ep = NULL; !found_root && (ep = readdir(dir));) {
found_root |= !!!strcmp("GNUmakefile", ep->d_name);
found_root |= !!!strcmp("makefile", ep->d_name);
found_root |= !!!strcmp("Makefile", ep->d_name);
found_root |= !!!strcmp(".git", ep->d_name);
}
if (closedir(dir)) {
perror("closedir");
return 1;
}
if (found_root) {
break;
}
if (chdir("..")) {
perror("chdir");
return 1;
}
}
argv[0] = "make";
if (execvp(argv[0], argv)) {
perror("execvp");
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment