Example #1
0
// load a 256x256 RGB .RAW file as a texture
// http://www.nullterminator.net/gltexture.html
GLuint gl_load_texture_raw(const char * filename, u_int width, u_int height, u_int alpha) {
  char* data;
  FILE* file;

  printf("loading raw texture [%s]\n", filename);

  // open texture data
  file = fopen(filename, "rb");
  if ( file == NULL ) return 0;

  // allocate buffer
  //  width = 256;
  //  height = 256;
  width = 512;
  height = 512;

  data = (char*)  malloc(width * height * (alpha ? 4 : 3));
  if (data == NULL)
    pexit(errno, "gl_load_texture_raw: can't allocate space for image data");

  // read texture data
  fread(data, width * height * (alpha ? 4 : 3), 1, file );
  fclose(file);

  return gl_load_texture_from_data(data, 512, 512, (alpha ? 4 : 3));
}
Example #2
0
/*
 *  this is the body for all kproc's
 */
static void
linkproc(void)
{
	spllo();
	up->kpfun(up->kparg);
	pexit("kproc exiting", 0);
}
Example #3
0
void *merge(void *hnd, c4snet_data_t *OutFile, c4snet_data_t *Result,
            int X, c4snet_data_t *Out, int N, int B, int K, int J, int I)
{
    const int   P = N / B;

    if (verbose) {
        printf("%s: %p, %p, X=%d, %p, N=%d, B=%d, (%d, %d, %d)\n",
                __func__, OutFile, Result, X, Out, N, B, K, J, I);
    }
    if (compute) {
        c4snet_data_t **result = C4SNetGetData(Result);
        assert(result[J + I * P] == NULL);
        result[J + I * P] = Out;
    }
    if (X > 1) {
        C4SNetOut(hnd, 1, OutFile, Result, X - 1);
    }
    else if (compute) {
        struct timespec end;
        if (clock_gettime(CLOCK_REALTIME, &end)) {
            pexit("clock_gettime");
        }
        printf("Time for size %d x %d : %lf sec\n", N, N, delta_time(begin, end));
        write_matrix(Result, N, B, OutFile);
        C4SNetOut(hnd, 2, C4SNetCreate(CTYPE_char, 5, "Done."));
        C4SNetFree(OutFile);
        C4SNetFree(Result);
    }
    return hnd;
}
Example #4
0
/* Read a block. */
void
get_block(block_t n, void *buf)
{
  ssize_t k;

  /* First access returns a zero block */
  if (read_and_set(n) == 0) {
	memcpy(buf, zero, block_size);
	return;
  }
  if (lseek64(fd, mul64u(n, block_size), SEEK_SET, NULL) == (off_t)(-1))
	pexit("get_block couldn't seek");
  k = read(fd, buf, block_size);
  if (k != block_size)
	pexit("get_block couldn't read block #%u", (unsigned)n);
}
Example #5
0
int
main() {
  logInfo("Parent", "creating child");

  switch (fork()) {
    case -1:
      pexit("fork");
      break;

    case 0:
      /* child code: give a chance for parent finish itself (very basic means of
       * process synchronization */
      logInfo("Child", "child created, waiting parent to finish");
      sleep(5);

      char msg[BUFSIZ];
      snprintf(msg, BUFSIZ, "parent PID = %ld", (long) getppid());

      logInfo("Child", msg);
      break;

    default:
      /* parent: nothing to do but finish */
      logInfo("Parent", "finishing up");
      exit(EXIT_SUCCESS);
  }

  /* only child should get to this point */
  exit(EXIT_SUCCESS);
}
Example #6
0
File: t1.c Project: piotrm0/progs
void toggle_lock_cursor(tile* t) {
  u_int r;
  u_int new_lock = cursor_lock;

  if (NULL == t)
    pexit(EFAULT, "lock_cursor: got NULL tile");

  r = get_robot_at(t);

  switch(cursor_lock) {
  case CURSOR_LOCK_NONE:
    if (ROBOT_NONE != r) {
      new_lock = CURSOR_LOCK_ROBOT;
      cursor_lock_robot = r;
    } else {
      printf("nothing to lock onto\n");
    }
    break;
  case CURSOR_LOCK_ROBOT:
    if ((ROBOT_NONE == r) ||
	(cursor_lock_robot == r)) {
      //      place_robot(cursor_lock_robot, t);
      new_lock = CURSOR_LOCK_NONE;
      cursor_lock_robot = ROBOT_NONE;
      compile_robots();
    } else {
      printf("can't place a robot over another robot\n");
    }
    break;
  }
  cursor_lock = new_lock;
}
Example #7
0
uintptr
sysexits(va_list list)
{
	char *status;
	char *inval = "invalid exit string";
	char buf[ERRMAX];

	status = va_arg(list, char*);
	if(status != nil){
		if(waserror())
			status = inval;
		else{
			validaddr((uintptr)status, 1, 0);
			if(vmemchr(status, 0, ERRMAX) == 0){
				memmove(buf, status, ERRMAX);
				buf[ERRMAX-1] = 0;
				status = buf;
			}
			poperror();
		}

	}
	pexit(status, 1);
	return 0;	/* not reached */
}
Example #8
0
int
main(int argc, char *argv[]) {
  int fd, newfd;
  int numWritten;
  Bool useDup2;

  if (argc < 2 || argc > 3) {
    helpAndLeave(argv[0], EXIT_FAILURE);
  }

  fd = (int) atol(argv[1]);
  newfd = -1; /* if not using dup2, this variable will not be used */

  if (argc == 3) {
    useDup2 = TRUE;
    newfd = (int) atol(argv[2]);
  } else {
    useDup2 = FALSE;
  }

  newfd = duplicate(fd, newfd, useDup2);

  numWritten = write(newfd, writeStr, sizeof(writeStr));
  if (numWritten == -1) {
    pexit("write");
  }

  safeClose(fd);
  safeClose(newfd);

  printf("Done. Written %d bytes to the new file descriptor #%d\n", numWritten, newfd);

  return EXIT_SUCCESS;
}
Example #9
0
static void
vt6102lproc(void* arg)
{
	Ctlr *ctlr;
	Ether *edev;
	MiiPhy *phy;

	edev = arg;
	ctlr = edev->ctlr;
	for(;;){
		if(ctlr->mii == nil || ctlr->mii->curphy == nil)
			break;
		if(miistatus(ctlr->mii) < 0)
			goto enable;

		phy = ctlr->mii->curphy;
		ilock(&ctlr->clock);
		if(phy->fd)
			ctlr->cr |= Fdx;
		else
			ctlr->cr &= ~Fdx;
		csr16w(ctlr, Cr, ctlr->cr);
		iunlock(&ctlr->clock);
enable:
		ctlr->lwakeup = 0;
		vt6102imr(ctlr, Srci);

		ctlr->lsleep++;
		sleep(&ctlr->lrendez, vt6102wakeup, &ctlr->lwakeup);

	}
	pexit("vt6102lproc: done", 1);
}
Example #10
0
static void
loopbackread(void *a)
{
	Ipifc *ifc;
	Block *bp;
	LB *lb;

	ifc = a;
	lb = ifc->arg;
	lb->readp = up;	/* hide identity under a rock for unbind */
	if(waserror()){
		lb->readp = 0;
		pexit("hangup", 1);
	}
	for(;;){
		bp = qbread(lb->q, Maxtu);
		if(bp == nil)
			continue;
		ifc->in++;
		if(!canrlock(ifc)){
			freeb(bp);
			continue;
		}
		if(waserror()){
			runlock(ifc);
			nexterror();
		}
		if(ifc->lifc == nil)
			freeb(bp);
		else
			ipiput4(lb->f, ifc, bp);
		runlock(ifc);
		poperror();
	}
}
Example #11
0
/* Insert one bit into the bitmap */
void
insert_bit(block_t map, bit_t bit)
{
  int boff, w, s;
  unsigned int bits_per_block;
  block_t map_block;
  bitchunk_t *buf;

  buf = alloc_block();

  bits_per_block = FS_BITS_PER_BLOCK(block_size);
  map_block = map + bit / bits_per_block;
  if (map_block >= inode_offset)
	pexit("insertbit invades inodes area - this cannot happen");
  boff = bit % bits_per_block;

  assert(boff >=0);
  assert(boff < FS_BITS_PER_BLOCK(block_size));
  get_block(map_block, buf);
  w = boff / FS_BITCHUNK_BITS;
  s = boff % FS_BITCHUNK_BITS;
  buf[w] |= (1 << s);
  put_block(map_block, buf);

  free(buf);
}
Example #12
0
/* Increment the file-size in inode n */
void
incr_size(ino_t n, size_t count)
{
  block_t b;
  int off;

  b = ((n - 1) / inodes_per_block) + inode_offset;
  off = (n - 1) % inodes_per_block;
  {
	struct inode *inodes;

	assert(inodes_per_block * sizeof(*inodes) == block_size);
	if(!(inodes = alloc_block()))
		err(1, "couldn't allocate a block of inodes");

	get_block(b, inodes);
	/* Check overflow; avoid compiler spurious warnings */
	if (inodes[off].i_size+(int)count < inodes[off].i_size ||
	    inodes[off].i_size > MAX_MAX_SIZE-(int)count)
		pexit("File has become too big to be handled by MFS");
	inodes[off].i_size += count;
	put_block(b, inodes);
	free(inodes);
  }
}
Example #13
0
void * mem_alloc_align_func(size_t size,
							size_t align
#if defined(MEMDBG_ON)
							,
							char * file,
							int line
#endif
							)
{
	void * ptr = NULL;
#if defined(MEMDBG_ON)
	ptr = (char *) MEMDBG_alloc_align(size, align, file, line);
#elif HAVE_POSIX_MEMALIGN
	if (posix_memalign(&ptr, align, size)) pexit("posix_memalign");
#elif HAVE_ALIGNED_ALLOC
	/* According to the Linux man page, "size should be a multiple of
	   alignment", whatever they mean with "should"... This does not
	   make any sense whatsoever but we round it up to comply. */
	size = ((size + (align - 1)) / align) * align;
	if (!(ptr = aligned_alloc(align, size))) pexit("aligned_alloc");
#elif HAVE_MEMALIGN
/* Let's just pray this implementation can actually free it */
#if defined(__sparc__) || defined(__sparc) || defined(sparc)                   \
	|| defined(__sparcv9)
	if (!(ptr = memalign(align, size)))
#else
	if (!(ptr = memalign(&ptr, align, size)))
#endif
		perror("memalign");
#elif HAVE___MINGW_ALIGNED_MALLOC
	if (!(ptr = __mingw_aligned_malloc(size, align)))
		perror("__mingw_aligned_malloc");
#elif HAVE__ALIGNED_MALLOC
	if (!(ptr = _aligned_malloc(size, align))) perror("aligned_malloc");

#elif AC_BUILT
#error No suitable aligned alloc found, please report to john-dev mailing list (state your OS details).

/* we need an aligned alloc function for legacy builds */
#elif _ISOC11_SOURCE
	size = ((size + (align - 1)) / align) * align;
	if (!(ptr = aligned_alloc(align, size))) perror("aligned_alloc");
#else
	if (posix_memalign(&ptr, align, size)) perror("posix_memalign");
#endif
	return ptr;
}
Example #14
0
static void
w_timer(void* arg)
{
    Ether* ether = (Ether*) arg;
    Ctlr* ctlr = (Ctlr*)ether->ctlr;

    ctlr->timerproc = up;
    for(;;) {
        tsleep(&up->sleep, return0, 0, MSperTick);
        ctlr = (Ctlr*)ether->ctlr;
        if(ctlr == 0)
            break;
        if((ctlr->state & (Attached|Power)) != (Attached|Power))
            continue;
        ctlr->ticks++;

        ilock(ctlr);

        // Seems that the card gets frames BUT does
        // not send the interrupt; this is a problem because
        // I suspect it runs out of receive buffers and
        // stops receiving until a transmit watchdog
        // reenables the card.
        // The problem is serious because it leads to
        // poor rtts.
        // This can be seen clearly by commenting out
        // the next if and doing a ping: it will stop
        // receiving (although the icmp replies are being
        // issued from the remote) after a few seconds.
        // Of course this `bug' could be because I'm reading
        // the card frames in the wrong way; due to the
        // lack of documentation I cannot know.

        if(csr_ins(ctlr, WR_EvSts)&WEvs) {
            ctlr->tickintr++;
            w_intr(ether);
        }

        if((ctlr->ticks % 10) == 0) {
            if(ctlr->txtmout && --ctlr->txtmout == 0) {
                ctlr->nwatchdogs++;
                w_txdone(ctlr, WTxErrEv);
                if(w_enable(ether)) {
                    DEBUG("wavelan: wdog enable failed\n");
                }
                w_txstart(ether);
            }
            if((ctlr->ticks % 120) == 0)
                if(ctlr->txbusy == 0)
                    w_cmd(ctlr, WCmdEnquire, WTyp_Stats);
            if(ctlr->scanticks > 0)
                if((ctlr->ticks % ctlr->scanticks) == 0)
                    if(ctlr->txbusy == 0)
                        w_cmd(ctlr, WCmdEnquire, WTyp_Scan);
        }
        iunlock(ctlr);
    }
    pexit("terminated", 0);
}
Example #15
0
static void
linkproc(void)
{
	Proc *up = externup();
	spllo();
	up->kpfun(up->kparg);
	pexit("kproc dying", 0);
}
Example #16
0
static void *
thread_function(void *arg) {
	int tid = *((int *) arg);
	if (one_time_init(&control, init_function) == -1)
		pexit("one_time_init");

	printf("%d: value of shared variable: %d\n", tid, to_be_initialized);
	return NULL;
}
Example #17
0
static void log_file_flush(struct log_file *f)
{
	int count;

	if (f->fd < 0) return;

	count = f->ptr - f->buffer;
	if (count <= 0) return;

#if defined(LOCK_EX) && OS_FLOCK
	if (flock(f->fd, LOCK_EX)) pexit("flock");
#endif
	if (write_loop(f->fd, f->buffer, count) < 0) pexit("write");
	f->ptr = f->buffer;
#if defined(LOCK_EX) && OS_FLOCK
	if (flock(f->fd, LOCK_UN)) pexit("flock");
#endif
}
Example #18
0
static void log_file_fsync(struct log_file *f)
{
	if (f->fd < 0) return;

	log_file_flush(f);
#if !defined(__CYGWIN32__) && !defined(__MINGW32__) && !defined(_MSC_VER)
	if (fsync(f->fd)) pexit("fsync");
#endif
}
Example #19
0
static void log_file_fsync(struct log_file *f)
{
	if (f->fd < 0) return;

	log_file_flush(f);
#if HAVE_WINDOWS_H==0
	if (fsync(f->fd)) pexit("fsync");
#endif
}
Example #20
0
static void log_file_fsync(struct log_file *f)
{
	if (f->fd < 0) return;

	log_file_flush(f);
#ifndef __CYGWIN32__
	if (fsync(f->fd)) pexit("fsync");
#endif
}
Example #21
0
void * decompose (void *hnd, c4snet_data_t *InFile, c4snet_data_t *OutFile,
    int a_size, int bs)
{
    int p, i, j;
    double *array;
    tile *atiles, *ltiles;
    char *infile;

    if (bs <= 0) {
        fprintf (stderr, "A block size must be greater than 0\n");
        exit (1);
    }
    if (a_size <= 0) {
        fprintf (stderr, "The dimension of matrix must be greater than 0\n");
        exit (1);
    }
    if (a_size % bs) {
        fprintf(stderr, 
            "matrix size %d is not a multiple of the block size %d\n",
            a_size, bs);
        exit(1);
    }

    p = a_size / bs;

    /* Get the input filename as a string. */
    infile = chars_to_string(InFile);
    outfile = chars_to_string(OutFile);
    C4SNetFree(InFile);
    C4SNetFree(OutFile);

    array = SNetMemAlloc(a_size * a_size * sizeof(double));
    read_matrix(a_size, array, infile);
    free(infile);

    if (clock_gettime(CLOCK_REALTIME, &begin)) {
        pexit("clock_gettime");
    }

    atiles = (tile *) malloc (sizeof (tile) * p * p);
    ltiles = (tile *) malloc (sizeof (tile) * p * p);
    memset (atiles, 0, sizeof (tile) * p * p);
    memset (ltiles, 0, sizeof (tile) * p * p);
    for (i = 0; i < p; i++)
        for (j = 0; j <= i; j++) {
            atiles[j * p + i] = (double *) malloc (sizeof (double) * bs * bs);
            ltiles[j * p + i] = (double *) malloc (sizeof (double) * bs * bs);
            int ai, aj, ti, tj;
            for (ai = i * bs, ti = 0; ti < bs; ai++, ti++)
                for (aj = j * bs, tj = 0; tj < bs; aj++, tj++)
                    atiles[j * p + i][tj * bs + ti] = array[aj * a_size + ai];
        }
    C4SNetOut (hnd, 1, C4SNetCreate (CTYPE_char, sizeof (void *), &atiles),
               C4SNetCreate (CTYPE_char, sizeof (void *), &ltiles), bs, p, 0);
    free(array);
    return hnd;
}
Example #22
0
static void
linkproc(void)
{
	spllo();
	if (waserror())
		print("error() underflow: %r\n");
	else (*up->kpfun)(up->arg);
	pexit("end proc", 1);
}
Example #23
0
int ChangeMacAddr()
{
	struct ifreq ifr;
	struct sockaddr_in *sin = (void *) &ifr.ifr_ifru.ifru_addr;
	int s;
	unsigned char	ClientHwAddr[ETHER_ADDR_LEN];
	
	memset(&ifr, 0, sizeof(ifr));
	memcpy(ifr.ifr_name,"eth0\0",5);
	
	s = socket(AF_INET, SOCK_DGRAM, 0);
    if (s < 0) {
        pexit("stream socket");
    }
    ClientHwAddr[0] = 0x00;
    ClientHwAddr[1] = 0x88;
    ClientHwAddr[2] = 0x12;
    ClientHwAddr[3] = 0x34;
    ClientHwAddr[4] = 0x56;
    ClientHwAddr[5] = 0x78;
    
	memcpy(ifr.ifr_hwaddr.sa_data, ClientHwAddr, ETHER_ADDR_LEN);
	if ( ioctl(s,SIOCSIFHWADDR,&ifr) )
    {
      printf("ioctl SIOCSIFHWADDR: %d\n",errno);
      close(s);
      pexit("ioctl SIOCGIFHWADDR");
    }
	printf("Set HW addr success\n");
	
	if ( ioctl(s,SIOCGIFHWADDR,&ifr) )
    {
      printf("ioctl SIOCGIFHWADDR: %d\n",errno);
      close(s);
      pexit("ioctl SIOCGIFHWADDR");
    }
    if ( ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER )
    {
      printf("interface %s is not Ethernet %x\n",ifr.ifr_name,ifr.ifr_hwaddr.sa_family);
    }
    memcpy(ClientHwAddr,ifr.ifr_hwaddr.sa_data,ETHER_ADDR_LEN);
	printf("%02x:%02x:%02x:%02x:%02x:%02x\n",ClientHwAddr[0],ClientHwAddr[1],ClientHwAddr[2],ClientHwAddr[3],ClientHwAddr[4],
		ClientHwAddr[5]);
}
Example #24
0
int
main() {
	char *login;

	if ((login = _getlogin()) == NULL)
		pexit("_getlogin");

	printf("%s\n", login);
	exit(EXIT_SUCCESS);
}
Example #25
0
static void
logInfo(const char *who, const char *message) {
  char buf[BUFSIZ];
  int size;

  size = snprintf(buf, BUFSIZ, "[%s %ld] %s\n", who, (long) getpid(), message);
  if (write(STDOUT_FILENO, buf, size) == -1) {
    pexit("write");
  }
}
Example #26
0
static void rec_format_error(char *fn)
{
	if (fn && errno && ferror(rec_file))
		pexit("%s", fn);
	else {
		fprintf(stderr, "Incorrect crash recovery file: %s\n",
			path_expand(rec_name));
		error();
	}
}
Example #27
0
static void restore_line_number(void)
{
	char line[LINE_BUFFER_SIZE];
	if (skip_lines(rec_pos, line)) {
		if (ferror(word_file))
			pexit("fgets");
		fprintf(stderr, "fgets: Unexpected EOF\n");
		error();
	}
}
Example #28
0
copyfile()
{
	extern int fout[];
	register int c;

	(void) close(1);
	if (creat(firstname, 0644) != 1) {
		perror(firstname);
		pexit(ERRS);
	}
	if (lseek(fout[0], (off_t)0, 0) == -1)
		perror("copyfile: lseek"), panic("copyfile");
	while ((c = read(fout[0], &fout[3], 512)) > 0) {
		if (write(1, &fout[3], c) != c) {
			perror(firstname);
			pexit(ERRS);
		}
	}
}
Example #29
0
int
main() {
	/* read and write pipes file descriptions, from the perspective of the parent process */
	int writePfd[2], readPfd[2];
	long childPid;

	/* create read and write pipes */
	if (pipe(writePfd) == -1)
		pexit("pipe");

	if (pipe(readPfd) == -1)
		pexit("pipe");

	childPid = fork();

	switch (childPid) {
	case -1:
		pexit("fork");
	case 0:
		/* child process: closes the write end of the write pipe, and the read end of the read pipe. */
		if (close(writePfd[1]) == -1)
			pexit("close write end of write pipe - child");

		if (close(readPfd[0]) == -1)
			pexit("close read end of read pipe - child");

		childLoop(writePfd[0], readPfd[1]);

		/* child loop is finished: this means that the parent process received EOF,
		 * and the write end of writePfd was closed */
		_exit(EXIT_SUCCESS);

	default:
		/* parent process: closes the read end of the write pipe, and the write end of the read pipe */
		if (close(writePfd[0]) == -1)
			pexit("close read end of write pipe - parent");

		if (close(readPfd[1]) == -1)
			pexit("close write end of read pipe - parent");

		parentLoop(readPfd[0], writePfd[1]);

		/* EOF received. Close write end of writePfd */
		if (close(writePfd[1]) == -1)
			pexit("close write end of write pipe - parent");

		printf("\n");
	}

	/* parent: wait for the child to die and terminate */
	wait(NULL);
	exit(EXIT_SUCCESS);
}
Example #30
0
static void log_file_done(struct log_file *f)
{
	if (f->fd < 0) return;

	log_file_fsync(f);
	if (close(f->fd)) pexit("close");
	f->fd = -1;

	MEM_FREE(f->buffer);
}