need param.h for mbuf.h now
[unix-history] / usr / src / usr.bin / strip / strip.c
index c0d30cd..4f617a4 100644 (file)
-static char *sccsid = "@(#)strip.c     4.2 (Berkeley) %G%";
-#include <a.out.h>
-#include <signal.h>
-#include <pagsiz.h>
+/*
+ * Copyright (c) 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
 
 
-#define        BUFSIZ  1024
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1988 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
 
 
-char   *tname;
-char   *mktemp();
-struct exec head;
-int    status;
-int    tf;
+#ifndef lint
+static char sccsid[] = "@(#)strip.c    5.3 (Berkeley) %G%";
+#endif /* not lint */
 
 
-main(argc, argv)
-char *argv[];
-{
-       register i;
-
-       signal(SIGHUP, SIG_IGN);
-       signal(SIGINT, SIG_IGN);
-       signal(SIGQUIT, SIG_IGN);
-       tname = mktemp("/tmp/sXXXXX");
-       close(creat(tname, 0600));
-       tf = open(tname, 2);
-       if(tf < 0) {
-               printf("cannot create temp file\n");
-               exit(2);
-       }
-       for(i=1; i<argc; i++) {
-               strip(argv[i]);
-               if(status > 1)
-                       break;
-       }
-       close(tf);
-       unlink(tname);
-       exit(status);
-}
+#include <sys/types.h>
+#include <sys/file.h>
+#include <a.out.h>
+#include <stdio.h>
 
 
-strip(name)
-char *name;
+/* ARGSUSED */
+main(argc, argv)
+       int argc;
+       char **argv;
 {
 {
-       register f;
-       long size;
-       int i;
+       typedef struct exec EXEC;
+       register off_t fsize;
+       register int fd, n, pagesize;
+       EXEC head;
+       off_t lseek();
 
 
-       f = open(name, 0);
-       if(f < 0) {
-               printf("cannot open %s\n", name);
-               status = 1;
-               goto out;
-       }
-       read(f, (char *)&head, sizeof(head));
-       if (N_BADMAG(head)) {
-               printf("%s not in a.out format\n", name);
-               status = 1;
-               goto out;
-       }
-       if ((head.a_syms == 0) && (head.a_trsize == 0) && (head.a_drsize ==0)) {
-               printf("%s already stripped\n", name);
-               goto out;
-       }
-       size = (long)head.a_text + head.a_data;
-       head.a_syms = head.a_trsize = head.a_drsize = 0 ;
-       lseek(tf, (long)0, 0);
-       write(tf, (char *)&head, sizeof(head));
-       if (head.a_magic == ZMAGIC)
-               size += PAGSIZ - sizeof (head);
-       if (copy(name, f, tf, size)) {
-               status = 1;
-               goto out;
-       }
-       size += sizeof(head);
-       close(f);
-       f = creat(name, 0666);
-       if(f < 0) {
-               printf("%s cannot recreate\n", name);
-               status = 1;
-               goto out;
+       pagesize = getpagesize();
+       while (*++argv) {
+               if ((fd = open(*argv, O_RDWR)) < 0 ||
+                   (n = read(fd, (char *)&head, sizeof(EXEC))) == -1)
+                       error(*argv);
+               if (n != sizeof(EXEC) || N_BADMAG(head)) {
+                       fprintf(stderr, "strip: %s not in a.out format.\n",
+                           *argv);
+                       exit(1);
+               }
+               if (!head.a_syms && !head.a_trsize && !head.a_drsize) {
+                       fprintf(stderr, "strip: %s already stripped.\n", *argv);
+                       continue;
+               }
+               fsize = head.a_text + head.a_data;
+               if (head.a_magic == ZMAGIC)
+                       fsize += pagesize - sizeof(EXEC);
+               head.a_syms = head.a_trsize = head.a_drsize = 0;
+               if (ftruncate(fd, fsize + sizeof(EXEC)) ||
+                   lseek(fd, 0L, L_SET) == -1 ||
+                   write(fd, (char *)&head, sizeof(EXEC)) != sizeof(EXEC))
+                       error(*argv);
+               (void)close(fd);
        }
        }
-       lseek(tf, (long)0, 0);
-       if(copy(name, tf, f, size))
-               status = 2;
-
-out:
-       close(f);
+       exit(0);
 }
 
 }
 
-copy(name, fr, to, size)
-char *name;
-long size;
+static
+error(fname)
+       char *fname;
 {
 {
-       register s, n;
-       char buf[BUFSIZ];
-
-       while(size != 0) {
-               s = BUFSIZ;
-               if(size < BUFSIZ)
-                       s = size;
-               n = read(fr, buf, s);
-               if(n != s) {
-                       printf("%s unexpected eof\n", name);
-                       return(1);
-               }
-               n = write(to, buf, s);
-               if(n != s) {
-                       printf("%s unexpected write eof\n", name);
-                       return(1);
-               }
-               size -= s;
-       }
-       return(0);
+       fprintf(stderr, "strip: %s: ", fname);
+       perror((char *)NULL);
+       exit(1);
 }
 }