feat: standard argument parsing
This commit is contained in:
85
core/file.c
85
core/file.c
@@ -7,60 +7,71 @@
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
error_t touch(char *path, char *format, ...) {
|
||||
error_t err = {0};
|
||||
error_t
|
||||
touch (char *path, char *format, ...)
|
||||
{
|
||||
error_t err = { 0 };
|
||||
err.null = true;
|
||||
|
||||
FILE *fp = fopen(path, "w");
|
||||
if (!fp) {
|
||||
err.null = false;
|
||||
err.status = errno;
|
||||
err.src = strerror(errno);
|
||||
return err;
|
||||
}
|
||||
FILE *fp = fopen (path, "w");
|
||||
if (!fp)
|
||||
{
|
||||
err.null = false;
|
||||
err.status = errno;
|
||||
err.src = strerror (errno);
|
||||
return err;
|
||||
}
|
||||
|
||||
else {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vfprintf(fp, format, args);
|
||||
va_end(args);
|
||||
}
|
||||
else
|
||||
{
|
||||
va_list args;
|
||||
va_start (args, format);
|
||||
vfprintf (fp, format, args);
|
||||
va_end (args);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
fclose (fp);
|
||||
return err;
|
||||
}
|
||||
|
||||
error_t dir(char *format, ...) {
|
||||
error_t err = {0};
|
||||
error_t
|
||||
dir (char *format, ...)
|
||||
{
|
||||
error_t err = { 0 };
|
||||
err.null = true; // success by default
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
va_start (args, format);
|
||||
|
||||
char path[1024];
|
||||
vsnprintf(path, sizeof(path), format, args);
|
||||
vsnprintf (path, sizeof (path), format, args);
|
||||
|
||||
va_end(args);
|
||||
va_end (args);
|
||||
|
||||
if (mkdir(path, 0777) < 0) {
|
||||
err.null = false;
|
||||
err.status = errno;
|
||||
err.src = strerror(errno);
|
||||
}
|
||||
if (mkdir (path, 0777) < 0)
|
||||
{
|
||||
err.null = false;
|
||||
err.status = errno;
|
||||
err.src = strerror (errno);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
error_t take(const char *dirname) {
|
||||
error_t err = dir("%s", dirname);
|
||||
if (!err.null) {
|
||||
return err;
|
||||
}
|
||||
if (chdir(dirname) != 0) {
|
||||
err.null = false;
|
||||
err.status = errno;
|
||||
err.src = strerror(errno);
|
||||
return err;
|
||||
}
|
||||
error_t
|
||||
take (const char *dirname)
|
||||
{
|
||||
error_t err = dir ("%s", dirname);
|
||||
if (!err.null)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
if (chdir (dirname) != 0)
|
||||
{
|
||||
err.null = false;
|
||||
err.status = errno;
|
||||
err.src = strerror (errno);
|
||||
return err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
29
core/standard.c
Normal file
29
core/standard.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "standard.h"
|
||||
#include "../config.h"
|
||||
#include "print.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int
|
||||
parse_standard_options (void (*usage) (), int argc, char **argv)
|
||||
{
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
if (strcmp (argv[i], "--help") == 0)
|
||||
{
|
||||
usage ();
|
||||
exit (0);
|
||||
}
|
||||
else if (strcmp (argv[i], "--version") == 0)
|
||||
{
|
||||
printfn ("%s (%s) %s\nCopyright (C) %d %s.\n%s\n"
|
||||
"This is free software: you are free to change "
|
||||
"and redistribute it.\nThere is NO WARRANTY, to the extent "
|
||||
"permitted by law.",
|
||||
PROGRAM, ORGANIZATION, VERSION, YEAR, ORGANIZATION,
|
||||
LICENCE_LINE);
|
||||
exit (0);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
6
core/standard.h
Normal file
6
core/standard.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef STANDARD_H
|
||||
#define STANDARD_H
|
||||
|
||||
int parse_standard_options(void (*)(), int argc, char **argv);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user