This commit was manufactured by cvs2svn to create tag
[gnokii.git] / common / misc.c
1 /*
2
3   G N O K I I
4
5   A Linux/Unix toolset and driver for Nokia mobile phones.
6
7   Released under the terms of the GNU GPL, see file COPYING for more details.
8
9 */
10
11 #include <string.h>\r
12 #include <ctype.h>\r
13 #include <time.h>\r
14
15 #ifndef WIN32
16   #include <sys/types.h>
17   #include <sys/stat.h>
18   #include <stdlib.h>
19   #include <fcntl.h>
20   #include <signal.h>
21   #include <unistd.h>
22   #include <errno.h>
23 #endif\r
24 \r
25 #include "misc.h"\r
26 #include "gsm-common.h"\r
27
28 #ifndef HAVE_TIMEOPS
29
30 /* FIXME: I have timersub defined in sys/time.h :-( PJ
31    FIXME: Jano wants this function too... PJ
32
33 int timersub(struct timeval *a, struct timeval *b, struct timeval *result) {
34   do {
35     (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;
36     (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;
37     if ((result)->tv_usec < 0) {
38       --(result)->tv_sec;
39       (result)->tv_usec += 1000000;
40     }
41   } while (0);
42 }
43 */
44
45 #endif
46
47 int GetLine(FILE *File, char *Line, int count) {
48
49   char *ptr;
50
51   if (fgets(Line, count, File)) {
52     ptr=Line+strlen(Line)-1;
53
54     while ( (*ptr == '\n' || *ptr == '\r') && ptr>=Line) *ptr--='\0';
55
56     return strlen(Line);
57   } else return -1;
58 }
59
60 /*
61  * like atoi, but of a non-null-terminated string of a specified portion
62  */
63 int mem_to_int(const char str[], int len)
64 {
65   char aux[81];
66
67   strncpy(aux, str, len);
68   aux[len]=0;
69   return( atoi(aux) );
70
71
72 /*
73  * make hexdump of Message
74  */
75 #ifdef DEBUG
76 void hexdump(u16 MessageLength, u8 *MessageBuffer)
77 {
78  
79   int count;
80   int n=0;
81   char string1[80]="";
82   char string2[80]="";
83   char hex1[10];
84   char hex2[10];
85  
86   for (count = 0; count < MessageLength; count ++)
87   {
88     n++;
89
90     switch (MessageBuffer[count]) {
91       case 0x09:
92         sprintf(hex1,"%02x  ",MessageBuffer[count]);
93         strcpy(hex2,".");
94         break;
95       default:
96         if (isprint(MessageBuffer[count]))
97           sprintf(hex1,"%02x%c ",MessageBuffer[count],MessageBuffer[count]);
98         else
99           sprintf(hex1,"%02x  ",MessageBuffer[count]);
100
101         if (isprint(MessageBuffer[count])) sprintf(hex2,"%c",MessageBuffer[count]);
102                                       else strcpy(hex2,".");
103         break;
104     }
105
106     if ( n!=15 && count != MessageLength-1 ) hex1[3]='|';
107  
108     strcat(string1,hex1);
109     strcat(string2,hex2);
110  
111     if ( n==15 || count == MessageLength-1 )
112     {      
113       fprintf(stdout,"%-60s%03x %s\n",string1,count+1,string2);
114       strcpy(string1,"");
115       strcpy(string2,"");
116       n=0;
117     }
118   }//for count
119
120   if (n!=0) fprintf (stdout,_("\n")); 
121  
122   fflush(stdout);
123 }
124
125 void txhexdump(u16 MessageLength, u8 *MessageBuffer)
126
127   int count;
128   int n=0;
129  
130   for (count = 0; count < MessageLength; count ++)
131    {
132     n++;
133     fprintf(stdout,_("%02x"),MessageBuffer[count]);
134     switch (MessageBuffer[count]) {
135       case 0x09:
136         fprintf(stdout,_(" |"));
137         break;
138       default:
139         if (isprint(MessageBuffer[count])) fprintf(stdout, _("%c|"),MessageBuffer[count]);
140                                       else fprintf(stdout,_(" |"));
141         break;
142     }
143
144     if (n==18)
145     { 
146       fprintf (stdout,_("\n"));
147       n=0;
148     }
149    }//for count
150
151   if (n!=0) fprintf (stdout,_("\n")); 
152
153   fflush(stdout);
154 }
155 #endif