void copy_file (char const *from, char const *to, struct stat *tost, int to_flags, mode_t mode, bool to_dir_known_to_exist) { int tofd; if (debug & 4) say ("Copying %s %s to %s\n", S_ISLNK (mode) ? "symbolic link" : "file", quotearg_n (0, from), quotearg_n (1, to)); if (S_ISLNK (mode)) { char *buffer = xmalloc (PATH_MAX); if (readlink (from, buffer, PATH_MAX) < 0) pfatal ("Can't read %s %s", "symbolic link", from); if (symlink (buffer, to) != 0) pfatal ("Can't create %s %s", "symbolic link", to); if (tost && lstat (to, tost) != 0) pfatal ("Can't get file attributes of %s %s", "symbolic link", to); free (buffer); } else { assert (S_ISREG (mode)); tofd = create_file (to, O_WRONLY | O_BINARY | to_flags, mode, to_dir_known_to_exist); copy_to_fd (from, tofd); if (tost && fstat (tofd, tost) != 0) pfatal ("Can't get file attributes of %s %s", "file", to); if (close (tofd) != 0) write_fatal (); } }
void append_to_file (char const *from, char const *to) { int tofd; if ((tofd = open (to, O_WRONLY | O_BINARY | O_APPEND)) < 0) pfatal ("Can't reopen file %s", quotearg (to)); copy_to_fd (from, tofd); if (close (tofd) != 0) write_fatal (); }
void copy_file (char const *from, char const *to, int to_flags, mode_t mode) { int tofd; int fromfd; size_t i; if ((fromfd = open (from, O_RDONLY | O_BINARY, 0)) < 0) pfatal ("Can't reopen file %s", quotearg (from)); tofd = create_file (to, O_WRONLY | O_BINARY | to_flags, mode); while ((i = read (fromfd, buf, bufsize)) != 0) { if (i == (size_t) -1) read_fatal (); if (write (tofd, buf, i) != i) write_fatal (); } if (close (fromfd) != 0) read_fatal (); if (close (tofd) != 0) write_fatal (); }
static void copy_to_fd (const char *from, int tofd) { int fromfd; ssize_t i; if ((fromfd = open (from, O_RDONLY | O_BINARY)) < 0) pfatal ("Can't reopen file %s", quotearg (from)); while ((i = read (fromfd, buf, bufsize)) != 0) { if (i == (ssize_t) -1) read_fatal (); if (full_write (tofd, buf, i) != i) write_fatal (); } if (close (fromfd) != 0) read_fatal (); }
static void plan_b (char const *filename) { register FILE *ifp; register int c; register size_t len; register size_t maxlen; register bool found_revision; register size_t i; register char const *rev; register size_t revlen; register LINENUM line = 1; int exclusive; if (instat.st_size == 0) filename = NULL_DEVICE; if (! (ifp = fopen (filename, binary_transput ? "rb" : "rt"))) pfatal ("Can't open file %s", quotearg (filename)); exclusive = TMPINNAME_needs_removal ? 0 : O_EXCL; TMPINNAME_needs_removal = 1; tifd = create_file (TMPINNAME, O_RDWR | O_BINARY | exclusive, (mode_t) 0); i = 0; len = 0; maxlen = 1; rev = revision; found_revision = !rev; revlen = rev ? strlen (rev) : 0; while ((c = getc (ifp)) != EOF) { len++; if (c == '\n') { if (++line < 0) too_many_lines (filename); if (maxlen < len) maxlen = len; len = 0; } if (!found_revision) { if (i == revlen) { found_revision = ISSPACE ((unsigned char) c); i = (size_t) -1; } else if (i != (size_t) -1) i = rev[i]==c ? i + 1 : (size_t) -1; if (i == (size_t) -1 && ISSPACE ((unsigned char) c)) i = 0; } } if (revision) report_revision (found_revision); Fseek (ifp, (off_t) 0, SEEK_SET); /* rewind file */ for (tibufsize = TIBUFSIZE_MINIMUM; tibufsize < maxlen; tibufsize <<= 1) continue; lines_per_buf = tibufsize / maxlen; tireclen = maxlen; tibuf[0] = (char *)malloc (2 * tibufsize); tibuf[1] = tibuf[0] + tibufsize; for (line = 1; ; line++) { char *p = tibuf[0] + maxlen * (line % lines_per_buf); char const *p0 = p; if (! (line % lines_per_buf)) /* new block */ if (write (tifd, tibuf[0], tibufsize) != tibufsize) write_fatal (); if ((c = getc (ifp)) == EOF) break; for (;;) { *p++ = c; if (c == '\n') { last_line_size = p - p0; break; } if ((c = getc (ifp)) == EOF) { last_line_size = p - p0; line++; goto EOF_reached; } } } EOF_reached: if (ferror (ifp) || fclose (ifp) != 0) read_fatal (); if (line % lines_per_buf != 0) if (write (tifd, tibuf[0], tibufsize) != tibufsize) write_fatal (); input_lines = line - 1; }