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: ghudson $ |
---|
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 |
---|
12 | static char rcsid_check_viewable_c[] = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/gms/check_viewable.c,v 1.5 1996-09-19 22:39:16 ghudson Exp $"; |
---|
13 | #endif lint |
---|
14 | |
---|
15 | #include "globalmessage.h" |
---|
16 | #include <sys/types.h> |
---|
17 | #include <sys/file.h> |
---|
18 | #include <pwd.h> |
---|
19 | |
---|
20 | Code_t check_viewable(message, checktime, updateuser) |
---|
21 | char *message; |
---|
22 | int checktime; /* should we check the timestamp at all? */ |
---|
23 | int updateuser; /* should we update the user timestamp file? */ |
---|
24 | { |
---|
25 | char *ptr, *usertfile; |
---|
26 | time_t ftime, utime; |
---|
27 | int status, usersize, ufd; |
---|
28 | char *usertfilename; |
---|
29 | |
---|
30 | if(strncmp(message, GMS_VERSION_STRING, GMS_VERSION_STRING_LEN)) { |
---|
31 | return(GMS_SERVER_VERSION); |
---|
32 | } |
---|
33 | |
---|
34 | ptr = strchr(message, '\n'); |
---|
35 | if(!ptr) { |
---|
36 | return(GMS_SERVER_GARBLED); |
---|
37 | } |
---|
38 | |
---|
39 | ftime = atol(&message[GMS_VERSION_STRING_LEN+1]); |
---|
40 | |
---|
41 | { |
---|
42 | struct passwd *pw; |
---|
43 | char *userdir; |
---|
44 | |
---|
45 | pw = getpwuid(getuid()); |
---|
46 | |
---|
47 | if(pw) { |
---|
48 | userdir = pw->pw_dir; |
---|
49 | } else { |
---|
50 | /* couldn't check user's file, probably better send it. */ |
---|
51 | free(usertfilename); |
---|
52 | return(0); |
---|
53 | } |
---|
54 | |
---|
55 | usertfilename = malloc(GMS_USERFILE_NAME_LEN+strlen(userdir)+1); |
---|
56 | strcpy(usertfilename, userdir); |
---|
57 | strcat(usertfilename, GMS_USERFILE_NAME); |
---|
58 | |
---|
59 | ufd = open(usertfilename, O_RDONLY, 0666); |
---|
60 | } |
---|
61 | |
---|
62 | if(ufd != -1) { |
---|
63 | /* read the file and close it */ |
---|
64 | status = read_to_memory(&usertfile, &usersize, ufd); |
---|
65 | /* check the version string */ |
---|
66 | if(!status) { |
---|
67 | if(!strncmp(usertfile, GMS_VERSION_STRING, GMS_VERSION_STRING_LEN)) { |
---|
68 | /* now check for end of first line */ |
---|
69 | ptr = strchr(usertfile, '\n'); |
---|
70 | if(ptr) { |
---|
71 | /* now we check the time stamp */ |
---|
72 | utime = atol(&usertfile[GMS_VERSION_STRING_LEN+1]); |
---|
73 | if(ftime <= utime) { |
---|
74 | /* user has already seen, we punt. */ |
---|
75 | free(usertfile); |
---|
76 | free(usertfilename); |
---|
77 | /* but we only punt if they have asked for it... */ |
---|
78 | if(checktime) { |
---|
79 | return(GMS_OLD_MESSAGE); |
---|
80 | } else { |
---|
81 | return(0); |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | /* only valid if read_to_memory worked... */ |
---|
87 | free(usertfile); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | /* only write out the new time stamp if they want it. */ |
---|
92 | if(updateuser) { |
---|
93 | /* now, reopen/create the file to write new timestamp */ |
---|
94 | ufd = open(usertfilename, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
---|
95 | if(ufd != -1) { |
---|
96 | char *msg = &message[GMS_VERSION_STRING_LEN]; |
---|
97 | |
---|
98 | /* write out the version number */ |
---|
99 | write(ufd, GMS_VERSION_STRING, GMS_VERSION_STRING_LEN); |
---|
100 | |
---|
101 | /* write out the timestring from the message file */ |
---|
102 | ptr = strchr(message, '\n')+1; |
---|
103 | write(ufd, msg, ptr - msg); |
---|
104 | close(ufd); |
---|
105 | } |
---|
106 | } |
---|
107 | /* filename no longer needed... */ |
---|
108 | free(usertfilename); |
---|
109 | /* if we tried to write, we want to print... */ |
---|
110 | return(0); |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | |
---|