Beispiel #1
0
static void set_game_filename(const char *name)
{
  n_free(game_filename);
  game_filename = 0;

#if defined(_GNU_SOURCE)
  game_filename = canonicalize_file_name(name);
#else
#if defined(_BSD_SOURCE) || defined(_XOPEN_SOURCE)
  game_filename = (char *) n_malloc(PATH_MAX);
  if(!realpath(name, game_filename)) {
    n_free(game_filename);
    game_filename = 0;
  }
#else
#ifdef __DJGPP__
  game_filename = (char *) n_malloc(FILENAME_MAX);
  _fixpath(name, game_filename);
#endif
#endif
#endif

  if(!game_filename)
    game_filename = n_strdup(name);
}
Beispiel #2
0
void init_stack(offset initialstack_stack_size, zword initialframe_size)
{
  n_free(stack_stack);
  stack_stack = (zword *) n_malloc(sizeof(*stack_stack) * 
				   initialstack_stack_size);
  stack_pointer = 0;
  stack_min = 0;
  stack_max = initialstack_stack_size;

  n_free(stack_frames);
  stack_frames = (Stack_frame *) n_malloc(sizeof(*stack_frames) *
					  initialframe_size);
  frame_count = 0;
  if(stacklimit && initialframe_size > stacklimit)
    frame_max = stacklimit;
  else
    frame_max = initialframe_size;

  stack_frames[frame_count].stack_stack_start = 0;
  stack_frames[frame_count].return_PC = 0;
  stack_frames[frame_count].num_locals = 0;
  stack_frames[frame_count].arguments = 0;
  stack_frames[frame_count].result_variable = -2;
  local_vars = stack_stack + stack_frames[frame_count].stack_stack_start;
}
Beispiel #3
0
BOOL fast_saveundo(void)
{
  strid_t stack;
  glui32 stack_size;
  /* Avoids realloc() in hopes that'll make it a teensy bit faster */
  if(automap_undoslot.z_memsize < dynamic_size) {
    n_free(automap_undoslot.z_mem);
    automap_undoslot.z_mem = n_malloc(dynamic_size);
    automap_undoslot.z_memsize = dynamic_size;
  }
  n_memcpy(automap_undoslot.z_mem, z_memory, dynamic_size);

  automap_undoslot.PC = oldPC;

  automap_undoslot.stacklength = stack_size = get_quetzal_stack_size();
  if(automap_undoslot.stackchunksize < stack_size) {
    free(automap_undoslot.stackchunk);
    automap_undoslot.stackchunk = (zbyte *) n_malloc(stack_size);
    automap_undoslot.stackchunksize = stack_size;
  }
  
  stack = glk_stream_open_memory((char *) automap_undoslot.stackchunk,
				 automap_undoslot.stacklength,
				 filemode_Write, 0);
  if(!stack)
    return FALSE;
  if(!quetzal_stack_save(stack)) {
    glk_stream_close(stack, NULL);
    return FALSE;
  }
  glk_stream_close(stack, NULL);
  return TRUE;
}
Beispiel #4
0
bool CMeshShape::LoadFromFile(nMeshLoader& MeshLoader)
{
	n_assert(FileName.IsValid());
	n_assert(pVBuffer);
	n_assert(pIBuffer);

	// open file and read header data
	MeshLoader.SetFilename(FileName.Get());
	MeshLoader.SetIndexType(nMeshLoader::Index32);
	if (!MeshLoader.Open())
	{
		n_error("CMeshShape: Failed to open file '%Sphere'!", FileName.Get());
		FAIL;
	}

	// transfer mesh attributes
	VertexCount = MeshLoader.GetNumVertices();
	IndexCount  = MeshLoader.GetNumIndices();
	VertexWidth = MeshLoader.GetVertexWidth();

	// allocate vertex and index buffer
	int VBSize = MeshLoader.GetNumVertices() * MeshLoader.GetVertexWidth() * sizeof(float);
	int IBSize = MeshLoader.GetNumIndices() * sizeof(uint);
	pVBuffer = (float*)n_malloc(VBSize);
	pIBuffer = (int*)n_malloc(IBSize);

	// read vertices and indices
	MeshLoader.ReadVertices(pVBuffer, VBSize);
	MeshLoader.ReadIndices(pIBuffer, IBSize);

	// close mesh loader
	MeshLoader.Close();

	OK;
}
Beispiel #5
0
void mymap_init(int width, int height)
{
  int i;
  int max;
  mapwidth = width * 2;
  mapheight = height * 2;
  max = mapwidth * mapheight;
  n_free(mymap);
  n_free(mymapnode);
  mymap = (char *) n_malloc(max);
  mymapnode = (loc_node **) n_malloc(max * sizeof(*mymapnode));
  for(i = 0; i < max; i++) {
    mymap[i] = ' ';
    mymapnode[i] = NULL;
  }
}
bool CDataServer::CopyFile(const nString& SrcPath, const nString& DestPath)
{
	//???mangle here for perf reasons?

	if (IsFileReadOnly(DestPath))
		SetFileReadOnly(DestPath, false);

	CFileStream Src, Dest;
	if (!Src.Open(SrcPath, SAM_READ, SAP_SEQUENTIAL)) FAIL;
	if (!Dest.Open(DestPath, SAM_WRITE, SAP_SEQUENTIAL))
	{
		Src.Close();
		FAIL;
	}

	int Size = Src.GetSize();
	char* pBuffer = (char*)n_malloc(Size);
	int RealSize = Src.Read(pBuffer, Size);
	n_assert(RealSize == Size);
	Src.Close();

	RealSize = Dest.Write(pBuffer, Size);
	n_assert(RealSize == Size);
	Dest.Close();
	n_free(pBuffer);

	OK;
}
Beispiel #7
0
/*
  Stolen from su for GNU.
  Copyright (C) 1992-2004 Free Software Foundation, Inc.
  License: GPL v2 or later.
*/
static
void modify_environment(const struct passwd *pw)
{
  char *term;
  char *display;
  char *path;
  
  term = getenv("TERM");
  display = getenv("DISPLAY");
  path = getenv("PATH");
  
#if HAVE_CLEARENV
  clearenv();
#else
  extern char **environ;   
  environ = n_malloc(2 * sizeof (char *));
  environ[0] = 0;
#endif
  
  if (term)
      do_putenv("TERM", term);
  
  if (display)
      do_putenv("DISPLAY", display);

  if (path)
      do_putenv("PATH", path);
  
  do_putenv("HOME", pw->pw_dir);
  do_putenv("SHELL", pw->pw_shell);
  do_putenv("USER", pw->pw_name);
  do_putenv("LOGNAME", pw->pw_name);
}
static char *parse_cmdl_pathspec(const char *pathspec, const char **path)
{
    const char  *p;
    char        *type;
    int         len, seplen = 1;

    
    p = pathspec;
    while (isalnum(*p))
        p++;

    if (p == pathspec)
        return NULL;

    if (*p != '#' && (seplen = strspn(p, ",")) != 2)
        return NULL;
    
    len  = p - pathspec + 1;
    type = n_malloc(len);
    memcpy(type, pathspec, len - 1);
    type[len - 1] = '\0';

    *path = p + seplen;
    return type;
}
Beispiel #9
0
static
error_t parse_opt(int key, char *arg, struct argp_state *state)
{
    struct poclidek_opgroup_rt   *rt;
    struct arg_s *arg_s;
    
    
    rt = state->input;
    if (rt->_opdata) {
        arg_s = rt->_opdata;
        
    } else {
        arg_s = n_malloc(sizeof(*arg_s));
        arg_s->cnflags = 0;
        arg_s->size = arg_s->first_free_space = 0;
        arg_s->prefix = NULL;
        rt->_opdata = arg_s;
        rt->_opdata_free = arg_s_free;
        rt->run = oprun;
    }

    switch (key) {
        case OPT_SPLITSIZE: {
            char *p;
            int  rc;
            
            if ((p = strrchr(arg, ':'))) {
                rc = sscanf(arg, "%d:%d", &arg_s->size,
                            &arg_s->first_free_space);
                rc = (rc == 2);

            } else {
                rc = sscanf(arg, "%d", &arg_s->size);
                rc = (rc == 1);
            }
            
            if (rc)
                arg_s->cnflags |= DO_SPLIT;
            
            else {
                logn(LOGERR, _("split: bad option argument"));
                exit(EXIT_FAILURE);
            }
        }
            break;
            
        case OPT_SPLITCONF:
            poldek_configure(rt->ctx, POLDEK_CONF_PRIFILE, arg);
            break;

        case OPT_SPLITPREFIX:
            arg_s->prefix = n_strdup(arg);
            break;

        default:
            return ARGP_ERR_UNKNOWN;
    }
    
    return 0;
}
Beispiel #10
0
tn_array *capreq_arr_restore(tn_alloc *na, tn_buf *nbuf) 
{
    struct capreq  *cr;
    tn_buf_it      nbufi;
    int16_t        arr_size;
    void           **cr_buf;
    register int   i, n;

    n_buf_it_init(&nbufi, nbuf);
    n_buf_it_get_int16(&nbufi, &arr_size);

    DBGF("%d, arr_size = %d\n", n_buf_size(nbuf), arr_size);
    
    //cr_buf = alloca(arr_size * sizeof(void*));
    cr_buf = n_malloc(arr_size * sizeof(void*));
    n = 0;
    for (i=0; i < arr_size; i++) {
        if ((cr = capreq_restore(na, &nbufi))) {
            if (capreq_is_bastard(cr))
                continue;
            cr_buf[n++] = cr;
        }
    }
    
    if (n == 0) {
        free(cr_buf);
        return NULL;
    }
    
    //arr = capreq_arr_new_ex(n, n_memdup(cr_buf, n * sizeof(void*)));
    return capreq_arr_new_ex(n, cr_buf);
}
Beispiel #11
0
static char *recode(const char *val, const char *valencoding) 
{
    char *p, *val_utf8, *usrencoding, *tmpval;
    size_t vlen, u_vlen, tmpvlen;
    iconv_t cd;

    usrencoding = nl_langinfo(CODESET);
    if (usrencoding == NULL)
        return (char*)val;

    valencoding = "UTF-8";   /* XXX, support for others needed? */

    vlen = u_vlen = tmpvlen = strlen(val);
    p = val_utf8 = n_malloc(u_vlen + 1);

    /* FIXME: iconv leave val content untouched */ 
    tmpval = (char*) val; 
    
    cd = iconv_open(usrencoding, valencoding);
    if (iconv(cd, &tmpval, &tmpvlen, &p, &u_vlen) == (size_t)-1) {
        iconv_close(cd);
        free(val_utf8);
        return (char*)val;
    }
    iconv_close(cd);
    n_assert(p <= val_utf8 + vlen);
    *p = '\0';
    return val_utf8;
}
Beispiel #12
0
void init_undo(void)
{
  kill_undo();

  prevstate = (zbyte *) n_malloc(dynamic_size);
  n_memcpy(prevstate, z_memory, dynamic_size);
}
Beispiel #13
0
struct iset *iset_new(void) 
{
    struct iset *iset; 

