void open_files(void) { create_file_names(); if (input_file == 0) { input_file = fopen(input_file_name, "r"); if (input_file == 0) open_error(input_file_name); } action_file = fopen(action_file_name, "w"); if (action_file == 0) open_error(action_file_name); entry_file = fopen(entry_file_name, "w"); if (entry_file == 0) open_error(entry_file_name); text_file = fopen(text_file_name, "w"); if (text_file == 0) open_error(text_file_name); if (vflag) { verbose_file = fopen(verbose_file_name, "w"); if (verbose_file == 0) open_error(verbose_file_name); } if (dflag) { defines_file = fopen(defines_file_name, "w"); if (defines_file == 0) open_error(defines_file_name); union_file = fopen(union_file_name, "w"); if (union_file == 0) open_error(union_file_name); } output_file = fopen(output_file_name, "w"); if (output_file == 0) open_error(output_file_name); if (rflag) { code_file = fopen(code_file_name, "w"); if (code_file == 0) open_error(code_file_name); } else code_file = output_file; interface_file = fopen(interface_file_name, "w"); if (interface_file == 0) open_error(interface_file_name); }
void output_stored_text() { register int c; register FILE *in, *out; register int state; /* 0=middle of line, 1=start of line, 2=seen '#' */ state = 1; fclose(text_file); text_file = fopen(text_file_name, "r"); if (text_file == NULL) open_error(text_file_name); in = text_file; if ((c = getc(in)) == EOF) return; out = code_file; do { if (c == '\n') { ++outline; if (state == 2) { fprintf(out, line_format+1, outline + 1, code_file_name); state = 1; continue; } state = 1; } else if (state == 1 && c == '#') state = 2; else state = 0; putc(c, out); } while ((c = getc(in)) != EOF); if (!lflag) fprintf(out, line_format, ++outline + 1, code_file_name); }
int exec_redir_left(char **cmd_redir, int number, t_vars *p) { int file_fd; pid_t pid; int i; i = 1; while (cmd_redir[i] != NULL) { if (number == 1) { if ((file_fd = open(epur(strdup(cmd_redir[i]), ' '), O_RDONLY)) == -1) { open_error(epur(strdup(cmd_redir[i]), ' ')); return (-1); } } else if (number == 2) return (exec_redir_left_double(cmd_redir[i - 1], epur(strdup(cmd_redir[i]), ' '), p)); if ((pid = fork()) > 0) wait(NULL); else if (pid == 0) { dup2(file_fd, 0); execute(cmd_redir[i - 1], p); close(file_fd); return (0); } else return (-1); i++; } return (0); }
/** * \fn void open_file_(BrailleMusicEditor *editor, const gchar *path) * \brief This function actually opens a file in the editor's textview. * \param editor The GUI structure. * \param path the file path. */ void open_file_(BrailleMusicEditor *editor, const gchar *path) { GtkTextBuffer *buffer; GtkTextIter start; GtkTextIter end; FILE *file; gchar read[1024]; buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(editor->textview)); if(path == NULL) return; file = fopen(path,"r"); open_error(editor->window, file, "Can't open the file : \n%s", path); //save the current file path editor->filename = (gchar *)path; gtk_text_buffer_get_start_iter(buffer, &start); gtk_text_buffer_get_end_iter(buffer, &end); gtk_text_buffer_delete(buffer, &start, &end); while(fgets(read, 1024, file)){ gtk_text_buffer_get_end_iter(buffer, &end); gtk_text_buffer_insert(buffer, &end, read, -1); } fclose(file); gtk_text_buffer_get_start_iter(buffer, &start); gtk_text_buffer_place_cursor(buffer, &start); }
void read_from(enum eyefi_file __file) { int tries = 0; int ret; int fd; char *file = eyefi_file(__file); int nr_fresh; init_card(); retry: fd = open(file, O_RDONLY); if (fd < 0) open_error(file, fd); fd_flush(fd); // fd_flush() does not appear to be working 100% of the // time. It is not working on my Thinkpad, but works // fine on the same kernel on the Ideapad. Bizarre. // This at least works around it by detecting when we // did and did not actually bring in pages from the // disk. nr_fresh = nr_fresh_pages(fd, EYEFI_BUF_SIZE); if (!nr_fresh) { tries++; debug_printf(2, "fd_flush(%d) was unsuccessful(%d), retrying (%d)...\n", fd, nr_fresh, tries); close(fd); goto retry; } ret = read(fd, eyefi_buf, EYEFI_BUF_SIZE); if ((eyefi_debug_level >= 3) || (eyefi_debug_level >= 2 && (__file == RSPM))) { printf("%s:", eyefi_file_name(__file)); dumpbuf(eyefi_buf, 128); } if (ret < 0) { close(fd); perror("bad read, retrying..."); goto retry; exit(1); } debug_printf(4, "read '%s': bytes: %d\n", file, ret); /* * There was a time when I was carefully recording how each response * looked, and I counted the zeros in each response. I don't care * any more. u8 c; int zeros = 0; int i; for (i=0; i < EYEFI_BUF_SIZE; i++) { c = ((char *)eyefi_buf)[i]; if (c == '\0') { zeros++; continue; } } */ free(file); close(fd); }
void output_defines() { register int c, i; register char *s; FILE *dc_file; if(dflag) { fprintf(defines_file, "#ifndef _yacc_defines_h_\n"); fprintf(defines_file, "#define _yacc_defines_h_\n\n"); } /* VM: Print to either code file or defines file but not to both */ dc_file = dflag ? defines_file : code_file; for (i = 2; i < ntokens; ++i) { s = symbol_name[i]; if (is_C_identifier(s)) { fprintf(dc_file, "#define "); c = *s; if (c == '"') { while ((c = *++s) != '"') { putc(c, dc_file); } } else { do { putc(c, dc_file); } while ((c = *++s)); } ++outline; fprintf(dc_file, " %d\n", symbol_value[i]); } } ++outline; fprintf(dc_file, "#define YYERRCODE %d\n", symbol_value[1]); if (dflag && unionized) { fclose(union_file); union_file = fopen(union_file_name, "r"); if (union_file == NULL) open_error(union_file_name); while ((c = getc(union_file)) != EOF) { putc(c, defines_file); } fprintf(defines_file, "extern YYSTYPE yylval;\n"); } if(dflag) { fprintf(defines_file, "\n#endif\n"); } }
void open_diag (char const *name) { if (ignore_failed_read_option) open_warn (name); else open_error (name); }
/* Catenate file FILE_NAME to the archive without creating a header for it. It had better be a tar file or the archive is screwed. */ static void append_file (char *file_name) { int handle = open (file_name, O_RDONLY | O_BINARY); struct stat stat_data; if (handle < 0) { open_error (file_name); return; } if (fstat (handle, &stat_data) != 0) stat_error (file_name); else { off_t bytes_left = stat_data.st_size; while (bytes_left > 0) { union block *start = find_next_block (); size_t buffer_size = available_space_after (start); size_t status; char buf[UINTMAX_STRSIZE_BOUND]; if (bytes_left < buffer_size) { buffer_size = bytes_left; status = buffer_size % BLOCKSIZE; if (status) memset (start->buffer + bytes_left, 0, BLOCKSIZE - status); } status = safe_read (handle, start->buffer, buffer_size); if (status == SAFE_READ_ERROR) read_fatal_details (file_name, stat_data.st_size - bytes_left, buffer_size); if (status == 0) FATAL_ERROR ((0, 0, ngettext ("%s: File shrank by %s byte", "%s: File shrank by %s bytes", bytes_left), quotearg_colon (file_name), STRINGIFY_BIGINT (bytes_left, buf))); bytes_left -= status; set_next_block_after (start + (status - 1) / BLOCKSIZE); } } if (close (handle) != 0) close_error (file_name); }
void open_files(void) { create_file_names(); if (input_file == 0) { input_file = fopen(input_file_name, "r"); if (input_file == 0) open_error(input_file_name); } action_file = fopen(action_file_name, "w"); if (action_file == 0) open_error(action_file_name); text_file = fopen(text_file_name, "w"); if (text_file == 0) open_error(text_file_name); if (vflag) { verbose_file = fopen(verbose_file_name, "w"); if (verbose_file == 0) open_error(verbose_file_name); } if (dflag) { defines_file = fopen(defines_file_name, "w"); if (defines_file == 0) open_error(defines_file_name); union_file = fopen(union_file_name, "w"); if (union_file == 0) open_error(union_file_name); } output_file = fopen(output_file_name, "w"); if (output_file == 0) open_error(output_file_name); if (emitident) fprintf(output_file, "#ident \"%s: %s\"\n", myname, yaccversion); if (rflag) { code_file = fopen(code_file_name, "w"); if (code_file == 0) open_error(code_file_name); } else code_file = output_file; }
void write_to(enum eyefi_file __file, void *stuff, int len) { int ret; int wrote; int fd; char *file; if (fake_write) return; init_card(); file = eyefi_file(__file); if (len == -1) len = strlen(stuff); memset(eyefi_buf, 0, EYEFI_BUF_SIZE); memcpy(eyefi_buf, stuff, len); fd = open(file, O_RDWR|O_CREAT, 0600); if (fd < 0 ) open_error(file, fd); if ((eyefi_debug_level >= 3) || (eyefi_debug_level >= 2 && (__file == REQM))) { printf("%s:", eyefi_file_name(__file)); dumpbuf(eyefi_buf, 128); } wrote = write(fd, eyefi_buf, EYEFI_BUF_SIZE); if (wrote < 0) open_error(file, wrote); ret = fd_flush(fd); if (ret < 0) open_error(file, ret); close(fd); debug_printf(3, "wrote %d bytes to '%s' (string was %d bytes)\n", wrote, file, len); if (ret < 0) { fprintf(stderr, "error writing to '%s': ", file); perror(""); exit(ret); } free(file); }
void read_skel(char *name) { char buf[256]; int section = -2; int line = 0, sline = 1, eline = 1; int i; FILE *fp; if (!(fp = fopen(name, "r"))) open_error(name); while(fgets(buf, 255, fp)) { if ((sline = eline)) line++; if ((i = strlen(buf)) == 0) continue; if (buf[i-1] == '\n') { buf[--i] = 0; eline = 1; } else { buf[i++] = '\\'; buf[i] = 0; eline = 0; } if (sline && buf[0] == '%' && buf[1] == '%') { char *p = buf+2; if (section >= 0) { section_list[section].ptr = fin_section(); } section = -1; while(*p && isspace(*p)) p++; if (isalpha(*p)) { char *e = p; while(isalnum(*++e)); *e = 0; for (i=0; section_list[i].name; i++) if (!strcmp(section_list[i].name, p)) section = i; } if (section >= 0) add_fmt("#line %d \"%s\"", line+1, name); else if (*p) error(0, buf, p, "line %d of \"%s\", bad section name", line, name); } else if (section >= 0) { add_string(buf); } } if (section >= 0) section_list[section].ptr = fin_section(); if (section == -2) error(0, 0, 0, "No sections found in skeleton file \"%s\"", name); }
file_handle open_file(const std::string& path,const std::string& mode) { #ifdef WIN32 std::wstring wpath,wmode; utf8::utf8to16(path.begin(),path.end(),std::back_inserter(wpath)); utf8::utf8to16(mode.begin(),mode.end(),std::back_inserter(wmode)); file_handle result(_wfopen(wpath.c_str(),wmode.c_str()),std::fclose); #else file_handle result(std::fopen(path.c_str(),mode.c_str()),std::fclose); #endif if(result.empty()) throw open_error(path); return result; }
void open_ifstream(std::ifstream& stream,const std::string& path,bool binary) { std::ifstream::openmode mode=std::ifstream::in; if(binary) mode|=std::ifstream::binary; #ifdef WIN32 std::wstring wpath; utf8::utf8to16(path.begin(),path.end(),std::back_inserter(wpath)); stream.open(wpath.c_str(),mode); #else stream.open(path.c_str(),mode); #endif if(!stream.is_open()) throw open_error(path); }
/* ** ** main function ** */ int main( int argc, char *argv[] ) { #ifdef AMIGA STRPTR version_string = "$VER: 0.6.3 hsc (30.8.1995)"; #endif BOOL ok = FALSE; /* set program information */ set_prginfo( "hsc", "Tommy-Saftwörx", 0, 6, 3, "HTML Sucks Completely", "This is FreeWare!" ); #ifdef UMEM_TRACKING /* display a memory tracking report */ /* at end of execution */ atexit( atexit_uglymemory ); #endif /* use cleanup() as additional exit func */ atexit( cleanup ); /* ** main procedure */ if ( args_ok( argc, argv ) ) { /* display programm-info if requested */ if ( verbose ) fprintf_prginfo( stderr ); if ( open_error() /* init error file */ && open_output() /* open output file */ && config_ok() ) /* read config */ { /* include file parsed in args */ ok = include_hsc( inpfilename, outfile, IH_PARSE_END ); } } /* TODO: set return value 0,5,10,20 */ /* ->define symbols like RC_FAIL, vars like any_warn */ if ( ok ) return( 0 ); /* successful */ else return( 20 ); }
/* * Open a file for input and make future input come from it * */ void read_from_file(char *name) { file_info *n; FILE *fp; if (!name) { fp = stdin; } else if (!(fp = fopen(name, "r"))) { if (!input_file) open_error(name); /* fatal -- does not return */ error(input_file->lineno, 0, 0, "Cannot open include file %s", name); return; } n = NEW(file_info); n->next = input_file; n->file = fp; n->name = name ? strdup(name) : 0; n->lineno = 0; input_file = n; }
static void output_stored_text(FILE * fp) { int c; FILE *in; rewind(text_file); if (text_file == NULL) open_error("text_file"); in = text_file; if ((c = getc(in)) == EOF) return; putc_code(fp, c); while ((c = getc(in)) != EOF) { putc_code(fp, c); } write_code_lineno(fp); }
void output_semantic_actions() { register int c, last; register FILE *out; register int state; /* 0=middle of line, 1=start of line, 2=seen '#' */ state = 1; fclose(action_file); action_file = fopen(action_file_name, "r"); if (action_file == NULL) open_error(action_file_name); if ((c = getc(action_file)) == EOF) return; out = code_file; do { last = c; if (c == '\n') { ++outline; if (state == 2) { fprintf(out, line_format+1, outline + 1, code_file_name); state = 1; continue; } state = 1; } else if (state == 1 && c == '#') state = 2; else state = 0; putc(c, out); } while ((c = getc(action_file)) != EOF); if (last != '\n') { ++outline; putc('\n', out); } if (!lflag) fprintf(out, line_format, ++outline + 1, code_file_name); }
/* Return the filenames in directory NAME, relative to the chdir_fd. If the directory does not exist, report error if MUST_EXIST is true. Return NULL on errors. */ char * tar_savedir (const char *name, int must_exist) { char *ret = NULL; DIR *dir = NULL; int fd = openat (chdir_fd, name, open_read_flags | O_DIRECTORY); if (fd < 0) { if (!must_exist && errno == ENOENT) return NULL; open_error (name); } else if (! ((dir = fdopendir (fd)) && (ret = streamsavedir (dir)))) savedir_error (name); if (dir ? closedir (dir) != 0 : 0 <= fd && close (fd) != 0) savedir_error (name); return ret; }
void open_files() { create_file_names(); action_file = fopen(action_file_name, "w"); if (action_file == 0) open_error(action_file_name); text_file = fopen(text_file_name, "w"); if (text_file == 0) open_error(text_file_name); if (vflag) { verbose_file = fopen(verbose_file_name, "w"); if (verbose_file == 0) open_error(verbose_file_name); } if (dflag) { defines_file = fopen(defines_file_name, "w"); if (defines_file == 0) open_error(defines_file_name); union_file = fopen(union_file_name, "w"); if (union_file == 0) open_error(union_file_name); } output_file = fopen(output_file_name, "w"); if (output_file == 0) open_error(output_file_name); if (rflag) { code_file = fopen(code_file_name, "w"); if (code_file == 0) open_error(code_file_name); } else code_file = output_file; }
/* ** ** main function ** */ int main( int argc, char *argv[] ) { BOOL ok = FALSE; /* set program information */ set_prginfo( "hsc", "Tommy-Saftwörx", 0, 9, 2, "HTML Sucks Completely", "This is FreeWare." ); #ifdef UMEM_TRACKING /* display a memory tracking report */ /* at end of execution */ atexit( atexit_uglymemory ); #endif /* use cleanup() as additional exit func */ atexit( cleanup ); /* ** main procedure */ if ( args_ok( argc, argv ) ) { /* display programm-info if requested */ if ( verbose ) fprintf_prginfo( stderr ); if ( open_error() /* init error file */ && open_output() /* open output file */ && config_ok() /* read config */ && include_ok() ) /* read include files */ { /* include file parsed in args */ ok = include_hsc_file( inpfilename, outfile, IH_PARSE_END ); } } return( return_code ); }
static void open_files (void) { create_file_names(); if (input_file == 0) { input_file = fopen(input_file_name, "r"); if (input_file == 0) open_error(input_file_name); } action_file = fopen(action_file_name, "w"); if (action_file == 0) open_error(action_file_name); prolog_file = fopen(prolog_file_name, "w"); if (prolog_file == 0) open_error(prolog_file_name); local_file = fopen(local_file_name, "w"); if (local_file == 0) open_error(local_file_name); if (vflag) { verbose_file = fopen(verbose_file_name, "w"); if (verbose_file == 0) open_error(verbose_file_name); } if (output_file == 0) { if (output_file_name != 0) { output_file = fopen(output_file_name, "w"); if (output_file == 0) open_error(output_file_name); } else { output_file = stdout; } } }
void output_defines() { register int c, i; register char *s; FILE *dc_file; if(dflag) { char *p, *tmp = strdup(defines_file_name); for (p = tmp; *p; p++) if (!isalnum(*p)) *p = '_'; fprintf(defines_file, "#ifndef _%s_\n", tmp); fprintf(defines_file, "#define _%s_\n\n", tmp); free(tmp); } /* VM: Print to either code file or defines file but not to both */ dc_file = dflag ? defines_file : code_file; for (i = 2; i < ntokens; ++i) { s = symbol_name[i]; if (is_C_identifier(s)) { fprintf(dc_file, "#define "); c = *s; if (c == '"') { while ((c = *++s) != '"') { putc(c, dc_file); } } else { do { putc(c, dc_file); } while ((c = *++s)); } if (!dflag) ++outline; fprintf(dc_file, " %d\n", symbol_value[i]); } } ++outline; fprintf(dc_file, "#define YYERRCODE %d\n", symbol_value[1]); if (dflag && (unionized || location_defined)) { fclose(union_file); union_file = fopen(union_file_name, "r"); if (union_file == NULL) open_error(union_file_name); while ((c = getc(union_file)) != EOF) { putc(c, defines_file); } if (unionized) fprintf(defines_file, "extern YYSTYPE yylval;\n"); } if(dflag) { fprintf(defines_file, "#if defined(YYPOSN)\n" "extern YYPOSN yyposn;\n" "#endif\n"); fprintf(defines_file, "\n#endif\n"); } }
/* Read incremental snapshot file (directory file). If the file has older incremental version, make sure that it is processed correctly and that tar will use the most conservative backup method among possible alternatives (i.e. prefer ALL_CHILDREN over CHANGED_CHILDREN, etc.) This ensures that the snapshots are updated to the recent version without any loss of data. */ void read_directory_file (void) { int fd; char *buf = 0; size_t bufsize; /* Open the file for both read and write. That way, we can write it later without having to reopen it, and don't have to worry if we chdir in the meantime. */ fd = open (listed_incremental_option, O_RDWR | O_CREAT, MODE_RW); if (fd < 0) { open_error (listed_incremental_option); return; } listed_incremental_stream = fdopen (fd, "r+"); if (! listed_incremental_stream) { open_error (listed_incremental_option); close (fd); return; } if (0 < getline (&buf, &bufsize, listed_incremental_stream)) { char *ebuf; uintmax_t incremental_version; if (strncmp (buf, PACKAGE_NAME, sizeof PACKAGE_NAME - 1) == 0) { ebuf = buf + sizeof PACKAGE_NAME - 1; if (*ebuf++ != '-') ERROR((1, 0, _("Bad incremental file format"))); for (; *ebuf != '-'; ebuf++) if (!*ebuf) ERROR((1, 0, _("Bad incremental file format"))); incremental_version = strtoumax (ebuf + 1, NULL, 10); } else incremental_version = 0; switch (incremental_version) { case 0: case 1: read_incr_db_01 (incremental_version, buf); break; case TAR_INCREMENTAL_VERSION: read_incr_db_2 (); break; default: ERROR ((1, 0, _("Unsupported incremental format version: %"PRIuMAX), incremental_version)); } } if (ferror (listed_incremental_stream)) read_error (listed_incremental_option); if (buf) free (buf); }
/* Read incremental snapshot file (directory file). If the file has older incremental version, make sure that it is processed correctly and that tar will use the most conservative backup method among possible alternatives (i.e. prefer ALL_CHILDREN over CHANGED_CHILDREN, etc.) This ensures that the snapshots are updated to the recent version without any loss of data. */ void read_directory_file (void) { int fd; char *buf = NULL; size_t bufsize = 0; int flags = O_RDWR | O_CREAT; if (incremental_level == 0) flags |= O_TRUNC; /* Open the file for both read and write. That way, we can write it later without having to reopen it, and don't have to worry if we chdir in the meantime. */ fd = open (listed_incremental_option, flags, MODE_RW); if (fd < 0) { open_error (listed_incremental_option); return; } listed_incremental_stream = fdopen (fd, "r+"); if (! listed_incremental_stream) { open_error (listed_incremental_option); close (fd); return; } /* Consume the first name from the name list and reset the list afterwards. This is done to change to the new directory, if the first name is a chdir request (-C dir), which is necessary to recreate absolute file names. */ name_from_list (); blank_name_list (); if (0 < getline (&buf, &bufsize, listed_incremental_stream)) { char *ebuf; uintmax_t incremental_version; if (strncmp (buf, PACKAGE_NAME, sizeof PACKAGE_NAME - 1) == 0) { ebuf = buf + sizeof PACKAGE_NAME - 1; if (*ebuf++ != '-') ERROR((1, 0, _("Bad incremental file format"))); for (; *ebuf != '-'; ebuf++) if (!*ebuf) ERROR((1, 0, _("Bad incremental file format"))); incremental_version = strtoumax (ebuf + 1, NULL, 10); } else incremental_version = 0; switch (incremental_version) { case 0: case 1: read_incr_db_01 (incremental_version, buf); break; case TAR_INCREMENTAL_VERSION: read_incr_db_2 (); break; default: ERROR ((1, 0, _("Unsupported incremental format version: %"PRIuMAX), incremental_version)); } } if (ferror (listed_incremental_stream)) read_error (listed_incremental_option); if (buf) free (buf); }
void info_attach_exclist (struct tar_stat_info *dir) { struct excfile *file; struct exclist *head = NULL, *tail = NULL, *ent; struct vcs_ignore_file *vcsfile; if (dir->exclude_list) return; for (file = excfile_head; file; file = file->next) { if (faccessat (dir ? dir->fd : chdir_fd, file->name, F_OK, 0) == 0) { FILE *fp; struct exclude *ex = NULL; int fd = subfile_open (dir, file->name, O_RDONLY); if (fd == -1) { open_error (file->name); continue; } fp = fdopen (fd, "r"); if (!fp) { ERROR ((0, errno, _("%s: fdopen failed"), file->name)); close (fd); continue; } if (!ex) ex = new_exclude (); vcsfile = get_vcs_ignore_file (file->name); if (vcsfile->initfn) vcsfile->data = vcsfile->initfn (vcsfile->data); if (add_exclude_fp (vcsfile->addfn, ex, fp, EXCLUDE_WILDCARDS|EXCLUDE_ANCHORED, '\n', vcsfile->data)) { int e = errno; FATAL_ERROR ((0, e, "%s", quotearg_colon (file->name))); } fclose (fp); ent = xmalloc (sizeof (*ent)); ent->excluded = ex; ent->flags = file->flags == EXCL_DEFAULT ? file->flags : vcsfile->flags; ent->prev = tail; ent->next = NULL; if (tail) tail->next = ent; else head = ent; tail = ent; } } dir->exclude_list = head; }
struct solv_zchunk * solv_zchunk_open(FILE *fp, unsigned int streamid) { struct solv_zchunk *zck; unsigned char *p; unsigned int hdr_size; /* preface + index + signatures */ unsigned int lead_size; unsigned int preface_size; unsigned int index_size; zck = solv_calloc(1, sizeof(*zck)); /* read and parse the lead, read the complete header */ zck->hdr = solv_calloc(15, 1); zck->hdr_end = zck->hdr + 15; if (fread(zck->hdr, 15, 1, fp) != 1 || memcmp(zck->hdr, "\000ZCK1", 5) != 0) return open_error(zck); p = zck->hdr + 5; if ((p = getchksum(p, zck->hdr_end, &zck->hdr_chk_type, &zck->hdr_chk_len, &zck->hdr_chk_id)) == 0) return open_error(zck); if ((p = getuint(p, zck->hdr_end, &hdr_size)) == 0 || hdr_size > MAX_HDR_SIZE) return open_error(zck); lead_size = p - zck->hdr + zck->hdr_chk_len; zck->hdr = solv_realloc(zck->hdr, lead_size + hdr_size); zck->hdr_end = zck->hdr + lead_size + hdr_size; if (fread(zck->hdr + 15, lead_size + hdr_size - 15, 1, fp) != 1) return open_error(zck); /* verify header checksum to guard against corrupt files */ if (zck->hdr_chk_id) { Chksum *chk = solv_chksum_create(zck->hdr_chk_id); if (!chk) return open_error(zck); solv_chksum_add(chk, zck->hdr, lead_size - zck->hdr_chk_len); solv_chksum_add(chk, zck->hdr + lead_size, hdr_size); if (memcmp(solv_chksum_get(chk, 0), zck->hdr + (lead_size - zck->hdr_chk_len), zck->hdr_chk_len) != 0) { solv_chksum_free(chk, 0); return open_error(zck); } solv_chksum_free(chk, 0); } /* parse preface: data chksum, flags, compression */ p = zck->hdr + lead_size; if (p + zck->hdr_chk_len > zck->hdr_end) return open_error(zck); zck->data_chk_ptr = p; p += zck->hdr_chk_len; #ifdef VERIFY_DATA_CHKSUM if (zck->hdr_chk_id && (zck->data_chk = solv_chksum_create(zck->hdr_chk_id)) == 0) return open_error(zck); #endif if ((p = getuint(p, zck->hdr_end, &zck->flags)) == 0) return open_error(zck); if ((zck->flags & ~(3)) != 0) return open_error(zck); if ((p = getuint(p, zck->hdr_end, &zck->comp)) == 0 || (zck->comp != 0 && zck->comp != 2)) return open_error(zck); /* only uncompressed + zstd supported */ /* skip all optional elements if present */ if ((zck->flags & 2) != 0) { unsigned int nopt, lopt; if ((p = getuint(p, zck->hdr_end, &nopt)) == 0) return open_error(zck); for (; nopt != 0; nopt--) { if ((p = getuint(p, zck->hdr_end, &lopt)) == 0) return open_error(zck); if ((p = getuint(p, zck->hdr_end, &lopt)) == 0) return open_error(zck); if (p + lopt > zck->hdr_end) return open_error(zck); p += lopt; } } preface_size = p - (zck->hdr + lead_size); /* parse index: index size, index chksum type, num chunks, chunk data */ if ((p = getuint(p, zck->hdr_end, &index_size)) == 0) return open_error(zck); if (hdr_size < preface_size + index_size) return open_error(zck); if ((p = getchksum(p, zck->hdr_end, &zck->chunk_chk_type, &zck->chunk_chk_len, &zck->chunk_chk_id)) == 0) return open_error(zck); if ((p = getuint(p, zck->hdr_end, &zck->nchunks)) == 0 || zck->nchunks > MAX_CHUNK_CNT) return open_error(zck); /* setup decompressor */ if (zck->comp == 2) { if ((zck->dctx = ZSTD_createDCtx()) == 0) return open_error(zck); } zck->fp = fp; zck->chunks = p; zck->streamid = streamid; if (streamid == 0) { zck->nchunks = zck->nchunks ? 1 : 0; /* limit to dict chunk */ return zck; } /* setup dictionary */ if (!nextchunk(zck, 0)) { zck->fp = 0; return open_error(zck); } if (zck->comp == 2 && zck->buf_avail) { if ((zck->ddict = ZSTD_createDDict(zck->buf, zck->buf_avail)) == 0) { zck->fp = 0; return open_error(zck); } } zck->buf = solv_free(zck->buf); zck->buf_used = 0; zck->buf_avail = 0; /* ready to read the rest of the chunks */ return zck; }
/* * tmpfile() should be adequate, except that it may require special privileges * to use, e.g., MinGW and Windows 7 where it tries to use the root directory. */ static FILE * open_tmpfile(const char *label) { FILE *result; #if USE_MKSTEMP int fd; const char *tmpdir; char *name; const char *mark; if ((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0) { #ifdef P_tmpdir tmpdir = P_tmpdir; #else tmpdir = "/tmp"; #endif if (access(tmpdir, W_OK) != 0) tmpdir = "."; } name = malloc(strlen(tmpdir) + 10 + strlen(label)); result = 0; if (name != 0) { if ((mark = strrchr(label, '_')) == 0) mark = label + strlen(label); sprintf(name, "%s/%.*sXXXXXX", tmpdir, (int)(mark - label), label); fd = mkstemp(name); if (fd >= 0) { result = fdopen(fd, "w+"); if (result != 0) { MY_TMPFILES *item; if (my_tmpfiles == 0) { atexit(close_tmpfiles); } item = NEW(MY_TMPFILES); NO_SPACE(item); item->name = name; NO_SPACE(item->name); item->next = my_tmpfiles; my_tmpfiles = item; } } } #else result = tmpfile(); #endif if (result == 0) open_error(label); return result; }
static void open_files(void) { create_file_names(); if (input_file == 0) { input_file = fopen(input_file_name, "r"); if (input_file == 0) open_error(input_file_name); } action_file = open_tmpfile("action_file"); text_file = open_tmpfile("text_file"); if (vflag) { verbose_file = fopen(verbose_file_name, "w"); if (verbose_file == 0) open_error(verbose_file_name); } if (gflag) { graph_file = fopen(graph_file_name, "w"); if (graph_file == 0) open_error(graph_file_name); fprintf(graph_file, "digraph %s {\n", file_prefix); fprintf(graph_file, "\tedge [fontsize=10];\n"); fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n"); fprintf(graph_file, "\torientation=landscape;\n"); fprintf(graph_file, "\trankdir=LR;\n"); fprintf(graph_file, "\t/*\n"); fprintf(graph_file, "\tmargin=0.2;\n"); fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n"); fprintf(graph_file, "\tratio=auto;\n"); fprintf(graph_file, "\t*/\n"); } if (dflag) { defines_file = fopen(defines_file_name, "w"); if (defines_file == 0) open_error(defines_file_name); union_file = open_tmpfile("union_file"); } if (iflag) { externs_file = fopen(externs_file_name, "w"); if (externs_file == 0) open_error(externs_file_name); } output_file = fopen(output_file_name, "w"); if (output_file == 0) open_error(output_file_name); if (rflag) { code_file = fopen(code_file_name, "w"); if (code_file == 0) open_error(code_file_name); } else code_file = output_file; }
void open_files(void) { create_file_names(); if (input_file == 0) { input_file = fopen(input_file_name, "r"); if (input_file == 0) open_error(input_file_name); } #ifdef HAVE_MKSTEMP action_file = fdopen(action_fd, "w"); #else action_file = fopen(action_file_name, "w"); #endif if (action_file == 0) open_error(action_file_name); #ifdef HAVE_MKSTEMP entry_file = fdopen(entry_fd, "w"); #else entry_file = fopen(entry_file_name, "w"); #endif if (entry_file == 0) open_error(entry_file_name); #ifdef HAVE_MKSTEMP text_file = fdopen(text_fd, "w"); #else text_file = fopen(text_file_name, "w"); #endif if (text_file == 0) open_error(text_file_name); if (vflag) { verbose_file = fopen(verbose_file_name, "w"); if (verbose_file == 0) open_error(verbose_file_name); } if (dflag) { defines_file = fopen(defines_file_name, "w"); if (defines_file == 0) open_error(defines_file_name); #ifdef HAVE_MKSTEMP union_file = fdopen(union_fd, "w"); #else union_file = fopen(union_file_name, "w"); #endif if (union_file == 0) open_error(union_file_name); } output_file = fopen(output_file_name, "w"); if (output_file == 0) open_error(output_file_name); if (rflag) { code_file = fopen(code_file_name, "w"); if (code_file == 0) open_error(code_file_name); } else code_file = output_file; interface_file = fopen(interface_file_name, "w"); if (interface_file == 0) open_error(interface_file_name); }
void create_file_names(void) { int i, len; char *tmpdir; #ifdef NO_UNIX len = 0; i = sizeof(temp_form); #else tmpdir = getenv("TMPDIR"); if (tmpdir == 0) tmpdir = "/tmp"; len = strlen(tmpdir); i = len + sizeof(temp_form); if (len && tmpdir[len-1] != '/') ++i; #endif action_file_name = MALLOC(i); if (action_file_name == 0) no_space(); entry_file_name = MALLOC(i); if (entry_file_name == 0) no_space(); text_file_name = MALLOC(i); if (text_file_name == 0) no_space(); union_file_name = MALLOC(i); if (union_file_name == 0) no_space(); #ifndef NO_UNIX strcpy(action_file_name, tmpdir); strcpy(entry_file_name, tmpdir); strcpy(text_file_name, tmpdir); strcpy(union_file_name, tmpdir); if (len && tmpdir[len - 1] != '/') { action_file_name[len] = '/'; entry_file_name[len] = '/'; text_file_name[len] = '/'; union_file_name[len] = '/'; ++len; } #endif strcpy(action_file_name + len, temp_form); strcpy(entry_file_name + len, temp_form); strcpy(text_file_name + len, temp_form); strcpy(union_file_name + len, temp_form); action_file_name[len + 5] = 'a'; entry_file_name[len + 5] = 'e'; text_file_name[len + 5] = 't'; union_file_name[len + 5] = 'u'; #ifndef NO_UNIX #ifdef HAVE_MKSTEMP action_fd = mkstemp(action_file_name); if (action_fd == -1) open_error(action_file_name); entry_fd = mkstemp(entry_file_name); if (entry_fd == -1) open_error(entry_file_name); text_fd = mkstemp(text_file_name); if (text_fd == -1) open_error(text_file_name); union_fd = mkstemp(union_file_name); if (union_fd == -1) open_error(union_file_name); #else mktemp(action_file_name); mktemp(entry_file_name); mktemp(text_file_name); mktemp(union_file_name); #endif #endif len = strlen(file_prefix); output_file_name = MALLOC(len + 7); if (output_file_name == 0) no_space(); strcpy(output_file_name, file_prefix); strcpy(output_file_name + len, OUTPUT_SUFFIX); code_file_name = output_file_name; if (vflag) { verbose_file_name = MALLOC(len + 8); if (verbose_file_name == 0) no_space(); strcpy(verbose_file_name, file_prefix); strcpy(verbose_file_name + len, VERBOSE_SUFFIX); } interface_file_name = MALLOC(len + 8); if (interface_file_name == 0) no_space(); strcpy(interface_file_name, file_prefix); strcpy(interface_file_name + len, INTERFACE_SUFFIX); }