[BACK]Return to util.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / usr.bin / patch

Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.

Diff for /src/usr.bin/patch/util.c between version 1.9 and 1.10

version 1.9, 2002/03/08 21:57:33 version 1.10, 2002/03/11 18:47:51
Line 9  __RCSID("$NetBSD$");
Line 9  __RCSID("$NetBSD$");
 #include "INTERN.h"  #include "INTERN.h"
 #include "util.h"  #include "util.h"
 #include "backupfile.h"  #include "backupfile.h"
 #ifdef __STDC__  
 #include <stdarg.h>  
 #else  
 #include <varargs.h>  
 #endif  
   
   #include <stdarg.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <unistd.h>  #include <unistd.h>
 #include <fcntl.h>  #include <fcntl.h>
Line 34  move_file(char *from, char *to)
Line 30  move_file(char *from, char *to)
     if (strEQ(to, "-")) {      if (strEQ(to, "-")) {
 #ifdef DEBUGGING  #ifdef DEBUGGING
         if (debug & 4)          if (debug & 4)
             say2("Moving %s to stdout.\n", from);              say("Moving %s to stdout.\n", from);
 #endif  #endif
         fromfd = open(from, 0);          fromfd = open(from, 0);
         if (fromfd < 0)          if (fromfd < 0)
             pfatal2("internal error, can't reopen %s", from);              pfatal("internal error, can't reopen %s", from);
         while ((i=read(fromfd, buf, sizeof buf)) > 0)          while ((i=read(fromfd, buf, sizeof buf)) > 0)
             if (write(1, buf, i) != 1)              if (write(1, buf, i) != 1)
                 pfatal1("write failed");                  pfatal("write failed");
         Close(fromfd);          Close(fromfd);
         return 0;          return 0;
     }      }
Line 53  move_file(char *from, char *to)
Line 49  move_file(char *from, char *to)
 #ifndef NODIR  #ifndef NODIR
         char *backupname = find_backup_file_name(to);          char *backupname = find_backup_file_name(to);
         if (backupname == (char *) 0)          if (backupname == (char *) 0)
             fatal1("out of memory\n");              fatal("out of memory\n");
         Strcpy(bakname, backupname);          Strcpy(bakname, backupname);
         free(backupname);          free(backupname);
 #else /* NODIR */  #else /* NODIR */
Line 86  move_file(char *from, char *to)
Line 82  move_file(char *from, char *to)
         while (unlink(bakname) >= 0) ;  /* while() is for benefit of Eunice */          while (unlink(bakname) >= 0) ;  /* while() is for benefit of Eunice */
 #ifdef DEBUGGING  #ifdef DEBUGGING
         if (debug & 4)          if (debug & 4)
             say3("Moving %s to %s.\n", to, bakname);              say("Moving %s to %s.\n", to, bakname);
 #endif  #endif
         if (link(to, bakname) < 0) {          if (link(to, bakname) < 0) {
             /* Maybe `to' is a symlink into a different file system.              /* Maybe `to' is a symlink into a different file system.
Line 97  move_file(char *from, char *to)
Line 93  move_file(char *from, char *to)
   
             bakfd = creat(bakname, 0666);              bakfd = creat(bakname, 0666);
             if (bakfd < 0) {              if (bakfd < 0) {
                 say4("Can't backup %s, output is in %s: %s\n", to, from,                  say("Can't backup %s, output is in %s: %s\n", to, from,
                      strerror(errno));                      strerror(errno));
                 return -1;                  return -1;
             }              }
             tofd = open(to, 0);              tofd = open(to, 0);
             if (tofd < 0)              if (tofd < 0)
                 pfatal2("internal error, can't open %s", to);                  pfatal("internal error, can't open %s", to);
             while ((i=read(tofd, buf, sizeof buf)) > 0)              while ((i=read(tofd, buf, sizeof buf)) > 0)
                 if (write(bakfd, buf, i) != i)                  if (write(bakfd, buf, i) != i)
                     pfatal1("write failed");                      pfatal("write failed");
             Close(tofd);              Close(tofd);
             Close(bakfd);              Close(bakfd);
         }          }
Line 114  move_file(char *from, char *to)
Line 110  move_file(char *from, char *to)
     }      }
 #ifdef DEBUGGING  #ifdef DEBUGGING
     if (debug & 4)      if (debug & 4)
         say3("Moving %s to %s.\n", from, to);          say("Moving %s to %s.\n", from, to);
 #endif  #endif
     if (link(from, to) < 0) {           /* different file system? */      if (link(from, to) < 0) {           /* different file system? */
         int tofd;          int tofd;
   
         tofd = creat(to, 0666);          tofd = creat(to, 0666);
         if (tofd < 0) {          if (tofd < 0) {
             say4("Can't create %s, output is in %s: %s\n",              say("Can't create %s, output is in %s: %s\n",
               to, from, strerror(errno));                to, from, strerror(errno));
             return -1;              return -1;
         }          }
         fromfd = open(from, 0);          fromfd = open(from, 0);
         if (fromfd < 0)          if (fromfd < 0)
             pfatal2("internal error, can't reopen %s", from);              pfatal("internal error, can't reopen %s", from);
         while ((i=read(fromfd, buf, sizeof buf)) > 0)          while ((i=read(fromfd, buf, sizeof buf)) > 0)
             if (write(tofd, buf, i) != i)              if (write(tofd, buf, i) != i)
                 pfatal1("write failed");                  pfatal("write failed");
         Close(fromfd);          Close(fromfd);
         Close(tofd);          Close(tofd);
     }      }
Line 149  copy_file(char *from, char *to)
Line 145  copy_file(char *from, char *to)
   
     tofd = creat(to, 0666);      tofd = creat(to, 0666);
     if (tofd < 0)      if (tofd < 0)
         pfatal2("can't create %s", to);          pfatal("can't create %s", to);
     fromfd = open(from, 0);      fromfd = open(from, 0);
     if (fromfd < 0)      if (fromfd < 0)
         pfatal2("internal error, can't reopen %s", from);          pfatal("internal error, can't reopen %s", from);
     while ((i=read(fromfd, buf, sizeof buf)) > 0)      while ((i=read(fromfd, buf, sizeof buf)) > 0)
         if (write(tofd, buf, i) != i)          if (write(tofd, buf, i) != i)
             pfatal2("write to %s failed", to);              pfatal("write to %s failed", to);
     Close(fromfd);      Close(fromfd);
     Close(tofd);      Close(tofd);
 }  }
