char *archdep_tmpnam(void) { #ifdef HAVE_MKSTEMP char *tmp_name; const char mkstemp_template[] = "/vice.XXXXXX"; int fd; char *tmp; char *final_name; tmp_name = lib_malloc(ioutil_maxpathlen()); if ((tmp = getenv("TMPDIR")) != NULL) { strncpy(tmp_name, tmp, ioutil_maxpathlen()); tmp_name[ioutil_maxpathlen() - sizeof(mkstemp_template)] = '\0'; } else { strcpy(tmp_name, "/tmp"); } strcat(tmp_name, mkstemp_template); if ((fd = mkstemp(tmp_name)) < 0) { tmp_name[0] = '\0'; } else { close(fd); } final_name = lib_stralloc(tmp_name); lib_free(tmp_name); return final_name; #else return lib_stralloc(tmpnam(NULL)); #endif }
static int archdep_init_extra(int *argc, char **argv) { ssize_t read; #if !defined(USE_PROC_SELF_EXE) /* first try to get exe name from argv[0] */ if (argv[0]) { argv0 = lib_stralloc(argv[0]); return 0; } #endif argv0 = lib_malloc(ioutil_maxpathlen()); read = readlink("/proc/self/exe", argv0, ioutil_maxpathlen() - 1); if (read == -1) { return 1; } else { argv0[read] = '\0'; } /* FIXME: bad name of define, this probably should not be here either */ #ifdef USE_PROC_SELF_EXE /* set this up now to remove extra .vice directory */ archdep_pref_path = archdep_boot_path(); #endif return 0; }
static UI_CALLBACK(change_working_directory) { char *wd; int len; len = ioutil_maxpathlen(); wd = lib_malloc(len); ioutil_getcwd(wd, len); vsync_suspend_speed_eval(); #ifdef USE_GNOMEUI if (ui_change_dir(_("VICE setting"), _("Change current working directory"), wd, len) == UI_BUTTON_OK) { if (ioutil_chdir(wd) < 0) ui_error(_("Directory not found")); } #else if (ui_input_string(_("VICE setting"), _("Change current working directory"), wd, len) == UI_BUTTON_OK) { if (ioutil_chdir(wd) < 0) ui_error(_("Directory not found")); } #endif lib_free(wd); }
char *archdep_tmpnam(void) { #if defined(GP2X) || defined(WIZ) static unsigned int tmp_string_counter = 0; char tmp_string[32]; sprintf(tmp_string, "vice%d.tmp", tmp_string_counter++); return lib_stralloc(tmp_string); #else #ifdef HAVE_MKSTEMP char *tmp_name; const char mkstemp_template[] = "/vice.XXXXXX"; int fd; char *tmp; char *final_name; tmp_name = lib_malloc(ioutil_maxpathlen()); if ((tmp = getenv("TMPDIR")) != NULL) { strncpy(tmp_name, tmp, ioutil_maxpathlen()); tmp_name[ioutil_maxpathlen() - sizeof(mkstemp_template)] = '\0'; } else { strcpy(tmp_name, "/tmp"); } strcat(tmp_name, mkstemp_template); if ((fd = mkstemp(tmp_name)) < 0) { tmp_name[0] = '\0'; } else { close(fd); } final_name = lib_stralloc(tmp_name); lib_free(tmp_name); return final_name; #else return lib_stralloc(tmpnam(NULL)); #endif #endif }
static UI_CALLBACK(dump_keymap) { char *wd; int len; len = ioutil_maxpathlen(); wd = lib_malloc(len); ioutil_getcwd(wd, len); if (uilib_input_file(_("VICE setting"), _("Write to Keymap File:"), wd, len) == UI_BUTTON_OK) { if (keyboard_keymap_dump(wd) < 0) { ui_error(strerror(errno)); } } lib_free(wd); }
int fsdevice_flush_write_byte(vdrive_t *vdrive, BYTE data) { unsigned int dnr; int rc; dnr = vdrive->unit - 8; rc = SERIAL_OK; /* FIXME: Consider the real size of the input buffer. */ if (fsdevice_dev[dnr].cptr < ioutil_maxpathlen() - 1) { fsdevice_dev[dnr].cmdbuf[(fsdevice_dev[dnr].cptr)++] = data; rc = SERIAL_OK; } else { fsdevice_error(vdrive, CBMDOS_IPE_LONG_LINE); rc = SERIAL_ERROR; } return rc; }
static UI_CALLBACK(events_select_dir) { char *wd; unsigned int i, is_dir; int len; len = ioutil_maxpathlen(); wd = lib_malloc(len); ioutil_getcwd(wd, len); vsync_suspend_speed_eval(); if (ui_input_string(_("VICE setting"), _("Select history directory"), wd, len) == UI_BUTTON_OK) { ioutil_stat(wd, &i, &is_dir); if (!is_dir) ui_error(_("Directory not found")); else resources_set_string("EventSnapshotDir", wd); } lib_free(wd); }
void fsdevice_flush(vdrive_t *vdrive, unsigned int secondary) { unsigned int dnr; char *cmd, *realarg, *arg; char *cbmcmd; int er = CBMDOS_IPE_SYNTAX; dnr = vdrive->unit - 8; if ((secondary != 15) || (!(fsdevice_dev[dnr].cptr))) { return; } cbmcmd = lib_malloc(ioutil_maxpathlen()); /* FIXME: Use `vdrive_command_parse()'! */ /* remove trailing cr */ while (fsdevice_dev[dnr].cptr && (fsdevice_dev[dnr].cmdbuf[fsdevice_dev[dnr].cptr - 1] == 13)) { (fsdevice_dev[dnr].cptr)--; } fsdevice_dev[dnr].cmdbuf[fsdevice_dev[dnr].cptr] = 0; strcpy(cbmcmd, (char *)(fsdevice_dev[dnr].cmdbuf)); charset_petconvstring((BYTE *)cbmcmd, 1); /* CBM name to FSname */ cmd = cbmcmd; while (*cmd == ' ') { cmd++; } arg = strchr(cbmcmd, ':'); if (arg != NULL) { *arg++ = '\0'; } realarg = strchr((char *)(fsdevice_dev[dnr].cmdbuf), ':'); if (realarg != NULL) { *realarg++ = '\0'; } /* i v n r s c b-r chn drv trk sec u1 chn drv trk sec b-w chn drv trk sec u2 chn drv trk sec b-p chn pos b-a drv trk sec b-f drv trk sec b-e chn drv trk sec m-r lo hi len m-w lo hi len <data> m-e lo hi u9/ui switch mode u:/uj reset cd cd_ / md rd */ if (!strncmp((char *)(fsdevice_dev[dnr].cmdbuf), "M-R", 3)) { er = fsdevice_flush_mr(vdrive, realarg); } else if (!strncmp((char *)(fsdevice_dev[dnr].cmdbuf), "M-W", 3)) { er = fsdevice_flush_mw(vdrive, realarg); } else if (!strncmp((char *)(fsdevice_dev[dnr].cmdbuf), "M-E", 3)) { er = fsdevice_flush_me(vdrive, realarg); } else if (!strcmp(cmd, "u1")) { er = fsdevice_flush_u1(vdrive, realarg); } else if (!strcmp(cmd, "u2")) { er = fsdevice_flush_u2(vdrive, realarg); } else if (!strncmp((char *)(fsdevice_dev[dnr].cmdbuf), "B-A", 3)) { er = fsdevice_flush_ba(vdrive, realarg); } else if (!strncmp((char *)(fsdevice_dev[dnr].cmdbuf), "B-F", 3)) { er = fsdevice_flush_bf(vdrive, realarg); } else if (!strncmp((char *)(fsdevice_dev[dnr].cmdbuf), "B-R", 3)) { er = fsdevice_flush_br(vdrive, realarg); } else if (!strncmp((char *)(fsdevice_dev[dnr].cmdbuf), "B-W", 3)) { er = fsdevice_flush_bw(vdrive, realarg); } else if (!strncmp((char *)(fsdevice_dev[dnr].cmdbuf), "B-P", 3)) { er = fsdevice_flush_bp(vdrive, realarg); } else if (!strncmp((char *)(fsdevice_dev[dnr].cmdbuf), "B-E", 3)) { er = fsdevice_flush_be(vdrive, realarg); } else if (!strcmp(cmd, "cd")) { er = fsdevice_flush_cd(vdrive, arg); } else if (!strcmp((char *)(fsdevice_dev[dnr].cmdbuf), "CD_")) { er = fsdevice_flush_cdup(vdrive); } else if (*cmd == '/') { er = fsdevice_flush_partition(vdrive, arg); } else if (!strcmp(cmd, "md")) { er = fsdevice_flush_mkdir(arg); } else if (!strcmp(cmd, "rd")) { er = fsdevice_flush_remove(arg); } else if ((!strcmp(cmd, "ui")) || (!strcmp(cmd, "u9"))) { er = fsdevice_flush_reset(); } else if ((!strcmp(cmd, "uj")) || (!strcmp(cmd, "u:"))) { er = fsdevice_flush_reset(); } else if (*cmd == 'i') { /* additional args for I are ignored */ er = fsdevice_flush_initialize(vdrive); } else if (*cmd == 'v') { /* additional args for V are ignored */ er = fsdevice_flush_validate(vdrive); } else if (*cmd == 'n' && arg != NULL) { er = fsdevice_flush_new(vdrive, realarg); } else if (*cmd == 'r' && arg != NULL) { er = fsdevice_flush_rename(vdrive, realarg); } else if (*cmd == 's' && arg != NULL) { er = fsdevice_flush_scratch(vdrive, realarg); } fsdevice_error(vdrive, er); fsdevice_dev[dnr].cptr = 0; lib_free(cbmcmd); }
static void command_directory_get(vdrive_t *vdrive, bufinfo_t *bufinfo, BYTE *data, unsigned int secondary) { int i, l, f, statrc; unsigned int blocks; char *direntry; unsigned int filelen, isdir; fileio_info_t *finfo = NULL; unsigned int format = 0; char *buf; buf = (char *)lib_malloc(ioutil_maxpathlen()); bufinfo->bufp = bufinfo->name; if (fsdevice_convert_p00_enabled[(vdrive->unit) - 8]) format |= FILEIO_FORMAT_P00; if (!fsdevice_hide_cbm_files_enabled[vdrive->unit - 8]) format |= FILEIO_FORMAT_RAW; /* * Find the next directory entry and return it as a CBM * directory line. */ /* first test if dirmask is needed - maybe this should be replaced by some regex functions... */ f = 1; do { BYTE *p; finfo = NULL; /* iAN CooG 31/08 */ direntry = ioutil_readdir(bufinfo->ioutil_dir); if (direntry == NULL) break; finfo = fileio_open(direntry, bufinfo->dir, format, FILEIO_COMMAND_READ | FILEIO_COMMAND_FSNAME, FILEIO_TYPE_PRG); if (finfo == NULL) continue; bufinfo->type = finfo->type; if (bufinfo->dirmask[0] == '\0') break; l = strlen(bufinfo->dirmask); for (p = finfo->name, i = 0; *p && bufinfo->dirmask[i] && i < l; i++) { if (bufinfo->dirmask[i] == '?') { p++; } else if (bufinfo->dirmask[i] == '*') { if (!(bufinfo->dirmask[i + 1])) { f = 0; break; } /* end mask */ while (*p && (*p != bufinfo->dirmask[i + 1])) p++; } else { if (*p != bufinfo->dirmask[i]) break; p++; } if ((!*p) && (!(bufinfo->dirmask[i + 1]))) { f = 0; break; } } if (f > 0) fileio_close(finfo); } while (f); if (direntry != NULL) { BYTE *p = bufinfo->name; strcpy(buf, bufinfo->dir); strcat(buf, FSDEV_DIR_SEP_STR); strcat(buf, direntry); /* Line link, Length and spaces */ *p++ = 1; *p++ = 1; statrc = ioutil_stat(buf, &filelen, &isdir); if (statrc == 0) blocks = (filelen + 253) / 254; else blocks = 0; /* this file can't be opened */ if (blocks > 0xffff) blocks = 0xffff; /* Limit file size to 16 bits. */ SET_LO_HI(p, blocks); if (blocks < 10) *p++ = ' '; if (blocks < 100) *p++ = ' '; if (blocks < 1000) *p++ = ' '; /* * Filename */ *p++ = '"'; for (i = 0; finfo->name[i] && (*p = finfo->name[i]); ++i, ++p); *p++ = '"'; for (; i < 16; i++) *p++ = ' '; if (isdir != 0) { *p++ = ' '; /* normal file */ *p++ = 'D'; *p++ = 'I'; *p++ = 'R'; } else { if (blocks) *p++ = ' '; /* normal file */ else *p++ = '*'; /* splat file */ switch(bufinfo->type) { case CBMDOS_FT_DEL: *p++ = 'D'; *p++ = 'E'; *p++ = 'L'; break; case CBMDOS_FT_SEQ: *p++ = 'S'; *p++ = 'E'; *p++ = 'Q'; break; case CBMDOS_FT_PRG: *p++ = 'P'; *p++ = 'R'; *p++ = 'G'; break; case CBMDOS_FT_USR: *p++ = 'U'; *p++ = 'S'; *p++ = 'R'; break; case CBMDOS_FT_REL: *p++ = 'R'; *p++ = 'E'; *p++ = 'L'; break; } } if (ioutil_access(buf, IOUTIL_ACCESS_W_OK)) *p++ = '<'; /* read-only file */ *p = '\0'; /* to allow strlen */ /* some (really very) old programs rely on the directory entry to be 32 Bytes in total (incl. nullbyte) */ l = strlen((char *)(bufinfo->name + 4)) + 4; while (l < 31) { *p++ = ' '; l++; } *p++ = '\0'; bufinfo->buflen = (int)(p - bufinfo->name); } else { BYTE *p = bufinfo->name; /* EOF => End file */ *p++ = 1; *p++ = 1; *p++ = 0; *p++ = 0; memcpy(p, "BLOCKS FREE.", 12); p += 12; memset(p, ' ', 13); p += 13; memset(p, 0, 3); bufinfo->buflen = 32; bufinfo->eof++; } if (finfo != NULL) /* iAN CooG 31/08 */ fileio_close(finfo); lib_free(buf); }
char *findpath(const char *cmd, const char *syspath, int mode) { char *pd = NULL; char *c, *buf; size_t maxpathlen; maxpathlen = (size_t)ioutil_maxpathlen(); buf = lib_malloc(maxpathlen); buf[0] = '\0'; /* this will (and needs to) stay '\0' */ if (strchr(cmd, FSDEV_DIR_SEP_CHR)) { size_t l; int state; const char *ps; if (archdep_path_is_relative(cmd)) { if (ioutil_getcwd(buf + 1, (int)maxpathlen - 128) == NULL) goto fail; l = strlen(buf + 1); } else { l = 0; } if (l + strlen(cmd) >= maxpathlen - 5) goto fail; ps = cmd; pd = buf + l; /* buf + 1 + l - 1 */ #if !defined (__MSDOS__) && !defined (WIN32) && !defined (__OS2__) if (*pd++ != '/') *pd++ = '/'; #else pd++; #endif state = 1; /* delete extra `/./', '/../' and '//':s from the path */ while (*ps) { switch (state) { case 0: if (*ps == '/') state = 1; else state = 0; break; case 1: if (*ps == '.') { state = 2; break; } if (*ps == '/') pd--; else state = 0; break; case 2: if (*ps == '/') { state = 1; pd -= 2; break; } if (*ps == '.') state = 3; else state = 0; break; case 3: if (*ps != '/') { state = 0; break; } state = 1; pd -= 4; while (*pd != '/' && *pd != '\0') pd--; if (*pd == '\0') pd++; state = 1; break; } *pd++ = *ps++; } *pd = '\0'; pd = buf + 1; } else { const char *path = syspath; const char *s; size_t cl = strlen(cmd) + 1; for (s = path; s; path = s + 1) { char * p; int l; s = strchr(path, ARCHDEP_FINDPATH_SEPARATOR_CHAR); l = s ? (int)(s - path) : (int)strlen(path); if (l + cl > maxpathlen - 5) continue; memcpy(buf + 1, path, l); p = buf + l; /* buf + 1 + l - 1 */ if (*p++ != '/') *p++ = '/'; memcpy(p, cmd, cl); for (c = buf + 1; *c != '\0'; c++) #if defined (__MSDOS__) || defined (WIN32) || defined (__OS2__) if (*c == '/') *c = '\\'; #else if (*c == '\\') *c ='/'; #endif if (ioutil_access(buf + 1, mode) == 0) { pd = p /* + cl*/ ; break; } } } if (pd) { char *tmpbuf; #if 0 do pd--; while (*pd != '/'); /* there is at least one '/' */ if (*(pd - 1) == '\0') pd++; *pd = '\0'; #endif tmpbuf = lib_stralloc(buf + 1); lib_free(buf); return tmpbuf; } fail: lib_free(buf); return NULL; }