Fix copyright
[yaffs2.git] / direct / test-framework / timothy_tests / yaffs_and_linux_mirror_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
15 static int EXIT_ON_ERROR = 1;
16 static int  PRINT_LEVEL = 2;    //This sets the level of detail which is printed. There are 3 levels 0,1,2 and 3  
17                         //0 just prints the number of tests passed and failed.
18                         //1 is the basic print level. it will print the details of a failed test.
19                         //2 will print if a test passes and cleans. 
20 void set_print_level(int new_level)
21 {
22         PRINT_LEVEL=new_level;
23 }
24 int get_print_level(void)
25 {
26         return PRINT_LEVEL;
27 }
28
29 void set_exit_on_error(int num)
30 {
31         EXIT_ON_ERROR=num;
32 }
33
34 int get_exit_on_error(void)
35 {
36         return EXIT_ON_ERROR;
37 }
38
39 void display_error(void)
40 {
41         
42 }
43
44 void get_error_yaffs(void)
45 {
46         int error_code=0;
47         char message[30];
48         message[0]='\0';
49
50         error_code=yaffs_get_error();
51         sprintf(message,"yaffs_error code %d\n",error_code);
52         print_message(1,message);
53         sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
54         print_message(1,message);
55 }
56
57 void get_error_linux(void)
58 {
59         int error_code=0;
60         char message[30];
61         message[0]='\0';
62
63         error_code=errno;
64         sprintf(message,"linux_error code %d\n",error_code);
65         print_message(1,message);
66         strcpy(message,"error code");
67         sprintf(message,"error is : %s\n",yaffs_error_to_str(error_code));
68         //perror(message);      
69         print_message(1,message);
70 }
71 void  generate_random_string(char *ptr,int length_of_str){
72         unsigned int x;
73         unsigned int length=((rand() %(length_of_str-3))+3);    /*creates a int with the number of charecters been between 1 and 51*/           
74         char letter='\0';
75         strcpy(ptr,"");
76         //printf("generating string\n");
77         //printf("string length is %d\n",length);
78         for (x=0; x <= (length-2) &&length>2 ; x++)
79         {
80                 //printf("x=%d\n",x);   
81                 /* keep generating a charecter until the charecter is legal*/
82                 while((letter=='\0' )||(letter=='/')||(letter=='\\')){
83                         letter=(rand() % 125-59)+58;    /*generate a number between 32 and 126 and uses it as a charecter (letter) */
84                 }       
85                 ptr[x]=letter;
86                 //printf("charecter generated is %c\n",ptr[x]);
87         }
88         ptr[x+1]='\0';  /*adds NULL charecter to turn it into a string*/
89         
90 }
91
92 void join_paths(char *path1,char *path2,char *new_path )
93 {
94         char message[100];
95         print_message(3,"joining paths\n");
96         sprintf(message,"path1: %s\n",path1);
97         print_message(3,message);
98         sprintf(message,"path2: %s\n",path2);
99         print_message(3,message);
100         strcpy(new_path,"");
101         //strcat(new_path,path1);       /*since all functions have this then pull it out*/
102         if ( (path1[(sizeof(path1)/sizeof(char))-2]=='/') && path2[0]!='/') {
103                 /*paths are compatiable. concatanate them. note -2 is because of \0*/  
104                 strcat(new_path,path1);
105                 strcat(new_path,path2);         
106                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))];
107                 //strcpy(new_path,strcat(path1,path2)); 
108                 //return new_path;
109         } else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]=='/') {
110                 /*paths are compatiable. concatanate them*/  
111                 strcat(new_path,path1);
112                 strcat(new_path,path2);         
113
114         } else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]!='/') {
115                         /*need to add a "/". */  
116                 strcat(new_path,path1);
117                 strcat(new_path,"/");
118                 strcat(new_path,path2);
119
120         } else if ((path1[(sizeof(path1)/sizeof(char))-2]=='/') && path2[0]=='/') {
121                 /*need to remove a "/". */
122                 /*yaffs does not mind the extra slash. */               
123                 strcat(new_path,path1);
124                 strcat(new_path,path2);
125
126         } else {
127                 //error 
128                 //return -1;
129         }
130 }
131
132 void print_message(char print_level,char *message)
133 {
134         if (print_level <= PRINT_LEVEL){
135                 printf(message);
136         }
137 }
138         
139
140