GophHub - go4retro/tcpser/src/util.c


Raw File

    1	#include <pthread.h>
    2	#include <stdlib.h>       // for exit...
    3	#include <stdio.h>
    4	#include <unistd.h>
    5	
    6	#include "debug.h"
    7	#include "util.h"
    8	
    9	int writePipe(int fd, char msg) {
   10	  char tmp[1];
   11	
   12	  tmp[0] = msg;
   13	
   14	  return write(fd, tmp, 1);
   15	}
   16	
   17	int readPipe(int fd, unsigned char *buf, int len) {
   18	  return read(fd, buf, len);
   19	}
   20	
   21	int writeFile(char *name, int fd) {
   22	  FILE *file;
   23	  unsigned char buf[255];
   24	  size_t res;
   25	  size_t size = 1;
   26	  size_t max = sizeof(buf);
   27	
   28	  if(NULL != (file = fopen(name, "rb"))) {
   29	    do {
   30	      res = fread(buf, size, max, file);
   31	      if(res > 0)
   32	        res = write(fd, buf, res);
   33	    } while (res > 0);
   34	    fclose(file);
   35	    return 0;
   36	  }
   37	  return -1;
   38	}
   39	
   40	void spawn_thread(void * thread, void *arg, char *name) {
   41	  int rc;
   42	  pthread_t thread_id;
   43	
   44	  rc = pthread_create(&thread_id, NULL, thread, arg);
   45	  LOG(LOG_ALL, "%s thread ID = %ld", name, (long)thread_id);
   46	
   47	  if(rc < 0) {
   48	    ELOG(LOG_FATAL, "%s thread could not be started", name);
   49	    exit(-1);
   50	  }
   51	}
   52	

Generated by GNU Enscript 1.6.6, and GophHub 1.3.