summaryrefslogtreecommitdiff
path: root/entry.c
blob: d5f14252c4c7ad4cdfb8bc7ad84b1ea3c12df287 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <assert.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "entry.h"
#include "group.h"
#define BUF_LEN 1024

typedef struct entry{
	char name[BUF_LEN];
	char path[BUF_LEN];
	struct entry *next;
} ENTRY;

ENTRY *create_entry(char *new_path, char *new_group);
ENTRY *entry_add_last(ENTRY *tail, ENTRY *add);
ENTRY **get_entries(ENTRY *head, int count);
char *get_ename(ENTRY *e);
char *get_epath(ENTRY *e);

ENTRY *create_entry(char *new_path, char *new_group){
	ENTRY *new;
	char full_path[BUF_LEN] = "";

	if(new_group != NULL) strcat(full_path, new_group);
	strcat(full_path, new_path);

	//check if file exists
	if(access(full_path, F_OK) == -1){
		printf("Error: Invalid File Name \"%s\"\n", full_path);
		return NULL;
	}

	new = malloc(sizeof(ENTRY));

	strcpy(new->name, new_path);
	strcpy(new->path, full_path);
	new->next = NULL;

	return new;
}

ENTRY *entry_add_last(ENTRY *tail, ENTRY *add){
	assert(add != NULL);

	if(tail == NULL) tail = add;
	else{
		tail->next = add;
		tail = add;
	}

	return tail;
}

ENTRY **get_entries(ENTRY *head, int count){
	ENTRY **arr = malloc(sizeof(ENTRY *) * count);
	ENTRY *trav = head;
	int i;

	for(i = 0; i < count; i++){
		arr[i] = trav;
		trav = trav->next;
	}

	return arr;
}

char *get_ename(ENTRY *e){
	assert(e != NULL);
	return e->name;
}
char *get_epath(ENTRY *e){
	assert(e != NULL);
	return e->name;
	return e->path;
}

void entry_debug(ENTRY *trav){

	while(trav != NULL){
		printf("%s, \n", trav->name);
		trav = trav->next;
	}

	return;
}