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

Diff for /src/usr.bin/calendar/io.c between version 1.9 and 1.10

version 1.9, 2000/08/02 04:10:47 version 1.10, 2000/12/07 19:36:37
Line 53 
Line 53 
 #include <sys/types.h>  #include <sys/types.h>
 #include <sys/uio.h>  #include <sys/uio.h>
 #include <sys/wait.h>  #include <sys/wait.h>
   #include <sys/file.h>
   
 #include <ctype.h>  #include <ctype.h>
 #include <err.h>  #include <err.h>
Line 84 
Line 85 
 };  };
   
   
   int     openf(char *path);
   
 void  void
 cal()  cal()
 {  {
Line 105 
Line 108 
                         *p = '\0';                          *p = '\0';
                 else                  else
                         while ((ch = getchar()) != '\n' && ch != EOF);                          while ((ch = getchar()) != '\n' && ch != EOF);
                 for (l = strlen(buf);                  for (l = strlen(buf); l > 0 && isspace(buf[l - 1]); l--)
                      l > 0 && isspace(buf[l - 1]);  
                      l--)  
                         ;                          ;
                 buf[l] = '\0';                  buf[l] = '\0';
                 if (buf[0] == '\0')                  if (buf[0] == '\0')
Line 294 
Line 295 
   
 char path[MAXPATHLEN];  char path[MAXPATHLEN];
   
   int
   openf(path)
           char *path;
   {
           struct stat st;
           int fd;
   
           fd = open(path, O_RDONLY|O_NONBLOCK);
           if (fd == -1)
                   return (-1);
           if (fstat(fd, &st) == -1) {
                   close(fd);
                   return (-1);
           }
           if ((st.st_mode & S_IFMT) != S_IFREG) {
                   close (fd);
                   return (-1);
           }
   
           fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) &~ O_NONBLOCK);
           return (fd);
   }
   
 FILE *  FILE *
 opencal()  opencal()
 {  {
         int fd, pdes[2];          int fd, pdes[2];
           int fdin;
         struct stat sbuf;          struct stat sbuf;
   
         /* open up calendar file as stdin */          /* open up calendar file as stdin */
         if (!freopen(calendarFile, "r", stdin)) {          if ((fdin = openf(calendarFile)) == -1) {
                 if (doall) {                  if (doall) {
                     if (chdir(calendarHome) != 0)                          if (chdir(calendarHome) != 0)
                         return (NULL);                                  return (NULL);
                     if (stat(calendarNoMail, &sbuf) == 0)                          if (stat(calendarNoMail, &sbuf) == 0)
                         return (NULL);                                  return (NULL);
                     if (!freopen(calendarFile, "r", stdin))                          if ((fdin = openf(calendarFile)) == -1)
                         return (NULL);                                  return (NULL);
                 } else {                  } else {
                         char *home = getenv("HOME");                          char *home = getenv("HOME");
                         if (home == NULL || *home == '\0')                          if (home == NULL || *home == '\0')
                                 errx(1, "cannot get home directory");                                  errx(1, "cannot get home directory");
                         chdir(home);                          if (!(chdir(home) == 0 &&
                         if (!(chdir(calendarHome) == 0 &&                              chdir(calendarHome) == 0 &&
                               freopen(calendarFile, "r", stdin)))                              (fdin = openf(calendarFile)) != -1))
                                 errx(1, "no calendar file: ``%s'' or ``~/%s/%s",                                  errx(1, "no calendar file: ``%s'' or ``~/%s/%s",
                                     calendarFile, calendarHome, calendarFile);                                      calendarFile, calendarHome, calendarFile);
                 }                  }
Line 328 
Line 353 
                 (void)close(pdes[1]);                  (void)close(pdes[1]);
                 return (NULL);                  return (NULL);
         case 0:          case 0:
                 /* child -- stdin already setup, set stdout to pipe input */                  dup2(fdin, STDIN_FILENO);
                   /* child -- set stdout to pipe input */
                 if (pdes[1] != STDOUT_FILENO) {                  if (pdes[1] != STDOUT_FILENO) {
                         (void)dup2(pdes[1], STDOUT_FILENO);                          (void)dup2(pdes[1], STDOUT_FILENO);
                         (void)close(pdes[1]);                          (void)close(pdes[1]);
Line 402 
Line 428 
         (void)close(pdes[1]);          (void)close(pdes[1]);
 done:   (void)fclose(fp);  done:   (void)fclose(fp);
         (void)unlink(path);          (void)unlink(path);
         while (wait(&status) >= 0);          while (wait(&status) >= 0)
                   ;
 }  }
   
   

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