Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtksh / ksh93 / ship / shipop.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: shipop.c /main/4 1996/10/04 15:58:45 drk $ */
24 /***************************************************************
25 *                                                              *
26 *                      AT&T - PROPRIETARY                      *
27 *                                                              *
28 *         THIS IS PROPRIETARY SOURCE CODE LICENSED BY          *
29 *                          AT&T CORP.                          *
30 *                                                              *
31 *                Copyright (c) 1995 AT&T Corp.                 *
32 *                     All Rights Reserved                      *
33 *                                                              *
34 *           This software is licensed by AT&T Corp.            *
35 *       under the terms and conditions of the license in       *
36 *       http://www.research.att.com/orgs/ssr/book/reuse        *
37 *                                                              *
38 *               This software was created by the               *
39 *           Software Engineering Research Department           *
40 *                    AT&T Bell Laboratories                    *
41 *                                                              *
42 *               For further information contact                *
43 *                     gsf@research.att.com                     *
44 *                                                              *
45 ***************************************************************/
46
47 /* : : generated by proto : : */
48
49 #if !defined(__PROTO__)
50 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
51 #if defined(__cplusplus)
52 #define __MANGLE__      "C"
53 #else
54 #define __MANGLE__
55 #endif
56 #define __STDARG__
57 #define __PROTO__(x)    x
58 #define __OTORP__(x)
59 #define __PARAM__(n,o)  n
60 #if !defined(__STDC__) && !defined(__cplusplus)
61 #if !defined(c_plusplus)
62 #define const
63 #endif
64 #define signed
65 #define void            int
66 #define volatile
67 #define __V_            char
68 #else
69 #define __V_            void
70 #endif
71 #else
72 #define __PROTO__(x)    ()
73 #define __OTORP__(x)    x
74 #define __PARAM__(n,o)  o
75 #define __MANGLE__
76 #define __V_            char
77 #define const
78 #define signed
79 #define void            int
80 #define volatile
81 #endif
82 #if defined(__cplusplus) || defined(c_plusplus)
83 #define __VARARG__      ...
84 #else
85 #define __VARARG__
86 #endif
87 #if defined(__STDARG__)
88 #define __VA_START__(p,a)       va_start(p,a)
89 #else
90 #define __VA_START__(p,a)       va_start(p)
91 #endif
92 #endif
93 static const char id[] = "\n@(#)shipop (AT&T Bell Laboratories) 06/11/92\0\n";
94
95 #include <stdio.h>
96 #include <sys/types.h>
97 #include <sys/stat.h>
98
99 #define elementsof(x)   (sizeof(x)/sizeof(x[0]))
100
101 static char*    cmd = "shipop";
102 static char*    op;
103
104 static int      status;
105
106 extern __MANGLE__ int   close __PROTO__((int));
107 extern __MANGLE__ void  exit __PROTO__((int));
108 extern __MANGLE__ int   open __PROTO__((const char*, int, ...));
109 extern __MANGLE__ int   read __PROTO__((int, char*, int));
110 extern __MANGLE__ int   strcmp __PROTO__((const char*, const char*));
111 extern __MANGLE__ time_t        time __PROTO__((time_t*));
112
113 static void
114 error __PARAM__((int level, const char* file, const char* msg), (level, file, msg)) __OTORP__(int level; const char* file; const char* msg;){
115         fprintf(stderr, "%s: %s", cmd, op);
116         if (level == 1) fprintf(stderr, ": warning");
117         if (file) fprintf(stderr, ": %s", file);
118         if (msg) fprintf(stderr, ": %s", msg);
119         fprintf(stderr, "\n");
120         if (level > status)
121         {
122                 if (level > 2) exit(level - 2);
123                 status = level;
124         }
125 }
126
127 /*
128  * shipop newer [ reference [ file ... ] ]
129  */
130
131 static int
132 shipnewer __PARAM__((register char** argv), (argv)) __OTORP__(register char** argv;){
133         unsigned long   ref;
134         struct stat     st;
135
136         if (!*argv || stat(*argv, &st)) return(1);
137         ref = (unsigned long)st.st_mtime;
138         while (*++argv)
139                 if (stat(*argv, &st) || ref < (unsigned long)st.st_mtime) return(1);
140         return(0);
141 }
142
143 #define HASHPART(h,c)   (h = (h) * 987654321L + 123456879L + (c))
144
145 static char             buf[4096];
146
147 static unsigned long
148 sealfile __PARAM__((char* file, int fd, unsigned long s), (file, fd, s)) __OTORP__(char* file; int fd; unsigned long s;){
149         register unsigned char* b;
150         register unsigned char* e;
151         register int            n;
152
153         HASHPART(s, 0);
154         while ((n = read(fd, buf, sizeof(buf))) > 0)
155         {
156                 b = (unsigned char*)buf;
157                 e = b + n;
158                 while (b < e) HASHPART(s, *b++);
159         }
160         if (n < 0) error(2, file, "read error");
161         return(s);
162 }
163
164 /*
165  * shipop seal [ file ... ]
166  */
167
168 static int
169 shipseal __PARAM__((register char** argv), (argv)) __OTORP__(register char** argv;){
170         register char*  f;
171         register int    fd;
172         unsigned long   s;
173
174         s = 0;
175         if (!*argv) s = sealfile("/dev/stdin", 0, s);
176         else while (f = *argv++)
177         {
178                 if (*f == '-' && !*(f + 1)) s = sealfile("/dev/stdin", 0, s);
179                 else if ((fd = open(f, 0)) < 0) error(2, f, "cannot read");
180                 else
181                 {
182                         s = sealfile(f, fd, s);
183                         close(fd);
184                 }
185         }
186         printf("%08lx\n", s);
187         return(status);
188 }
189
190 /*
191  * shipop state reference [ file ... | <file-list ]
192  */
193
194 static int
195 shipstate __PARAM__((register char** argv), (argv)) __OTORP__(register char** argv;){
196         register char*  s;
197         register int    c;
198         long            ref;
199         struct stat     st;
200
201         if (!(s = *argv++) || stat(s, &st)) error(3, (char*)0, "reference file omitted");
202         ref = (long)st.st_mtime;
203         if (s = *argv++) do
204         {
205                 if (!stat(s, &st))
206                         printf("%s %ld\n", s, (long)st.st_mtime - ref);
207         } while (s = *argv++);
208         else do
209         {
210                 s = buf;
211                 while ((c = getchar()) != EOF && c != ' ' && c != '\n')
212                         if (s < buf + sizeof(buf) - 1) *s++ = c;
213                 if (s > buf)
214                 {
215                         *s = 0;
216                         if (!stat(buf, &st))
217                                 printf("%s %ld\n", buf, (long)st.st_mtime - ref);
218                 }
219         } while (c != EOF);
220         return(status);
221 }
222
223 /*
224  * shipop time [ file ]
225  */
226
227 static int
228 shiptime __PARAM__((register char** argv), (argv)) __OTORP__(register char** argv;){
229         struct stat     st;
230         time_t          date;
231
232         if (*argv && !stat(*argv, &st)) date = st.st_mtime;
233         else time(&date);
234         printf("%08lx\n", (long)date);
235         return(status);
236 }
237
238 /*
239  * shipop xap [ file ... ]
240  */
241
242 static int
243 shipxap __PARAM__((char** argv), (argv)) __OTORP__(char** argv;){
244         error(2, "xap not implemented yet", (char*)0);
245         return(1);
246 }
247
248 struct optab
249 {
250         char*   name;
251         int     (*func) __PROTO__((char**));
252 };
253
254 static struct optab ops[] =
255 {
256         "newer",        shipnewer,
257         "seal",         shipseal,
258         "state",        shipstate,
259         "time",         shiptime,
260         "xap",          shipxap,
261 };
262
263 int
264 main __PARAM__((int argc, char** argv), (argc, argv)) __OTORP__(int argc; char** argv;){
265         register char*  s;
266         register int    i;
267
268         if (!(s = *++argv))
269                 exit(0);
270         for (i = 0; i < elementsof(ops); i++)
271                 if (!strcmp(s, ops[i].name))
272                 {
273                         op = ops[i].name;
274                         exit((*ops[i].func)(argv + 1));
275                 }
276         fprintf(stderr, "Usage: %s %s", cmd, ops[0].name);
277         for (i = 1; i < elementsof(ops); i++)
278                 fprintf(stderr, " | %s", ops[i].name);
279         fprintf(stderr, " [ file ... ]\n");
280         exit(2);
281 }