uint8_t _concat(char *filename, char **inputs, uint16_t ninputs) { uint16_t i; int size; uint8_t *buf; FILE *outf; FILE *inf; char *infname; buf = NULL; outf = NULL; inf = NULL; filename = set_suffix(filename, "img"); if (filename == NULL) goto fail; outf = fopen(filename, "wb"); if (outf == NULL) goto fail; for (i = 0; i < ninputs; i++, inf = NULL, buf = NULL) { infname = set_suffix(inputs[i], "img"); if (infname == NULL) goto fail; inf = fopen(infname, "rb"); if (inf == NULL) goto fail; size = filesize(inf); buf = malloc(size); if (buf == NULL) goto fail; if (fread( buf, 1, size, inf) != size) goto fail; if (fwrite(buf, 1, size, outf) != size) goto fail; fclose(inf); free(buf); free(infname); infname = NULL; inf = NULL; } fclose(outf); free(filename); return 0; fail: if (outf != NULL) fclose(outf); if (inf != NULL) fclose(inf); if (filename != NULL) free(filename); if (buf != NULL) free(buf); return 1; }
static bool parse_suffixes(w_query *res, json_t *query) { json_t *suffixes; size_t i; suffixes = json_object_get(query, "suffix"); if (!suffixes) { return true; } if (json_is_string(suffixes)) { json_t *ele = suffixes; res->nsuffixes = 1; res->suffixes = calloc(res->nsuffixes, sizeof(w_string_t*)); return set_suffix(res, ele, res->suffixes); } if (!json_is_array(suffixes)) { res->errmsg = strdup("'suffix' must be a string or an array of strings"); return false; } res->nsuffixes = json_array_size(suffixes); res->suffixes = calloc(res->nsuffixes, sizeof(w_string_t*)); if (!res->suffixes) { return false; } for (i = 0; i < json_array_size(suffixes); i++) { json_t *ele = json_array_get(suffixes, i); if (!json_is_string(ele)) { res->errmsg = strdup("'suffix' must be a string or an array of strings"); return false; } if (!set_suffix(res, ele, res->suffixes + i)) { return false; } } return true; }
char * mkbak (const char *filename, backup_t type) { static char buf[FILENAME_MAX]; if (access (filename, R_OK) != 0) return (char *) filename; strcpy (buf, filename); set_suffix (buf, ".bak"); if (strcmp (filename, buf) != 0) { remove (buf); // *try* to remove or rename() will fail if (rename (filename, buf)) // keep file attributes like date, etc. { fprintf (stderr, "ERROR: Can't rename \"%s\" to \"%s\"\n", filename, buf); exit (1); } } else // handle the case where filename has the suffix ".bak". { char buf2[FILENAME_MAX]; if (!dirname2 (filename, buf)) { fprintf (stderr, "INTERNAL ERROR: dirname2() returned NULL\n"); exit (1); } if (buf[0] != 0) if (buf[strlen (buf) - 1] != FILE_SEPARATOR) strcat (buf, FILE_SEPARATOR_S); strcat (buf, basename2 (tmpnam2 (buf2))); if (rename (filename, buf)) { fprintf (stderr, "ERROR: Can't rename \"%s\" to \"%s\"\n", filename, buf); exit (1); } } switch (type) { case BAK_MOVE: return buf; case BAK_DUPE: default: if (fcopy (buf, 0, fsizeof (buf), filename, "wb")) { fprintf (stderr, "ERROR: Can't open \"%s\" for writing\n", filename); exit (1); } sync (); return buf; } }
uint8_t nifti1_load_hdr(char *filename, nifti1_hdr_t *hdr) { FILE *f; int sz; uint8_t *bytes; char *afile; f = NULL; bytes = NULL; afile = NULL; memset(hdr, 0, sizeof(nifti1_hdr_t)); afile = set_suffix(filename, "hdr"); if (afile == NULL) goto fail; f = fopen(afile, "rb"); if (f == NULL) goto fail; sz = filesize(f); if (sz != 348 && sz != 352) goto fail; bytes = malloc(sz); if (bytes == NULL) goto fail; if (fread(bytes, 1, sz, f) != sz) goto fail; memcpy(hdr, bytes, sz); if (hdr->dim[0] > 7) { nifti1_reverse_hdr(hdr); if (hdr->dim[0] > 7) goto fail; if (hdr->sizeof_hdr != 348) goto fail; hdr->rev = 1; } fclose(f); free(bytes); free(afile); return 0; fail: if (f != NULL) fclose(f); if (bytes != NULL) free(bytes); if (afile != NULL) free(afile); return 1; }
int lynx_lnx (st_rominfo_t *rominfo) { st_lnx_header_t header; char dest_name[FILENAME_MAX]; int size = ucon64.file_size; if (rominfo->buheader_len != 0) { fprintf (stderr, "ERROR: This seems to already be an LNX file\n\n"); return -1; } header.page_size_bank0 = size > 4 * MBIT ? 4 * MBIT / 256 : size / 256; header.page_size_bank1 = size > 4 * MBIT ? (size - (4 * MBIT)) / 256 : 0; #ifdef WORDS_BIGENDIAN header.page_size_bank0 = bswap_16 (header.page_size_bank0); header.page_size_bank1 = bswap_16 (header.page_size_bank1); #endif memset (header.cartname, 0, sizeof (header.cartname)); memset (header.manufname, 0, sizeof (header.manufname)); memset (header.spare, 0, sizeof (header.spare)); #ifdef WORDS_BIGENDIAN header.version = bswap_16 (1); #else header.version = 1; #endif memcpy (header.magic, "LYNX", 4); header.rotation = 0; strncpy (header.cartname, ucon64.rom, sizeof (header.cartname)); strcpy (header.manufname, "Atari"); strcpy (dest_name, ucon64.rom); set_suffix (dest_name, ".lnx"); ucon64_file_handler (dest_name, NULL, 0); ucon64_fwrite (&header, 0, sizeof (st_lnx_header_t), dest_name, "wb"); fcopy (ucon64.rom, 0, ucon64.file_size, dest_name, "ab"); printf (ucon64_msg[WROTE], dest_name); return 0; }
// header format is specified in src/backup/ffe.h int pce_msg (st_ucon64_nfo_t *rominfo) { char src_name[FILENAME_MAX], dest_name[FILENAME_MAX]; unsigned char *rom_buffer = NULL; st_msg_header_t header; int size = ucon64.file_size - rominfo->backup_header_len; if (rominfo->interleaved) if ((rom_buffer = (unsigned char *) malloc (size)) == NULL) { fprintf (stderr, ucon64_msg[ROM_BUFFER_ERROR], size); return -1; } memset (&header, 0, MSG_HEADER_LEN); header.size = (unsigned char) (size / 8192); header.emulation = size == 3 * MBIT ? 1 : 0; header.id1 = 0xaa; header.id2 = 0xbb; header.type = 2; strcpy (src_name, ucon64.fname); strcpy (dest_name, ucon64.fname); set_suffix (dest_name, ".msg"); ucon64_file_handler (dest_name, src_name, 0); ucon64_fwrite (&header, 0, MSG_HEADER_LEN, dest_name, "wb"); if (rominfo->interleaved) { // Magic Super Griffin files should not be "interleaved" ucon64_fread (rom_buffer, rominfo->backup_header_len, size, src_name); swapbits (rom_buffer, size); ucon64_fwrite (rom_buffer, MSG_HEADER_LEN, size, dest_name, "ab"); free (rom_buffer); } else fcopy (src_name, rominfo->backup_header_len, size, dest_name, "ab"); printf (ucon64_msg[WROTE], dest_name); remove_temp_file (); return 0; }
int lynx_lyx (st_rominfo_t *rominfo) { char dest_name[FILENAME_MAX]; if (!rominfo->buheader_len) { fprintf (stderr, "ERROR: This is no LNX file\n\n"); return -1; } strcpy (dest_name, ucon64.rom); set_suffix (dest_name, ".lyx"); ucon64_file_handler (dest_name, NULL, 0); fcopy (ucon64.rom, rominfo->buheader_len, ucon64.file_size, dest_name, "wb"); printf (ucon64_msg[WROTE], dest_name); return 0; }
void FSName::set_name(const char* file_name, const char* suffix, const int32_t cluster_id) { file_name_[0] = '\0'; cluster_id_ = cluster_id; memset(&filev1_, 0, sizeof(FileBitsV1)); memset(&filev2_, 0, sizeof(FileBitsV2)); if (NULL != file_name && file_name[0] != '\0') { TfsFileType file_type = check_file_type(file_name); if (INVALID_TFS_FILE_TYPE == file_type) { is_valid_ = false; } else { int32_t file_name_len = 0; if (TFS_FILE_NAME_V1 == version_) { file_name_len = FILE_NAME_LEN; decode(file_name + 2, (char*) &filev1_, file_name_len - 2); } else { file_name_len = FILE_NAME_LEN_V2; decode(file_name + 3, (char*) &filev2_, file_name_len - 3); } if (NULL == suffix) { suffix = file_name + file_name_len; } set_suffix(suffix); if (0 == cluster_id_) { cluster_id_ = file_name[1] - '0'; } } } }
// based on sourcecode of MakePPF v2.0 Linux/Unix by Icarus/Paradox int ppf_create (const char *orgname, const char *modname) { FILE *orgfile, *modfile, *ppffile; char ppfname[FILENAME_MAX], buffer[MAX_ID_SIZE], obuf[512], mbuf[512]; #if 0 char *fidname = "FILE_ID.DIZ"; #endif int x, osize, msize, blocksize, n_changes, total_changes = 0; unsigned int seekpos = 0, pos; osize = fsizeof (orgname); msize = fsizeof (modname); #ifndef DIFF_FSIZE if (osize != msize) { fprintf (stderr, "ERROR: File sizes do not match\n"); return -1; } #endif if ((orgfile = fopen (orgname, "rb")) == NULL) { fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], orgname); exit (1); } if ((modfile = fopen (modname, "rb")) == NULL) { fprintf (stderr, ucon64_msg[OPEN_READ_ERROR], modname); exit (1); } strcpy (ppfname, modname); set_suffix (ppfname, ".ppf"); ucon64_file_handler (ppfname, NULL, 0); if ((ppffile = fopen (ppfname, "wb")) == NULL) { fprintf (stderr, ucon64_msg[OPEN_WRITE_ERROR], ppfname); exit (1); } // creating PPF 2.0 header fwrite ("PPF20", 5, 1, ppffile); // magic fputc (1, ppffile); // encoding method memset (buffer, ' ', 50); fwrite (buffer, 50, 1, ppffile); // description line #ifdef WORDS_BIGENDIAN x = bswap_32 (osize); fwrite (&x, 4, 1, ppffile); #else fwrite (&osize, 4, 1, ppffile); // orgfile size #endif fseek (orgfile, 0x9320, SEEK_SET); memset (buffer, 0, 1024); // one little hack that makes PPF fread (buffer, 1024, 1, orgfile); // suitable for files < 38688 bytes fwrite (buffer, 1024, 1, ppffile); // 1024 byte block printf ("Writing patch data, please wait...\n"); // finding changes fseek (orgfile, 0, SEEK_SET); fseek (modfile, 0, SEEK_SET); while ((blocksize = fread (obuf, 1, 255, orgfile))) { blocksize = fread (mbuf, 1, blocksize, modfile); #ifdef DIFF_FSIZE if (blocksize == 0) break; #endif pos = seekpos; x = 0; while (x != blocksize) { if (obuf[x] != mbuf[x]) { pos = seekpos + x; n_changes = 0; do { buffer[n_changes] = mbuf[x]; n_changes++; x++; } while (x != blocksize && obuf[x] != mbuf[x]); total_changes += n_changes; #ifdef WORDS_BIGENDIAN pos = bswap_32 (pos); #endif fwrite (&pos, 4, 1, ppffile); fputc (n_changes, ppffile); fwrite (buffer, n_changes, 1, ppffile); } else x++; } seekpos += blocksize; } #ifdef DIFF_FSIZE if (msize > osize) { pos = seekpos; while ((blocksize = fread (buffer, 1, 255, modfile))) { total_changes += blocksize; #ifdef WORDS_BIGENDIAN x = bswap_32 (pos); fwrite (&x, 4, 1, ppffile); #else fwrite (&pos, 4, 1, ppffile); #endif fputc (blocksize, ppffile); fwrite (buffer, blocksize, 1, ppffile); pos += blocksize; } } else if (msize < osize) printf ("WARNING: %s is smaller than %s\n" " PPF can't store information about that fact\n", modname, orgname); #endif fclose (orgfile); fclose (modfile); if (total_changes == 0) { printf ("%s and %s are identical\n" "Removing: %s\n", orgname, modname, ppfname); fclose (ppffile); remove (ppfname); return -1; } #if 0 if (fidname) { int fsize = fsizeof (fidname); if (fsize > MAX_ID_SIZE) fsize = MAX_ID_SIZE; // File id only up to 3072 bytes! printf ("Adding FILE_ID.DIZ (%s)...\n", fidname); ucon64_fread (buffer, 0, fsize, fidname); fwrite ("@BEGIN_FILE_ID.DIZ", 18, 1, ppffile); fwrite (buffer, fsize, 1, ppffile); fwrite ("@END_FILE_ID.DIZ", 16, 1, ppffile); #ifdef WORDS_BIGENDIAN fsize = bswap_32 (fsize); // Write file size in little-endian format #endif fwrite (&fsize, 4, 1, ppffile); } #endif fclose (ppffile); printf (ucon64_msg[WROTE], ppfname); return 0; }
int property_check (const char *filename, int version, int verbose) { char buf[MAXBUFSIZE]; const char *p = NULL; int result = 0; if (access (filename, F_OK) != 0) { FILE *fh = NULL; if (verbose) { fprintf (stderr, "NOTE: %s not found: creating...", filename); fflush (stderr); } if (!(fh = fopen (filename, "w"))) // opening the file in text mode { // avoids trouble under DOS printf ("FAILED\n\n"); return -1; } fclose (fh); // we'll use set_property() from now } else { p = get_property (filename, "version", PROPERTY_MODE_TEXT); if (strtol (p ? p : "0", NULL, 10) >= version) return 0; // OK strcpy (buf, filename); set_suffix (buf, ".old"); if (verbose) { fprintf (stderr, "NOTE: updating config: will be renamed to %s...", buf); fflush (stderr); } rename (filename, buf); } // store new version sprintf (buf, "%d", version); result = set_property (filename, "version", buf, "configfile version (do NOT edit)"); if (result > 0) { if (verbose) fprintf (stderr, "OK\n\n"); } else { if (verbose) fprintf (stderr, "FAILED\n\n"); // remove the crap remove (filename); } if (verbose) fflush (stderr); return 1; }
int main (int argc, char **argv) { // int i = 0; // char buf[MAXBUFSIZE]; int c = 0, option_index = 0; int x = 0, y = 0; struct option long_only_options[ARGS_MAX]; int result = 0; // const char *p = NULL; const st_property_t props[] = { { "ansi_color", "1", "use ANSI colors in output? (1=yes; 0=no)" }, { "default_cmdline", "", "will be used when quh is started w/o args" }, { "settings", "100", "internal settings like volume, etc." }, #if 0 { "quh_configdir", PROPERTY_MODE_DIR ("quh"), "directory with additional config files" }, #endif {NULL, NULL, NULL} }; memset (&quh, 0, sizeof (st_quh_t)); // defaults quh.pid = 1; tmpnam3 (quh.tmp_file, 0); set_suffix (quh.tmp_file, ".wav"); if(!(quh.o = cache_open (MAXBUFSIZE, CACHE_MEM|CACHE_LIFO))) { fprintf (stderr, "ERROR: Could not malloc %d bytes\n", MAXBUFSIZE); return -1; } realpath2 (PROPERTY_HOME_RC ("quh"), quh.configfile); result = property_check (quh.configfile, QUH_CONFIG_VERSION, 1); if (result == 1) // update needed result = set_property_array (quh.configfile, props); if (result == -1) // property_check() or update failed return -1; signal (SIGINT, quh_signal_handler); signal (SIGTERM, quh_signal_handler); atexit (quh_exit); quh.ansi_color = get_property_int (quh.configfile, "ansi_color"); quh.settings = get_property_int (quh.configfile, "settings"); quh.argc = argc; quh.argv = argv; #if 0 // memorize cmdline if (quh.argc > 2) { for (; quh.argv[i] && i < quh.argc; i++) sprintf (strchr (buf, 0), "%s ", quh.argv[i]); set_property (quh.configfile, "default_cmdline", buf, "will be used when quh is started w/o args"); } else { p = get_property (quh.configfile, "default_cmdline", PROPERTY_MODE_TEXT); if (p) { strncpy (buf, p, MAXBUFSIZE)[MAXBUFSIZE - 1] = 0; quh.argc = strarg (quh.argv, buf, " ", QUH_MAX_ARGS); } } #endif // set default filter chain filters = 0; quh.filter_id[filters++] = QUH_CACHE_PASS; quh.filter_id[filters++] = QUH_CONSOLE_PASS; #ifdef USE_ID3 // quh.filter_id[filters++] = QUH_ID3_IN; #endif // quh.filter_id[filters++] = QUH_CDDB_IN; #ifdef USE_OSS quh.filter_id[filters++] = QUH_OSS_OUT; #elif defined USE_SDL quh.filter_id[filters++] = QUH_SDL_OUT; #endif // convert (st_getopt2_t **) to (st_getopt2_t *) memset (&options, 0, sizeof (st_getopt2_t) * QUH_MAX_ARGS); for (c = x = 0; option[x]; x++) for (y = 0; option[x][y].name || option[x][y].help; y++) if (c < QUH_MAX_ARGS) { memcpy (&options[c], &option[x][y], sizeof (st_getopt2_t)); c++; } #if 0 for (x = 0; quh_decode_usage[x]; x++) if (c < QUH_MAX_ARGS) { memcpy (&options[c], quh_decode_usage[x], sizeof (st_getopt2_t)); c++; } #endif for (x = 0; quh_filter_usage[x]; x++) if (c < QUH_MAX_ARGS) { memcpy (&options[c], quh_filter_usage[x], sizeof (st_getopt2_t)); c++; } for (x = 0; option2[x]; x++) for (y = 0; option2[x][y].name || option2[x][y].help; y++) if (c < QUH_MAX_ARGS) { memcpy (&options[c], &option2[x][y], sizeof (st_getopt2_t)); c++; } getopt2_long_only (long_only_options, options, ARGS_MAX); #if 0 // if no options or filenames were specified we use a default cmdline if (argc < 2) // || !optind) { p = get_property (quh.configfile, "default_cmdline", PROPERTY_MODE_TEXT); if (p) { strncpy (quh.cmdline, p, ARGS_MAX)[ARGS_MAX - 1] = 0; quh.argc = strarg (quh.argv, quh.cmdline, " ", QUH_MAX_ARGS); } } else // store cmdline { strcpy (quh.cmdline, argv[0]); for (x = 1; x < argc; x++) sprintf (strchr (quh.cmdline, 0), " \"%s\"", quh.argv[x]); set_property (quh.configfile, "default_cmdline", quh.cmdline, NULL); } for (x = 0; x < quh.argc; x++) printf ("quh.argv[%d] == %s\n", x, quh.argv[x]); fflush (stdout); #endif while ((c = getopt_long_only (quh.argc, quh.argv, "", long_only_options, &option_index)) != -1) quh_opts (c); // if (quh.verbose) if (!quh.quiet) fputs ("Quh " QUH_VERSION_S " 'Having ears makes sense again' 2005-2006 by NoisyB\n" "This may be freely redistributed under the terms of the GNU Public License\n\n", stdout); if (quh.argc < 2) // || !optind) { getopt2_usage (options); return -1; } #warning files? #if 0 if (!getfile (quh.argc, quh.argv, quh_set_fname, (GETFILE_FILES_ONLY | (quh.flags & QUH_RECURSIVE ? GETFILE_RECURSIVE : 0)))) // recursively? { if (!quh.quiet) getopt2_usage (options); return -1; } #endif if (!quh.filter_id[0]) { fputs ("ERROR: you haven't specified any filters\n", stderr); fflush (stderr); return -1; } if (!quh.files) { fputs ("ERROR: you haven't specified any files to play\n", stderr); fflush (stderr); return -1; } if (!(quh.filter_chain = filter_malloc_chain (quh_filter))) { fputs ("ERROR: filter_malloc_chain() failed\n", stderr); fflush (stderr); return -1; } if (filter_init (quh.filter_chain, NULL, NULL) == -1) { fputs ("ERROR: filter_init() failed\n", stderr); fflush (stderr); return -1; } quh_play (); return 0; }