wmii/libwmii/ixputil.c

94 lines
1.7 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
* (C)opyright MMIV-MMV Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "wmii.h"
2005-12-05 01:45:59 +03:00
static pid_t mypid;
static char *mysockfile;
2005-11-18 18:54:58 +03:00
2005-12-05 01:45:59 +03:00
File *wmii_create_ixpfile(IXPServer * s, char *key, char *val)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
File *f = ixp_create(s, key);
2005-11-18 18:54:58 +03:00
if (f && !is_directory(f)) {
2005-12-05 01:45:59 +03:00
size_t l = val ? strlen(val) : 0;
2005-11-18 18:54:58 +03:00
f->content = l ? strdup(val) : 0;
f->size = l;
return f;
}
/* forbidden, file is directory */
return 0;
}
2005-12-05 01:45:59 +03:00
void wmii_get_ixppath(File * f, char *path, size_t size)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
char buf[512];
2005-11-18 18:54:58 +03:00
buf[0] = 0;
2005-11-18 18:54:58 +03:00
if (path)
cext_strlcpy(buf, path, sizeof(buf));
2005-11-18 18:54:58 +03:00
snprintf(path, size, "%s/", f->name);
if (buf[0] != 0)
cext_strlcat(path, buf, size);
2005-11-18 18:54:58 +03:00
if (f->parent)
wmii_get_ixppath(f->parent, path, size);
}
2005-12-05 01:45:59 +03:00
void wmii_move_ixpfile(File * f, File * to_parent)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
File *p = f->parent;
File *fil = p->content;
2005-11-18 18:54:58 +03:00
/* detach */
if (p->content == f)
p->content = fil->next;
else {
while (fil->next != f)
fil = fil->next;
fil->next = f->next;
}
f->next = 0;
/* attach */
if (!to_parent->content)
to_parent->content = f;
else {
for (fil = to_parent->content; fil->next; fil = fil->next);
fil->next = f;
}
f->parent = to_parent;
}
2005-12-05 01:45:59 +03:00
static void exit_cleanup()
2005-11-18 18:54:58 +03:00
{
if (mypid == getpid())
unlink(mysockfile);
}
2005-12-05 01:45:59 +03:00
IXPServer *wmii_setup_server(char *sockfile)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
IXPServer *s;
2005-11-18 18:54:58 +03:00
if (!sockfile) {
fprintf(stderr, "%s\n", "libwmii: no socket file provided");
exit(1);
}
mysockfile = sockfile;
mypid = getpid();
s = init_server(sockfile, exit_cleanup);
if (!s) {
perror("libwmii: cannot initialize IXP server");
exit(1);
}
return s;
}