    iset = n_malloc(sizeof(*iset));
    iset->pkgs = pkgs_array_new(128);
    iset->pkgs_by_recno = pkgs_array_new_ex(128, pkg_cmp_recno);
    iset->capcache = n_hash_new(128, NULL);
    iset->pms = pkgmark_set_new(0, 0);
    return iset;
}
Beispiel #14
0
strid_t startup_findfile(void)
{
  static DIR *dir = NULL;
  static char *pathstart = NULL;
  static char *path = NULL;
  strid_t stream;
  struct dirent *d;
  char *name = NULL;

  if(!pathstart) {
    char *p = search_path;
    if(!p)
      return 0;
    pathstart = n_strdup(p);
    if(!(path = n_strtok(pathstart, ":"))) {
      n_free(pathstart);
      pathstart = 0;
      return 0;
    }
  }

  do {
    if(!dir) {
      dir = opendir(path);
      if(!dir) {
	n_free(pathstart);
	pathstart = 0;
	return 0;
      }
    }
    d = readdir(dir);
    if(!d) {
      closedir(dir);
      dir = NULL;
      if(!(path = n_strtok(NULL, ":"))) {
	n_free(pathstart);
	pathstart = 0;
	return 0;
      }
    }
  } while(!dir);

  name = (char *) n_malloc(n_strlen(path) + n_strlen(d->d_name) + 2);
  n_strcpy(name, path);
  n_strcat(name, "/");
  n_strcat(name, d->d_name);
  stream = glkunix_stream_open_pathname(name, fileusage_Data |
					fileusage_BinaryMode, 0);
  if(stream)
    set_game_filename(name);
  n_free(name);
  return stream;
}
Beispiel #15
0
struct poclidek_rcmd *poclidek_rcmd_new(struct poclidek_ctx *cctx,
                                        struct poldek_ts *ts)
{
    struct poclidek_rcmd *rcmd = n_malloc(sizeof(*rcmd));
    rcmd->_flags = 0;
    rcmd->_cctx = cctx;
    rcmd->_ts = ts;
    rcmd->rpkgs = NULL;
    rcmd->rbuf = NULL;
    rcmd->rc = -1;
    return rcmd;
}
Beispiel #16
0
static struct pkg_unreq *pkg_unreq_new(struct capreq *req, int mismatch)
{
    struct pkg_unreq *unreq;
    char s[512];
    int n;

