source: trunk/athena/bin/gms/check_viewable.c @ 1498

Revision 1498, 3.1 KB checked in by eichin, 36 years ago (diff)
fixed an inverted status check
Line 
1/* This file is part of the Project Athena Global Message System.
2 * Created by: Mark W. Eichin <eichin@athena.mit.edu>
3 * $Source: /afs/dev.mit.edu/source/repository/athena/bin/gms/check_viewable.c,v $
4 * $Author: eichin $
5 *
6 *      Copyright (c) 1988 by the Massachusetts Institute of Technology.
7 *      For copying and distribution information, see the file
8 *      "mit-copyright.h".
9 */
10#include <mit-copyright.h>
11#ifndef lint
12static char rcsid_check_viewable_c[] = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/gms/check_viewable.c,v 1.4 1988-10-03 16:52:52 eichin Exp $";
13#endif lint
14
15#include "globalmessage.h"
16#include <strings.h>
17#include <sys/types.h>
18#include <sys/file.h>
19#include <pwd.h>
20
21Code_t check_viewable(message, checktime, updateuser)
22     char *message;
23     int checktime;             /* should we check the timestamp at all? */
24     int updateuser;            /* should we update the user timestamp file? */
25{
26  char *ptr, *usertfile;
27  time_t ftime, utime;
28  int status, usersize, ufd;
29  char *usertfilename;
30 
31  if(strncmp(message, GMS_VERSION_STRING, GMS_VERSION_STRING_LEN)) {
32    return(GMS_SERVER_VERSION);
33  }
34
35  ptr = index(message, '\n');
36  if(!ptr) {
37    return(GMS_SERVER_GARBLED);
38  }
39 
40  ftime = atol(&message[GMS_VERSION_STRING_LEN+1]);
41
42  {
43    struct passwd *pw;
44    char *userdir;
45
46    pw = getpwuid(getuid());
47     
48    if(pw) {
49      userdir = pw->pw_dir;
50    } else {
51      /* couldn't check user's file, probably better send it. */
52      free(usertfilename);
53      return(0);
54    }
55
56    usertfilename = malloc(GMS_USERFILE_NAME_LEN+strlen(userdir)+1);
57    strcpy(usertfilename, userdir);
58    strcat(usertfilename, GMS_USERFILE_NAME);
59
60    ufd = open(usertfilename, O_RDONLY, 0666);
61  }
62 
63  if(ufd != -1) {
64    /* read the file and close it */
65    status = read_to_memory(&usertfile, &usersize, ufd);
66    /* check the version string */
67    if(!status) {
68      if(!strncmp(usertfile, GMS_VERSION_STRING, GMS_VERSION_STRING_LEN)) {
69        /* now check for end of first line */
70        ptr = index(usertfile, '\n');
71        if(ptr) {
72          /* now we check the time stamp */
73          utime = atol(&usertfile[GMS_VERSION_STRING_LEN+1]);
74          if(ftime <= utime) {
75            /* user has already seen, we punt. */
76            free(usertfile);
77            free(usertfilename);
78            /* but we only punt if they have asked for it... */
79            if(checktime) {
80              return(GMS_OLD_MESSAGE);
81            } else {
82              return(0);
83            }
84          }
85        }
86      }
87      /* only valid if read_to_memory worked... */
88      free(usertfile);
89    }
90  }
91
92  /* only write out the new time stamp if they want it. */
93  if(updateuser) {
94    /* now, reopen/create the file to write new timestamp */
95    ufd = open(usertfilename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
96    if(ufd != -1) {
97      char *msg = &message[GMS_VERSION_STRING_LEN];
98   
99      /* write out the version number */
100      write(ufd, GMS_VERSION_STRING, GMS_VERSION_STRING_LEN);
101   
102      /* write out the timestring from the message file */
103      ptr = index(message, '\n')+1;
104      write(ufd, msg, ptr - msg);
105      close(ufd);
106    }
107  }
108  /* filename no longer needed... */
109  free(usertfilename);
110  /* if we tried to write, we want to print... */
111  return(0);
112}
113 
114
115
Note: See TracBrowser for help on using the repository browser.