Krishna iResearch Intelligent Cloud Platform - VIRtual Generic Os - VIRGO - Linux kernel extensions for cloud
 All Classes
sscanftest.c
1 /***************************************************************************************
2 VIRGO - VIRtual Generic Os - linux kernel extensions for cloud
3 
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 --------------------------------------------------------------------------------------------------
18 Copyright(C):
19 Srinivasan Kannan (alias) Ka.Shrinivaasan (alias) Shrinivas Kannan
20 Independent Open Source Developer, Researcher and Consultant
21 Ph: 9789346927, 9003082186, 9791165980
22 Open Source Products Profile(Krishna iResearch): http://sourceforge.net/users/ka_shrinivaasan
23 Personal website(research): https://sites.google.com/site/kuja27/
24 emails: ka.shrinivaasan@gmail.com, shrinivas.kannan@gmail.com, kashrinivaasan@live.com
25 --------------------------------------------------------------------------------------------------
26 
27 *****************************************************************************************/
28 
29 
30 
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdarg.h>
35 
36 /*
37 
38 userspace sscanf and vsscanf %p modifier testcase
39 
40 Outputs:
41 --------
42 p=0x8048770, str after sprintf: [0x8048770]
43 str=[0x8048770], p2 after sscanf: 0x8048770
44 after var_sscanf(): str=[0x8048770], p3 after sscanf: 0x8048770
45 sizeof(p3) = 4
46 lltovoidptr = 0x8048770, data pointed to should be [this is example string] = it is [this is example string]
47 
48 similar sscanf() works for kernel space in virgo_cloud_mempool_kernelspace.ko
49 but doesn't work in virgo_malloc.c and returns null
50 
51 */
52 
53 
54 void var_sscanf(char *str, const char* fmt, ...);
55 
56 int main()
57 {
58  const char* p="this is example string";
59  void* p2;
60  void* p3;
61  char* endptr;
62  char str[300];
63  sprintf(str, "%p", p);
64  printf("p=%p, str after sprintf: [%s]\n", p, str);
65  sscanf(str, "%p", (void**)&p2);
66  printf("str=[%s], p2 after sscanf: %p \n", str, p2);
67  var_sscanf(str, "%p", (void**)&p3);
68  printf("after var_sscanf(): str=[%s], p3 after sscanf: %p \n", str, p3);
69  printf("sizeof(p3) = %d \n", sizeof(p3));
70 
71  /*
72  bit of a hack but a nice one when sscanf() doesn't work the way it is expected to be.
73  scan the pointer address in string into a long long and in base 16 and reinterpret cast
74  it to void*.
75  */
76  long long ll=strtoll(str, &endptr, 16);
77  void* lltovoidptr= (void*)ll;
78  printf("lltovoidptr = %p, data pointed to should be [%s] = it is [%s]\n", lltovoidptr, p, lltovoidptr);
79  return 0;
80 }
81 
82 void var_sscanf(char *str, const char* fmt, ...)
83 {
84  va_list vargs;
85  va_start(vargs, fmt);
86  vsscanf(str, fmt, vargs);
87  va_end(vargs);
88 }
89