Fixed a memory leak in lash. It seems that close_all was calling close()
[oweals/busybox.git] / ar.c
diff --git a/ar.c b/ar.c
index bd54bb6b869b5806cb2ab710d8cfc1ab70a9f49c..59f51815fe88355ee89d7183e2b2518c3b017f42 100644 (file)
--- a/ar.c
+++ b/ar.c
  *
  */
 #include <fcntl.h>
+#include <string.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <unistd.h>
 #include "busybox.h"
 
 typedef struct ar_headers_s {
@@ -39,7 +43,7 @@ typedef struct ar_headers_s {
 /*
  * return the headerL_t struct for the filename descriptor
  */
-static ar_headers_t get_headers(int srcFd)
+extern ar_headers_t get_ar_headers(int srcFd)
 {
        typedef struct raw_ar_header_s {        /* Byte Offset */
                char name[16];  /*  0-15 */
@@ -50,6 +54,7 @@ static ar_headers_t get_headers(int srcFd)
                char size[10];  /* 48-57 */
                char fmag[2];   /* 58-59 */
        } raw_ar_header_t;
+
        raw_ar_header_t raw_ar_header;
 
        ar_headers_t *head, *entry;
@@ -60,21 +65,24 @@ static ar_headers_t get_headers(int srcFd)
        entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
        
        /* check ar magic */
-       if (full_read(srcFd, ar_magic, 8) != 8)
-               error_msg_and_die("cannot read magic\n");
-       if (strncmp(ar_magic,"!<arch>",7) != 0)
-               error_msg_and_die("invalid magic\n");
+       if (full_read(srcFd, ar_magic, 8) != 8) {
+               error_msg_and_die("cannot read magic");
+       }
+
+       if (strncmp(ar_magic,"!<arch>",7) != 0) {
+               error_msg_and_die("invalid magic");
+       }
 
        while (full_read(srcFd, (char *) &raw_ar_header, 60)==60) {
                /* check the end of header markers are valid */
                if ((raw_ar_header.fmag[0]!='`') || (raw_ar_header.fmag[1]!='\n')) {
-                       char newline[1];
+                       char newline;
                        if (raw_ar_header.fmag[1]!='`') {
                                break;
                        }
                        /* some version of ar, have an extra '\n' after each entry */
-                       read(srcFd, newline, 1);
-                       if (newline[0]!='\n') {
+                       read(srcFd, &newline, 1);
+                       if (newline!='\n') {
                                break;
                        }
                        /* fix up the header, we started reading 1 byte too early due to a '\n' */
@@ -155,19 +163,19 @@ extern int ar_main(int argc, char **argv)
                        funct |= extract_to_file;
                        break;
                default:
-                       usage(ar_usage);
+                       show_usage();
                }
        }
  
        /* check the src filename was specified */
        if (optind == argc)
-               usage(ar_usage);
+               show_usage();
        
        if ( (srcFd = open(argv[optind], O_RDONLY)) < 0)
-               error_msg_and_die("Cannot read %s\n", argv[optind]);
+               error_msg_and_die("Cannot read %s", argv[optind]);
 
        optind++;       
-       head = get_headers(srcFd);
+       head = get_ar_headers(srcFd);
 
        /* find files to extract or display */
        /* search through argv and build extract list */