ioutil: Remove unnecessary casts and fix const-discarding
[openocd.git] / src / helper / ioutil.c
blob55319004b87bd5a554f7ddf46dece9230a9954a2
1 /***************************************************************************
2 * Copyright (C) 2007-2010 by Øyvind Harboe *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
18 ***************************************************************************/
20 /* this file contains various functionality useful to standalone systems */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include "log.h"
27 #include "time_support.h"
29 #ifdef HAVE_ARPA_INET_H
30 #include <arpa/inet.h>
31 #endif
32 #ifdef HAVE_DIRENT_H
33 #include <dirent.h>
34 #endif
35 #ifdef HAVE_NETDB_H
36 #include <netdb.h>
37 #endif
38 #ifdef HAVE_NET_IF_H
39 #include <net/if.h>
40 #endif
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
43 #endif
44 #ifdef HAVE_SYS_STAT_H
45 #include <sys/stat.h>
46 #endif
47 #ifdef HAVE_IFADDRS_H
48 #include <ifaddrs.h>
49 #endif
50 #ifdef HAVE_MALLOC_H
51 #include <malloc.h>
52 #endif
54 /* loads a file and returns a pointer to it in memory. The file contains
55 * a 0 byte(sentinel) after len bytes - the length of the file. */
56 static int loadFile(const char *fileName, char **data, size_t *len)
58 /* ensure returned length is always sane */
59 *len = 0;
61 FILE *pFile;
62 pFile = fopen(fileName, "rb");
63 if (pFile == NULL) {
64 LOG_ERROR("Can't open %s", fileName);
65 return ERROR_FAIL;
67 if (fseek(pFile, 0, SEEK_END) != 0) {
68 LOG_ERROR("Can't open %s", fileName);
69 fclose(pFile);
70 return ERROR_FAIL;
72 long fsize = ftell(pFile);
73 if (fsize == -1) {
74 LOG_ERROR("Can't open %s", fileName);
75 fclose(pFile);
76 return ERROR_FAIL;
78 *len = fsize;
80 if (fseek(pFile, 0, SEEK_SET) != 0) {
81 LOG_ERROR("Can't open %s", fileName);
82 fclose(pFile);
83 return ERROR_FAIL;
85 *data = malloc(*len + 1);
86 if (*data == NULL) {
87 LOG_ERROR("Can't open %s", fileName);
88 fclose(pFile);
89 return ERROR_FAIL;
92 if (fread(*data, 1, *len, pFile) != *len) {
93 fclose(pFile);
94 free(*data);
95 LOG_ERROR("Can't open %s", fileName);
96 return ERROR_FAIL;
98 fclose(pFile);
100 /* 0-byte after buffer (not included in *len) serves as a sentinel */
101 (*data)[*len] = 0;
103 return ERROR_OK;
106 COMMAND_HANDLER(handle_cat_command)
108 if (CMD_ARGC != 1)
109 return ERROR_COMMAND_SYNTAX_ERROR;
111 /* NOTE!!! we only have line printing capability so we print the entire file as a single
112 * line. */
113 char *data;
114 size_t len;
116 int retval = loadFile(CMD_ARGV[0], &data, &len);
117 if (retval == ERROR_OK) {
118 command_print(CMD_CTX, "%s", data);
119 free(data);
120 } else
121 command_print(CMD_CTX, "%s not found", CMD_ARGV[0]);
123 return ERROR_OK;
126 COMMAND_HANDLER(handle_trunc_command)
128 if (CMD_ARGC != 1)
129 return ERROR_COMMAND_SYNTAX_ERROR;
131 FILE *config_file = NULL;
132 config_file = fopen(CMD_ARGV[0], "w");
133 if (config_file != NULL)
134 fclose(config_file);
136 return ERROR_OK;
139 #ifdef HAVE_MALLOC_H
140 COMMAND_HANDLER(handle_meminfo_command)
142 static int prev;
143 struct mallinfo info;
145 if (CMD_ARGC != 0)
146 return ERROR_COMMAND_SYNTAX_ERROR;
148 info = mallinfo();
150 if (prev > 0)
151 command_print(CMD_CTX, "Diff: %d", prev - info.fordblks);
152 prev = info.fordblks;
154 command_print(CMD_CTX, "Available ram: %d", info.fordblks);
156 return ERROR_OK;
158 #endif
160 COMMAND_HANDLER(handle_append_command)
162 if (CMD_ARGC < 1)
163 return ERROR_COMMAND_SYNTAX_ERROR;
165 int retval = ERROR_FAIL;
166 FILE *config_file = NULL;
168 config_file = fopen(CMD_ARGV[0], "a");
169 if (config_file != NULL) {
170 fseek(config_file, 0, SEEK_END);
172 unsigned i;
173 for (i = 1; i < CMD_ARGC; i++) {
174 if (fwrite(CMD_ARGV[i], 1, strlen(CMD_ARGV[i]),
175 config_file) != strlen(CMD_ARGV[i]))
176 break;
177 if (i != CMD_ARGC - 1) {
178 if (fwrite(" ", 1, 1, config_file) != 1)
179 break;
182 if ((i == CMD_ARGC) && (fwrite("\n", 1, 1, config_file) == 1))
183 retval = ERROR_OK;
185 fclose(config_file);
188 return retval;
191 COMMAND_HANDLER(handle_cp_command)
193 if (CMD_ARGC != 2)
194 return ERROR_COMMAND_SYNTAX_ERROR;
196 /* NOTE!!! we only have line printing capability so we print the entire file as a single
197 * line. */
198 char *data;
199 size_t len;
201 int retval = loadFile(CMD_ARGV[0], &data, &len);
202 if (retval != ERROR_OK)
203 return retval;
205 FILE *f = fopen(CMD_ARGV[1], "wb");
206 if (f == NULL)
207 retval = ERROR_COMMAND_SYNTAX_ERROR;
209 size_t pos = 0;
210 for (;; ) {
211 size_t chunk = len - pos;
212 static const size_t maxChunk = 512 * 1024; /* ~1/sec */
213 if (chunk > maxChunk)
214 chunk = maxChunk;
216 if ((retval == ERROR_OK) && (fwrite(data + pos, 1, chunk, f) != chunk))
217 retval = ERROR_COMMAND_SYNTAX_ERROR;
219 if (retval != ERROR_OK)
220 break;
222 command_print(CMD_CTX, "%zu", len - pos);
224 pos += chunk;
226 if (pos == len)
227 break;
230 if (retval == ERROR_OK)
231 command_print(CMD_CTX, "Copied %s to %s", CMD_ARGV[0], CMD_ARGV[1]);
232 else
233 command_print(CMD_CTX, "copy failed");
235 if (data != NULL)
236 free(data);
237 if (f != NULL)
238 fclose(f);
240 if (retval != ERROR_OK)
241 unlink(CMD_ARGV[1]);
243 return retval;
246 #define SHOW_RESULT(a, b) LOG_ERROR(# a " failed %d\n", (int)b)
248 #define IOSIZE 512
249 void copyfile(char *name2, char *name1)
252 int err;
253 char buf[IOSIZE];
254 int fd1, fd2;
255 ssize_t done, wrote;
257 fd1 = open(name1, O_WRONLY | O_CREAT, 0664);
258 if (fd1 < 0)
259 SHOW_RESULT(open, fd1);
261 fd2 = open(name2, O_RDONLY);
262 if (fd2 < 0)
263 SHOW_RESULT(open, fd2);
265 for (;; ) {
266 done = read(fd2, buf, IOSIZE);
267 if (done < 0) {
268 SHOW_RESULT(read, done);
269 break;
272 if (done == 0)
273 break;
275 wrote = write(fd1, buf, done);
276 if (wrote != done)
277 SHOW_RESULT(write, wrote);
279 if (wrote != done)
280 break;
283 err = close(fd1);
284 if (err < 0)
285 SHOW_RESULT(close, err);
287 err = close(fd2);
288 if (err < 0)
289 SHOW_RESULT(close, err);
292 /* utility fn to copy a directory */
293 void copydir(char *name, char *destdir)
295 int err;
296 DIR *dirp;
298 dirp = opendir(destdir);
299 if (dirp == NULL)
300 mkdir(destdir, 0777);
301 else
302 err = closedir(dirp);
304 dirp = opendir(name);
305 if (dirp == NULL)
306 SHOW_RESULT(opendir, -1);
308 for (;; ) {
309 struct dirent *entry = readdir(dirp);
311 if (entry == NULL)
312 break;
314 if (strcmp(entry->d_name, ".") == 0)
315 continue;
316 if (strcmp(entry->d_name, "..") == 0)
317 continue;
319 int isDir = 0;
320 struct stat buf;
321 char fullPath[PATH_MAX];
322 strncpy(fullPath, name, PATH_MAX);
323 strcat(fullPath, "/");
324 strncat(fullPath, entry->d_name, PATH_MAX - strlen(fullPath));
326 if (stat(fullPath, &buf) == -1) {
327 LOG_ERROR("unable to read status from %s", fullPath);
328 break;
330 isDir = S_ISDIR(buf.st_mode) != 0;
332 if (isDir)
333 continue;
335 /* diag_printf("<INFO>: entry %14s",entry->d_name); */
336 char fullname[PATH_MAX];
337 char fullname2[PATH_MAX];
339 strcpy(fullname, name);
340 strcat(fullname, "/");
341 strcat(fullname, entry->d_name);
343 strcpy(fullname2, destdir);
344 strcat(fullname2, "/");
345 strcat(fullname2, entry->d_name);
346 /* diag_printf("from %s to %s\n", fullname, fullname2); */
347 copyfile(fullname, fullname2);
349 /* diag_printf("\n"); */
352 err = closedir(dirp);
353 if (err < 0)
354 SHOW_RESULT(stat, err);
357 COMMAND_HANDLER(handle_rm_command)
359 if (CMD_ARGC != 1)
360 return ERROR_COMMAND_SYNTAX_ERROR;
362 bool del = false;
363 if (rmdir(CMD_ARGV[0]) == 0)
364 del = true;
365 else if (unlink(CMD_ARGV[0]) == 0)
366 del = true;
368 return del ? ERROR_OK : ERROR_FAIL;
371 static int ioutil_Jim_Command_ls(Jim_Interp *interp,
372 int argc,
373 Jim_Obj * const *argv)
375 if (argc != 2) {
376 Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?");
377 return JIM_ERR;
380 const char *name = Jim_GetString(argv[1], NULL);
382 DIR *dirp = NULL;
383 dirp = opendir(name);
384 if (dirp == NULL)
385 return JIM_ERR;
386 Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);
388 for (;; ) {
389 struct dirent *entry = NULL;
390 entry = readdir(dirp);
391 if (entry == NULL)
392 break;
394 if ((strcmp(".", entry->d_name) == 0) || (strcmp("..", entry->d_name) == 0))
395 continue;
397 Jim_ListAppendElement(interp, objPtr,
398 Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name)));
400 closedir(dirp);
402 Jim_SetResult(interp, objPtr);
404 return JIM_OK;
407 static int ioutil_Jim_Command_peek(Jim_Interp *interp,
408 int argc,
409 Jim_Obj *const *argv)
411 if (argc != 2) {
412 Jim_WrongNumArgs(interp, 1, argv, "peek ?address?");
413 return JIM_ERR;
416 long address;
417 if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
418 return JIM_ERR;
420 int value = *((volatile int *) address);
422 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
424 return JIM_OK;
427 static int ioutil_Jim_Command_poke(Jim_Interp *interp,
428 int argc,
429 Jim_Obj *const *argv)
431 if (argc != 3) {
432 Jim_WrongNumArgs(interp, 1, argv, "poke ?address? ?value?");
433 return JIM_ERR;
436 long address;
437 if (Jim_GetLong(interp, argv[1], &address) != JIM_OK)
438 return JIM_ERR;
439 long value;
440 if (Jim_GetLong(interp, argv[2], &value) != JIM_OK)
441 return JIM_ERR;
443 *((volatile int *) address) = value;
445 return JIM_OK;
448 /* not so pretty code to fish out ip number*/
449 static int ioutil_Jim_Command_ip(Jim_Interp *interp, int argc,
450 Jim_Obj *const *argv)
452 #if !defined(__CYGWIN__)
453 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
455 struct ifaddrs *ifa = NULL, *ifp = NULL;
457 if (getifaddrs(&ifp) < 0)
458 return JIM_ERR;
460 for (ifa = ifp; ifa; ifa = ifa->ifa_next) {
461 char ip[200];
462 socklen_t salen;
464 if (ifa->ifa_addr->sa_family == AF_INET)
465 salen = sizeof(struct sockaddr_in);
466 else if (ifa->ifa_addr->sa_family == AF_INET6)
467 salen = sizeof(struct sockaddr_in6);
468 else
469 continue;
471 if (getnameinfo(ifa->ifa_addr, salen, ip, sizeof(ip), NULL, 0,
472 NI_NUMERICHOST) < 0)
473 continue;
475 Jim_AppendString(interp, tclOutput, ip, strlen(ip));
476 break;
480 freeifaddrs(ifp);
481 #else
482 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "fixme!!!", 0);
483 LOG_ERROR("NOT IMPLEMENTED!!!");
484 #endif
485 Jim_SetResult(interp, tclOutput);
487 return JIM_OK;
490 #ifdef HAVE_SYS_IOCTL_H
491 #ifdef SIOCGIFHWADDR
492 /* not so pretty code to fish out eth0 mac address */
493 static int ioutil_Jim_Command_mac(Jim_Interp *interp, int argc,
494 Jim_Obj *const *argv)
496 struct ifreq *ifr, *ifend;
497 struct ifreq ifreq;
498 struct ifconf ifc;
499 struct ifreq ifs[5];
500 int SockFD;
502 SockFD = socket(AF_INET, SOCK_DGRAM, 0);
503 if (SockFD < 0)
504 return JIM_ERR;
506 ifc.ifc_len = sizeof(ifs);
507 ifc.ifc_req = ifs;
508 if (ioctl(SockFD, SIOCGIFCONF, &ifc) < 0) {
509 close(SockFD);
510 return JIM_ERR;
513 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
514 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
515 /* if (ifr->ifr_addr.sa_family == AF_INET) */
517 if (strcmp("eth0", ifr->ifr_name) != 0)
518 continue;
519 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
520 if (ioctl(SockFD, SIOCGIFHWADDR, &ifreq) < 0) {
521 close(SockFD);
522 return JIM_ERR;
525 close(SockFD);
527 Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
529 char buffer[256];
530 sprintf(buffer, "%02x-%02x-%02x-%02x-%02x-%02x",
531 ifreq.ifr_hwaddr.sa_data[0]&0xff,
532 ifreq.ifr_hwaddr.sa_data[1]&0xff,
533 ifreq.ifr_hwaddr.sa_data[2]&0xff,
534 ifreq.ifr_hwaddr.sa_data[3]&0xff,
535 ifreq.ifr_hwaddr.sa_data[4]&0xff,
536 ifreq.ifr_hwaddr.sa_data[5]&0xff);
538 Jim_AppendString(interp, tclOutput, buffer, strlen(buffer));
540 Jim_SetResult(interp, tclOutput);
542 return JIM_OK;
545 close(SockFD);
547 return JIM_ERR;
550 #endif
551 #endif
553 static const struct command_registration ioutil_command_handlers[] = {
555 .name = "cat",
556 .handler = handle_cat_command,
557 .mode = COMMAND_ANY,
558 .help = "display text file content",
559 .usage = "file_name",
562 .name = "trunc",
563 .handler = handle_trunc_command,
564 .mode = COMMAND_ANY,
565 .help = "truncate a file to zero length",
566 .usage = "file_name",
569 .name = "cp",
570 .handler = handle_cp_command,
571 .mode = COMMAND_ANY,
572 .help = "copy a file",
573 .usage = "src_file_name dst_file_name",
576 .name = "append_file",
577 .handler = handle_append_command,
578 .mode = COMMAND_ANY,
579 .help = "append a variable number of strings to a file",
580 .usage = "file_name [<string1>, [<string2>, ...]]",
582 #ifdef HAVE_MALLOC_H
584 .name = "meminfo",
585 .handler = handle_meminfo_command,
586 .mode = COMMAND_ANY,
587 .help = "display free heap space",
589 #endif
591 .name = "rm",
592 .mode = COMMAND_ANY,
593 .handler = handle_rm_command,
594 .help = "remove a directory or file",
595 .usage = "file_name",
599 * Peek and poke are security holes -- they manipulate
600 * server-internal addresses.
603 /* jim handlers */
605 .name = "peek",
606 .mode = COMMAND_ANY,
607 .jim_handler = ioutil_Jim_Command_peek,
608 .help = "peek at a memory address",
609 .usage = "address",
612 .name = "poke",
613 .mode = COMMAND_ANY,
614 .jim_handler = ioutil_Jim_Command_poke,
615 .help = "poke at a memory address",
616 .usage = "address value",
619 .name = "ls",
620 .mode = COMMAND_ANY,
621 .jim_handler = ioutil_Jim_Command_ls,
622 .help = "show a listing of files",
623 .usage = "dirname",
625 #ifdef HAVE_SYS_IOCTL_H
626 #ifdef SIOCGIFHWADDR
628 .name = "mac",
629 .mode = COMMAND_ANY,
630 .jim_handler = ioutil_Jim_Command_mac,
631 .help = "show MAC address",
633 #endif
634 #endif
636 .name = "ip",
637 .jim_handler = ioutil_Jim_Command_ip,
638 .mode = COMMAND_ANY,
639 .help = "show IP address",
641 COMMAND_REGISTRATION_DONE
644 int ioutil_init(struct command_context *cmd_ctx)
646 return register_commands(cmd_ctx, NULL, ioutil_command_handlers);