Fix unmatched temporary buffer allocations
[yaffs2.git] / direct / test-framework / timothy_tests / linux_tests / lib.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2018 Aleph One Ltd.
5  *
6  * Created by Timothy Manning <timothy@yaffs.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include "lib.h"
14 static char message[200];
15 static int  PRINT_LEVEL = 3;
16 static int exit_on_error_val =1;
17 char string[FILE_NAME_LENGTH+1];
18
19
20 int get_exit_on_error(void)
21 {
22         return exit_on_error_val;
23 }
24
25 void set_exit_on_error(int val)
26 {
27         exit_on_error_val=val;
28 }
29
30 node * linked_list_add_node(int pos,node *head_node)
31 {
32         node *new_node=NULL;
33         if (pos==HEAD){
34                 new_node=malloc(sizeof(node));
35                 memset(new_node, 0, sizeof(node));
36                 new_node->string=NULL;
37                 new_node->next=head_node;
38                 return new_node;
39         }
40         return NULL;
41 }
42
43 void node_print_pointers(node *current_node)
44 {
45         while (current_node != NULL){
46                 sprintf(message,"current_node: %p, string: %s next_node: %p\n",current_node,current_node->string,current_node->next);
47                 print_message(3,message);
48                 current_node=current_node->next;
49         }
50 }
51
52 int delete_linked_list(node *head_node)
53 {
54         node *next_node=NULL;
55         node *current_node=head_node;
56
57                 while (current_node != NULL){
58                         next_node=current_node->next;
59                         free(current_node);
60                         current_node=next_node;
61                 }
62
63         return 1;
64 }
65
66 char * generate_random_string(unsigned int length)
67 {
68
69         unsigned int x;
70         for (x=0;x<(length-1);x++)
71         {
72                 string[x]=(rand() % NAME_RANGE)+65;
73         }
74         string[x]='\0';
75         return string;
76 }
77
78 void set_print_level(int new_level)
79 {
80         PRINT_LEVEL=new_level;
81 }
82 int get_print_level(void)
83 {
84         return PRINT_LEVEL;
85 }
86 void print_message(char print_level,char *message)
87 {
88         if (print_level <= PRINT_LEVEL){
89                 printf("%s",message);
90         }
91 }
92 int random_int(void)
93 {
94         return (random()%1000000); 
95 }
96
97 void check_function(int output)
98 {
99         if (output>=0){
100                 print_message(3,"test_passed\n");
101         } else {
102                 print_message(3,"test_failed\n");
103                 get_error_linux();
104         }
105 }
106
107 void get_error_linux(void)
108 {
109         int error_code=0;
110         char message[30];
111         message[0]='\0';
112
113         error_code=errno;
114         sprintf(message,"linux_error code %d\n",error_code);
115         print_message(1,message);
116         
117         strcpy(message,"error is");
118         perror(message);
119 //      sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
120         //perror(message);      
121         //print_message(1,message);
122 }