VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/VBoxBs2Linker.cpp@ 58679

Last change on this file since 58679 was 58153, checked in by vboxsync, 9 years ago

cppcheck warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
1/* $Id: VBoxBs2Linker.cpp 58153 2015-10-09 13:42:14Z vboxsync $ */
2/** @file
3 * VirtualBox Validation Kit - Boot Sector 2 "linker".
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include <iprt/types.h>
35
36
37int main(int argc, char **argv)
38{
39 const char *pszOutput = NULL;
40 const char **papszInputs = (const char **)calloc(argc, sizeof(const char *));
41 unsigned cInputs = 0;
42
43 /*
44 * Scan the arguments.
45 */
46 for (int i = 1; i < argc; i++)
47 {
48 if (argv[i][0] == '-')
49 {
50 const char *pszOpt = &argv[i][1];
51 if (*pszOpt == '-')
52 {
53 /* Convert long options to short ones. */
54 pszOpt--;
55 if (!strcmp(pszOpt, "--output"))
56 pszOpt = "o";
57 else if (!strcmp(pszOpt, "--version"))
58 pszOpt = "V";
59 else if (!strcmp(pszOpt, "--help"))
60 pszOpt = "h";
61 else
62 {
63 fprintf(stderr, "syntax errro: Unknown options '%s'\n", pszOpt);
64 free(papszInputs);
65 return 2;
66 }
67 }
68
69 /* Process the list of short options. */
70 while (*pszOpt)
71 {
72 switch (*pszOpt++)
73 {
74 case 'o':
75 {
76 const char *pszValue = pszOpt;
77 pszOpt = strchr(pszOpt, '\0');
78 if (*pszValue == '=')
79 pszValue++;
80 else if (!*pszValue)
81 {
82 if (i + 1 >= argc)
83 {
84 fprintf(stderr, "syntax error: The --output option expects a filename.\n");
85 return 12;
86 }
87 pszValue = argv[++i];
88 }
89 if (pszOutput)
90 {
91 fprintf(stderr, "Only one output file is allowed. You've specified '%s' and '%s'\n",
92 pszOutput, pszValue);
93 return 2;
94 }
95 pszOutput = pszValue;
96 pszOpt = "";
97 break;
98 }
99
100 case 'V':
101 printf("%s\n", "$Revision: 58153 $");
102 return 0;
103
104 case '?':
105 case 'h':
106 printf("usage: %s [options] -o <output> <input1> [input2 ... [inputN]]\n",
107 argv[0]);
108 return 0;
109 }
110 }
111 }
112 else
113 papszInputs[cInputs++] = argv[i];
114 }
115
116 if (!pszOutput)
117 {
118 fprintf(stderr, "syntax error: No output file was specified (-o or --output).\n");
119 return 2;
120 }
121 if (cInputs == 0)
122 {
123 fprintf(stderr, "syntax error: No input files was specified.\n");
124 return 2;
125 }
126
127
128 /*
129 * Do the job.
130 */
131 /* Open the output file. */
132#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
133 FILE *pOutput = fopen(pszOutput, "wb");
134#else
135 FILE *pOutput = fopen(pszOutput, "w");
136#endif
137 if (!pOutput)
138 {
139 fprintf(stderr, "error: Failed to open output file '%s' for writing\n", pszOutput);
140 return 1;
141 }
142
143 /* Copy the input files to the output file, with sector padding applied. */
144 int rcExit = 0;
145 size_t off = 0;
146 for (unsigned i = 0; i < cInputs && rcExit == 0; i++)
147 {
148#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
149 FILE *pInput = fopen(papszInputs[i], "rb");
150#else
151 FILE *pInput = fopen(papszInputs[i], "r");
152#endif
153 if (pInput)
154 {
155 for (;;)
156 {
157 /* Read a block from the input file. */
158 uint8_t abBuf[4096];
159 size_t cbRead = fread(abBuf, sizeof(uint8_t), 4096, pInput);
160 if (!cbRead || ferror(pInput))
161 break;
162
163 /* Padd the end of the file if necessary. */
164 if (cbRead != 4096 && !feof(pInput))
165 {
166 fprintf(stderr, "error: fread returned %u bytes, but we're not at the end of the file yet...\n",
167 (unsigned)cbRead);
168 rcExit = 1;
169 break;
170 }
171 if ((cbRead & 0x1ff) != 0)
172 {
173 memset(&abBuf[cbRead], 0, 4096 - cbRead);
174 cbRead = (cbRead + 0x1ff) & ~0x1ffU;
175 }
176
177 /* Write the block to the output file. */
178 if (fwrite(abBuf, sizeof(uint8_t), cbRead, pOutput) == cbRead)
179 off += cbRead;
180 else
181 {
182 fprintf(stderr, "error: fwrite failed\n");
183 rcExit = 1;
184 break;
185 }
186 }
187
188 if (ferror(pInput))
189 {
190 fprintf(stderr, "error: Error reading '%s'.\n", papszInputs[i]);
191 rcExit = 1;
192 }
193 fclose(pInput);
194 }
195 else
196 {
197 fprintf(stderr, "error: Failed to open '%s' for reading.\n", papszInputs[i]);
198 rcExit = 1;
199 }
200 }
201
202 /* Finally, close the output file (can fail because of buffered data). */
203 if (fclose(stderr) != 0)
204 {
205 fprintf(stderr, "error: Error closing '%s'.\n", pszOutput);
206 rcExit = 1;
207 }
208
209 fclose(pOutput);
210 return rcExit;
211}
212
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette