Moved utility functions to src/util.c

This commit is contained in:
Mike Dvorkin
2010-07-27 21:24:38 -07:00
parent 33d51d70b6
commit be2bea9141
6 changed files with 82 additions and 58 deletions

View File

@@ -2,53 +2,10 @@
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include "pit.h"
#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()
{

4
src/pager.c Normal file
View File

@@ -0,0 +1,4 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "pit.h"

View File

@@ -6,7 +6,7 @@ typedef unsigned long ulong;
typedef unsigned char uchar;
#include <time.h>
#include "models.h"
#include "object.h"
#include "table.h"
/* Externals. */
@@ -22,27 +22,31 @@ extern PTable users;
#define for_each_task(ptr) for (ptr = (PTask)tasks->slots; (ptr - (PTask)tasks->slots) / sizeof(PTask) < tasks->number_of_records; ptr++)
#define for_each_user(ptr) for (ptr = (PUser)users->slots; (ptr - (PUser)users->slots) / sizeof(PUser) < users->number_of_records; ptr++)
/* Command line parsing. */
/* args.c */
int pit_arg_is_option(char **arg);
int pit_arg_option(char **arg);
char *pit_arg_string(char **arg, char *required);
ulong pit_arg_number(char **arg, char *required);
time_t pit_arg_time(char **arg, char *required);
/* Database. */
/* db.c */
int pit_init(char *argv[]);
void pit_db_load();
void pit_db_save();
void pit_db_initialize();
/* Models. */
/* activity.c project.c task.c user.c */
int pit_project(char *argv[]);
int pit_task(char *argv[]);
char *pit_current_user();
PActivity pit_add_activity(ulong id, char *subject, char *message, ulong user_id);
/* Utilities. */
/* util.c */
void die(char *msg, ...);
void perish(char *prefix);
char *mem2str(char *mem, int len);
char *home_dir(char *username, int len);
char *expand_path(char *path, char *expanded);
#endif

49
src/util.c Normal file
View File

@@ -0,0 +1,49 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include "pit.h"
char *mem2str(char *mem, int len) {
char *str = malloc(len + 1);
memcpy(str, mem, len);
str[len] = '\0';
return str;
}
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);
}
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;
}