github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/src/liblink/go.c (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // go-specific code shared across loaders (5l, 6l, 8l).
     6  
     7  #include <u.h>
     8  #include <libc.h>
     9  #include <bio.h>
    10  #include <link.h>
    11  
    12  // replace all "". with pkg.
    13  char*
    14  expandpkg(char *t0, char *pkg)
    15  {
    16  	int n;
    17  	char *p;
    18  	char *w, *w0, *t;
    19  
    20  	n = 0;
    21  	for(p=t0; (p=strstr(p, "\"\".")) != nil; p+=3)
    22  		n++;
    23  
    24  	if(n == 0)
    25  		return estrdup(t0);
    26  
    27  	w0 = emallocz(strlen(t0) + strlen(pkg)*n);
    28  	w = w0;
    29  	for(p=t=t0; (p=strstr(p, "\"\".")) != nil; p=t) {
    30  		memmove(w, t, p - t);
    31  		w += p-t;
    32  		strcpy(w, pkg);
    33  		w += strlen(pkg);
    34  		t = p+2;
    35  	}
    36  	strcpy(w, t);
    37  	return w0;
    38  }
    39  
    40  void*
    41  emallocz(long n)
    42  {
    43  	void *p;
    44  
    45  	p = malloc(n);
    46  	if(p == nil)
    47  		sysfatal("out of memory");
    48  	memset(p, 0, n);
    49  	return p;
    50  }
    51  
    52  char*
    53  estrdup(char *p)
    54  {
    55  	p = strdup(p);
    56  	if(p == nil)
    57  		sysfatal("out of memory");
    58  	return p;
    59  }
    60  
    61  void*
    62  erealloc(void *p, long n)
    63  {
    64  	p = realloc(p, n);
    65  	if(p == nil)
    66  		sysfatal("out of memory");
    67  	return p;
    68  }
    69  
    70  void
    71  double2ieee(uint64 *ieee, float64 f)
    72  {
    73  	memmove(ieee, &f, 8);
    74  }