    n = capreq_snprintf(s, sizeof(s), req);
    
    unreq = n_malloc(sizeof(*unreq) + n + 1);
    unreq->mismatch = mismatch;
    memcpy(unreq->req, s, n + 1);
    return unreq;
}
Beispiel #17
0
static edge *automap_new_edge(loc_node *src, loc_node *dest, BOOL is_oneway)
{
  edgelist newedge;
  newedge.node = n_malloc(sizeof(edge));
  newedge.node->dest[0] = src;
  newedge.node->dest[1] = dest;
  newedge.node->is_oneway = is_oneway;
  newedge.node->touched = FALSE;
  newedge.node->min_length = is_oneway ? 4 : 2;
  newedge.node->guess_length = is_oneway ? 4 : 2;  
  LEadd(all_edges, newedge);
  return newedge.node;
}
Beispiel #18
0
static void do_putenv(const char *var, const char *val)
{
#ifdef HAVE_SETENV        
        setenv(var, val, 1);
#else
        {
            int len = strlen(var) + strlen(val) + 3;
            char *tmp = n_malloc(len);
            n_snprintf(tmp, len, "%s=%s", var, val);
            putenv(tmp);
        }
#endif
}
Beispiel #19
0
/* Allocate and initialise a chunk of memory.
 */
