mirror of
https://github.com/michaeldv/pit.git
synced 2025-12-09 16:05:35 +00:00
Added expand_path() and perish()
This commit is contained in:
117
src/db.c
117
src/db.c
@@ -1,26 +1,101 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <pwd.h>
|
||||
#include <sys/types.h>
|
||||
#include "pit.h"
|
||||
#include "db.h"
|
||||
#include "activity.h"
|
||||
#include "user.h"
|
||||
|
||||
#define PITFILE "/tmp/.pit"
|
||||
#define PITFILE "~/.pit"
|
||||
|
||||
char *mem2str(char *mem, int len) {
|
||||
char *str = malloc(len + 1);
|
||||
memcpy(str, mem, len);
|
||||
str[len] = '\0';
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
static char *home_dir(char *username, int len) {
|
||||
char *str = mem2str(username, len);
|
||||
struct passwd *pw = getpwnam(str);
|
||||
free(str);
|
||||
|
||||
return (pw ? pw->pw_dir : NULL);
|
||||
}
|
||||
|
||||
static char *expand_path(char *path, char *expanded) {
|
||||
if (!path || *path != '~') {
|
||||
return path;
|
||||
} else {
|
||||
char *next = path + 1;
|
||||
if (*next == '/') { /* Path without the username, i.e. ~/file */
|
||||
strcpy(expanded, getenv("HOME"));
|
||||
strcat(expanded, next);
|
||||
} else { /* Path with the username, i.e. ~username/file */
|
||||
char *slash = strchr(next, '/');
|
||||
if (!slash) {
|
||||
slash = next + strlen(next);
|
||||
}
|
||||
char *home = home_dir(next, slash - next);
|
||||
if (!home) { /* Ex. non-existent username. */
|
||||
perish(path);
|
||||
} else {
|
||||
strcpy(expanded, home);
|
||||
strcat(expanded, slash);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expanded;
|
||||
}
|
||||
|
||||
static char *pit_file_name()
|
||||
{
|
||||
static char file_name[128];
|
||||
|
||||
if (!*file_name) {
|
||||
strcpy(file_name, expand_path(PITFILE, file_name));
|
||||
}
|
||||
|
||||
return file_name;
|
||||
}
|
||||
|
||||
int pit_init(char *argv[]) {
|
||||
char **arg = &argv[1];
|
||||
char *file_name = pit_file_name();
|
||||
int reply = 'Y';
|
||||
|
||||
if (!access(file_name, F_OK)) {
|
||||
if (!*arg || strcmp(*arg, "-f")) { /* Do not prompt user if forced init (-f). */
|
||||
printf("%s already exist, do you want to override it [y/N]: ", file_name);
|
||||
reply = getchar();
|
||||
}
|
||||
}
|
||||
|
||||
if (reply == 'y' || reply == 'Y') {
|
||||
pit_db_initialize();
|
||||
printf("Created empty %s\n", file_name);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void pit_db_load() {
|
||||
FILE *pitfile = fopen(PITFILE, "r");
|
||||
char *file_name = pit_file_name();
|
||||
FILE *file = fopen(file_name, "r");
|
||||
|
||||
if (pitfile) {
|
||||
projects = pit_table_load(pitfile);
|
||||
tasks = pit_table_load(pitfile);
|
||||
notes = pit_table_load(pitfile);
|
||||
activities = pit_table_load(pitfile);
|
||||
users = pit_table_load(pitfile);
|
||||
fclose(pitfile);
|
||||
if (!file) {
|
||||
perish(file_name);
|
||||
} else {
|
||||
pit_db_initialize();
|
||||
projects = pit_table_load(file);
|
||||
tasks = pit_table_load(file);
|
||||
notes = pit_table_load(file);
|
||||
activities = pit_table_load(file);
|
||||
users = pit_table_load(file);
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,17 +113,17 @@ void pit_db_initialize() {
|
||||
}
|
||||
|
||||
void pit_db_save() {
|
||||
FILE *pitfile = fopen(PITFILE, "wb");
|
||||
char *file_name = pit_file_name();
|
||||
FILE *file = fopen(file_name, "wb");
|
||||
|
||||
if (!pitfile) {
|
||||
die_with_errno(PITFILE);
|
||||
if (!file) {
|
||||
perish(file_name);
|
||||
} else {
|
||||
pit_table_save(pitfile, projects);
|
||||
pit_table_save(pitfile, tasks);
|
||||
pit_table_save(pitfile, notes);
|
||||
pit_table_save(pitfile, activities);
|
||||
pit_table_save(pitfile, users);
|
||||
|
||||
fclose(pitfile);
|
||||
pit_table_save(file, projects);
|
||||
pit_table_save(file, tasks);
|
||||
pit_table_save(file, notes);
|
||||
pit_table_save(file, activities);
|
||||
pit_table_save(file, users);
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
7
src/db.h
7
src/db.h
@@ -1,7 +1,8 @@
|
||||
#if !defined(__DB_H__)
|
||||
#define __DB_H__
|
||||
|
||||
void pit_db_load();
|
||||
void pit_db_save();
|
||||
void pit_db_initialize();
|
||||
int pit_init(char *argv[]);
|
||||
void pit_db_load();
|
||||
void pit_db_save();
|
||||
void pit_db_initialize();
|
||||
#endif
|
||||
|
||||
24
src/pit.c
24
src/pit.c
@@ -3,6 +3,7 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include "pit.h"
|
||||
#include "db.h"
|
||||
#include "project.h"
|
||||
#include "task.h"
|
||||
|
||||
@@ -17,21 +18,30 @@ static int usage() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** Suicide.
|
||||
*/
|
||||
void die(char *msg)
|
||||
{
|
||||
fprintf(stderr, "pit (fatal): %s\n", msg);
|
||||
fprintf(stderr, "pit: %s\n", msg);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void die_with_errno(char *prefix)
|
||||
/*
|
||||
** Forceful death.
|
||||
*/
|
||||
void perish(char *prefix)
|
||||
{
|
||||
fprintf(stderr, "pit (fatal): %s: ", prefix);
|
||||
fprintf(stderr, "pit (fatal): ");
|
||||
if (prefix) {
|
||||
fprintf(stderr, "%s - ", prefix);
|
||||
}
|
||||
perror(NULL);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char *commands[] = { "project", "task", "note", "log" };
|
||||
char *commands[] = { "project", "task", "note", "log", "init" };
|
||||
|
||||
/***
|
||||
int i;
|
||||
@@ -46,6 +56,12 @@ int main(int argc, char *argv[]) {
|
||||
return pit_project(&argv[1]);
|
||||
} else if (strstr(commands[1], argv[1]) == commands[1]) {
|
||||
return pit_task(&argv[1]);
|
||||
} else if (strstr(commands[2], argv[1]) == commands[2]) {
|
||||
return 1; /* pit_note(&argv[1]); */
|
||||
} else if (strstr(commands[3], argv[1]) == commands[3]) {
|
||||
return 1; /* pit_log(&argv[1]); */
|
||||
} else if (strstr(commands[4], argv[1]) == commands[4]) {
|
||||
return pit_init(&argv[1]);
|
||||
}
|
||||
}
|
||||
return usage();
|
||||
|
||||
@@ -70,6 +70,6 @@ extern PTable activities;
|
||||
extern PTable users;
|
||||
|
||||
void die(char *msg);
|
||||
void die_with_errno(char *prefix);
|
||||
void perish(char *prefix);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -34,13 +34,17 @@ static void list_projects()
|
||||
static void create_project(char *name, char *status)
|
||||
{
|
||||
pit_db_load();
|
||||
printf("creating project [%s], status [%s]\n", name, status);
|
||||
|
||||
if (!already_exist(name)) {
|
||||
Project p;
|
||||
|
||||
memset(&p, 0, sizeof(p));
|
||||
|
||||
if (!status) {
|
||||
status = "open";
|
||||
}
|
||||
printf("creating project [%s], status [%s]\n", name, status);
|
||||
|
||||
strncpy(p.name, name, sizeof(p.name) - 1);
|
||||
strncpy(p.status, status, sizeof(p.status) - 1);
|
||||
p.current = 1;
|
||||
@@ -70,23 +74,22 @@ static int delete_project(unsigned long number)
|
||||
int pit_project(char *argv[])
|
||||
{
|
||||
char **arg = &argv[1];
|
||||
char *error = NULL;
|
||||
|
||||
if (!*arg) {
|
||||
list_projects();
|
||||
} else if (!strcmp(*arg, "-c")) {
|
||||
if (!*++arg) {
|
||||
error = "missing project name";
|
||||
die("missing project name");
|
||||
} else {
|
||||
create_project(*arg, *(arg + 1));
|
||||
}
|
||||
} else if (!strcmp(*arg, "-d") || !strcmp(*arg, "-s")) {
|
||||
if (!*++arg) {
|
||||
error = "missing project number";
|
||||
die("missing project number");
|
||||
} else {
|
||||
unsigned long number = atoi(*arg);
|
||||
if (!number) {
|
||||
error = "invalid project number";
|
||||
die("invalid project number");
|
||||
} else {
|
||||
if (!strcmp(*--arg, "-d")) {
|
||||
delete_project(number);
|
||||
@@ -97,9 +100,5 @@ int pit_project(char *argv[])
|
||||
}
|
||||
}
|
||||
|
||||
if (error) {
|
||||
printf("%s\n", error);
|
||||
}
|
||||
|
||||
return error == NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
0
src/table.c
Executable file → Normal file
0
src/table.c
Executable file → Normal file
Reference in New Issue
Block a user