Пример #1
0
void findWords(SymbolTable *table) {
	char *request, *respond, *rest;
	String token[SIZE];

	if((request = myMalloc(sizeof(char), TEMPSIZE)) == NULL) {
		return;
	}

	if((respond = myMalloc(sizeof(char), TEMPSIZE)) == NULL) {
		return;
	}

	printf("Please enter the domain name which you want to access: \n");
	fgets(request, SIZE, stdin);
	request[strlen(request) - 1] = '\0';

	if((respond = getEntry(request, table)) != NULL) {
		printf("You can not access this domain name. It is blocked\n");
	} else {
		printf("You can access this domain name.\n");
	}

	free(request);
	free(respond);
}
Пример #2
0
bool test_sn_coap_parser_release_allocated_coap_msg_mem()
{
    struct coap_s* coap = (struct coap_s*)malloc(sizeof(struct coap_s));
    coap->sn_coap_protocol_malloc = myMalloc;
    coap->sn_coap_protocol_free = myFree;
    retCounter = 99;

    sn_coap_parser_release_allocated_coap_msg_mem( NULL, NULL );

    sn_coap_hdr_s* ptr = (sn_coap_hdr_s*)myMalloc(sizeof(sn_coap_hdr_s));
    ptr->uri_path_ptr = (uint8_t*)malloc(sizeof(uint8_t));
    ptr->token_ptr = (uint8_t*)malloc(sizeof(uint8_t));
    //ptr->payload_ptr = (uint8_t*)malloc(sizeof(uint8_t));
    ptr->options_list_ptr = (sn_coap_options_list_s*)myMalloc(sizeof(sn_coap_options_list_s));

    ptr->options_list_ptr->max_age = 1;
    ptr->options_list_ptr->proxy_uri_ptr = (uint8_t*)malloc(sizeof(uint8_t));
    ptr->options_list_ptr->etag_ptr = (uint8_t*)malloc(sizeof(uint8_t));
    ptr->options_list_ptr->uri_host_ptr = (uint8_t*)malloc(sizeof(uint8_t));
    ptr->options_list_ptr->location_path_ptr = (uint8_t*)malloc(sizeof(uint8_t));
    ptr->options_list_ptr->uri_port = 8;
    ptr->options_list_ptr->location_query_ptr = (uint8_t*)malloc(sizeof(uint8_t));
    ptr->options_list_ptr->observe = 0;
    ptr->options_list_ptr->uri_query_ptr = (uint8_t*)malloc(sizeof(uint8_t));

    sn_coap_parser_release_allocated_coap_msg_mem( coap, ptr );

    free(coap);
    return true; //this is a memory leak check, so that will pass/fail
}
Пример #3
0
void findWords(SymbolTable *table) {
	char *request, *respond, *rest, *token;
	char *ptr;

	if((request = myMalloc(sizeof(char), TEMPSIZE)) == NULL) {
		return;
	}

	if((respond = myMalloc(sizeof(char), TEMPSIZE)) == NULL) {
		return;
	}

	ptr = request;
	printf("Please enter the string: \n");
	fgets(request, SIZE, stdin);
	request[strlen(request) - 1] = '\0';

	while((token = strtok_r(ptr, " ", &rest)) != NULL) {
		ptr = rest;
		if((respond = getEntry(token, table)) == NULL) {
			printf("The word \"%s\" is not in the dictionary\n", token);
			continue;
		}
		printf("The word \"%s\" is in the dictionary\n", token);
	}

	free(request);
	free(respond);
}
Пример #4
0
/*---------------------------------------------------------------------------
// Construct an empty MYlist with specified capacity and capacity increment
//-------------------------------------------------------------------------*/
MYlist myList2(int elementSize, int capacity, int capacityIncrement) {
  MYlist list;
  void * data;

  if (elementSize<1) {
    fprintf(stderr, "myList(): elementSize must be a postive integer!\n");
    exit(1);
  }
  if (capacity<0) {
    fprintf(stderr, "myList(): capacity must not be negative!\n");
    exit(1);
  }

  list = (MYlist)myMalloc(sizeof(MYlistStruct));

  myListSetElementSize(list, elementSize);
  myListSetSize(list, 0);
  myListSetCapacity(list, capacity);
  myListSetCapacityIncrement(list, capacityIncrement);
  if (capacity == 0)
    myListSetData(list, NULL);
  else {
    data = (void *)myMalloc(elementSize*capacity);
    myListSetData(list, data);
  }

  return(list);
}
Пример #5
0
// initializing an array, sort it and check it
void testFoursort() {
  printf("Running testFoursort ...\n");
  int siz = 1024 * 1024 * 8;
  // int siz = 1024 * 1024;
  // int siz = 15;
  // create array
  struct intval *pi;
  void **A = myMalloc("testFoursort 1", sizeof(pi) * siz);
  int i;
  for (i = 0; i < siz; i++) {
    pi = myMalloc("testFoursort 2", sizeof (struct intval));
    A[i] = pi;
  };
  // fill its content
  fillarray(A, siz, 100);
  // sort it
  // int t0 = clock();
    struct timeval tim;
    gettimeofday(&tim, NULL);
    double t0=tim.tv_sec+(tim.tv_usec/1000000.0);
  int compareIntVal();
  foursort(A, siz, compareIntVal, NUMTHREADS);
  // int t1 = clock();
    gettimeofday(&tim, NULL);
    double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
  // and check it
  check(A, 0, siz-1);
  // printf("Sorting size: %d time: %d\n", siz, t1-t0);
  printf("Sorting time: siz: %d duration: %.3lf\n", siz, t1-t0);
} // end testFoursort()
Пример #6
0
// Create a graph from an input file. The format for the input file is specified
// in the graph.h header file
Graph* createGraph(FILE *inputFile)
{
  Graph *g = myMalloc(sizeof(Graph));
  g->numVertices = countLines(inputFile);
  g->numEdges = (countNumWords(inputFile)-(g->numVertices))/2;
  // init vertices
  g->vertices = myMalloc(sizeof(Vertex) * g->numVertices);
  for(int i = 0; i < g->numVertices; i++){
    Vertex *vp = g->vertices+i;
    vp->label = i+1;
    vp->head = NULL;
    vp->tail = NULL;
  }
  // init edges
  g->edges = myMalloc(sizeof(Edge) * g->numEdges);
  for(int i = 0; i < g->numEdges; i++){
    Edge *ep = g->edges+i;
    ep->endpoint1 = NULL;
    ep->endpoint2 = NULL;
  }
  //init connector elements
  g->connectors = myMalloc(sizeof(ConnectorElement) * g->numEdges * 2);
  for(int i = 0; i < g->numEdges*2; i++){
    ConnectorElement *connector = g->connectors+i;
    connector->adjacentEdge=NULL;
    connector->sourceVertex=NULL;
    connector->prev=NULL;
    connector->next=NULL;
  }
  hookupGraph(g, inputFile);
  rewind(inputFile);

  return g;
}
Пример #7
0
// Run an algorithm and report the time used
void timeTest() {
  double algTime, T;
  int seed;
  int seedLimit = 3;
  int z;
  // int siz = 1024 * 1024 * 16;
  int siz = 1024 * 1024 * 4;
  printf("timeTest() on size: %d \n", siz);
  // construct array
  struct intval *pi;
  void **A = myMalloc("timeTest 1", sizeof(pi) * siz);
  int i;
  for (i = 0; i < siz; i++) {
    pi = myMalloc("timeTest 2", sizeof (struct intval));
    A[i] = pi;
  };

  // warm up the process
  fillarray(A, siz, 666); 
  float sumTimes = 0;
  for (z = 0; z < 3; z++) { // repeat to check stability
    algTime = 0;
    // measure the array fill time
    // int TFill = clock();
      struct timeval tim;
      gettimeofday(&tim, NULL);
      double TFILL=tim.tv_sec+(tim.tv_usec/1000000.0);
    for (seed = 0; seed < seedLimit; seed++) 
      fillarray(A, siz, seed);
      // here alternative ways to fill the array
      // int k;
      // for ( k = 0; k < siz; k++ ) A[k] = 0;
      // for ( k = 0; k < siz; k++ ) A[k] = k%5;
      // for ( k = 0; k < siz; k++ ) A[k] = siz-k;
    // TFill = clock() - TFill;
    gettimeofday(&tim, NULL);
    TFILL=tim.tv_sec+(tim.tv_usec/1000000.0) - TFILL;
    // now we know how much time it takes to fill the array
    // measure the time to fill & sort the array
    // T = clock();
    gettimeofday(&tim, NULL);
    T=tim.tv_sec+(tim.tv_usec/1000000.0);
    for (seed = 0; seed < seedLimit; seed++) { 
      fillarray(A, siz, seed);
      // for ( k = 0; k < siz; k++ ) A[k] = 0;
      // for ( k = 0; k < siz; k++ ) A[k] = k%5;
      // for ( k = 0; k < siz; k++ ) A[k] = siz-k;
      // foursort(A, siz, compareIntVal, NUMTHREADS);
      callCut2(A, siz, compareIntVal);
    }
    // ... and subtract the fill time to obtain the sort time
    // algTime = clock() - T - TFill;
    gettimeofday(&tim, NULL);
    algTime=tim.tv_sec+(tim.tv_usec/1000000.0) - T - TFILL;
    printf("algTime: %f \n", algTime);
    sumTimes = sumTimes + algTime;
  }
  printf("%s %f %s", "sumTimes: ", sumTimes, "\n");
} // end timeTest()
Пример #8
0
/*
it should return a pointer to (i.e., the address
of) the first byte of that region.
*/
TEST_F(MemoryTest, ReturnedAddress) {
    int size = 8;
    Region *r = test_mmr->base_region;
    void *ptr = myMalloc(size);
    EXPECT_EQ((void *) r->data, ptr);
    void *ptr2 = myMalloc(size);
    uintptr_t shift = size + sizeof(Region);
    EXPECT_EQ((void *) (r->data + shift), ptr2);
}
Пример #9
0
/*
If the storage cannot be
successfully allocated for any reason, the function should return a
NULL pointer (i.e., the value 0).
*/
TEST_F(MemoryTest, CannotAllocate) {
    void *ptr, *ptr2;
    EXPECT_EQ(NULL, myMalloc(MAX_ALLOCATABLE_SPACE + 1));

    /* shouldn't be able to allocate half of all space twice */
    /* since the region takes up room */
    ptr = myMalloc(MAX_ALLOCATABLE_SPACE / 2);
    ptr2 = myMalloc(MAX_ALLOCATABLE_SPACE / 2);
    EXPECT_EQ(NULL, ptr2);
}
Пример #10
0
static Round * initRounds(size_t num) {
  int i;
  Round * rds = (Round *) myMalloc(num * sizeof(Round)); 
  for (i = 0; i < num; ++i) {
    rds[i] = (Round) myMalloc(sizeof(struct perround));
    rds[i]->N = (int) floor((double) c->n_min * pow(((double) c->n_multiplier / 100), i));
    rds[i]->usecs = 0.0;
    rds[i]->stdev = 0.0;
    rds[i]->data = NULL;
  }
  return rds;
}
Пример #11
0
/* Compare functions */
Entry makeDictionaryEntry(void *wordKey, void *wordValue) {
	Entry result;
	String *key = (String*)wordKey;
	String *value = (String*)wordValue;

	result.key = myMalloc(sizeof(String), 1);
	memcpy(result.key, key, sizeof(String));
	result.value = myMalloc(sizeof(String), 1);
	memcpy(result.value, value, sizeof(String));

	return result;
}
Пример #12
0
/*
*   Private helper function to create a new block for a new file and initialize file params.
*   Parameters:
*       o const char* name - name of file.
*       Return : pointer to new block of file data
*/
static struct mfs_file* mfs_construct(const char* name)
{
	void *fileHeaderMemory = myMalloc(sizeof(struct mfs_file));
	setMemoryPid(fileHeaderMemory,SPECIAL_PID);
	struct mfs_file* newfile = (struct mfs_file*)fileHeaderMemory;
	void * blockMemory = myMalloc(BLOCK_SIZE);
	setMemoryPid(blockMemory,SPECIAL_PID);
	newfile->data = (unsigned char *)blockMemory;
	newfile->size = 0;
	newfile->fname = new_str(name);
	return newfile;
}
Пример #13
0
TEST_F(MemoryTest, IsValidPointer) {
    void *ptr;
    ptr = test_mmr;
    int size = 8;
    EXPECT_EQ(FALSE, is_valid_pointer(test_mmr, ptr));

    ptr = myMalloc(size);
    EXPECT_EQ(TRUE, is_valid_pointer(test_mmr, ptr));

    ptr = myMalloc(size);
    EXPECT_EQ(TRUE, is_valid_pointer(test_mmr, ptr));

    ptr = myMalloc(size);
    EXPECT_EQ(TRUE, is_valid_pointer(test_mmr, ptr));
}
Пример #14
0
/*---------------------------------------------------------------------------
// Add an integer to this list (must be a list consists of only integers)
//-------------------------------------------------------------------------*/
void myListAddInt(MYlist list, int element) {
  int size, capacity, elementSize, capacityIncrement;
  int *data;

  size        = myListSize(list);
  capacity    = myListCapacity(list);
  elementSize = myListElementSize(list);
  data        = myListData(list);

  if (size >= capacity) {
    capacityIncrement = myListCapacityIncrement(list);
    capacity += capacityIncrement;
    myListSetCapacity(list, capacity);
    if (data == NULL) {
      /* initial list */
      data = (int *)myMalloc(elementSize * capacity);
    } else {
      /* allocate a larger list */
      data = (int *)myRealloc(data, elementSize*capacity);
    }
    myListSetData(list, data);
  }

  data[size] = element;
  myListSetSize(list, size+1);
}
Пример #15
0
/*---------------------------------------------------------------------------
// Add an element to this list
//-------------------------------------------------------------------------*/
void myListAddElement(MYlist list, void *element) {
  int size, capacity, elementSize, capacityIncrement;
  void *data;

  size        = myListSize(list);
  capacity    = myListCapacity(list);
  elementSize = myListElementSize(list);
  data        = myListData(list);
  if (size >= capacity) {
    capacityIncrement = myListCapacityIncrement(list);
    capacity += capacityIncrement;
    myListSetCapacity(list, capacity);
    if (data == NULL) {
      /* initial list */
      data = (void *)myMalloc(elementSize * capacity);
    } else {
      /* allocate a larger list */
      data = (void *)myRealloc(data, elementSize*capacity);
    }
    myListSetData(list, data);
  }

  memcpy((char *)data+size*elementSize, (char *)element, elementSize);
  myListSetSize(list, size+1);
}
Пример #16
0
void permula_iterator(int **bucket,int n,int level_total,int level,int *tmp,int *pos,int pos_i){
    //static int pos;//why not?Becuae when call the function mulity times out of recursive,the pos will not be reset to 0
    int i;
    //if(level<0){
    //    print_arr(tmp,sizeof(tmp)/sizeof(tmp[0]));
    //    bucket[pos++]=tmp;
    //    return;
    //}
    //for(i=n-1; i>=0; i--){
    //    tmp[level]=i;
    //    permula_iterator(bucket,n-1,level-1,tmp);
    //}
    if(level==level_total){
        print_arr(tmp,level_total);
        //can not share the tmp for mulity branches
        i=*pos;
        bucket[i]=myMalloc(level_total*sizeof(int));
        memmove(bucket[i],tmp,level_total*sizeof(int));
        //print_arr(bucket[i],level_total);//why bucket[*pos] is NULL?
        (*pos)++;
        return;
    }
    printf("permula-iterator--level:%d,n:%d,tmp:%d,pos:%d,pos_i:%d\n",level,n,*tmp,*pos,pos_i);
    for(i=pos_i;i<n;i++){
        tmp[level]=i;
        //printf("(%d,%d,%d)\n",level,pos_i,i);
        permula_iterator(bucket,n,level_total,level+1,tmp,pos,i+1);
    }
}
Пример #17
0
// Used when the current block needs to grow and there are no
// adjacent free blocks large enough to grow into. Instead, a new block 
// will be allocated and moved into. Then the orginal block will be freed
void *reallocIntoCompletelyNewBlock(header *headerPointer, uint32_t size) {
   void *block = myMalloc(size);
   myMemmove(block, headerPointer + 1, headerPointer->size);
   myFree(headerPointer + 1);

   return block;
}
Пример #18
0
stopwatch_struct* StartStopWatch(Uint32 time)
{
    stopwatch_struct* watch = (stopwatch_struct*)myMalloc(sizeof(stopwatch_struct));
    watch->Start = StopWatch.InterruptCount;
    watch->Time = time;
    return watch;
}
Пример #19
0
void readMetroLineFromFile(FILE *f, Graph graph) {
	String temp;
	String tmpList[SIZE];
	String *list;
	int i;
	int n;

	rewind(f);
	while(fgets(temp.content, SIZE, f) != NULL) {
		if(strstr(temp.content, "[LINES]") != NULL) {
			break;
		}
	}

	while(fscanf(f, "%[^=]=", temp.content) != EOF) {
		fscanf(f, "%[^\n]\n", temp.content);
		if((n = split(temp.content, ' ', tmpList)) == -1) {
			return;
		}
 
		if((list = myMalloc(sizeof(String), n)) == NULL) {
			return;
		}

		strcpy(list[0].content, tmpList[0].content);
		for(i = 1; i < n; i++) {
			strcpy(list[i].content, tmpList[i].content);
			addEdge(graph, new_jval_v(&list[i - 1]), new_jval_v(&list[i]), stringCompare);
			printf("%s - %s\n", list[i - 1].content, list[i].content);
		}
	}
	rewind(f);
}
Пример #20
0
/*
*   Implements open interface
*   Parameters:
*       o const char *name - file name
*       o char mode - 'rR','wW' or 'aA' 
*   Return : stream object if successful, otherwise NULL
*/
void* mfs_open(const char *name,char mode)
{
	struct mfs_file* file = Head;
	while(file)
	{
		if(strcompare(file->fname,name))
		{
			struct mfs_stream* stream = (struct mfs_stream*)myMalloc(sizeof(struct mfs_stream));
			stream->offset = 0;
			stream->readoffset = 0;
			stream->file = file;		
			if(mode == 'a' || mode == 'A')
			{
				stream->offset = file->size;
			}
			else if(mode == 'w' || mode == 'W')
			{
				file->size = 0;
			}
			return stream;
		}
		file = file->next;
	}
	return NULL;
}
Пример #21
0
TEST_F(MemoryTest, RegionForPointer) {
    void *ptr = myMalloc(8);
    Region *r = region_for_pointer(ptr);
    EXPECT_EQ(((Region *) ptr - 1), r);
    EXPECT_EQ(0, r->free);
    EXPECT_EQ(8, r->size);
}
Пример #22
0
static uintptr_t * loadData(size_t cur_N) {
  uintptr_t i;
  uintptr_t * data = (uintptr_t *) myMalloc(cur_N * sizeof(uintptr_t));

  switch (c->input_order) {
  case ASCENDING:
    for (i = 0; i < cur_N; ++i)
      data[i] = i + 1;
    break;
  case DESCENDING:
    for (i = 0; i < cur_N; ++i)
      data[i] = cur_N - i;
    break;
  case ALTERNATING:
    for (i = 0; i < cur_N; i += 2) {
      data[i] = i/2 + 1; 
      if (i+1 < cur_N)
        data[i+1] = cur_N - i/2;
    }
    break;
  case RANDOM:
    for (i = 0; i < cur_N; ++i)
      data[i] = (rand() % cur_N) + 1;
    break;
  }
  return data;
}
Пример #23
0
Stu *insertList(Stu *head)
{
    Stu *pnew = NULL;
    if(!head)
    {
        printf("sorry, doesn't exist\n");
        return NULL;
    }
    //创建一个新的节点
    pnew = myMalloc();
    //判断是否创建成功
    if(!pnew)
    {
        printf("fail to create\n");
        return head;//链表没有被修改
    }
    head = insertNode(head, pnew);
    //再判断
    if(!head)
    {
        printf("fail to insert node\n");
        return NULL;
    }
    //否则 返回插入成功以后的新链表
    return head;
}
Пример #24
0
int readStationListFromFile(FILE *f, JRB stationList) {
	String temp;
	StationInfo *station;
	int n = 0;
	
	printf("Station list: \n");
	rewind(f);
	fgets(temp.content, SIZE, f);
	while(fscanf(f, "%[^=]=", temp.content) != EOF) {
		if(strstr(temp.content, "[LINES]") != NULL) {
			break;
		}

		if((station = myMalloc(sizeof(StationInfo), 1)) == NULL) {
			return -1;
		}

		strcpy(station->code.content, temp.content);
		fgets(temp.content, SIZE, f);
		temp.content[strlen(temp.content) - 1] = '\0';
		strcpy(station->name.content, temp.content);

		jrb_insert_gen(stationList, new_jval_v(&station->name), new_jval_v(&station->code), stringCompare);

		printf("Station name: \"%s\" - \"%s\"\n", station->name.content, station->code.content);
	}
	return n;
}
Пример #25
0
Page createMainMenuPage(){

	//Add Background
	SDL_Surface *bkg = loadImage(MAIN_MENU_BKG);
	addImageToSurface(bkg, NULL, containerPage.page, NULL);
	currentPage.bkg = bkg;

	// CreateButtons
	Button newGameBtn = createButton_settings(NEW_GAME_BTN_URL, NULL, 1, 150, 42, SCREEN_WIDTH / 2 - 150 / 2, 200);
	Button loadGameBtn = createButton_settings(LOAD_GAME_BTN_URL, NULL, 2, 150, 42, SCREEN_WIDTH / 2 - 150 / 2, newGameBtn.buttonsDestRect.y + newGameBtn.buttonsDestRect.h + SPACE);
	Button quitGameBtn = createButton_settings(QUIT_GAME_BTN_URL, NULL, 3, 150, 42, SCREEN_WIDTH / 2 - 150 / 2, loadGameBtn.buttonsDestRect.y + loadGameBtn.buttonsDestRect.h + SPACE);
	Button *btnLst = (Button*)myMalloc(sizeof(Button)*3);
	if (btnLst == NULL){
		currentPage.isError = 1;
		return currentPage;
	}

	btnLst[0] = newGameBtn;
	btnLst[1] = loadGameBtn;
	btnLst[2] = quitGameBtn;

	currentPage.btnList = btnLst;
	currentPage.btnListLen = 3;
	currentPage.id = 1;

	addButtons(btnLst, 3, containerPage.page);

	restorDefaultSettings();

	return currentPage;
}
Пример #26
0
/*---------------------------------------------------------------------------
// Add an array to this list
//-------------------------------------------------------------------------*/
void    myListAddArray(MYlist list, void *array, int num) {
  int size, capacity, elementSize, capacityIncrement, actualIncrement;
  void *data;

  size        = myListSize(list);
  capacity    = myListCapacity(list);
  elementSize = myListElementSize(list);
  data        = myListData(list);
  if (size + num > capacity) {
    capacityIncrement = myListCapacityIncrement(list);
    actualIncrement = (capacityIncrement > num)? capacityIncrement: num;
    capacity += actualIncrement;
    myListSetCapacity(list, capacity);

    if (data == NULL) {
      /* initial list */
      data = (void *)myMalloc(elementSize * capacity);
    } else {
      /* allocate a larger list */
      data = (void *)myRealloc(data, elementSize*capacity);
    }
    myListSetData(list, data);
  }

  memcpy((char *)data+size*elementSize, (char *)array, num*elementSize);
  myListSetSize(list, size+num);
}
Пример #27
0
/*---------------------------------------------------------------------------
// Insert an element into the list at the specified index
//-------------------------------------------------------------------------*/
int myListInsertElementAt(MYlist list, int index, void *element) {
  int size, elementSize;
  void *data;
  void *tempPtr;
  char *currentPtr, *nextPtr;
  int i;

  size        = myListSize(list);
  elementSize = myListElementSize(list);

  if (index<0 || index>size-1) {
    return(-1); /* out of bound error */
  }

  tempPtr = (void *)myMalloc(elementSize);
  myListAddElement(list, tempPtr);

  data        = myListData(list);

  for (i=size-1; i>=index; i--) {
    currentPtr = (char *)data+i*elementSize;
    nextPtr    = (char *)currentPtr + elementSize;
    memcpy(nextPtr, currentPtr, elementSize);
  }

  memcpy((char *)data+index*elementSize, (char *)element, elementSize);

  return(0);
}
Пример #28
0
// newNode()
// Returns reference to new Node object. Initializes next, previous and data fields.
// Private.
Node newNode(int data){
   Node N = myMalloc(sizeof(NodeObj));
   N->data = data;
   N->next = NULL;
   N->previous = NULL;
   return(N);
}
Пример #29
0
/*
*   Implements open interface
*   Parameters:
*       o const char *name - led name
*       o char mode - 'rR','wW' or 'aA' 
*   Return : stream object if successful, otherwise NULL
*   sets my_errno to NOT_A_LED if led name is invalid.
*/
void* led_open(const char *name,char mode)
{
	enum LED_MINOR minor = NLED;
	if(strcompare(ORANGE_LED,name))
	{
		minor = ORANGE;
	}
	else if(strcompare(YELLOW_LED,name))
	{
		minor = YELLOW;
	}
	else if(strcompare(GREEN_LED,name))
	{
		minor = GREEN;
	}
	else if(strcompare(BLUE_LED,name))
	{
		minor = BLUE;
	}
	else
	{
		my_errno = NOT_A_LED;
		return NULL;
	}
	struct led_stream *stream = myMalloc(sizeof(struct led_stream));
	stream->minor = minor;
	return stream;
}
Пример #30
0
static char *new_str(const char *source,int size)
/*
*   Private helper function to create a copy of source string on heap 
*   Parameters:
*       o const char *source - null terminated source string
*       o int size - size of source string  
*   Return : dynamically allocated copy of source string
*   Caller of this function is responsible for freeing the dynamically 
*   allocated memory for string
*/
{
	char tempstr[1024];
	int i;//source string iterator
	char* newstr = NULL;//destination string
	//allocate memory for arguments, caller is responsible to free memory
	newstr = (char*)(myMalloc((size + 1)*sizeof(char)));
	if(newstr == NULL)//if malloc fails
	{
		sprintf(tempstr,"myMalloc failed: %s\r\n",strerror(errno));uprintf(tempstr);
		exit(EXIT_FAILURE);
	}
	for(i = 0;i < size;++i)
	{
		newstr[i] = source[i];//copy from source
	}
	newstr[size] = '\0';//terminate string
	return newstr;
}