shipop.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 libraries 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. /* : : generated by proto : : */
  47. #if !defined(__PROTO__)
  48. #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
  49. #if defined(__cplusplus)
  50. #define __MANGLE__ "C"
  51. #else
  52. #define __MANGLE__
  53. #endif
  54. #define __STDARG__
  55. #define __PROTO__(x) x
  56. #define __OTORP__(x)
  57. #define __PARAM__(n,o) n
  58. #if !defined(__STDC__) && !defined(__cplusplus)
  59. #if !defined(c_plusplus)
  60. #define const
  61. #endif
  62. #define signed
  63. #define void int
  64. #define volatile
  65. #define __V_ char
  66. #else
  67. #define __V_ void
  68. #endif
  69. #else
  70. #define __PROTO__(x) ()
  71. #define __OTORP__(x) x
  72. #define __PARAM__(n,o) o
  73. #define __MANGLE__
  74. #define __V_ char
  75. #define const
  76. #define signed
  77. #define void int
  78. #define volatile
  79. #endif
  80. #if defined(__cplusplus) || defined(c_plusplus)
  81. #define __VARARG__ ...
  82. #else
  83. #define __VARARG__
  84. #endif
  85. #if defined(__STDARG__)
  86. #define __VA_START__(p,a) va_start(p,a)
  87. #else
  88. #define __VA_START__(p,a) va_start(p)
  89. #endif
  90. #endif
  91. static const char id[] = "\n@(#)shipop (AT&T Bell Laboratories) 06/11/92\0\n";
  92. #include <stdio.h>
  93. #include <sys/types.h>
  94. #include <sys/stat.h>
  95. #define elementsof(x) (sizeof(x)/sizeof(x[0]))
  96. static char* cmd = "shipop";
  97. static char* op;
  98. static int status;
  99. extern __MANGLE__ int close __PROTO__((int));
  100. extern __MANGLE__ void exit __PROTO__((int));
  101. extern __MANGLE__ int open __PROTO__((const char*, int, ...));
  102. extern __MANGLE__ int read __PROTO__((int, char*, int));
  103. extern __MANGLE__ int strcmp __PROTO__((const char*, const char*));
  104. extern __MANGLE__ time_t time __PROTO__((time_t*));
  105. static void
  106. error __PARAM__((int level, const char* file, const char* msg), (level, file, msg)) __OTORP__(int level; const char* file; const char* msg;){
  107. fprintf(stderr, "%s: %s", cmd, op);
  108. if (level == 1) fprintf(stderr, ": warning");
  109. if (file) fprintf(stderr, ": %s", file);
  110. if (msg) fprintf(stderr, ": %s", msg);
  111. fprintf(stderr, "\n");
  112. if (level > status)
  113. {
  114. if (level > 2) exit(level - 2);
  115. status = level;
  116. }
  117. }
  118. /*
  119. * shipop newer [ reference [ file ... ] ]
  120. */
  121. static int
  122. shipnewer __PARAM__((char** argv), (argv)) __OTORP__(char** argv;){
  123. unsigned long ref;
  124. struct stat st;
  125. if (!*argv || stat(*argv, &st)) return(1);
  126. ref = (unsigned long)st.st_mtime;
  127. while (*++argv)
  128. if (stat(*argv, &st) || ref < (unsigned long)st.st_mtime) return(1);
  129. return(0);
  130. }
  131. #define HASHPART(h,c) (h = (h) * 987654321L + 123456879L + (c))
  132. static char buf[4096];
  133. static unsigned long
  134. sealfile __PARAM__((char* file, int fd, unsigned long s), (file, fd, s)) __OTORP__(char* file; int fd; unsigned long s;){
  135. unsigned char* b;
  136. unsigned char* e;
  137. int n;
  138. HASHPART(s, 0);
  139. while ((n = read(fd, buf, sizeof(buf))) > 0)
  140. {
  141. b = (unsigned char*)buf;
  142. e = b + n;
  143. while (b < e) HASHPART(s, *b++);
  144. }
  145. if (n < 0) error(2, file, "read error");
  146. return(s);
  147. }
  148. /*
  149. * shipop seal [ file ... ]
  150. */
  151. static int
  152. shipseal __PARAM__((char** argv), (argv)) __OTORP__(char** argv;){
  153. char* f;
  154. int fd;
  155. unsigned long s;
  156. s = 0;
  157. if (!*argv) s = sealfile("/dev/stdin", 0, s);
  158. else while (f = *argv++)
  159. {
  160. if (*f == '-' && !*(f + 1)) s = sealfile("/dev/stdin", 0, s);
  161. else if ((fd = open(f, 0)) < 0) error(2, f, "cannot read");
  162. else
  163. {
  164. s = sealfile(f, fd, s);
  165. close(fd);
  166. }
  167. }
  168. printf("%08lx\n", s);
  169. return(status);
  170. }
  171. /*
  172. * shipop state reference [ file ... | <file-list ]
  173. */
  174. static int
  175. shipstate __PARAM__((char** argv), (argv)) __OTORP__(char** argv;){
  176. char* s;
  177. int c;
  178. long ref;
  179. struct stat st;
  180. if (!(s = *argv++) || stat(s, &st)) error(3, (char*)0, "reference file omitted");
  181. ref = (long)st.st_mtime;
  182. if (s = *argv++) do
  183. {
  184. if (!stat(s, &st))
  185. printf("%s %ld\n", s, (long)st.st_mtime - ref);
  186. } while (s = *argv++);
  187. else do
  188. {
  189. s = buf;
  190. while ((c = getchar()) != EOF && c != ' ' && c != '\n')
  191. if (s < buf + sizeof(buf) - 1) *s++ = c;
  192. if (s > buf)
  193. {
  194. *s = 0;
  195. if (!stat(buf, &st))
  196. printf("%s %ld\n", buf, (long)st.st_mtime - ref);
  197. }
  198. } while (c != EOF);
  199. return(status);
  200. }
  201. /*
  202. * shipop time [ file ]
  203. */
  204. static int
  205. shiptime __PARAM__((char** argv), (argv)) __OTORP__(char** argv;){
  206. struct stat st;
  207. time_t date;
  208. if (*argv && !stat(*argv, &st)) date = st.st_mtime;
  209. else time(&date);
  210. printf("%08lx\n", (long)date);
  211. return(status);
  212. }
  213. /*
  214. * shipop xap [ file ... ]
  215. */
  216. static int
  217. shipxap __PARAM__((char** argv), (argv)) __OTORP__(char** argv;){
  218. error(2, "xap not implemented yet", (char*)0);
  219. return(1);
  220. }
  221. struct optab
  222. {
  223. char* name;
  224. int (*func) __PROTO__((char**));
  225. };
  226. static struct optab ops[] =
  227. {
  228. "newer", shipnewer,
  229. "seal", shipseal,
  230. "state", shipstate,
  231. "time", shiptime,
  232. "xap", shipxap,
  233. };
  234. int main __PARAM__((int argc, char** argv), (argc, argv)) __OTORP__(int argc; char** argv;){
  235. char* s;
  236. int i;
  237. if (!(s = *++argv))
  238. exit(0);
  239. for (i = 0; i < elementsof(ops); i++)
  240. if (!strcmp(s, ops[i].name))
  241. {
  242. op = ops[i].name;
  243. exit((*ops[i].func)(argv + 1));
  244. }
  245. fprintf(stderr, "Usage: %s %s", cmd, ops[0].name);
  246. for (i = 1; i < elementsof(ops); i++)
  247. fprintf(stderr, " | %s", ops[i].name);
  248. fprintf(stderr, " [ file ... ]\n");
  249. exit(2);
  250. }