source: mainline/uspace/app/bdsh/cmds/modules/mkfile/mkfile.c@ 286286c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 286286c was 36ab7c7, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Standardize formatting of copyright headers in Bdsh and add some that were
missing.

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (c) 2009 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <dirent.h>
32#include <fcntl.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <fcntl.h>
36#include <macros.h>
37#include <getopt.h>
38#include <stdarg.h>
39#include <str.h>
40#include <ctype.h>
41
42#include "config.h"
43#include "errors.h"
44#include "util.h"
45#include "entry.h"
46#include "mkfile.h"
47#include "cmds.h"
48
49/** Number of bytes to write at a time */
50#define BUFFER_SIZE 16384
51
52static const char *cmdname = "mkfile";
53
54static struct option const long_options[] = {
55 {"size", required_argument, 0, 's'},
56 {"help", no_argument, 0, 'h'},
57 {0, 0, 0, 0}
58};
59
60void help_cmd_mkfile(unsigned int level)
61{
62 if (level == HELP_SHORT) {
63 printf("`%s' creates a new zero-filled file\n", cmdname);
64 } else {
65 help_cmd_mkfile(HELP_SHORT);
66 printf(
67 "Usage: %s [options] <path>\n"
68 "Options:\n"
69 " -h, --help A short option summary\n"
70 " -s, --size sz Size of the file\n"
71 "\n"
72 "Size is a number followed by 'k', 'm' or 'g' for kB, MB, GB.\n"
73 "E.g. 100k, 2m, 1g.\n",
74 cmdname);
75 }
76
77 return;
78}
79
80/** Parse size specification.
81 *
82 * Size specification is in the form <decimal_number><unit> where
83 * <unit> is 'k', 'm' or 'g' for kB, MB, GB.
84 *
85 * @param str String containing the size specification.
86 * @return Non-negative size in bytes on success, -1 on failure.
87 */
88static ssize_t read_size(const char *str)
89{
90 ssize_t number, unit;
91 char *ep;
92
93 number = strtol(str, &ep, 10);
94 if (ep[0] == '\0')
95 return number;
96
97 if (ep[1] != '\0')
98 return -1;
99
100 switch (tolower(ep[0])) {
101 case 'k': unit = 1024; break;
102 case 'm': unit = 1024*1024; break;
103 case 'g': unit = 1024*1024*1024; break;
104 default: return -1;
105 }
106
107 return number * unit;
108}
109
110int cmd_mkfile(char **argv)
111{
112 unsigned int argc;
113 int c, opt_ind;
114 int fd;
115 ssize_t file_size;
116 ssize_t total_written;
117 ssize_t to_write, rc;
118 char *file_name;
119 void *buffer;
120
121 file_size = 0;
122
123 argc = cli_count_args(argv);
124
125 for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
126 c = getopt_long(argc, argv, "s:h", long_options, &opt_ind);
127 switch (c) {
128 case 'h':
129 help_cmd_mkfile(HELP_LONG);
130 return CMD_SUCCESS;
131 case 's':
132 file_size = read_size(optarg);
133 if (file_size < 0) {
134 printf("%s: Invalid file size specification.\n",
135 cmdname);
136 return CMD_FAILURE;
137 }
138 break;
139 }
140 }
141
142 argc -= optind;
143
144 if (argc != 1) {
145 printf("%s: incorrect number of arguments. Try `%s --help'\n",
146 cmdname, cmdname);
147 return CMD_FAILURE;
148 }
149
150 file_name = argv[optind];
151
152 fd = open(file_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
153 if (fd < 0) {
154 printf("%s: failed to create file %s.\n", cmdname, file_name);
155 return CMD_FAILURE;
156 }
157
158 buffer = calloc(BUFFER_SIZE, 1);
159 if (buffer == NULL) {
160 printf("%s: Error, out of memory.\n", cmdname);
161 return CMD_FAILURE;
162 }
163
164 total_written = 0;
165 while (total_written < file_size) {
166 to_write = min(file_size - total_written, BUFFER_SIZE);
167 rc = write(fd, buffer, to_write);
168 if (rc <= 0) {
169 printf("%s: Error writing file (%zd).\n", cmdname, rc);
170 close(fd);
171 return CMD_FAILURE;
172 }
173 total_written += rc;
174 }
175
176 rc = close(fd);
177 if (rc != 0) {
178 printf("%s: Error writing file (%zd).\n", cmdname, rc);
179 return CMD_FAILURE;
180 }
181
182 free(buffer);
183
184 return CMD_SUCCESS;
185}
Note: See TracBrowser for help on using the repository browser.