1 | /********************************************************************** |
---|
2 | * File Exchange return client |
---|
3 | * |
---|
4 | * $Id: return.c,v 1.1 1999-09-28 22:10:59 danw Exp $ |
---|
5 | * |
---|
6 | * Copyright 1989, 1990 by the Massachusetts Institute of Technology. |
---|
7 | * |
---|
8 | * For copying and distribution information, please see the file |
---|
9 | * <mit-copyright.h>. |
---|
10 | **********************************************************************/ |
---|
11 | |
---|
12 | #include <mit-copyright.h> |
---|
13 | |
---|
14 | #ifndef lint |
---|
15 | static char rcsid_return_c[] = "$Id: return.c,v 1.1 1999-09-28 22:10:59 danw Exp $"; |
---|
16 | #endif /* lint */ |
---|
17 | |
---|
18 | #include <stdio.h> |
---|
19 | #include <ctype.h> |
---|
20 | #include <string.h> |
---|
21 | #include <sys/types.h> |
---|
22 | #include <sys/stat.h> |
---|
23 | #include <sys/errno.h> |
---|
24 | #include "fxmain.h" |
---|
25 | |
---|
26 | /* |
---|
27 | * compar -- compares two papers by modify time, used by qsort |
---|
28 | */ |
---|
29 | |
---|
30 | compar(p1, p2) |
---|
31 | Paper **p1, **p2; |
---|
32 | { |
---|
33 | int ret; |
---|
34 | |
---|
35 | ret = strcmp((*p1)->author, (*p2)->author); |
---|
36 | if (ret) return(ret); |
---|
37 | ret = strcmp((*p1)->filename, (*p2)->filename); |
---|
38 | if (ret) return(ret); |
---|
39 | ret = (int) (*p1)->modified.tv_sec - (*p2)->modified.tv_sec; |
---|
40 | if (ret) return(ret); |
---|
41 | return((int) ((*p1)->modified.tv_usec - (*p2)->modified.tv_usec)); |
---|
42 | } |
---|
43 | |
---|
44 | /* |
---|
45 | * do_return -- returns papers from files |
---|
46 | */ |
---|
47 | |
---|
48 | /*ARGSUSED*/ |
---|
49 | long |
---|
50 | do_return(fxp, criterion, flags, arg) |
---|
51 | FX *fxp; |
---|
52 | Paper *criterion; |
---|
53 | int flags; |
---|
54 | char *arg; |
---|
55 | { |
---|
56 | extern int errno; |
---|
57 | long code; |
---|
58 | Paperlist_res *plist; |
---|
59 | int count, i; |
---|
60 | char *s; |
---|
61 | char filename[256]; |
---|
62 | Paper **paperv; |
---|
63 | struct stat buf; |
---|
64 | Paper gpaper; |
---|
65 | PaperType newtype; |
---|
66 | |
---|
67 | newtype = criterion->type; |
---|
68 | criterion->type = TAKEN; |
---|
69 | /******** get list of papers from server ********/ |
---|
70 | code = fx_list(fxp, criterion, &plist); |
---|
71 | criterion->type = newtype; |
---|
72 | if (code) { |
---|
73 | strcpy(fxmain_error_context, "while retrieving list"); |
---|
74 | return(code); |
---|
75 | } |
---|
76 | |
---|
77 | count = get_array(plist->Paperlist_res_u.list, &paperv); |
---|
78 | |
---|
79 | /******** deal with empty list ********/ |
---|
80 | if (count == 0) { |
---|
81 | if (flags & VERBOSE) |
---|
82 | printf("No papers to return.\n"); |
---|
83 | return(0L); |
---|
84 | } |
---|
85 | |
---|
86 | /******** main loop through list ********/ |
---|
87 | for (i=0; i<count; i++) { |
---|
88 | |
---|
89 | /*** Skip duplicates ***/ |
---|
90 | if (i < count-1 && |
---|
91 | !strcmp(paperv[i]->author, paperv[i+1]->author) && |
---|
92 | !strcmp(paperv[i]->filename, paperv[i+1]->filename)) { |
---|
93 | if (!(flags & LISTONLY)) fx_delete(fxp, paperv[i]); |
---|
94 | continue; |
---|
95 | } |
---|
96 | |
---|
97 | /******* Skip papers not in time range ********/ |
---|
98 | if (paperv[i]->modified.tv_sec < criterion->created.tv_sec || |
---|
99 | paperv[i]->modified.tv_sec > criterion->modified.tv_sec) continue; |
---|
100 | |
---|
101 | /*** Form filename ***/ |
---|
102 | (void) sprintf(filename, "%s/%s", paperv[i]->author, |
---|
103 | paperv[i]->filename); |
---|
104 | /* change spaces to underscores */ |
---|
105 | for (s=filename; *s != '\0'; s++) |
---|
106 | if (isspace(*s)) *s = '_'; |
---|
107 | |
---|
108 | if (flags & VERBOSE) { |
---|
109 | /******** print information about file ********/ |
---|
110 | printf("%5d %-9s %9d %-16.16s %s\n", paperv[i]->assignment, |
---|
111 | paperv[i]->owner, paperv[i]->size, |
---|
112 | ctime(&(paperv[i]->created.tv_sec)), filename); |
---|
113 | } |
---|
114 | |
---|
115 | if (stat(filename, &buf)) { |
---|
116 | printf(" Couldn't return %s (%s)\n", filename, |
---|
117 | error_message((long) errno)); |
---|
118 | continue; |
---|
119 | } |
---|
120 | |
---|
121 | if (buf.st_mtime == paperv[i]->created.tv_sec) { |
---|
122 | printf(" Won't return %s (not modified)\n", filename); |
---|
123 | continue; |
---|
124 | } |
---|
125 | if (!(flags & LISTONLY)) { |
---|
126 | /******** return file to server ********/ |
---|
127 | paper_copy(paperv[i], &gpaper); |
---|
128 | gpaper.type = newtype; |
---|
129 | code = fx_send_file(fxp, &gpaper, filename); |
---|
130 | if (code) { |
---|
131 | sprintf(fxmain_error_context, "while returning \"%s\"", filename); |
---|
132 | return(code); |
---|
133 | } |
---|
134 | printf(" Returned %s to %s.\n", filename, full_name(gpaper.author)); |
---|
135 | fx_delete(fxp, paperv[i]); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | /******** clean up ********/ |
---|
141 | fx_list_destroy(&plist); |
---|
142 | free((char *) paperv); |
---|
143 | return(0L); |
---|
144 | } |
---|
145 | |
---|
146 | main(argc, argv) |
---|
147 | int argc; |
---|
148 | char *argv[]; |
---|
149 | { |
---|
150 | Paper p; |
---|
151 | |
---|
152 | paper_clear(&p); |
---|
153 | p.type = GRADED; |
---|
154 | if (fxmain(argc, argv, |
---|
155 | "Usage: %s [-c course] [options] [assignment] [student ...]\n", |
---|
156 | &p, NULL, do_return)) exit(1); |
---|
157 | exit(0); |
---|
158 | } |
---|