Line 172  savestr(char *s)
Line 168  savestr(char *s)
         s = "Oops";          s = "Oops";
     t = s;      t = s;
     while (*t++);      while (*t++);
     rv = malloc((MEM) (t - s));      rv = malloc(t - s);
     if (rv == Nullch) {      if (rv == NULL) {
         if (using_plan_a)          if (using_plan_a)
             out_of_mem = TRUE;              out_of_mem = TRUE;
         else          else
             fatal1("out of memory\n");              fatal("out of memory\n");
     }      }
     else {      else {
         t = rv;          t = rv;
Line 186  savestr(char *s)
Line 182  savestr(char *s)
     return rv;      return rv;
 }  }
   
 #if defined(lint) && defined(CANVARARG)  
   
 /*VARARGS ARGSUSED*/  
 say(pat) char *pat; { ; }  
 /*VARARGS ARGSUSED*/  
 fatal(pat) char *pat; { ; }  
 /*VARARGS ARGSUSED*/  
 pfatal(pat) char *pat; { ; }  
 /*VARARGS ARGSUSED*/  
 ask(pat) char *pat; { ; }  
   
 #else  
   
 /* Vanilla terminal output (buffered). */  /* Vanilla terminal output (buffered). */
   
 void  void
 #ifdef __STDC__  
 say(const char *pat, ...)  say(const char *pat, ...)
 #else  
 say(va_alist)  
         va_dcl  
 #endif  
 {  {
     va_list ap;      va_list ap;
 #ifdef __STDC__  
     va_start(ap, pat);      va_start(ap, pat);
 #else  
     const char *pat;  
   
     va_start(ap);  
     pat = va_arg(ap, const char *);  
 #endif  
   
     vfprintf(stderr, pat, ap);      vfprintf(stderr, pat, ap);
     va_end(ap);      va_end(ap);
Line 227  say(va_alist)
Line 198  say(va_alist)
 /* Terminal output, pun intended. */  /* Terminal output, pun intended. */
   
 void                            /* very void */  void                            /* very void */
 #ifdef __STDC__  
 fatal(const char *pat, ...)  fatal(const char *pat, ...)
 #else  
 fatal(va_alist)  
         va_dcl  
 #endif  
 {  {
     va_list ap;      va_list ap;
 #ifdef __STDC__  
     va_start(ap, pat);      va_start(ap, pat);
 #else  
     const char *pat;  
   
     va_start(ap);  
     pat = va_arg(ap, const char *);  
 #endif  
   
     fprintf(stderr, "patch: **** ");      fprintf(stderr, "patch: **** ");
     vfprintf(stderr, pat, ap);      vfprintf(stderr, pat, ap);
Line 253  fatal(va_alist)
Line 212  fatal(va_alist)
 /* Say something from patch, something from the system, then silence . . . */  /* Say something from patch, something from the system, then silence . . . */
   
 void                            /* very void */  void                            /* very void */
 #ifdef __STDC__  
 pfatal(const char *pat, ...)  pfatal(const char *pat, ...)
 #else  
 pfatal(va_alist)  
         va_dcl  
 #endif  
 {  {
     va_list ap;      va_list ap;
     int errnum = errno;      int errnum = errno;
 #ifdef __STDC__  
     va_start(ap, pat);      va_start(ap, pat);
 #else  
     const char *pat;  
   
     va_start(ap);  
     pat = va_arg(ap, const char *);  
 #endif  
   
     fprintf(stderr, "patch: **** ");      fprintf(stderr, "patch: **** ");
     vfprintf(stderr, pat, ap);      vfprintf(stderr, pat, ap);
Line 280  pfatal(va_alist)
Line 227  pfatal(va_alist)
 /* Get a response from the user, somehow or other. */  /* Get a response from the user, somehow or other. */
   
 void  void
 #ifdef __STDC__  
 ask(const char *pat, ...)  ask(const char *pat, ...)
 #else  
 ask(va_alist)  
         va_dcl  
 #endif  
 {  {
     int ttyfd;      int ttyfd;
     int r;      int r;
     bool tty2 = isatty(2);      bool tty2 = isatty(2);
     va_list ap;      va_list ap;
 #ifdef __STDC__  
     va_start(ap, pat);      va_start(ap, pat);
 #else  
     const char *pat;  
   
     va_start(ap);  
     pat = va_arg(ap, const char *);  
 #endif  
   
     (void) vsprintf(buf, pat, ap);      (void) vsprintf(buf, pat, ap);
     va_end(ap);      va_end(ap);
Line 332  ask(va_alist)
Line 267  ask(va_alist)
     else      else
         buf[r] = '\0';          buf[r] = '\0';
     if (!tty2)      if (!tty2)
         say2("%s",buf);          say("%s",buf);
 }  }
 #endif /* lint */  
   
 /* How to handle certain events when not in a critical region. */  /* How to handle certain events when not in a critical region. */
   
 void  void
 set_signals(int reset)  set_signals(int reset)
 {  {
 #ifndef lint      static void (*hupval)(int),(*intval)(int);
 #ifdef VOIDSIG  
     static void (*hupval) __P((int)),(*intval) __P((int));  
 #else  
     static int (*hupval) __P((int)),(*intval)__P((int));  
 #endif  
   
     if (!reset) {      if (!reset) {
         hupval = signal(SIGHUP, SIG_IGN);          hupval = signal(SIGHUP, SIG_IGN);
         if (hupval != SIG_IGN)          if (hupval != SIG_IGN)
 #ifdef VOIDSIG  
             hupval = my_exit;              hupval = my_exit;
 #else  
             hupval = (int(*) __P((int)))my_exit;  
 #endif  
         intval = signal(SIGINT, SIG_IGN);          intval = signal(SIGINT, SIG_IGN);
         if (intval != SIG_IGN)          if (intval != SIG_IGN)
 #ifdef VOIDSIG  
             intval = my_exit;              intval = my_exit;
 #else  
             intval = (int(*) __P((int)))my_exit;  
 #endif  
     }      }
     Signal(SIGHUP, hupval);      Signal(SIGHUP, hupval);
     Signal(SIGINT, intval);      Signal(SIGINT, intval);
 #endif  
 }  }
   
 /* How to handle certain events when in a critical region. */  /* How to handle certain events when in a critical region. */
Line 437  fetchname(char *at, int strip_leading, i
Line 357  fetchname(char *at, int strip_leading, i
     int sleading = strip_leading;      int sleading = strip_leading;
   
     if (!at)      if (!at)
         return Nullch;          return NULL;
     while (isspace((unsigned char)*at))      while (isspace((unsigned char)*at))
         at++;          at++;
 #ifdef DEBUGGING  #ifdef DEBUGGING
     if (debug & 128)      if (debug & 128)
         say4("fetchname %s %d %d\n",at,strip_leading,assume_exists);          say("fetchname %s %d %d\n",at,strip_leading,assume_exists);
 #endif  #endif
     filename_is_dev_null = FALSE;      filename_is_dev_null = FALSE;
     if (strnEQ(at, "/dev/null", 9)) {   /* so files can be created by diffing */      if (strnEQ(at, "/dev/null", 9)) {   /* so files can be created by diffing */
         filename_is_dev_null = TRUE;          filename_is_dev_null = TRUE;
         return Nullch;                  /*   against /dev/null. */          return NULL;                    /*   against /dev/null. */
     }      }
     name = fullname = t = savestr(at);      name = fullname = t = savestr(at);
   
Line 489  fetchname(char *at, int strip_leading, i
Line 409  fetchname(char *at, int strip_leading, i
             || try(     "%s%s", SCCSPREFIX, filebase))              || try(     "%s%s", SCCSPREFIX, filebase))
           return name;            return name;
         free(name);          free(name);
         name = Nullch;          name = NULL;
     }      }
   
     return name;      return name;

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

CVSweb <webmaster@jp.NetBSD.org>