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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "config.h"
#include "support.h"
#define USRDIR ".ppxp"
char *usrPPxP, *sysPPxP;
char *
Strdup(char *org)
{
char *new;
if ((new = strdup(org)) == NULL) {
fprintf(stderr, "Strdup: memory allocation error!!\n");
exit(-1);
}
return(new);
}
void *
Malloc(size_t size)
{
void *p;
if ((p = malloc(size)) == NULL) {
fprintf(stderr, "Malloc: memory allocation error!!\n");
exit(-1);
}
return(p);
}
void *
Calloc(size_t num, size_t size)
{
void *p;
if ((p = calloc(num, size)) == NULL) {
fprintf(stderr, "memory allocation error!!\n");
exit(-1);
}
return(p);
}
void *
Realloc(char *org, size_t size)
{
char *new;
if ((new = realloc(org, size)) == NULL) {
fprintf(stderr, "Realloc: memory allocation error!!\n");
exit(-1);
}
return(new);
}
int
DecodeArgs(char *argv[], unsigned char *buf, int len, int max)
{
unsigned char *ep;
int argc = 0;
ep = buf + len;
max --;
while (buf < ep) {
argv[argc ++] = Strdup(buf);
buf += strlen(buf) + 1;
if (argc == max) break;
}
argv[argc] = NULL;
return(argc);
}
void
FreeArgs(int argc, char *argv[])
{
int a;
for (a = 0; a < argc; a ++) if (argv[a]) Free(argv[a]);
}
void
DirNameInit(uid_t uid)
{
if (usrPPxP) {
Free(usrPPxP);
usrPPxP = NULL;
}
if (uid) {
struct passwd *pw;
if ((pw = getpwuid(uid)) != NULL && pw->pw_dir) {
usrPPxP = Malloc(strlen(pw->pw_dir) + 1
+ sizeof(USRDIR) + 1);
sprintf(usrPPxP, "%s/"USRDIR, pw->pw_dir);
}
}
if (!sysPPxP) sysPPxP = Strdup(PPXPDIR);
}
struct list_s *
FileList(char *top, const char *name)
{
struct dirent *dir;
DIR *dp;
struct stat st;
char path[1024], *np;
struct list_s *lp, *hp=NULL;
sprintf(path, "%s/%s/", top, name);
np = path + strlen(path);
if ((dp = opendir(path)) == NULL) return(NULL);
while ((dir = readdir(dp)) != NULL) {
strcpy(np, dir->d_name);
if (!lstat(path, &st) && S_ISREG(st.st_mode)) {
lp = TCALLOC(struct list_s);
lp->name = Strdup(dir->d_name);
lp->data = Strdup(path);
lp->next = hp;
hp = lp;
}
}
closedir(dp);
return(hp);
}
struct list_s *
SortList(struct list_s *op)
{
struct list_s *hp, *lp, *lp0, *tp, *tpn;
int i, n;
hp = op;
tp = op->next;
hp->next = NULL;
while (tp) {
tpn = tp->next;
lp0 = NULL;
lp = hp;
while (lp) {
if (strcasecmp(tp->name, lp->name) < 0) break;
lp0 = lp;
lp = lp->next;
}
if (lp0) {
tp->next = lp0->next;
lp0->next = tp;
} else {
tp->next = hp;
hp = tp;
}
tp = tpn;
}
return(hp);
}
void
DestroyList(struct list_s *lp)
{
struct list_s *tp;
while (lp) {
if (lp->data) Free(lp->data);
if (lp->name) Free(lp->name);
tp = lp->next;
Free(lp);
lp = tp;
}
}
|