save
This commit is contained in:
@@ -17,166 +17,166 @@
|
||||
#include "licences/licences.h"
|
||||
#include "util.h"
|
||||
|
||||
int create_project(manifest_t manifest)
|
||||
{
|
||||
int status;
|
||||
char buffer[BUFSIZ], *main_source;
|
||||
|
||||
status = mkdir_p(manifest.project);
|
||||
if (status)
|
||||
return 1;
|
||||
|
||||
status = chdir(manifest.project);
|
||||
if (status)
|
||||
return 1;
|
||||
|
||||
cfprintf(
|
||||
"README",
|
||||
"%s ( short description )\n\nThis cool project actions adverbly.\n",
|
||||
manifest.project);
|
||||
|
||||
if (manifest.build != BARE) {
|
||||
main_source = manifest.flat ? "main.c" : "src/main.c";
|
||||
cfprintf(main_source, "#include <stdio.h>\n"
|
||||
"\n"
|
||||
"int main()\n"
|
||||
"{\n"
|
||||
"\tputs(\"Hello, World!\");\n"
|
||||
"\treturn 0;\n"
|
||||
"}\n");
|
||||
}
|
||||
char *upr_name = tostrupr(manifest.project);
|
||||
switch (manifest.build) {
|
||||
case MAKE:
|
||||
cfprintf(
|
||||
"Makefile",
|
||||
"PREFIX = /usr/bin\n"
|
||||
"\n"
|
||||
"%s_SRCS := $(wildcard src/*.c)\n"
|
||||
"%s_OBJS := $(patsubst src/%.c,build/obj/%.o,$(%s_SRCS))"
|
||||
"\n"
|
||||
"%s := bin/%s"
|
||||
"\n"
|
||||
"-include config.mak\n"
|
||||
"\n"
|
||||
"ifeq ($(wildcard config.mak),)\n",
|
||||
upr_name, upr_name, upr_name, upr_name,
|
||||
manifest.project);
|
||||
break;
|
||||
case CMAKE:
|
||||
cfprintf("CMakeLists.txt",
|
||||
"cmake_minimum_required(VERSION 3.16)\n"
|
||||
"\n"
|
||||
"project(%s\n"
|
||||
" VERSION 0.1.0\n"
|
||||
" LANGUAGES C)\n"
|
||||
"\n"
|
||||
"set(CMAKE_C_STANDARD 23)\n"
|
||||
"set(CMAKE_C_STANDARD_REQUIRED ON)\n"
|
||||
"set(CMAKE_C_EXTENSIONS OFF)\n"
|
||||
"\n"
|
||||
"include(GNUInstallDirs)\n"
|
||||
"\n"
|
||||
"add_executable(%s\n"
|
||||
" src/main.c\n"
|
||||
")\n"
|
||||
"\n"
|
||||
"install(TARGETS %s\n"
|
||||
" RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}\n"
|
||||
")\n",
|
||||
manifest.project, manifest.project, manifest.project);
|
||||
break;
|
||||
case AUTOTOOLS:
|
||||
cfprintf("configure.ac",
|
||||
"AC_PREREQ([2.69])\n"
|
||||
"AC_INIT([%s], [0.1], [bug-report@exmaple.com])\n"
|
||||
"AM_INIT_AUTOMAKE([foreign -Wall])\n"
|
||||
"AC_CONFIG_SRCDIR([src/main.c])\n"
|
||||
"AC_CONFIG_HEADERS([config.h])"
|
||||
"AC_PROG_CC\n"
|
||||
"AC_CONFIG_FILES([Makefile src/Makefile])\n"
|
||||
"AC_OUTPUT\n",
|
||||
manifest.project);
|
||||
cfprintf("Makefile.am", "SUBDIRS = src\n");
|
||||
cfprintf("src/Makefile.am",
|
||||
"bin_PROGRAMS = %s\n"
|
||||
"%s_SOURCES = main.c\n",
|
||||
manifest.project, manifest.project);
|
||||
cfprintf("bootstrap",
|
||||
"#!/bin/sh\n"
|
||||
"set -e\n"
|
||||
"\n"
|
||||
"autoreconf --install --verbose --force\n");
|
||||
break;
|
||||
case BARE:
|
||||
snprintf(buffer, BUFSIZ, "%s.c", manifest.project);
|
||||
cfprintf(buffer, "");
|
||||
main_source = str_dup(buffer);
|
||||
cfprintf("Makefile",
|
||||
".POSIX:\n"
|
||||
"CC ::= gcc\n"
|
||||
"CFLAGS ::= -std=c23 -Wall -Wextra -Wpedantic\n"
|
||||
"\n"
|
||||
"all: %s\n"
|
||||
"\n"
|
||||
"clean:\n"
|
||||
"\t$(RM) %s",
|
||||
manifest.project, manifest.project);
|
||||
break;
|
||||
case BCOUNT:
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
|
||||
flast = true;
|
||||
switch (manifest.licence) {
|
||||
case MIT:
|
||||
cfprintf("COPYING", "%s", MIT_txt);
|
||||
break;
|
||||
case GPL:
|
||||
cfprintf("COPYING", "%s", GPL_3_0_txt);
|
||||
break;
|
||||
case BSD:
|
||||
cfprintf("COPYING", "%s", BSD_3_Clause_txt);
|
||||
break;
|
||||
case UNL:
|
||||
cfprintf("COPYING", "%s", UNLICENSE_txt);
|
||||
break;
|
||||
case LCOUNT:
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
|
||||
if (!manifest.git) {
|
||||
fprintf(stderr, "Initializing git reposity");
|
||||
status = system("git init --quiet");
|
||||
if (status)
|
||||
fprintf(stderr, ", failed.\n");
|
||||
else
|
||||
fprintf(stderr, ", done.\n");
|
||||
}
|
||||
|
||||
if (manifest.build == AUTOTOOLS) {
|
||||
fprintf(stderr, "Changing files permissions 1");
|
||||
struct stat st;
|
||||
if (stat("bootstrap", &st) == -1) {
|
||||
perror("stat failed");
|
||||
return 1;
|
||||
}
|
||||
mode_t new_mode = st.st_mode | S_IXUSR;
|
||||
if (chmod("bootstrap", new_mode) == -1) {
|
||||
perror("chmod failed");
|
||||
return 1;
|
||||
}
|
||||
fprintf(stderr, ", done.\n");
|
||||
}
|
||||
|
||||
if (manifest.open_editor) {
|
||||
snprintf(buffer, BUFSIZ, "$EDITOR %s", main_source);
|
||||
status = system(buffer);
|
||||
if (status)
|
||||
fprintf(stderr, "Could not open editor");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// int create_project(manifest_t manifest)
|
||||
// {
|
||||
// int status;
|
||||
// char buffer[BUFSIZ], *main_source;
|
||||
//
|
||||
// status = mkdir_p(manifest.project);
|
||||
// if (status)
|
||||
// return 1;
|
||||
//
|
||||
// status = chdir(manifest.project);
|
||||
// if (status)
|
||||
// return 1;
|
||||
//
|
||||
// cfprintf(
|
||||
// "README",
|
||||
// "%s ( short description )\n\nThis cool project actions adverbly.\n",
|
||||
// manifest.project);
|
||||
//
|
||||
// if (manifest.build != BARE) {
|
||||
// main_source = manifest.flat ? "main.c" : "src/main.c";
|
||||
// cfprintf(main_source, "#include <stdio.h>\n"
|
||||
// "\n"
|
||||
// "int main()\n"
|
||||
// "{\n"
|
||||
// "\tputs(\"Hello, World!\");\n"
|
||||
// "\treturn 0;\n"
|
||||
// "}\n");
|
||||
// }
|
||||
// char *upr_name = tostrupr(manifest.project);
|
||||
// switch (manifest.build) {
|
||||
// case MAKE:
|
||||
// cfprintf(
|
||||
// "Makefile",
|
||||
// "PREFIX = /usr/bin\n"
|
||||
// "\n"
|
||||
// "%s_SRCS := $(wildcard src/*.c)\n"
|
||||
// "%s_OBJS := $(patsubst src/%.c,build/obj/%.o,$(%s_SRCS))"
|
||||
// "\n"
|
||||
// "%s := bin/%s"
|
||||
// "\n"
|
||||
// "-include config.mak\n"
|
||||
// "\n"
|
||||
// "ifeq ($(wildcard config.mak),)\n",
|
||||
// upr_name, upr_name, upr_name, upr_name,
|
||||
// manifest.project);
|
||||
// break;
|
||||
// case CMAKE:
|
||||
// cfprintf("CMakeLists.txt",
|
||||
// "cmake_minimum_required(VERSION 3.16)\n"
|
||||
// "\n"
|
||||
// "project(%s\n"
|
||||
// " VERSION 0.1.0\n"
|
||||
// " LANGUAGES C)\n"
|
||||
// "\n"
|
||||
// "set(CMAKE_C_STANDARD 23)\n"
|
||||
// "set(CMAKE_C_STANDARD_REQUIRED ON)\n"
|
||||
// "set(CMAKE_C_EXTENSIONS OFF)\n"
|
||||
// "\n"
|
||||
// "include(GNUInstallDirs)\n"
|
||||
// "\n"
|
||||
// "add_executable(%s\n"
|
||||
// " src/main.c\n"
|
||||
// ")\n"
|
||||
// "\n"
|
||||
// "install(TARGETS %s\n"
|
||||
// " RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}\n"
|
||||
// ")\n",
|
||||
// manifest.project, manifest.project, manifest.project);
|
||||
// break;
|
||||
// case AUTOTOOLS:
|
||||
// cfprintf("configure.ac",
|
||||
// "AC_PREREQ([2.69])\n"
|
||||
// "AC_INIT([%s], [0.1], [bug-report@exmaple.com])\n"
|
||||
// "AM_INIT_AUTOMAKE([foreign -Wall])\n"
|
||||
// "AC_CONFIG_SRCDIR([src/main.c])\n"
|
||||
// "AC_CONFIG_HEADERS([config.h])"
|
||||
// "AC_PROG_CC\n"
|
||||
// "AC_CONFIG_FILES([Makefile src/Makefile])\n"
|
||||
// "AC_OUTPUT\n",
|
||||
// manifest.project);
|
||||
// cfprintf("Makefile.am", "SUBDIRS = src\n");
|
||||
// cfprintf("src/Makefile.am",
|
||||
// "bin_PROGRAMS = %s\n"
|
||||
// "%s_SOURCES = main.c\n",
|
||||
// manifest.project, manifest.project);
|
||||
// cfprintf("bootstrap",
|
||||
// "#!/bin/sh\n"
|
||||
// "set -e\n"
|
||||
// "\n"
|
||||
// "autoreconf --install --verbose --force\n");
|
||||
// break;
|
||||
// case BARE:
|
||||
// snprintf(buffer, BUFSIZ, "%s.c", manifest.project);
|
||||
// cfprintf(buffer, "");
|
||||
// main_source = str_dup(buffer);
|
||||
// cfprintf("Makefile",
|
||||
// ".POSIX:\n"
|
||||
// "CC ::= gcc\n"
|
||||
// "CFLAGS ::= -std=c23 -Wall -Wextra -Wpedantic\n"
|
||||
// "\n"
|
||||
// "all: %s\n"
|
||||
// "\n"
|
||||
// "clean:\n"
|
||||
// "\t$(RM) %s",
|
||||
// manifest.project, manifest.project);
|
||||
// break;
|
||||
// case BCOUNT:
|
||||
// default:
|
||||
// abort();
|
||||
// }
|
||||
//
|
||||
// flast = true;
|
||||
// switch (manifest.licence) {
|
||||
// case MIT:
|
||||
// cfprintf("COPYING", "%s", MIT_txt);
|
||||
// break;
|
||||
// case GPL:
|
||||
// cfprintf("COPYING", "%s", GPL_3_0_txt);
|
||||
// break;
|
||||
// case BSD:
|
||||
// cfprintf("COPYING", "%s", BSD_3_Clause_txt);
|
||||
// break;
|
||||
// case UNL:
|
||||
// cfprintf("COPYING", "%s", UNLICENSE_txt);
|
||||
// break;
|
||||
// case LCOUNT:
|
||||
// default:
|
||||
// abort();
|
||||
// }
|
||||
//
|
||||
// if (!manifest.git) {
|
||||
// fprintf(stderr, "Initializing git reposity");
|
||||
// status = system("git init --quiet");
|
||||
// if (status)
|
||||
// fprintf(stderr, ", failed.\n");
|
||||
// else
|
||||
// fprintf(stderr, ", done.\n");
|
||||
// }
|
||||
//
|
||||
// if (manifest.build == AUTOTOOLS) {
|
||||
// fprintf(stderr, "Changing files permissions 1");
|
||||
// struct stat st;
|
||||
// if (stat("bootstrap", &st) == -1) {
|
||||
// perror("stat failed");
|
||||
// return 1;
|
||||
// }
|
||||
// mode_t new_mode = st.st_mode | S_IXUSR;
|
||||
// if (chmod("bootstrap", new_mode) == -1) {
|
||||
// perror("chmod failed");
|
||||
// return 1;
|
||||
// }
|
||||
// fprintf(stderr, ", done.\n");
|
||||
// }
|
||||
//
|
||||
// if (manifest.open_editor) {
|
||||
// snprintf(buffer, BUFSIZ, "$EDITOR %s", main_source);
|
||||
// status = system(buffer);
|
||||
// if (status)
|
||||
// fprintf(stderr, "Could not open editor");
|
||||
// }
|
||||
//
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
289
src/main.c
289
src/main.c
@@ -8,23 +8,80 @@
|
||||
|
||||
// Usage: yait [OPTION]... <PROJECT>
|
||||
|
||||
#define _POSIX_C_SOURCE 200809L // popen extention
|
||||
#include <getopt.h>
|
||||
#include <pwd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../lib/proginfo.h"
|
||||
#include "../lib/err.h"
|
||||
#include "../include/yait.h"
|
||||
#include "standard.h"
|
||||
#include "util.h"
|
||||
#include "../config.h"
|
||||
#include "name.h"
|
||||
|
||||
#define print_option(option, description) \
|
||||
printf(" %-20s %-20s\n", option, description)
|
||||
static struct option longopts[] = { { "author", required_argument, 0, 'a' },
|
||||
{ "licence", required_argument, 0, 'l' },
|
||||
{ 0, 0, 0, 0 } };
|
||||
|
||||
static int exit_status;
|
||||
|
||||
static void usage(int status);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int optc;
|
||||
int lose = 0;
|
||||
set_prog_name(argv[0]);
|
||||
|
||||
exit_status = EXIT_SUCCESS;
|
||||
|
||||
manifest_t manifest = {
|
||||
.author = get_name(),
|
||||
.editor = NULL,
|
||||
.licence = BSD,
|
||||
.project = "Project",
|
||||
};
|
||||
|
||||
parse_standard_options(argc, argv, usage, emit_version);
|
||||
|
||||
while ((optc = getopt_long(argc, argv, "a:l:E", longopts, NULL)) != -1)
|
||||
|
||||
switch (optc) {
|
||||
case 'l':
|
||||
if (!strcmp(optarg, "list")) {
|
||||
printf("BSD\nGPL\nMIT\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
if (!strcmp(optarg, "GPL"))
|
||||
manifest.licence = GPL;
|
||||
else if (!strcmp(optarg, "MIT"))
|
||||
manifest.licence = MIT;
|
||||
else {
|
||||
printf("BSD\nGPL\nMIT\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
break;
|
||||
case 'E':
|
||||
manifest.editor = getenv("EDITOR");
|
||||
break;
|
||||
default:
|
||||
lose = 1;
|
||||
}
|
||||
|
||||
char *cwd = getcwd(NULL, 0);
|
||||
if (!cwd) {
|
||||
fatalfa(errno);
|
||||
}
|
||||
fprintf(stderr, "Created %s at\n %s\n", manifest.project, cwd);
|
||||
free(cwd);
|
||||
|
||||
return exit_status;
|
||||
}
|
||||
|
||||
static void usage(int status)
|
||||
{
|
||||
@@ -44,222 +101,8 @@ Generates an optionated C project.\n",
|
||||
stdout);
|
||||
puts("");
|
||||
fputs("\
|
||||
--git Initialize git repository (default)\n\
|
||||
--no-git Do not initialize git repository\n\
|
||||
--lib Make this a library\n\
|
||||
-l <licence> Set licence. This list can be found by passing 'list'\n\
|
||||
-E Open $EDITOR after project creation\n\
|
||||
--autotools Use the autotools build system\n\
|
||||
--cmake Use the cmake build system\n\
|
||||
--make Use the GNU make build system (default)\n\
|
||||
--bare Minimal C project structure\n\
|
||||
--flat All files in project root.\n\
|
||||
--extras=<arg1>,<arg2> Extra build options, Pass list to list out options.\n",
|
||||
-E Open $EDITOR after project creation (default false)\n\
|
||||
--author=NAME Set the program author (default git username)\n\
|
||||
--licence=LICENCE Set the program licence (default BSD)\n",
|
||||
stdout);
|
||||
}
|
||||
|
||||
void print_lines(const char *first, ...)
|
||||
{
|
||||
va_list args;
|
||||
const char *s;
|
||||
|
||||
va_start(args, first);
|
||||
|
||||
for (s = first; s != NULL; s = va_arg(args, const char *))
|
||||
printf("%s\n", s);
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
static inline int parse_extras_token(manifest_t *conf, const char *s)
|
||||
{
|
||||
if (!strcmp(s, "list")) {
|
||||
print_lines("nob", "Cleanup", "format", NULL);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
if (!strcmp(s, "nob"))
|
||||
return conf->extra.build_nob = true, 0;
|
||||
if (!strcmp(s, "Cleanup"))
|
||||
return conf->extra.tools_Cleanup = true, 0;
|
||||
if (!strcmp(s, "format"))
|
||||
return conf->extra.tools_format = true, 0;
|
||||
fprintf(stderr, "Option '%s' is not valid. See %s --extras=list\n", s,
|
||||
PROGRAM);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int parse_arguments(manifest_t *conf, int argc, char **argv)
|
||||
{
|
||||
int opt, option_index;
|
||||
|
||||
// clang-format off
|
||||
static struct option long_opts[] = {
|
||||
{ "git", no_argument, 0, 'g' },
|
||||
{ "no-git", no_argument, 0, 'G' },
|
||||
{ "lib", no_argument, 0, 'L' },
|
||||
{ "autotools", no_argument, 0, 'a' },
|
||||
{ "cmake", no_argument, 0, 'c' },
|
||||
{ "make", no_argument, 0, 'm' },
|
||||
{ "bare", no_argument, 0, 'B' },
|
||||
{ "flat", no_argument, 0, 'f' },
|
||||
{ "author", required_argument, 0, 'A' },
|
||||
{ "extras", required_argument, 0, 0 },
|
||||
{ 0, 0, 0, 0 } };
|
||||
// clang-format on
|
||||
|
||||
while ((opt = getopt_long(argc, argv, "gGLbacmBfAl:E", long_opts,
|
||||
&option_index)) != -1) {
|
||||
if (opt == 0 &&
|
||||
strcmp(long_opts[option_index].name, "extras") == 0) {
|
||||
int err;
|
||||
char *arg = optarg;
|
||||
char *token = strtok(arg, ",");
|
||||
while (token) {
|
||||
err = parse_extras_token(conf, token);
|
||||
if (err)
|
||||
exit(err);
|
||||
token = strtok(NULL, ",");
|
||||
}
|
||||
}
|
||||
switch (opt) {
|
||||
case 'g':
|
||||
conf->git = true;
|
||||
break;
|
||||
case 'G':
|
||||
conf->git = false;
|
||||
break;
|
||||
case 'L':
|
||||
conf->lib = true;
|
||||
break;
|
||||
case 'a':
|
||||
conf->build = AUTOTOOLS;
|
||||
break;
|
||||
case 'c':
|
||||
conf->build = CMAKE;
|
||||
break;
|
||||
case 'm':
|
||||
conf->build = MAKE;
|
||||
break;
|
||||
case 'B':
|
||||
conf->build = BARE;
|
||||
break;
|
||||
case 'f':
|
||||
conf->flat = true;
|
||||
break;
|
||||
case 'A':
|
||||
conf->author = optarg;
|
||||
break;
|
||||
case 'l':
|
||||
if (!strcmp(optarg, "list")) {
|
||||
print_lines("MIT", "BSD", "GPL", "UNL", NULL);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
conf->licence = TOlicence(optarg);
|
||||
break;
|
||||
case 'E':
|
||||
conf->open_editor = true;
|
||||
conf->editor = getenv("EDITOR");
|
||||
break;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (optind >= argc) {
|
||||
return HELP_REQUESTED;
|
||||
}
|
||||
conf->project = str_dup(argv[optind]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_name(char **output)
|
||||
{
|
||||
FILE *fp;
|
||||
char buf[256];
|
||||
char *res = NULL;
|
||||
struct passwd *pw;
|
||||
|
||||
fp = popen("git config --get user.name", "r");
|
||||
if (fp) {
|
||||
if (fgets(buf, sizeof buf, fp)) {
|
||||
size_t len = strlen(buf);
|
||||
if (len > 0 && buf[len - 1] == '\n')
|
||||
buf[len - 1] = '\0';
|
||||
res = strdup(buf);
|
||||
}
|
||||
pclose(fp);
|
||||
}
|
||||
|
||||
if (!res) {
|
||||
pw = getpwuid(getuid());
|
||||
if (!pw || !pw->pw_name)
|
||||
return -1;
|
||||
res = strdup(pw->pw_name);
|
||||
}
|
||||
|
||||
if (!res)
|
||||
return -1;
|
||||
|
||||
*output = res;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int status;
|
||||
manifest_t manifest = {
|
||||
.project = "Project",
|
||||
.author = "author",
|
||||
.editor = "vim",
|
||||
|
||||
.licence = UNL,
|
||||
|
||||
.git = true,
|
||||
.build = MAKE,
|
||||
.flat = false,
|
||||
.open_editor = false,
|
||||
.lib = false,
|
||||
|
||||
.extra.build_nob = false,
|
||||
.extra.tools_format = false,
|
||||
.extra.tools_Cleanup = false,
|
||||
};
|
||||
|
||||
status = parse_standard_options(usage, argc, argv);
|
||||
if (status != 0 && status != HELP_REQUESTED)
|
||||
return fprintf(stderr, "error: %s\n", strerror(status)),
|
||||
EXIT_FAILURE;
|
||||
|
||||
status = parse_arguments(&manifest, argc, argv);
|
||||
if (status == HELP_REQUESTED) {
|
||||
usage(0);
|
||||
}
|
||||
|
||||
if (status) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
get_name(&manifest.author);
|
||||
status = create_project(manifest);
|
||||
|
||||
if (status) {
|
||||
fprintf(stderr, "%s\n", strerror(status));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
char *cwd;
|
||||
|
||||
cwd = getcwd(NULL, 0);
|
||||
if (cwd == NULL) {
|
||||
perror("getcwd");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
fprintf(stderr, "Created %s at\n %s\n", manifest.project, cwd);
|
||||
|
||||
free(cwd);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
61
src/name.c
Normal file
61
src/name.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/* Copyright (C) GCK
|
||||
*
|
||||
* This file is part of yait
|
||||
*
|
||||
* This project and file is licenced under the BSD-3-Clause licence.
|
||||
* <https://opensource.org/licence/bsd-3-clause>
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <pwd.h>
|
||||
|
||||
#include "../lib/str_dup.h"
|
||||
|
||||
#include "name.h"
|
||||
|
||||
char *get_name()
|
||||
{
|
||||
int fds[2];
|
||||
if (pipe(fds) == -1)
|
||||
goto sysuser;
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid == -1) {
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
goto sysuser;
|
||||
}
|
||||
|
||||
if (pid == 0) {
|
||||
dup2(fds[1], STDOUT_FILENO);
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
execlp("git", "git", "config", "--get", "user.name",
|
||||
(char *)NULL);
|
||||
_exit(127);
|
||||
}
|
||||
|
||||
close(fds[1]);
|
||||
char buf[256];
|
||||
ssize_t n = read(fds[0], buf, sizeof buf - 1);
|
||||
close(fds[0]);
|
||||
int status;
|
||||
waitpid(pid, &status, 0);
|
||||
if (n > 0 && WIFEXITED(status) && WEXITSTATUS(status) == 0) {
|
||||
buf[n] = 0;
|
||||
buf[strcspn(buf, "\n")] = 0;
|
||||
return str_dup(buf);
|
||||
}
|
||||
|
||||
sysuser: {
|
||||
char *name = getlogin();
|
||||
if (name)
|
||||
return str_dup(name);
|
||||
struct passwd *pw = getpwuid(getuid());
|
||||
if (pw && pw->pw_name)
|
||||
return str_dup(pw->pw_name);
|
||||
}
|
||||
return "author";
|
||||
}
|
||||
14
src/name.h
Normal file
14
src/name.h
Normal file
@@ -0,0 +1,14 @@
|
||||
/* Copyright (C) GCK
|
||||
*
|
||||
* This file is part of yait
|
||||
*
|
||||
* This project and file is licenced under the BSD-3-Clause licence.
|
||||
* <https://opensource.org/licence/bsd-3-clause>
|
||||
*/
|
||||
|
||||
#ifndef NAME_H
|
||||
#define NAME_H
|
||||
|
||||
char *get_name();
|
||||
|
||||
#endif
|
||||
@@ -1,34 +0,0 @@
|
||||
/* Copyright (C) GCK
|
||||
*
|
||||
* This file is part of yait
|
||||
*
|
||||
* This project and file is licenced under the BSD-3-Clause licence.
|
||||
* <https://opensource.org/license/bsd-3-clause>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../config.h"
|
||||
#include "standard.h"
|
||||
|
||||
int parse_standard_options(void (*usage)(int), int argc, char **argv)
|
||||
{
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
if (strcmp(argv[i], "--help") == 0) {
|
||||
usage(0);
|
||||
exit(EXIT_SUCCESS);
|
||||
} else if (strcmp(argv[i], "--version") == 0) {
|
||||
printf("%s %s %d\nCopyright (C) %d %s.\n%s\nThis is "
|
||||
"free software: "
|
||||
"you are free to change and redistribute "
|
||||
"it.\nThere is NO "
|
||||
"WARRNTY, to the extent permitted by law.\n",
|
||||
PROGRAM, VERSION, COMMIT, YEAR, AUTHORS,
|
||||
LICENSE_LINE);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
}
|
||||
return HELP_REQUESTED;
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
#ifndef STANDARD_H
|
||||
#define STANDARD_H
|
||||
|
||||
#define HELP_REQUESTED 2
|
||||
|
||||
int parse_standard_options(void (*usage)(int), int argc, char **argv);
|
||||
|
||||
#endif
|
||||
@@ -28,7 +28,7 @@ licence_t TOlicence(char *src)
|
||||
if (!strcmp(s, "BSD"))
|
||||
return BSD;
|
||||
free(s);
|
||||
return UNL;
|
||||
return BSD;
|
||||
}
|
||||
|
||||
char *str_dup(const char *s)
|
||||
|
||||
Reference in New Issue
Block a user