static inline void *n_malloc_null(int size, const char *file, int line)
{
#ifdef DEBUG_MEM
#  define nmalloc_null(size) n_malloc_null(size, __FILE__, __LINE__)
  void *ptr = n_malloc(size, file, line);
#else
#  define nmalloc_null(size) n_malloc_null(size, NULL, 0)
  void *ptr = nmalloc(size);
#endif

  egg_memset(ptr, 0, size);
  return ptr;
}
Beispiel #20
0
/* This helps the memory debugging
 */
void *_get_data_ptr(int size, char *file, int line)
{
  char *p;
#ifdef DEBUG_MEM
  char x[1024];

  p = strrchr(file, '/');
  egg_snprintf(x, sizeof x, "dccutil.c:%s", p ? p + 1 : file);
  p = n_malloc(size, x, line);
#else
  p = nmalloc(size);
#endif
  egg_bzero(p, size);
  return p;
}
Beispiel #21
0
static void args_init(struct poclidek_ctx *cctx, struct poldek_ts *ts,
                      int argc, char **argv, int mode) 
{
    memset(&args, 0, sizeof(args));
        
    args.ctx = cctx->ctx;
    args.cctx = cctx;
    
    args.argc = 0;
    args.argv = n_malloc(sizeof(*argv) * argc);
    args.argv[0] = NULL;

    args.mode = mode;
    args.ts = ts;
    args.opctx = poclidek_op_ctx_new();
}
Beispiel #22
0
static
struct poclidek_cmd *command_new_alias(const char *name, const char *cmdline) 
{
    struct poclidek_cmd *alias;

    alias = n_malloc(sizeof(*alias));
    memset(alias, 0, sizeof(*alias));
    alias->flags = COMMAND_IS_ALIAS | COMMAND__MALLOCED;
    alias->name = n_strdup(name);
    alias->cmdline = n_strdup(cmdline);
    alias->aliasto = NULL;
    alias->_free = free_alias;
    if (strchr(alias->cmdline, '%'))
        alias->flags |= COMMAND_PARAMETERIZED;
    return alias;
}
Beispiel #23
0
static
inline struct capreq *capreq_malloc(tn_alloc *na, int size)
{
    struct capreq *cr;
    
    n_assert(size < UINT8_MAX);
    if (na) {
        cr = na->na_malloc(na, sizeof(*cr) + size + 1);
        cr->cr_relflags = __NAALLOC;
        
    } else {
        cr = n_malloc(sizeof(*cr) + size + 1);
        cr->cr_relflags = 0;
    }
    cr->_buf[size] = '\0';
    return cr;
}
Beispiel #24
0
struct pm_ctx *pm_new(const char *name)
{
    struct pm_ctx *ctx;
    const struct pm_module *mod;
    void *modh;
    
    if ((mod = pm_module_find(name)) == NULL)
        return NULL;

    if ((modh = mod->init()) == NULL)
        return NULL;
    
