-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.c
More file actions
169 lines (142 loc) · 3.63 KB
/
misc.c
File metadata and controls
169 lines (142 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* $Id: misc.c,v 1.20 2009/04/13 07:21:42 geni Exp $
*
* Copyright (c) 2005 Huidae Cho
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#define _LIBDLUSB_MISC_C_
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/stat.h>
#include "dlusb.h"
char *
squeeze_spaces(char *buf)
{
int i, l, s;
l = strlen(buf);
for(i = 0; i < l && buf[i] == ' '; i++);
s = i;
for(i = l-1; i >= 0 && buf[i] == ' '; i--);
buf[i+1] = 0;
return buf + s;
}
char *
read_line(FILE *fp)
{
char buf[BUFSIZ], *line;
int i, l;
line = NULL;
l = 0;
do{
if(feof(fp) || fgets(buf, BUFSIZ, fp) == NULL){
if(line)
free(line);
return NULL;
}
i = strlen(buf);
l += i;
line = (char *)realloc(line, l+1);
strncpy(line+l-i, buf, i);
}while(buf[i-1] != '\r' && buf[i-1] != '\n');
l--;
if(line[l-1] == '\r' || line[l-1] == '\n')
l--;
line[l] = 0;
return line;
}
int
read_file(char *file, u8 **data, u16 *len)
{
struct stat sb;
int fd;
if(stat(file, &sb)){
ERROR("%s: stat failed", file);
return -1;
}
*len = sb.st_size;
if((fd = open(file, O_RDONLY)) < 0){
ERROR("%s: open failed", file);
return -1;
}
*data = (u8 *)malloc(*len);
if(read(fd, *data, *len) != *len){
ERROR("%s: read failed", file);
close(fd);
return -1;
}
close(fd);
return 0;
}
int
convert_ch(u8 *fontch, u8 ch)
{
u8 uch;
int space = -1;
u8 i, n;
uch = toupper(ch);
n = strlen((char *)fontch);
for(i = 0; i < n && uch != fontch[i]; i++){
if(fontch[i] == ' ' && space == -1)
space = i;
}
return (i == n ? space : i);
}
void
print_rdm(u8 *msg, u8 msg_len, FILE *fp)
{
int i;
for(i = 0; i < msg_len && msg[i] != DM_SENTINEL; i++)
fprintf(fp, "%c", rdm(msg[i]));
return;
}
int
str_chr(char *str, char ch)
{
char *ptr;
if((ptr = strchr(str, ch)))
return (int)(ptr - str);
return -1;
}
int
str_str(char *big, char *little)
{
return strstr(big, little) == big;
}
int
day_of_week(int y, int m, int d)
{
/* http://www.mathematik.uni-bielefeld.de/~sillke/ALGORITHMS/calendar/weekday.c
*
* Keith & Craver;
* The ultimate perpetual calendar?, JoRM 22:4 (1990) 280-282
* day of the week as a 44 character expression in C. (illegal use of --)
* The following 45 character C expression by Keith is correct.
* dow(y,m,d) { return (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7; }
*
* sunday = 0, ...
*/
return (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7;
}