-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpadbin.c
97 lines (81 loc) · 2.81 KB
/
padbin.c
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
/*
"$Id: padbin.c,v 1.2 2005-06-15 16:25:12 wntrmute Exp $"
padbin.c
pad a binary to a given size boundary
Copyright (C) 2002 Damian Yerrick
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
GNU licenses can be viewed online at http://www.gnu.org/copyleft/
In addition, as a special exception, Damian Yerrick gives
permission to link the code of this program with import libraries
distributed as part of the plug-in development tools published by
the vendor of any image manipulation program, and distribute
linked combinations including the two. You must obey the GNU
General Public License in all respects for all of the code used
other than the plug-in interface. If you modify this file, you
may extend this exception to your version of the file, but you
are not obligated to do so. If you do not wish to do so, delete
this exception statement from your version.
Visit http://www.pineight.com/ for more information.
"$Header: /lvm/shared/ds/ds/cvs/devkitpro-cvsbackup/tools/general/padbin.c,v 1.2 2005-06-15 16:25:12 wntrmute Exp $"
*/
#include <stdio.h>
#include <stdlib.h>
char zeroz[1024];
int main(int argc, char **argv)
{
FILE *fp;
unsigned long overage;
unsigned long factor;
/* clear to 0xff for faster flash writing */
for(factor = 0; factor < 1024; factor++)
zeroz[factor] = 0xff;
if(argc != 3)
{
fputs("pads a binary file to an integer multiple of a given number of bytes\n"
"syntax: padbin FACTOR FILE\n"
"FACTOR can be decimal (e.g. 256), octal (e.g. 0400), or hex (e.g. 0x100)\n", stderr);
return 1;
}
factor = strtoul(argv[1], NULL, 0);
if(factor < 2)
{
fputs("error: FACTOR must be greater than or equal to 2\n", stderr);
return 1;
}
fp = fopen(argv[2], "rb+");
if(!fp)
{
fputs("could not open", stderr);
perror(argv[2]);
return 1;
}
/* find the amount the file has over the limit */
fseek(fp, 0, SEEK_END);
overage = ftell(fp) % factor;
if(overage != 0)
{
unsigned long extension = factor - overage;
while(extension >= 1024)
{
fwrite(zeroz, 1, 1024, fp);
extension -= 1024;
}
if(extension >= 0)
{
fwrite(zeroz, 1, extension, fp);
}
}
fclose(fp);
return 0;
}