Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/lib/libsa reduce size by 1K by sharing the ls code.



details:   https://anonhg.NetBSD.org/src/rev/a8adefc1a11b
branches:  trunk
changeset: 327885:a8adefc1a11b
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Mar 20 03:13:18 2014 +0000

description:
reduce size by 1K by sharing the ls code.

diffstat:

 sys/lib/libsa/cd9660.c   |   6 +-
 sys/lib/libsa/dosfs.c    |   6 +-
 sys/lib/libsa/ext2fs.c   |  83 ++++++++++-------------------------------
 sys/lib/libsa/ls.c       |  67 +++++++++++++++++++++++++++++++++-
 sys/lib/libsa/minixfs3.c |  62 +++---------------------------
 sys/lib/libsa/nfs.c      |   6 +-
 sys/lib/libsa/nullfs.c   |   6 +-
 sys/lib/libsa/tftp.c     |   6 +-
 sys/lib/libsa/ufs.c      |  96 +++++++++++++----------------------------------
 sys/lib/libsa/ustarfs.c  |   5 +-
 10 files changed, 138 insertions(+), 205 deletions(-)

diffs (truncated from 581 to 300 lines):

diff -r 0438dd08ec7e -r a8adefc1a11b sys/lib/libsa/cd9660.c
--- a/sys/lib/libsa/cd9660.c    Thu Mar 20 01:46:19 2014 +0000
+++ b/sys/lib/libsa/cd9660.c    Thu Mar 20 03:13:18 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cd9660.c,v 1.29 2012/07/23 00:49:20 mhitch Exp $       */
+/*     $NetBSD: cd9660.c,v 1.30 2014/03/20 03:13:18 christos Exp $     */
 
 /*
  * Copyright (C) 1996 Wolfgang Solfrank.
@@ -401,10 +401,10 @@
 }
 
 #if defined(LIBSA_ENABLE_LS_OP)
+#include "ls.h"
 __compactcall void
 cd9660_ls(struct open_file *f, const char *pattern)
 {
-       printf("Currently ls command is unsupported by cd9660\n");
-       return;
+       lsunsup("cd9660");
 }
 #endif
diff -r 0438dd08ec7e -r a8adefc1a11b sys/lib/libsa/dosfs.c
--- a/sys/lib/libsa/dosfs.c     Thu Mar 20 01:46:19 2014 +0000
+++ b/sys/lib/libsa/dosfs.c     Thu Mar 20 03:13:18 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dosfs.c,v 1.19 2013/10/20 17:15:42 christos Exp $      */
+/*     $NetBSD: dosfs.c,v 1.20 2014/03/20 03:13:18 christos Exp $      */
 
 /*
  * Copyright (c) 1996, 1998 Robert Nordier
@@ -406,11 +406,11 @@
 }
 
 #if defined(LIBSA_ENABLE_LS_OP)
+#include "ls.h"
 __compactcall void
 dosfs_ls(struct open_file *f, const char *pattern)
 {
-       printf("Currently ls command is unsupported by dosfs\n");
-       return;
+       lsunsup("dosfs");
 }
 #endif
 
diff -r 0438dd08ec7e -r a8adefc1a11b sys/lib/libsa/ext2fs.c
--- a/sys/lib/libsa/ext2fs.c    Thu Mar 20 01:46:19 2014 +0000
+++ b/sys/lib/libsa/ext2fs.c    Thu Mar 20 03:13:18 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ext2fs.c,v 1.19 2013/10/20 17:17:30 christos Exp $     */
+/*     $NetBSD: ext2fs.c,v 1.20 2014/03/20 03:13:18 christos Exp $     */
 
 /*
  * Copyright (c) 1997 Manuel Bouyer.
@@ -146,30 +146,6 @@
        daddr_t         f_buf_blkno;    /* block number of data block */
 };
 
-#if defined(LIBSA_ENABLE_LS_OP)
-
-#define NELEM(x) (sizeof (x) / sizeof(*x))
-
-typedef struct entry_t entry_t;
-struct entry_t {
-       entry_t *e_next;
-       ino32_t e_ino;
-       uint8_t e_type;
-       char    e_name[1];
-};
-
-static const char    *const typestr[] = {
-       "unknown",
-       "REG",
-       "DIR",
-       "CHR",
-       "BLK",
-       "FIFO",
-       "SOCK",
-       "LNK"
-};
-
-#endif /* LIBSA_ENABLE_LS_OP */
 
 static int read_inode(ino32_t, struct open_file *);
 static int block_map(struct open_file *, indp_t, indp_t *);
@@ -828,6 +804,20 @@
 }
 
 #if defined(LIBSA_ENABLE_LS_OP)
+
+#include "ls.h"
+
+static const char    *const typestr[] = {
+       "unknown",
+       "REG",
+       "DIR",
+       "CHR",
+       "BLK",
+       "FIFO",
+       "SOCK",
+       "LNK"
+};
+
 __compactcall void
 ext2fs_ls(struct open_file *f, const char *pattern)
 {
@@ -835,7 +825,7 @@
        size_t block_size = fp->f_fs->e2fs_bsize;
        char *buf;
        size_t buf_size;
-       entry_t *names = 0, *n, **np;
+       lsentry_t *names = NULL;
 
        fp->f_seekp = 0;
        while (fp->f_seekp < (off_t)fp->f_di.e2di_size) {
@@ -874,47 +864,14 @@
                                printf("bad dir entry\n");
                                goto out;
                        }
-                       if (pattern && !fnmatch(dp->e2d_name, pattern))
-                               continue;
-                       n = alloc(sizeof *n + strlen(dp->e2d_name));
-                       if (!n) {
-                               printf("%d: %s (%s)\n",
-                                       fs2h32(dp->e2d_ino), dp->e2d_name, t);
-                               continue;
-                       }
-                       n->e_ino = fs2h32(dp->e2d_ino);
-                       n->e_type = dp->e2d_type;
-                       strcpy(n->e_name, dp->e2d_name);
-                       for (np = &names; *np; np = &(*np)->e_next) {
-                               if (strcmp(n->e_name, (*np)->e_name) < 0)
-                                       break;
-                       }
-                       n->e_next = *np;
-                       *np = n;
+                       lsadd(&names, pattern, dp->e2d_name,
+                           strlen(dp->e2d_name), fs2h32(dp->e2d_ino), t);
                }
                fp->f_seekp += buf_size;
        }
 