    ctx = n_malloc(sizeof(*ctx));
    ctx->mod = mod;
    ctx->modh = modh;
    return ctx;
}
Beispiel #25
0
tn_tuple *n_tuple_new(tn_alloc *na, int size, void **data)
{
    tn_tuple *tu;

    n_assert(size < UINT16_MAX);
    if (na)
        tu = na->na_malloc(na, sizeof(*tu) + (size * sizeof(void*)));
    else
        tu = n_malloc(sizeof(*tu) + (size * sizeof(void*)));
    
    tu->_refcnt = 0;
    tu->size = size;
    if (data)
        memcpy(tu->data, data, size * sizeof(void*));
    else if (size)
        memset(tu->data, 0, size * sizeof(void*));
    return tu;
}
Beispiel #26
0
strid_t startup_open(const char *name)
{
  strid_t str;
  char *s;

  str = glkunix_stream_open_pathname((char *) name, fileusage_Data | fileusage_BinaryMode, 0);
  if(str) {
    set_game_filename(name);
#ifdef USE_GARGLK_FEATURES
    s = strrchr(name, '\\');
    if (!s) s = strrchr(name, '/');
    garglk_set_story_name(s ? s + 1 : name);
#endif
  } else {
    char *path = search_path;
    if(path) {
      char *p;
      char *newname = (char *) n_malloc(strlen(path) + strlen(name) + 2);
      path = n_strdup(path);
      for(p = n_strtok(path, ":"); p; p = n_strtok(NULL, ":")) {
	n_strcpy(newname, p);
	n_strcat(newname, "/");
	n_strcat(newname, name);
	str = glkunix_stream_open_pathname((char *) newname, fileusage_Data |
					   fileusage_BinaryMode, 0);
	if(str) {
	  set_game_filename(newname);
#ifdef USE_GARGLK_FEATURES
	  s = strrchr(newname, '\\');
	  if (!s) s = strrchr(newname, '/');
	  garglk_set_story_name(s ? s + 1 : newname);
#endif
	  break;
        }
      }
      n_free(path);
    }
  }

  if(!str)
    fprintf(stderr, "Cannot open '%s'\n", name);

  return str;
}
Beispiel #27
0
struct capreq *capreq_clone(tn_alloc *na, const struct capreq *cr) 
{
    uint8_t size;
    struct capreq *newcr;
    
    size = capreq_sizeof(cr);
    if (na)
        newcr = na->na_malloc(na, size);
    else
        newcr = n_malloc(size);
    
    memcpy(newcr, cr, size);
    if (na)
        newcr->cr_relflags |= __NAALLOC;
    else
        newcr->cr_relflags &= ~(__NAALLOC);
    
    return newcr;
}
struct source *source_malloc(void)
{
    struct source *src;
    
    src = n_malloc(sizeof(*src));
    memset(src, '\0', sizeof(*src));

    src->type = src->original_type = NULL;
    src->flags = src->subopt_flags = 0;
    src->pri = 0;
    src->no = 0;
    //src->flags |= PKGSOURCE_PRI;
    src->name = src->path = src->pkg_prefix = NULL;
    src->dscr = src->type = NULL;
    src->lc_lang = NULL;
    src->_refcnt = 0;
    src->exclude_path = n_array_new(4, free, (tn_fn_cmp)strcmp);
    src->ign_patterns = n_array_new(4, free, (tn_fn_cmp)strcmp);
    return src;
}
Beispiel #29
0
static
int posthook_diff(struct pkgdir *pd1, struct pkgdir* pd2, struct pkgdir *diff)
{
	struct pndir *idx, *idx2;

    pd2 = pd2;                  /* unused */
	if ((idx2 = pd1->mod_data) == NULL)
		return 0;
	
	idx = diff->mod_data;
	
	if (idx == NULL) {
        idx = n_malloc(sizeof(*idx));
        pndir_init(idx);
        diff->mod_data = idx;
	}
	
	idx->md_orig = n_strdup(idx2->dg->md);
	return 1;
}
Beispiel #30
0
DWORD CDataServer::LoadFileToBuffer(const nString& FileName, char*& Buffer)
{
	CFileStream File;

	int BytesRead;

	if (File.Open(FileName, SAM_READ, SAP_SEQUENTIAL))
	{
		int FileSize = File.GetSize();
		Buffer = (char*)n_malloc(FileSize);
		BytesRead = File.Read(Buffer, FileSize);
		File.Close();
	}
	else
	{
		Buffer = NULL;
		BytesRead = 0;
	}

	return BytesRead;
}