save
This commit is contained in:
11
core/e.c
Normal file
11
core/e.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "e.h"
|
||||
#include "print.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
error_t unwrap(error_t err) {
|
||||
if (!err.null) {
|
||||
printfn("error: %s", err.src);
|
||||
exit(err.status);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
7
core/e.h
7
core/e.h
@@ -1,9 +1,14 @@
|
||||
#ifndef ERROR_H
|
||||
#define ERROR_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
int stat;
|
||||
bool null;
|
||||
int status;
|
||||
const char *src;
|
||||
} error_t;
|
||||
|
||||
error_t unwrap(error_t);
|
||||
|
||||
#endif
|
||||
|
||||
40
core/file.c
40
core/file.c
@@ -4,19 +4,49 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
error_t touch(char *path, char *format, ...) {
|
||||
error_t err = {0};
|
||||
err.null = true; // success by default
|
||||
|
||||
error_t write(char *path, char *format, ...) {
|
||||
error_t err;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
FILE *fp;
|
||||
fp = fopen(path, "w");
|
||||
|
||||
FILE *fp = fopen(path, "w");
|
||||
if (!fp) {
|
||||
err.stat = errno;
|
||||
err.null = false;
|
||||
err.status = errno;
|
||||
err.src = strerror(errno);
|
||||
va_end(args);
|
||||
return err;
|
||||
}
|
||||
|
||||
vfprintf(fp, format, args);
|
||||
fclose(fp);
|
||||
|
||||
va_end(args);
|
||||
return err;
|
||||
}
|
||||
|
||||
error_t dir(char *format, ...) {
|
||||
error_t err = {0};
|
||||
err.null = true; // success by default
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
|
||||
char path[1024];
|
||||
vsnprintf(path, sizeof(path), format, args);
|
||||
|
||||
va_end(args);
|
||||
|
||||
if (mkdir(path, 0777) < 0) {
|
||||
err.null = false;
|
||||
err.status = errno;
|
||||
err.src = strerror(errno);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -2,8 +2,13 @@
|
||||
#define FILE_H
|
||||
|
||||
#include "e.h"
|
||||
#include <unistd.h>
|
||||
|
||||
error_t write(char *, char *, ...);
|
||||
void printfn(char *, ...);
|
||||
#define take(x, ...) \
|
||||
dir(x, ##__VA_ARGS__); \
|
||||
chdir(x);
|
||||
|
||||
error_t touch(char *, char *, ...);
|
||||
error_t dir(char *, ...);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user