mirror of
https://github.com/0intro/wmii
synced 2025-01-03 09:04:32 +03:00
sanitized emalloc into emallocz which let's us get rid of various init crap
This commit is contained in:
parent
244f66731a
commit
629b23ae5d
@ -9,15 +9,12 @@
|
|||||||
|
|
||||||
#include "wm.h"
|
#include "wm.h"
|
||||||
|
|
||||||
static Area zero_area = {0};
|
|
||||||
|
|
||||||
Area *alloc_area(Page *p, XRectangle * r, char *layout)
|
Area *alloc_area(Page *p, XRectangle * r, char *layout)
|
||||||
{
|
{
|
||||||
char buf[MAX_BUF];
|
char buf[MAX_BUF];
|
||||||
Area *a = (Area *) cext_emalloc(sizeof(Area));
|
Area *a = (Area *) cext_emallocz(sizeof(Area));
|
||||||
size_t id = cext_sizeof(&p->areas);
|
size_t id = cext_sizeof(&p->areas);
|
||||||
|
|
||||||
*a = zero_area;
|
|
||||||
a->rect = *r;
|
a->rect = *r;
|
||||||
a->page = p;
|
a->page = p;
|
||||||
snprintf(buf, MAX_BUF, "/%s/a/%d", p->file[P_PREFIX]->name, id);
|
snprintf(buf, MAX_BUF, "/%s/a/%d", p->file[P_PREFIX]->name, id);
|
||||||
|
@ -10,16 +10,13 @@
|
|||||||
|
|
||||||
#include "wm.h"
|
#include "wm.h"
|
||||||
|
|
||||||
static Client zero_client = { 0 };
|
|
||||||
|
|
||||||
Client *alloc_client(Window w)
|
Client *alloc_client(Window w)
|
||||||
{
|
{
|
||||||
static int id = 0;
|
static int id = 0;
|
||||||
char buf[MAX_BUF];
|
char buf[MAX_BUF];
|
||||||
char buf2[MAX_BUF];
|
char buf2[MAX_BUF];
|
||||||
Client *c = (Client *) cext_emalloc(sizeof(Client));
|
Client *c = (Client *) cext_emallocz(sizeof(Client));
|
||||||
|
|
||||||
*c = zero_client;
|
|
||||||
c->win = w;
|
c->win = w;
|
||||||
snprintf(buf, MAX_BUF, "/detached/c/%d", id);
|
snprintf(buf, MAX_BUF, "/detached/c/%d", id);
|
||||||
c->file[C_PREFIX] = ixp_create(ixps, buf);
|
c->file[C_PREFIX] = ixp_create(ixps, buf);
|
||||||
|
@ -10,8 +10,6 @@
|
|||||||
|
|
||||||
#include "wm.h"
|
#include "wm.h"
|
||||||
|
|
||||||
static Frame zero_frame = { 0 };
|
|
||||||
|
|
||||||
static void select_client(void *obj, char *cmd);
|
static void select_client(void *obj, char *cmd);
|
||||||
static void handle_after_write_frame(IXPServer * s, File * f);
|
static void handle_after_write_frame(IXPServer * s, File * f);
|
||||||
static void handle_before_read_frame(IXPServer * s, File * f);
|
static void handle_before_read_frame(IXPServer * s, File * f);
|
||||||
@ -27,10 +25,9 @@ Frame *alloc_frame(XRectangle * r)
|
|||||||
XSetWindowAttributes wa;
|
XSetWindowAttributes wa;
|
||||||
static int id = 0;
|
static int id = 0;
|
||||||
char buf[MAX_BUF];
|
char buf[MAX_BUF];
|
||||||
Frame *f = (Frame *) cext_emalloc(sizeof(Frame));
|
Frame *f = (Frame *) cext_emallocz(sizeof(Frame));
|
||||||
int bw, th;
|
int bw, th;
|
||||||
|
|
||||||
*f = zero_frame;
|
|
||||||
f->rect = *r;
|
f->rect = *r;
|
||||||
f->cursor = normal_cursor;
|
f->cursor = normal_cursor;
|
||||||
|
|
||||||
|
@ -34,9 +34,6 @@ static void resize_col(Frame * f, XRectangle * new, XPoint * pt);
|
|||||||
|
|
||||||
static Layout lcol = { "col", init_col, deinit_col, arrange_col, attach_col, detach_col, resize_col };
|
static Layout lcol = { "col", init_col, deinit_col, arrange_col, attach_col, detach_col, resize_col };
|
||||||
|
|
||||||
static Column zero_column = { 0 };
|
|
||||||
static Acme zero_acme = { 0 };
|
|
||||||
|
|
||||||
void init_layout_column()
|
void init_layout_column()
|
||||||
{
|
{
|
||||||
cext_attach_item(&layouts, &lcol);
|
cext_attach_item(&layouts, &lcol);
|
||||||
@ -76,13 +73,12 @@ static void arrange_col(Area *a)
|
|||||||
|
|
||||||
static void init_col(Area *a)
|
static void init_col(Area *a)
|
||||||
{
|
{
|
||||||
Acme *acme = cext_emalloc(sizeof(Acme));
|
Acme *acme = cext_emallocz(sizeof(Acme));
|
||||||
int i, cols = 1;
|
int i, cols = 1;
|
||||||
unsigned int width;
|
unsigned int width;
|
||||||
//size_t size;
|
//size_t size;
|
||||||
Column *col;
|
Column *col;
|
||||||
|
|
||||||
*acme = zero_acme;
|
|
||||||
a->aux = acme;
|
a->aux = acme;
|
||||||
|
|
||||||
/* processing argv */
|
/* processing argv */
|
||||||
@ -100,8 +96,7 @@ static void init_col(Area *a)
|
|||||||
|
|
||||||
width = a->rect.width / cols;
|
width = a->rect.width / cols;
|
||||||
for (i = 0; i < cols; i++) {
|
for (i = 0; i < cols; i++) {
|
||||||
col = cext_emalloc(sizeof(Column));
|
col = cext_emallocz(sizeof(Column));
|
||||||
*col = zero_column;
|
|
||||||
col->rect = a->rect;
|
col->rect = a->rect;
|
||||||
col->rect.x = i * width;
|
col->rect.x = i * width;
|
||||||
col->rect.width = width;
|
col->rect.width = width;
|
||||||
|
@ -37,8 +37,7 @@ static void iter_attach_float(void *client, void *area)
|
|||||||
|
|
||||||
static void init_float(Area *a)
|
static void init_float(Area *a)
|
||||||
{
|
{
|
||||||
Container *c = cext_emalloc(sizeof(Container));
|
Container *c = cext_emallocz(sizeof(Container));
|
||||||
c->list = c->stack = nil;
|
|
||||||
a->aux = c;
|
a->aux = c;
|
||||||
cext_iterate(&a->clients, a, iter_attach_float);
|
cext_iterate(&a->clients, a, iter_attach_float);
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ Action page_acttbl[] = {
|
|||||||
|
|
||||||
Page *alloc_page()
|
Page *alloc_page()
|
||||||
{
|
{
|
||||||
Page *p = cext_emalloc(sizeof(Page));
|
Page *p = cext_emallocz(sizeof(Page));
|
||||||
char buf[MAX_BUF], buf2[16];
|
char buf[MAX_BUF], buf2[16];
|
||||||
size_t id = cext_sizeof(&pages);
|
size_t id = cext_sizeof(&pages);
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ XRectangle *rectangles(unsigned int *num)
|
|||||||
XRectangle r;
|
XRectangle r;
|
||||||
|
|
||||||
if (XQueryTree(dpy, root, &d1, &d2, &wins, num)) {
|
if (XQueryTree(dpy, root, &d1, &d2, &wins, num)) {
|
||||||
result = cext_emalloc(*num * sizeof(XRectangle));
|
result = cext_emallocz(*num * sizeof(XRectangle));
|
||||||
for (i = 0; i < *num; i++) {
|
for (i = 0; i < *num; i++) {
|
||||||
if (!XGetWindowAttributes(dpy, wins[i], &wa))
|
if (!XGetWindowAttributes(dpy, wins[i], &wa))
|
||||||
continue;
|
continue;
|
||||||
|
@ -288,14 +288,14 @@ static void draw_bar(void *obj, char *arg)
|
|||||||
n = 0;
|
n = 0;
|
||||||
for (f = label; f; f = f->next)
|
for (f = label; f; f = f->next)
|
||||||
n++;
|
n++;
|
||||||
paths = cext_emalloc(sizeof(char *) * n);
|
paths = cext_emallocz(sizeof(char *) * n);
|
||||||
i = 0;
|
i = 0;
|
||||||
for (f = label; f; f = f->next)
|
for (f = label; f; f = f->next)
|
||||||
paths[i++] = f->name;
|
paths[i++] = f->name;
|
||||||
qsort(paths, n, sizeof(char *), comp_str);
|
qsort(paths, n, sizeof(char *), comp_str);
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
snprintf(buf, sizeof(buf), "/%s", paths[i]);
|
snprintf(buf, sizeof(buf), "/%s", paths[i]);
|
||||||
item = cext_emalloc(sizeof(Item));
|
item = cext_emallocz(sizeof(Item));
|
||||||
items = (Item **) attach_item_end((void **) items, item, sizeof(Item *));
|
items = (Item **) attach_item_end((void **) items, item, sizeof(Item *));
|
||||||
init_item(buf, item);
|
init_item(buf, item);
|
||||||
}
|
}
|
||||||
|
@ -207,13 +207,11 @@ static int make_qid(Qid * dir, char *wname, Qid * new)
|
|||||||
|
|
||||||
static int attach(IXPServer * s, IXPConn * c)
|
static int attach(IXPServer * s, IXPConn * c)
|
||||||
{
|
{
|
||||||
Map *map = cext_emalloc(sizeof(Map));
|
Map *map = cext_emallocz(sizeof(Map));
|
||||||
fprintf(stderr, "attaching %d %s %s\n", s->fcall.afid, s->fcall.uname,
|
fprintf(stderr, "attaching %d %s %s\n", s->fcall.afid, s->fcall.uname, s->fcall.aname);
|
||||||
s->fcall.aname);
|
|
||||||
map->qid = root_qid;
|
map->qid = root_qid;
|
||||||
map->fid = s->fcall.fid;
|
map->fid = s->fcall.fid;
|
||||||
c->aux =
|
c->aux = (Map **) attach_item_begin((void **) c->aux, map, sizeof(Map *));
|
||||||
(Map **) attach_item_begin((void **) c->aux, map, sizeof(Map *));
|
|
||||||
s->fcall.id = RATTACH;
|
s->fcall.id = RATTACH;
|
||||||
s->fcall.qid = root_qid;
|
s->fcall.qid = root_qid;
|
||||||
return TRUE;
|
return TRUE;
|
||||||
@ -252,16 +250,13 @@ static int walk(IXPServer * s, IXPConn * c)
|
|||||||
*/
|
*/
|
||||||
if (nwqid == s->fcall.nwname) {
|
if (nwqid == s->fcall.nwname) {
|
||||||
if (s->fcall.fid == s->fcall.newfid) {
|
if (s->fcall.fid == s->fcall.newfid) {
|
||||||
c->aux =
|
c->aux = (Map **) detach_item((void **) c->aux, map, sizeof(Map *));
|
||||||
(Map **) detach_item((void **) c->aux, map, sizeof(Map *));
|
|
||||||
free(map);
|
free(map);
|
||||||
}
|
}
|
||||||
map = cext_emalloc(sizeof(Map));
|
map = cext_emallocz(sizeof(Map));
|
||||||
map->qid = qid;
|
map->qid = qid;
|
||||||
map->fid = s->fcall.newfid;
|
map->fid = s->fcall.newfid;
|
||||||
c->aux =
|
c->aux = (Map **) attach_item_begin((void **) c->aux, map, sizeof(Map *));
|
||||||
(Map **) attach_item_begin((void **) c->aux, map,
|
|
||||||
sizeof(Map *));
|
|
||||||
}
|
}
|
||||||
s->fcall.id = RWALK;
|
s->fcall.id = RWALK;
|
||||||
s->fcall.nwqid = nwqid;
|
s->fcall.nwqid = nwqid;
|
||||||
@ -437,7 +432,7 @@ int main(int argc, char *argv[])
|
|||||||
atexit(exit_cleanup);
|
atexit(exit_cleanup);
|
||||||
|
|
||||||
/* default item settings */
|
/* default item settings */
|
||||||
item = cext_emalloc(sizeof(Item));
|
item = cext_emallocz(sizeof(Item));
|
||||||
item->id = 0;
|
item->id = 0;
|
||||||
item->text[0] = '\0';
|
item->text[0] = '\0';
|
||||||
item->value = 0;
|
item->value = 0;
|
||||||
|
@ -29,7 +29,6 @@ struct Bind {
|
|||||||
char *prefix;
|
char *prefix;
|
||||||
};
|
};
|
||||||
|
|
||||||
static Bind zero_bind = { 0 };
|
|
||||||
static Display *dpy;
|
static Display *dpy;
|
||||||
static IXPServer *ixps;
|
static IXPServer *ixps;
|
||||||
static char *sockfile = 0;
|
static char *sockfile = 0;
|
||||||
@ -135,9 +134,7 @@ static void bind(void *obj, char *arg)
|
|||||||
arg);
|
arg);
|
||||||
return; /* shortcut with empty argument */
|
return; /* shortcut with empty argument */
|
||||||
}
|
}
|
||||||
b = cext_emalloc(sizeof(Bind));
|
b = cext_emallocz(sizeof(Bind));
|
||||||
*b = zero_bind;
|
|
||||||
|
|
||||||
b->client = init_ixp_client(sfile);
|
b->client = init_ixp_client(sfile);
|
||||||
|
|
||||||
if (!b->client) {
|
if (!b->client) {
|
||||||
|
@ -54,8 +54,6 @@ static unsigned int num_lock_mask, valid_mask;
|
|||||||
static char buf[MAX_BUF];
|
static char buf[MAX_BUF];
|
||||||
static XFontStruct *font;
|
static XFontStruct *font;
|
||||||
|
|
||||||
static Shortcut zero_shortcut = { "", 0, 0, 0, 0 };
|
|
||||||
|
|
||||||
static void grab_shortcut(Shortcut * s);
|
static void grab_shortcut(Shortcut * s);
|
||||||
static void ungrab_shortcut(Shortcut * s);
|
static void ungrab_shortcut(Shortcut * s);
|
||||||
static void draw_shortcut_box(char *text);
|
static void draw_shortcut_box(char *text);
|
||||||
@ -126,12 +124,11 @@ static void create_shortcut(File * f)
|
|||||||
|
|
||||||
for (i = 0; i < toks; i++) {
|
for (i = 0; i < toks; i++) {
|
||||||
if (!s)
|
if (!s)
|
||||||
r = s = cext_emalloc(sizeof(Shortcut));
|
r = s = cext_emallocz(sizeof(Shortcut));
|
||||||
else {
|
else {
|
||||||
s->next = cext_emalloc(sizeof(Shortcut));
|
s->next = cext_emallocz(sizeof(Shortcut));
|
||||||
s = s->next;
|
s = s->next;
|
||||||
}
|
}
|
||||||
*s = zero_shortcut;
|
|
||||||
cext_strlcpy(s->name, chain[i], MAX_BUF);
|
cext_strlcpy(s->name, chain[i], MAX_BUF);
|
||||||
k = strrchr(chain[i], '-');
|
k = strrchr(chain[i], '-');
|
||||||
if (k)
|
if (k)
|
||||||
|
@ -111,9 +111,8 @@ static void _exec(char *cmd)
|
|||||||
add_history(cmd);
|
add_history(cmd);
|
||||||
if (files[M_PRE_COMMAND]->content) {
|
if (files[M_PRE_COMMAND]->content) {
|
||||||
size_t len = strlen(cmd) + files[M_PRE_COMMAND]->size + 2;
|
size_t len = strlen(cmd) + files[M_PRE_COMMAND]->size + 2;
|
||||||
rc = cext_emalloc(len);
|
rc = cext_emallocz(len);
|
||||||
snprintf(rc, len, "%s %s", (char *) files[M_PRE_COMMAND]->content,
|
snprintf(rc, len, "%s %s", (char *) files[M_PRE_COMMAND]->content, cmd);
|
||||||
cmd);
|
|
||||||
}
|
}
|
||||||
/* fallback */
|
/* fallback */
|
||||||
spawn(dpy, rc);
|
spawn(dpy, rc);
|
||||||
@ -240,7 +239,7 @@ static int update_items(char *pattern)
|
|||||||
items = 0;
|
items = 0;
|
||||||
item_size = size;
|
item_size = size;
|
||||||
if (item_size)
|
if (item_size)
|
||||||
items = (File **) cext_emalloc((item_size + 1) * sizeof(File *));
|
items = (File **) cext_emallocz((item_size + 1) * sizeof(File *));
|
||||||
}
|
}
|
||||||
size = 0;
|
size = 0;
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
include ../config.mk
|
include ../config.mk
|
||||||
|
|
||||||
SRC = container.c emalloc.c estrdup.c strlcat.c strlcpy.c strtonum.c tokenize.c
|
SRC = container.c emallocz.c estrdup.c strlcat.c strlcpy.c strtonum.c tokenize.c
|
||||||
|
|
||||||
OBJ = ${SRC:.c=.o}
|
OBJ = ${SRC:.c=.o}
|
||||||
|
|
||||||
|
@ -52,8 +52,8 @@ int count_items(void **items);
|
|||||||
int index_next_item(void **items, void *item);
|
int index_next_item(void **items, void *item);
|
||||||
int index_prev_item(void **items, void *item);
|
int index_prev_item(void **items, void *item);
|
||||||
|
|
||||||
/* emalloc.c */
|
/* emallocz.c */
|
||||||
void *cext_emalloc(size_t size);
|
void *cext_emallocz(size_t size);
|
||||||
|
|
||||||
/* estrdup.c */
|
/* estrdup.c */
|
||||||
char *cext_estrdup(const char *s);
|
char *cext_estrdup(const char *s);
|
||||||
|
@ -9,8 +9,6 @@
|
|||||||
|
|
||||||
#include "cext.h"
|
#include "cext.h"
|
||||||
|
|
||||||
static CItem zero_item = { 0 };
|
|
||||||
|
|
||||||
static int comp_ptr(void *p1, void *p2)
|
static int comp_ptr(void *p1, void *p2)
|
||||||
{
|
{
|
||||||
return p1 == p2;
|
return p1 == p2;
|
||||||
@ -51,9 +49,8 @@ static void attach_to_stack(Container *c, CItem *i)
|
|||||||
|
|
||||||
void cext_attach_item(Container *c, void *item)
|
void cext_attach_item(Container *c, void *item)
|
||||||
{
|
{
|
||||||
CItem *i, *new = cext_emalloc(sizeof(CItem));
|
CItem *i, *new = cext_emallocz(sizeof(CItem));
|
||||||
fprintf(stderr, "XX %s\n", "cext_attach_item()");
|
fprintf(stderr, "XX %s\n", "cext_attach_item()");
|
||||||
*new = zero_item;
|
|
||||||
new->item = item;
|
new->item = item;
|
||||||
if (!c->list)
|
if (!c->list)
|
||||||
c->list = new;
|
c->list = new;
|
||||||
@ -204,7 +201,7 @@ void **attach_item_begin(void **old, void *item, size_t size_item)
|
|||||||
int i, size_old;
|
int i, size_old;
|
||||||
void **result = 0;
|
void **result = 0;
|
||||||
for (size_old = 0; old && old[size_old]; size_old++);
|
for (size_old = 0; old && old[size_old]; size_old++);
|
||||||
result = cext_emalloc(size_item * (size_old + 2));
|
result = cext_emallocz(size_item * (size_old + 2));
|
||||||
result[0] = item;
|
result[0] = item;
|
||||||
for (i = 0; old && old[i]; i++)
|
for (i = 0; old && old[i]; i++)
|
||||||
result[i + 1] = old[i];
|
result[i + 1] = old[i];
|
||||||
@ -219,7 +216,7 @@ void **attach_item_end(void **old, void *item, size_t size_item)
|
|||||||
int i, size_old;
|
int i, size_old;
|
||||||
void **result = 0;
|
void **result = 0;
|
||||||
for (size_old = 0; old && old[size_old]; size_old++);
|
for (size_old = 0; old && old[size_old]; size_old++);
|
||||||
result = cext_emalloc(size_item * (size_old + 2));
|
result = cext_emallocz(size_item * (size_old + 2));
|
||||||
for (i = 0; old && old[i]; i++)
|
for (i = 0; old && old[i]; i++)
|
||||||
result[i] = old[i];
|
result[i] = old[i];
|
||||||
result[i++] = item;
|
result[i++] = item;
|
||||||
@ -235,7 +232,7 @@ void **detach_item(void **old, void *item, size_t size_item)
|
|||||||
void **result = 0;
|
void **result = 0;
|
||||||
for (size_old = 0; old && old[size_old]; size_old++);
|
for (size_old = 0; old && old[size_old]; size_old++);
|
||||||
if (size_old != 1) {
|
if (size_old != 1) {
|
||||||
result = cext_emalloc(size_item * size_old);
|
result = cext_emallocz(size_item * size_old);
|
||||||
for (i = 0; old[i]; i++)
|
for (i = 0; old[i]; i++)
|
||||||
if (old[i] != item)
|
if (old[i] != item)
|
||||||
result[j++] = old[i];
|
result[j++] = old[i];
|
||||||
|
@ -8,9 +8,9 @@
|
|||||||
|
|
||||||
#include "cext.h"
|
#include "cext.h"
|
||||||
|
|
||||||
void *cext_emalloc(size_t size)
|
void *cext_emallocz(size_t size)
|
||||||
{
|
{
|
||||||
void *res = malloc(size);
|
void *res = calloc(1, size);
|
||||||
|
|
||||||
if (!res) {
|
if (!res) {
|
||||||
fprintf(stderr, "fatal: could not malloc() %d bytes\n",
|
fprintf(stderr, "fatal: could not malloc() %d bytes\n",
|
@ -13,7 +13,7 @@ char *cext_estrdup(const char *s)
|
|||||||
{
|
{
|
||||||
char *tmp;
|
char *tmp;
|
||||||
|
|
||||||
tmp = (char *) cext_emalloc(strlen(s) + 1);
|
tmp = (char *) cext_emallocz(strlen(s) + 1);
|
||||||
strcpy(tmp, (char *) s);
|
strcpy(tmp, (char *) s);
|
||||||
|
|
||||||
return tmp;
|
return tmp;
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
#include <cext.h>
|
#include <cext.h>
|
||||||
|
|
||||||
static IXPClient zero_client = { 0 };
|
|
||||||
static size_t offsets[MAX_CONN * MAX_OPEN_FILES][2]; /* set of possible fd's */
|
static size_t offsets[MAX_CONN * MAX_OPEN_FILES][2]; /* set of possible fd's */
|
||||||
|
|
||||||
static void check_error(IXPClient * c, void *msg)
|
static void check_error(IXPClient * c, void *msg)
|
||||||
@ -117,7 +116,7 @@ static void *poll_server(IXPClient * c, void *request, size_t req_len,
|
|||||||
handle_dead_server(c);
|
handle_dead_server(c);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
result = cext_emalloc(*out_len);
|
result = cext_emallocz(*out_len);
|
||||||
header = 1;
|
header = 1;
|
||||||
}
|
}
|
||||||
r = read(c->fd, ((char *) result) + num, *out_len - num);
|
r = read(c->fd, ((char *) result) + num, *out_len - num);
|
||||||
@ -256,8 +255,7 @@ IXPClient *init_ixp_client(char *sockfile)
|
|||||||
socklen_t su_len;
|
socklen_t su_len;
|
||||||
|
|
||||||
/* init */
|
/* init */
|
||||||
IXPClient *c = (IXPClient *) cext_emalloc(sizeof(IXPClient));
|
IXPClient *c = (IXPClient *) cext_emallocz(sizeof(IXPClient));
|
||||||
*c = zero_client;
|
|
||||||
c->create = cixp_create;
|
c->create = cixp_create;
|
||||||
c->open = cixp_open;
|
c->open = cixp_open;
|
||||||
c->read = cixp_read;
|
c->read = cixp_read;
|
||||||
|
@ -10,14 +10,12 @@
|
|||||||
|
|
||||||
#include <cext.h>
|
#include <cext.h>
|
||||||
|
|
||||||
/* request messages ------------------------------------------------ */
|
|
||||||
|
|
||||||
void *tcreate_message(char *path, size_t * msg_len)
|
void *tcreate_message(char *path, size_t * msg_len)
|
||||||
{
|
{
|
||||||
char *msg;
|
char *msg;
|
||||||
ReqHeader h;
|
ReqHeader h;
|
||||||
*msg_len = sizeof(ReqHeader) + strlen(path) + 1;
|
*msg_len = sizeof(ReqHeader) + strlen(path) + 1;
|
||||||
msg = cext_emalloc(*msg_len);
|
msg = cext_emallocz(*msg_len);
|
||||||
h.req = TCREATE;
|
h.req = TCREATE;
|
||||||
memcpy(msg, &h, sizeof(ReqHeader));
|
memcpy(msg, &h, sizeof(ReqHeader));
|
||||||
memcpy(msg + sizeof(ReqHeader), path, strlen(path) + 1);
|
memcpy(msg + sizeof(ReqHeader), path, strlen(path) + 1);
|
||||||
@ -29,7 +27,7 @@ void *topen_message(char *path, size_t * msg_len)
|
|||||||
char *msg;
|
char *msg;
|
||||||
ReqHeader h;
|
ReqHeader h;
|
||||||
*msg_len = sizeof(ReqHeader) + strlen(path) + 1;
|
*msg_len = sizeof(ReqHeader) + strlen(path) + 1;
|
||||||
msg = cext_emalloc(*msg_len);
|
msg = cext_emallocz(*msg_len);
|
||||||
h.req = TOPEN;
|
h.req = TOPEN;
|
||||||
memcpy(msg, &h, sizeof(ReqHeader));
|
memcpy(msg, &h, sizeof(ReqHeader));
|
||||||
memcpy(msg + sizeof(ReqHeader), path, strlen(path) + 1);
|
memcpy(msg + sizeof(ReqHeader), path, strlen(path) + 1);
|
||||||
@ -42,7 +40,7 @@ void *tread_message(int fd, size_t offset, size_t buf_len,
|
|||||||
char *msg;
|
char *msg;
|
||||||
ReqHeader h;
|
ReqHeader h;
|
||||||
*msg_len = sizeof(ReqHeader);
|
*msg_len = sizeof(ReqHeader);
|
||||||
msg = cext_emalloc(*msg_len);
|
msg = cext_emallocz(*msg_len);
|
||||||
h.req = TREAD;
|
h.req = TREAD;
|
||||||
h.fd = fd;
|
h.fd = fd;
|
||||||
h.offset = offset;
|
h.offset = offset;
|
||||||
@ -57,7 +55,7 @@ void *twrite_message(int fd, size_t offset, void *content,
|
|||||||
char *msg;
|
char *msg;
|
||||||
ReqHeader h;
|
ReqHeader h;
|
||||||
*msg_len = sizeof(ReqHeader) + content_len;
|
*msg_len = sizeof(ReqHeader) + content_len;
|
||||||
msg = cext_emalloc(*msg_len);
|
msg = cext_emallocz(*msg_len);
|
||||||
h.req = TWRITE;
|
h.req = TWRITE;
|
||||||
h.fd = fd;
|
h.fd = fd;
|
||||||
h.offset = offset;
|
h.offset = offset;
|
||||||
@ -72,7 +70,7 @@ void *tclose_message(int fd, size_t * msg_len)
|
|||||||
char *msg;
|
char *msg;
|
||||||
ReqHeader h;
|
ReqHeader h;
|
||||||
*msg_len = sizeof(ReqHeader);
|
*msg_len = sizeof(ReqHeader);
|
||||||
msg = cext_emalloc(*msg_len);
|
msg = cext_emallocz(*msg_len);
|
||||||
h.req = TCLUNK;
|
h.req = TCLUNK;
|
||||||
h.fd = fd;
|
h.fd = fd;
|
||||||
memcpy(msg, &h, sizeof(ReqHeader));
|
memcpy(msg, &h, sizeof(ReqHeader));
|
||||||
@ -84,21 +82,19 @@ void *tremove_message(char *path, size_t * msg_len)
|
|||||||
char *msg;
|
char *msg;
|
||||||
ReqHeader h;
|
ReqHeader h;
|
||||||
*msg_len = sizeof(ReqHeader) + strlen(path) + 1;
|
*msg_len = sizeof(ReqHeader) + strlen(path) + 1;
|
||||||
msg = cext_emalloc(*msg_len);
|
msg = cext_emallocz(*msg_len);
|
||||||
h.req = TREMOVE;
|
h.req = TREMOVE;
|
||||||
memcpy(msg, &h, sizeof(ReqHeader));
|
memcpy(msg, &h, sizeof(ReqHeader));
|
||||||
memcpy(msg + sizeof(ReqHeader), path, strlen(path) + 1);
|
memcpy(msg + sizeof(ReqHeader), path, strlen(path) + 1);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* response messages ----------------------------------------------- */
|
|
||||||
|
|
||||||
void *rcreate_message(size_t * msg_len)
|
void *rcreate_message(size_t * msg_len)
|
||||||
{
|
{
|
||||||
char *msg;
|
char *msg;
|
||||||
ResHeader h;
|
ResHeader h;
|
||||||
*msg_len = sizeof(ResHeader);
|
*msg_len = sizeof(ResHeader);
|
||||||
msg = cext_emalloc(*msg_len);
|
msg = cext_emallocz(*msg_len);
|
||||||
h.res = RCREATE;
|
h.res = RCREATE;
|
||||||
memcpy(msg, &h, sizeof(ResHeader));
|
memcpy(msg, &h, sizeof(ResHeader));
|
||||||
return msg;
|
return msg;
|
||||||
@ -109,7 +105,7 @@ void *ropen_message(int fd, size_t * msg_len)
|
|||||||
char *msg;
|
char *msg;
|
||||||
ResHeader h;
|
ResHeader h;
|
||||||
*msg_len = sizeof(ResHeader);
|
*msg_len = sizeof(ResHeader);
|
||||||
msg = cext_emalloc(*msg_len);
|
msg = cext_emallocz(*msg_len);
|
||||||
h.res = ROPEN;
|
h.res = ROPEN;
|
||||||
h.fd = fd;
|
h.fd = fd;
|
||||||
memcpy(msg, &h, sizeof(ResHeader));
|
memcpy(msg, &h, sizeof(ResHeader));
|
||||||
@ -121,7 +117,7 @@ void *rread_message(void *content, size_t content_len, size_t * msg_len)
|
|||||||
char *msg;
|
char *msg;
|
||||||
ResHeader h;
|
ResHeader h;
|
||||||
*msg_len = sizeof(ResHeader) + content_len;
|
*msg_len = sizeof(ResHeader) + content_len;
|
||||||
msg = cext_emalloc(*msg_len);
|
msg = cext_emallocz(*msg_len);
|
||||||
h.res = RREAD;
|
h.res = RREAD;
|
||||||
h.buf_len = content_len;
|
h.buf_len = content_len;
|
||||||
memcpy(msg, &h, sizeof(ResHeader));
|
memcpy(msg, &h, sizeof(ResHeader));
|
||||||
@ -134,7 +130,7 @@ void *rwrite_message(size_t * msg_len)
|
|||||||
char *msg;
|
char *msg;
|
||||||
ResHeader h;
|
ResHeader h;
|
||||||
*msg_len = sizeof(ResHeader);
|
*msg_len = sizeof(ResHeader);
|
||||||
msg = cext_emalloc(*msg_len);
|
msg = cext_emallocz(*msg_len);
|
||||||
h.res = RWRITE;
|
h.res = RWRITE;
|
||||||
memcpy(msg, &h, sizeof(ResHeader));
|
memcpy(msg, &h, sizeof(ResHeader));
|
||||||
return msg;
|
return msg;
|
||||||
@ -145,7 +141,7 @@ void *rclose_message(size_t * msg_len)
|
|||||||
char *msg;
|
char *msg;
|
||||||
ResHeader h;
|
ResHeader h;
|
||||||
*msg_len = sizeof(ResHeader);
|
*msg_len = sizeof(ResHeader);
|
||||||
msg = cext_emalloc(*msg_len);
|
msg = cext_emallocz(*msg_len);
|
||||||
h.res = RCLUNK;
|
h.res = RCLUNK;
|
||||||
memcpy(msg, &h, sizeof(ResHeader));
|
memcpy(msg, &h, sizeof(ResHeader));
|
||||||
return msg;
|
return msg;
|
||||||
@ -156,7 +152,7 @@ void *rremove_message(size_t * msg_len)
|
|||||||
char *msg;
|
char *msg;
|
||||||
ResHeader h;
|
ResHeader h;
|
||||||
*msg_len = sizeof(ResHeader);
|
*msg_len = sizeof(ResHeader);
|
||||||
msg = cext_emalloc(*msg_len);
|
msg = cext_emallocz(*msg_len);
|
||||||
h.res = RREMOVE;
|
h.res = RREMOVE;
|
||||||
memcpy(msg, &h, sizeof(ResHeader));
|
memcpy(msg, &h, sizeof(ResHeader));
|
||||||
return msg;
|
return msg;
|
||||||
@ -168,7 +164,7 @@ void *rerror_message(char *errstr, size_t * msg_len)
|
|||||||
size_t len = strlen(errstr) + 1;
|
size_t len = strlen(errstr) + 1;
|
||||||
ResHeader h;
|
ResHeader h;
|
||||||
*msg_len = sizeof(ResHeader) + len;
|
*msg_len = sizeof(ResHeader) + len;
|
||||||
msg = cext_emalloc(*msg_len);
|
msg = cext_emallocz(*msg_len);
|
||||||
h.res = RERROR;
|
h.res = RERROR;
|
||||||
memcpy(msg, &h, sizeof(ResHeader));
|
memcpy(msg, &h, sizeof(ResHeader));
|
||||||
memmove(msg + sizeof(ResHeader), errstr, len);
|
memmove(msg + sizeof(ResHeader), errstr, len);
|
||||||
|
@ -11,8 +11,6 @@
|
|||||||
|
|
||||||
#include <cext.h>
|
#include <cext.h>
|
||||||
|
|
||||||
static File zero_file = { 0 };
|
|
||||||
|
|
||||||
int is_directory(File * f)
|
int is_directory(File * f)
|
||||||
{
|
{
|
||||||
return !f->size && f->content;
|
return !f->size && f->content;
|
||||||
@ -53,8 +51,7 @@ File *ixp_create(IXPServer * s, char *path)
|
|||||||
}
|
}
|
||||||
/* only create missing parts, if file is directory */
|
/* only create missing parts, if file is directory */
|
||||||
while (tok) {
|
while (tok) {
|
||||||
f = (File *) cext_emalloc(sizeof(File));
|
f = (File *) cext_emallocz(sizeof(File));
|
||||||
*f = zero_file;
|
|
||||||
f->name = strdup(tok);
|
f->name = strdup(tok);
|
||||||
f->parent = p;
|
f->parent = p;
|
||||||
if (p->content) {
|
if (p->content) {
|
||||||
@ -90,7 +87,7 @@ static char *_ls(File * f)
|
|||||||
|
|
||||||
for (p = f; p; p = p->next)
|
for (p = f; p; p = p->next)
|
||||||
num++;
|
num++;
|
||||||
tmp = cext_emalloc(sizeof(File *) * num);
|
tmp = cext_emallocz(sizeof(File *) * num);
|
||||||
i = 0;
|
i = 0;
|
||||||
for (p = f; p; p = p->next) {
|
for (p = f; p; p = p->next) {
|
||||||
size += strlen(p->name) + 1;
|
size += strlen(p->name) + 1;
|
||||||
@ -99,7 +96,7 @@ static char *_ls(File * f)
|
|||||||
tmp[i++] = p;
|
tmp[i++] = p;
|
||||||
}
|
}
|
||||||
qsort(tmp, num, sizeof(char *), comp_file_name);
|
qsort(tmp, num, sizeof(char *), comp_file_name);
|
||||||
result = cext_emalloc(size);
|
result = cext_emallocz(size);
|
||||||
result[0] = '\0';
|
result[0] = '\0';
|
||||||
for (i = 0; i < num; i++) {
|
for (i = 0; i < num; i++) {
|
||||||
strncat(result, tmp[i]->name, size);
|
strncat(result, tmp[i]->name, size);
|
||||||
|
@ -17,8 +17,6 @@
|
|||||||
|
|
||||||
#include <cext.h>
|
#include <cext.h>
|
||||||
|
|
||||||
static File zero_file = { 0 };
|
|
||||||
static IXPServer zero_server = { 0 };
|
|
||||||
static Connection zero_conn = { 0 };
|
static Connection zero_conn = { 0 };
|
||||||
static int user_fd = -1;
|
static int user_fd = -1;
|
||||||
|
|
||||||
@ -81,7 +79,7 @@ static void handle_ixp_read(Connection * c, ReqHeader * h)
|
|||||||
void *data = 0;
|
void *data = 0;
|
||||||
size_t out_len;
|
size_t out_len;
|
||||||
|
|
||||||
data = cext_emalloc(h->buf_len);
|
data = cext_emallocz(h->buf_len);
|
||||||
out_len = c->s->read(c->s, h->fd, h->offset, data, h->buf_len);
|
out_len = c->s->read(c->s, h->fd, h->offset, data, h->buf_len);
|
||||||
free(c->data);
|
free(c->data);
|
||||||
if (c->s->errstr) {
|
if (c->s->errstr) {
|
||||||
@ -219,7 +217,7 @@ static void read_conn(Connection * c)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
c->remain = c->len;
|
c->remain = c->len;
|
||||||
c->data = cext_emalloc(c->len);
|
c->data = cext_emallocz(c->len);
|
||||||
c->header = 1;
|
c->header = 1;
|
||||||
}
|
}
|
||||||
r = read(c->fd, ((char *) c->data) + c->len - c->remain, c->remain);
|
r = read(c->fd, ((char *) c->data) + c->len - c->remain, c->remain);
|
||||||
@ -347,10 +345,9 @@ IXPServer *init_server(char *sockfile, void (*cleanup) (void))
|
|||||||
IXPServer *s;
|
IXPServer *s;
|
||||||
|
|
||||||
/* init */
|
/* init */
|
||||||
s = (IXPServer *) cext_emalloc(sizeof(IXPServer));
|
s = (IXPServer *) cext_emallocz(sizeof(IXPServer));
|
||||||
*s = zero_server;
|
|
||||||
s->sockfile = sockfile;
|
s->sockfile = sockfile;
|
||||||
s->root = (File *) cext_emalloc(sizeof(File));
|
s->root = (File *) cext_emallocz(sizeof(File));
|
||||||
s->runlevel = HALT; /* initially server is not running */
|
s->runlevel = HALT; /* initially server is not running */
|
||||||
s->create = ixp_create;
|
s->create = ixp_create;
|
||||||
s->remove = ixp_remove;
|
s->remove = ixp_remove;
|
||||||
@ -358,7 +355,6 @@ IXPServer *init_server(char *sockfile, void (*cleanup) (void))
|
|||||||
s->close = ixp_close;
|
s->close = ixp_close;
|
||||||
s->read = ixp_read;
|
s->read = ixp_read;
|
||||||
s->write = ixp_write;
|
s->write = ixp_write;
|
||||||
*s->root = zero_file;
|
|
||||||
s->root->name = strdup("");
|
s->root->name = strdup("");
|
||||||
for (i = 0; i < MAX_CONN; i++) {
|
for (i = 0; i < MAX_CONN; i++) {
|
||||||
s->conn[i].s = s;
|
s->conn[i].s = s;
|
||||||
|
@ -21,7 +21,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
printf("--------------------------------\n");
|
printf("--------------------------------\n");
|
||||||
{
|
{
|
||||||
int *e = cext_emalloc(sizeof(int));
|
int *e = cext_emallocz(sizeof(int));
|
||||||
cext_attach_item(&c, e);
|
cext_attach_item(&c, e);
|
||||||
cext_iterate(&c, nil, iter_print_container);
|
cext_iterate(&c, nil, iter_print_container);
|
||||||
cext_detach_item(&c, e);
|
cext_detach_item(&c, e);
|
||||||
@ -31,7 +31,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
printf("--------------------------------\n");
|
printf("--------------------------------\n");
|
||||||
for (i = 0; i < 10; i++) {
|
for (i = 0; i < 10; i++) {
|
||||||
int *e = cext_emalloc(sizeof(int));
|
int *e = cext_emallocz(sizeof(int));
|
||||||
*e = i;
|
*e = i;
|
||||||
cext_attach_item(&c, e);
|
cext_attach_item(&c, e);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user