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