-       if (names) {
-               entry_t *p_names = names;
-               do {
-                       n = p_names;
-                       printf("%d: %s (%s)\n",
-                               n->e_ino, n->e_name, typestr[n->e_type]);
-                       p_names = n->e_next;
-               } while (p_names);
-       } else {
-               printf("not found\n");
-       }
-out:
-       if (names) {
-               do {
-                       n = names;
-                       names = n->e_next;
-                       dealloc(n, 0);
-               } while (names);
-       }
-       return;
+       lsprint(names);
+out:   lsfree(names);
 }
 #endif
 
diff -r 0438dd08ec7e -r a8adefc1a11b sys/lib/libsa/ls.c
--- a/sys/lib/libsa/ls.c        Thu Mar 20 01:46:19 2014 +0000
+++ b/sys/lib/libsa/ls.c        Thu Mar 20 03:13:18 2014 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: ls.c,v 1.4 2012/03/02 12:08:44 tsutsui Exp $ */
+/* $NetBSD: ls.c,v 1.5 2014/03/20 03:13:18 christos Exp $ */
 
 /*-
  * Copyright (c) 2011
@@ -85,6 +85,7 @@
 
 
 #include "stand.h"
+#include "ls.h"
 #include <sys/stat.h>
 #include <lib/libkern/libkern.h>
 
@@ -160,3 +161,67 @@
 out:
        close(fd);
 }
+
+struct lsentry {
+       struct lsentry *e_next;
+       uint32_t e_ino;
+       const char *e_type;
+       char    e_name[1];
+};
+
+__compactcall void
+lsadd(lsentry_t **names, const char *pattern, const char *name, size_t namelen,
+    uint32_t ino, const char *type)
+{
+       lsentry_t *n, **np;
+
+       if (pattern && !fnmatch(name, pattern))
+               return;
+
+       n = alloc(sizeof *n + namelen);
+       if (!n) {
+               printf("%d: %.*s (%s)\n", ino, (int)namelen, name, type);
+               return;
+       }
+
+       n->e_ino = ino;
+       n->e_type = type;
+       memcpy(n->e_name, name, namelen);
+       n->e_name[namelen] = '\0';
+
+       for (np = names; *np; np = &(*np)->e_next) {
+               if (strcmp(n->e_name, (*np)->e_name) < 0)
+                       break;
+       }
+       n->e_next = *np;
+       *np = n;
+}
+
+__compactcall void
+lsprint(lsentry_t *names) {
+       if (!names) {
+               printf("not found\n");
+               return;
+       }
+       do {
+               lsentry_t *n = names;
+               printf("%d: %s (%s)\n", n->e_ino, n->e_name, n->e_type);
+               names = n->e_next;
+       } while (names);
+}
+
+__compactcall void
+lsfree(lsentry_t *names) {
+       if (!names)
+               return;
+       do {
+               lsentry_t *n = names;
+               names = n->e_next;
+               dealloc(n, 0);
+       } while (names);
+}
+
+__compactcall void
+lsunsup(const char *name) {
+       printf("The ls command is not currently supported for %s\n", name);
+}
diff -r 0438dd08ec7e -r a8adefc1a11b sys/lib/libsa/minixfs3.c
--- a/sys/lib/libsa/minixfs3.c  Thu Mar 20 01:46:19 2014 +0000
+++ b/sys/lib/libsa/minixfs3.c  Thu Mar 20 03:13:18 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: minixfs3.c,v 1.6 2013/11/03 00:44:34 christos Exp $    */
+/*     $NetBSD: minixfs3.c,v 1.7 2014/03/20 03:13:18 christos Exp $    */
 
 /*-
  * Copyright (c) 2012
@@ -169,20 +169,6 @@
        daddr_t         f_buf_blkno;    /* block number of data block */
 };
 
-#if defined(LIBSA_ENABLE_LS_OP)
-
-#define NELEM(x) (sizeof (x) / sizeof(*x))
-
-typedef struct entry_t entry_t;
-struct entry_t {
-       entry_t *e_next;
-       ino32_t e_ino;
-       char    e_name[1];
-};
-
-#endif /* LIBSA_ENABLE_LS_OP */
-
-
 static int read_inode(ino32_t, struct open_file *);
 static int block_map(struct open_file *, block_t, block_t *);
 static int buf_read_file(struct open_file *, void *, size_t *);
@@ -833,6 +819,7 @@
 }
 
 #if defined(LIBSA_ENABLE_LS_OP)
+#include "ls.h"
 __compactcall void
 minixfs3_ls(struct open_file *f, const char *pattern)
 {
@@ -841,7 +828,7 @@
        struct mfs_direct *dp;
        struct mfs_direct *dbuf;
        size_t buf_size;
-       entry_t *names = 0, *n, **np;
+       lsentry_t *names = 0;
 
        fp->f_seekp = 0;
        while (fp->f_seekp < (off_t)fp->f_di.mdi_size) {
@@ -864,9 +851,6 @@



Home | Main Index | Thread Index | Old Index