[BACK]Return to pch.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / patch

Diff for /src/usr.bin/patch/pch.c between version 1.60 and 1.61

version 1.60, 2018/04/07 14:55:13 version 1.61, 2019/12/02 22:17:32
Line 103 
Line 103 
                 pfp = fopen(TMPPATNAME, "w");                  pfp = fopen(TMPPATNAME, "w");
                 if (pfp == NULL)                  if (pfp == NULL)
                         pfatal("can't create %s", TMPPATNAME);                          pfatal("can't create %s", TMPPATNAME);
                 while (fgets(buf, sizeof buf, stdin) != NULL)                  while (getline(&buf, &bufsz, stdin) != -1)
                         fputs(buf, pfp);                          fprintf(pfp, "%s", buf);
                 fclose(pfp);                  fclose(pfp);
                 filename = TMPPATNAME;                  filename = TMPPATNAME;
         }          }
Line 266 
Line 266 
                 this_line = ftello(pfp);                  this_line = ftello(pfp);
                 indent = 0;                  indent = 0;
                 p_input_line++;                  p_input_line++;
                 if (fgets(buf, sizeof buf, pfp) == NULL) {                  if (getline(&buf, &bufsz, pfp) == -1) {
                         if (first_command_line >= 0) {                          if (first_command_line >= 0) {
                                 /* nothing but deletes!? */                                  /* nothing but deletes!? */
                                 p_start = first_command_line;                                  p_start = first_command_line;
Line 433 
Line 433 
 static void  static void
 skip_to(off_t file_pos, LINENUM file_line)  skip_to(off_t file_pos, LINENUM file_line)
 {  {
         char    *ret;          int     ret;
   
         if (p_base > file_pos)          if (p_base > file_pos)
                 fatal("Internal error: seek %lld>%lld\n",                  fatal("Internal error: seek %lld>%lld\n",
Line 442 
Line 442 
                 fseeko(pfp, p_base, SEEK_SET);                  fseeko(pfp, p_base, SEEK_SET);
                 say("The text leading up to this was:\n--------------------------\n");                  say("The text leading up to this was:\n--------------------------\n");
                 while (ftello(pfp) < file_pos) {                  while (ftello(pfp) < file_pos) {
                         ret = fgets(buf, sizeof buf, pfp);                          ret = getline(&buf, &bufsz, pfp);
                         if (ret == NULL)                          if (ret == -1)
                                 fatal("Unexpected end of file\n");                                  fatal("Unexpected end of file\n");
                         say("|%s", buf);                          say("|%s", buf);
                 }                  }
Line 501 
Line 501 
         off_t   repl_backtrack_position;        /* file pos of first repl line */          off_t   repl_backtrack_position;        /* file pos of first repl line */
         LINENUM repl_patch_line;                /* input line number for same */          LINENUM repl_patch_line;                /* input line number for same */
         LINENUM ptrn_copiable;                  /* # of copiable lines in ptrn */          LINENUM ptrn_copiable;                  /* # of copiable lines in ptrn */
         char    *s, *ret;          char    *s;
         int     context = 0;          int     context = 0;
           int     ret;
   
         while (p_end >= 0) {          while (p_end >= 0) {
                 if (p_end == p_efake)                  if (p_end == p_efake)
Line 526 
Line 527 
                 repl_patch_line = 0;                  repl_patch_line = 0;
                 ptrn_copiable = 0;                  ptrn_copiable = 0;
   
                 ret = pgets(buf, sizeof buf, pfp);                  ret = pgetline(&buf, &bufsz, pfp);
                 p_input_line++;                  p_input_line++;
                 if (ret == NULL || strnNE(buf, "********", 8)) {                  if (ret == -1 || strnNE(buf, "********", 8)) {
                         next_intuit_at(line_beginning, p_input_line);                          next_intuit_at(line_beginning, p_input_line);
                         return false;                          return false;
                 }                  }
Line 536 
Line 537 
                 p_hunk_beg = p_input_line + 1;                  p_hunk_beg = p_input_line + 1;
                 while (p_end < p_max) {                  while (p_end < p_max) {
                         line_beginning = ftello(pfp);                          line_beginning = ftello(pfp);
                         ret = pgets(buf, sizeof buf, pfp);                          ret = pgetline(&buf, &bufsz, pfp);
                         p_input_line++;                          p_input_line++;
                         if (ret == NULL) {                          if (ret == -1) {
                                 if (p_max - p_end < 4) {                                  if (p_max - p_end < 4) {
                                         /* assume blank lines got chopped */                                          /* assume blank lines got chopped */
                                         strlcpy(buf, "  \n", sizeof buf);                                          strlcpy(buf, "  \n", bufsz);
                                 } else {                                  } else {
                                         if (repl_beginning && repl_could_be_missing) {                                          if (repl_beginning && repl_could_be_missing) {
                                                 repl_missing = true;                                                  repl_missing = true;
Line 695 
Line 696 
                                 repl_could_be_missing = false;                                  repl_could_be_missing = false;
                 change_line:                  change_line:
                                 if (buf[1] == '\n' && canonicalize)                                  if (buf[1] == '\n' && canonicalize)
                                         strlcpy(buf + 1, " \n", sizeof buf - 1);                                          strlcpy(buf + 1, " \n", bufsz - 1);
                                 if (!isspace((unsigned char)buf[1]) &&                                  if (!isspace((unsigned char)buf[1]) &&
                                     buf[1] != '>' && buf[1] != '<' &&                                      buf[1] != '>' && buf[1] != '<' &&
                                     repl_beginning && repl_could_be_missing) {                                      repl_beginning && repl_could_be_missing) {
Line 861 
Line 862 
                 LINENUM filldst;        /* index of new lines */                  LINENUM filldst;        /* index of new lines */
                 char    ch;                  char    ch;
   
                 ret = pgets(buf, sizeof buf, pfp);                  ret = pgetline(&buf, &bufsz, pfp);
                 p_input_line++;                  p_input_line++;
                 if (ret == NULL || strnNE(buf, "@@ -", 4)) {                  if (ret == -1 || strnNE(buf, "@@ -", 4)) {
                         next_intuit_at(line_beginning, p_input_line);                          next_intuit_at(line_beginning, p_input_line);
                         return false;                          return false;
                 }                  }
Line 900 
Line 901 
                 fillsrc = 1;                  fillsrc = 1;
                 filldst = fillsrc + p_ptrn_lines;                  filldst = fillsrc + p_ptrn_lines;
                 p_end = filldst + p_repl_lines;                  p_end = filldst + p_repl_lines;
                 snprintf(buf, sizeof buf, "*** %ld,%ld ****\n", p_first,                  snprintf(buf, bufsz, "*** %ld,%ld ****\n", p_first,
                     p_first + p_ptrn_lines - 1);                      p_first + p_ptrn_lines - 1);
                 p_line[0] = savestr(buf);                  p_line[0] = savestr(buf);
                 if (out_of_mem) {                  if (out_of_mem) {
Line 908 
Line 909 
                         return false;                          return false;
                 }                  }
                 p_char[0] = '*';                  p_char[0] = '*';
                 snprintf(buf, sizeof buf, "--- %ld,%ld ----\n", p_newfirst,                  snprintf(buf, bufsz, "--- %ld,%ld ----\n", p_newfirst,
                     p_newfirst + p_repl_lines - 1);                      p_newfirst + p_repl_lines - 1);
                 p_line[filldst] = savestr(buf);                  p_line[filldst] = savestr(buf);
                 if (out_of_mem) {                  if (out_of_mem) {
Line 921 
Line 922 
                 p_hunk_beg = p_input_line + 1;                  p_hunk_beg = p_input_line + 1;
                 while (fillsrc <= p_ptrn_lines || filldst <= p_end) {                  while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
                         line_beginning = ftello(pfp);                          line_beginning = ftello(pfp);
                         ret = pgets(buf, sizeof buf, pfp);                          ret = pgetline(&buf, &bufsz, pfp);
                         p_input_line++;                          p_input_line++;
                         if (ret == NULL) {                          if (ret == -1) {
                                 if (p_max - filldst < 3) {                                  if (p_max - filldst < 3) {
                                         /* assume blank lines got chopped */                                          /* assume blank lines got chopped */
                                         strlcpy(buf, " \n", sizeof buf);                                          strlcpy(buf, " \n", bufsz);
                                 } else {                                  } else {
                                         fatal("unexpected end of file in patch\n");                                          fatal("unexpected end of file in patch\n");
                                 }                                  }
Line 1025 
Line 1026 
                 off_t   line_beginning = ftello(pfp);                  off_t   line_beginning = ftello(pfp);
   
                 p_context = 0;                  p_context = 0;
                 ret = pgets(buf, sizeof buf, pfp);                  ret = pgetline(&buf, &bufsz, pfp);
                 p_input_line++;                  p_input_line++;
                 if (ret == NULL || !isdigit((unsigned char)*buf)) {                  if (ret == -1 || !isdigit((unsigned char)*buf)) {
                         next_intuit_at(line_beginning, p_input_line);                          next_intuit_at(line_beginning, p_input_line);
                         return false;                          return false;
                 }                  }
Line 1063 
Line 1064 
                             p_end, p_input_line, buf);                              p_end, p_input_line, buf);
                 while (p_end >= hunkmax)                  while (p_end >= hunkmax)
                         grow_hunkmax();                          grow_hunkmax();
                 snprintf(buf, sizeof buf, "*** %ld,%ld\n", p_first,                  snprintf(buf, bufsz, "*** %ld,%ld\n", p_first,
                     p_first + p_ptrn_lines - 1);                      p_first + p_ptrn_lines - 1);
                 p_line[0] = savestr(buf);                  p_line[0] = savestr(buf);
                 if (out_of_mem) {                  if (out_of_mem) {
Line 1072 
Line 1073 
                 }                  }
                 p_char[0] = '*';                  p_char[0] = '*';
                 for (i = 1; i <= p_ptrn_lines; i++) {                  for (i = 1; i <= p_ptrn_lines; i++) {
                         ret = pgets(buf, sizeof buf, pfp);                          ret = pgetline(&buf, &bufsz, pfp);
                         p_input_line++;                          p_input_line++;
                         if (ret == NULL)                          if (ret == -1)
                                 fatal("unexpected end of file in patch at line %ld\n",                                  fatal("unexpected end of file in patch at line %ld\n",
                                     p_input_line);                                      p_input_line);
                         if (*buf != '<')                          if (*buf != '<')
Line 1094 
Line 1095 
                         (p_line[i - 1])[p_len[i - 1]] = 0;                          (p_line[i - 1])[p_len[i - 1]] = 0;
                 }                  }
                 if (hunk_type == 'c') {                  if (hunk_type == 'c') {
                         ret = pgets(buf, sizeof buf, pfp);                          ret = pgetline(&buf, &bufsz, pfp);
                         p_input_line++;                          p_input_line++;
                         if (ret == NULL)                          if (ret == -1)
                                 fatal("unexpected end of file in patch at line %ld\n",                                  fatal("unexpected end of file in patch at line %ld\n",
                                     p_input_line);                                      p_input_line);
                         if (*buf != '-')                          if (*buf != '-')
                                 fatal("--- expected at line %ld of patch\n",                                  fatal("--- expected at line %ld of patch\n",
                                     p_input_line);                                      p_input_line);
                 }                  }
                 snprintf(buf, sizeof(buf), "--- %ld,%ld\n", min, max);                  snprintf(buf, bufsz, "--- %ld,%ld\n", min, max);
                 p_line[i] = savestr(buf);                  p_line[i] = savestr(buf);
                 if (out_of_mem) {                  if (out_of_mem) {
                         p_end = i - 1;                          p_end = i - 1;
Line 1111 
Line 1112 
                 }                  }
                 p_char[i] = '=';                  p_char[i] = '=';
                 for (i++; i <= p_end; i++) {                  for (i++; i <= p_end; i++) {
                         ret = pgets(buf, sizeof buf, pfp);                          ret = pgetline(&buf, &bufsz, pfp);
                         p_input_line++;                          p_input_line++;
                         if (ret == NULL)                          if (ret == -1)
                                 fatal("unexpected end of file in patch at line %ld\n",                                  fatal("unexpected end of file in patch at line %ld\n",
                                     p_input_line);                                      p_input_line);
                         if (*buf != '>')                          if (*buf != '>')
Line 1160 
Line 1161 
 /*  /*
  * Input a line from the patch file, worrying about indentation.   * Input a line from the patch file, worrying about indentation.
  */   */
 char *  int
 pgets(char *bf, int sz, FILE *fp)  pgetline(char **bf, size_t *sz, FILE *fp)
 {  {
         char    *s, *ret = fgets(bf, sz, fp);          char    *s;
         int     indent = 0;          int     indent = 0;
           int     ret;
   
         if (p_indent && ret != NULL) {          ret = getline(bf, sz, fp);
   
           if (p_indent && ret != -1) {
                 for (s = buf;                  for (s = buf;
                     indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X');                      indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X');
                     s++) {                      s++) {
Line 1175 
Line 1179 
                         else                          else
                                 indent++;                                  indent++;
                 }                  }
                 if (buf != s && strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))                  if (buf != s && strlcpy(buf, s, bufsz) >= bufsz)
                         fatal("buffer too small in pgets()\n");                          fatal("buffer too small in pgetline()\n");
         }          }
         return ret;          return ret;
 }  }

Legend:
Removed from v.1.60  
changed lines
  Added in v.1.61