1998-10-08 16:15:21 +08:00
|
|
|
/* parasite.c
|
|
|
|
* Copyright (C) 1998 Jay Cox <jaycox@earthlink.net>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
1999-11-18 05:13:50 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
1998-10-08 16:15:21 +08:00
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
1999-11-18 05:13:50 +08:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
1998-10-08 16:15:21 +08:00
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
1999-03-07 20:56:03 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
1998-10-08 16:15:21 +08:00
|
|
|
#include "parasiteP.h"
|
|
|
|
#include "parasite.h"
|
1998-11-06 08:51:39 +08:00
|
|
|
#include <stdio.h>
|
1999-03-07 20:56:03 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1998-11-06 08:51:39 +08:00
|
|
|
#include <unistd.h>
|
1999-03-07 20:56:03 +08:00
|
|
|
#endif
|
1998-10-08 16:15:21 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
|
|
|
|
1999-10-05 03:26:07 +08:00
|
|
|
#ifdef G_OS_WIN32
|
1999-03-07 20:56:03 +08:00
|
|
|
#include <process.h> /* For _getpid() */
|
|
|
|
#endif
|
|
|
|
|
1998-12-22 14:03:53 +08:00
|
|
|
#ifdef DEBUG
|
2000-01-11 07:27:25 +08:00
|
|
|
static void
|
|
|
|
parasite_print (Parasite *p)
|
1998-10-30 18:21:33 +08:00
|
|
|
{
|
|
|
|
if (p == NULL)
|
|
|
|
{
|
|
|
|
printf("(pid %d)attempt to print a null parasite\n", getpid());
|
|
|
|
return;
|
|
|
|
}
|
1998-11-06 08:51:39 +08:00
|
|
|
printf("(pid %d), parasite: %p\n", getpid(), p);
|
1998-10-30 18:21:33 +08:00
|
|
|
if (p->name)
|
|
|
|
printf("\tname: %s\n", p->name);
|
|
|
|
else
|
|
|
|
printf("\tname: NULL\n");
|
|
|
|
printf("\tflags: %d\n", p->flags);
|
|
|
|
printf("\tsize: %d\n", p->size);
|
|
|
|
if (p->size > 0)
|
1998-11-06 08:51:39 +08:00
|
|
|
printf("\tdata: %p\n", p->data);
|
1998-10-30 18:21:33 +08:00
|
|
|
}
|
1998-12-22 14:03:53 +08:00
|
|
|
#endif
|
1998-10-30 18:21:33 +08:00
|
|
|
|
1998-10-08 16:15:21 +08:00
|
|
|
Parasite *
|
2000-01-11 07:27:25 +08:00
|
|
|
parasite_new (const char *name,
|
|
|
|
guint32 flags,
|
|
|
|
guint32 size,
|
|
|
|
const void *data)
|
1998-10-08 16:15:21 +08:00
|
|
|
{
|
|
|
|
Parasite *p;
|
1999-02-03 17:17:37 +08:00
|
|
|
p = g_new (Parasite, 1);
|
1998-10-14 10:54:02 +08:00
|
|
|
if (name)
|
2000-01-11 07:27:25 +08:00
|
|
|
p->name = g_strdup (name);
|
1998-10-08 16:15:21 +08:00
|
|
|
else
|
2000-01-11 07:27:25 +08:00
|
|
|
{
|
|
|
|
g_free (p);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-12-16 19:23:30 +08:00
|
|
|
p->flags = (flags & 0xFF);
|
1998-10-08 16:15:21 +08:00
|
|
|
p->size = size;
|
|
|
|
if (size)
|
2000-01-11 07:27:25 +08:00
|
|
|
p->data = g_memdup (data, size);
|
1998-10-08 16:15:21 +08:00
|
|
|
else
|
|
|
|
p->data = NULL;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
parasite_free (Parasite *parasite)
|
|
|
|
{
|
1998-11-13 12:00:54 +08:00
|
|
|
if (parasite == NULL)
|
|
|
|
return;
|
1998-10-14 10:54:02 +08:00
|
|
|
if (parasite->name)
|
2000-01-11 07:27:25 +08:00
|
|
|
g_free (parasite->name);
|
1998-10-08 16:15:21 +08:00
|
|
|
if (parasite->data)
|
2000-01-11 07:27:25 +08:00
|
|
|
g_free (parasite->data);
|
|
|
|
g_free (parasite);
|
1998-10-08 16:15:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-01-11 07:27:25 +08:00
|
|
|
parasite_is_type (const Parasite *parasite,
|
|
|
|
const char *name)
|
1998-10-08 16:15:21 +08:00
|
|
|
{
|
1998-10-30 18:21:33 +08:00
|
|
|
if (!parasite || !parasite->name)
|
1998-10-08 16:15:21 +08:00
|
|
|
return FALSE;
|
1998-10-14 10:54:02 +08:00
|
|
|
return (strcmp(parasite->name, name) == 0);
|
1998-10-08 16:15:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Parasite *
|
|
|
|
parasite_copy (const Parasite *parasite)
|
|
|
|
{
|
|
|
|
if (parasite == NULL)
|
|
|
|
return NULL;
|
1998-10-14 10:54:02 +08:00
|
|
|
return parasite_new (parasite->name, parasite->flags,
|
|
|
|
parasite->size, parasite->data);
|
1998-10-08 16:15:21 +08:00
|
|
|
}
|
|
|
|
|
1999-02-16 16:53:54 +08:00
|
|
|
int
|
2000-01-11 07:27:25 +08:00
|
|
|
parasite_compare (const Parasite *a,
|
|
|
|
const Parasite *b)
|
1999-02-16 16:53:54 +08:00
|
|
|
{
|
|
|
|
if (a && b && a->name && b->name && strcmp(a->name, b->name) == 0 &&
|
|
|
|
a->flags == b->flags && a->size == b->size )
|
1999-03-20 12:41:59 +08:00
|
|
|
{
|
|
|
|
if (a->data == NULL && b->data == NULL)
|
|
|
|
return TRUE;
|
|
|
|
else if (a->data && b->data && memcmp(a->data, b->data, a->size) == 0)
|
|
|
|
return TRUE;
|
|
|
|
}
|
1999-02-16 16:53:54 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
1999-04-12 06:28:51 +08:00
|
|
|
gulong
|
2000-01-11 07:27:25 +08:00
|
|
|
parasite_flags (const Parasite *p)
|
1999-04-12 06:28:51 +08:00
|
|
|
{
|
|
|
|
if (p == NULL)
|
|
|
|
return 0;
|
|
|
|
return p->flags;
|
|
|
|
}
|
|
|
|
|
1998-10-08 16:15:21 +08:00
|
|
|
int
|
2000-01-11 07:27:25 +08:00
|
|
|
parasite_is_persistent (const Parasite *p)
|
1998-10-08 16:15:21 +08:00
|
|
|
{
|
|
|
|
if (p == NULL)
|
|
|
|
return FALSE;
|
1998-12-16 19:23:30 +08:00
|
|
|
return (p->flags & PARASITE_PERSISTENT);
|
|
|
|
}
|
|
|
|
|
1999-04-23 14:07:09 +08:00
|
|
|
int
|
2000-01-11 07:27:25 +08:00
|
|
|
parasite_is_undoable (const Parasite *p)
|
1999-04-23 14:07:09 +08:00
|
|
|
{
|
|
|
|
if (p == NULL)
|
|
|
|
return FALSE;
|
|
|
|
return (p->flags & PARASITE_UNDOABLE);
|
|
|
|
}
|
|
|
|
|
1998-12-16 19:23:30 +08:00
|
|
|
int
|
2000-01-11 07:27:25 +08:00
|
|
|
parasite_has_flag (const Parasite *p,
|
|
|
|
gulong flag)
|
1998-12-16 19:23:30 +08:00
|
|
|
{
|
|
|
|
if (p == NULL)
|
|
|
|
return FALSE;
|
|
|
|
return (p->flags & flag);
|
|
|
|
}
|
|
|
|
|
1999-02-16 16:53:54 +08:00
|
|
|
const char *
|
2000-01-11 07:27:25 +08:00
|
|
|
parasite_name (const Parasite *p)
|
1999-02-16 16:53:54 +08:00
|
|
|
{
|
|
|
|
if (p)
|
|
|
|
return p->name;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-01-11 07:27:25 +08:00
|
|
|
void *
|
|
|
|
parasite_data (const Parasite *p)
|
1998-12-16 19:23:30 +08:00
|
|
|
{
|
|
|
|
if (p)
|
|
|
|
return p->data;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-01-11 07:27:25 +08:00
|
|
|
long
|
|
|
|
parasite_data_size (const Parasite *p)
|
1998-12-16 19:23:30 +08:00
|
|
|
{
|
|
|
|
if (p)
|
|
|
|
return p->size;
|
|
|
|
return 0;
|
1998-10-08 16:15:21 +08:00
|
|
|
}
|
2000-01-11 07:27:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
|