reduce size by 1K by sharing the ls code.

This commit is contained in:
christos 2014-03-20 03:13:18 +00:00
parent 89ecf27180
commit 303a695e1b
10 changed files with 138 additions and 205 deletions

View File

@ -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 @@ cd9660_stat(struct open_file *f, struct stat *sb)
}
#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

View File

@ -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 @@ dosfs_stat(struct open_file *fd, struct stat *sb)
}
#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

View File

@ -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 @@ struct file {
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 @@ ext2fs_stat(struct open_file *f, struct stat *sb)
}
#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 @@ ext2fs_ls(struct open_file *f, const char *pattern)
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 @@ ext2fs_ls(struct open_file *f, const char *pattern)
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

View File

@ -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 @@ ls(const char *path)
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);
}

View File

@ -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 @@ struct file {
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 @@ minixfs3_stat(struct open_file *f, struct stat *sb)
}
#if defined(LIBSA_ENABLE_LS_OP)
#include "ls.h"
__compactcall void
minixfs3_ls(struct open_file *f, const char *pattern)
{
@ -841,7 +828,7 @@ minixfs3_ls(struct open_file *f, const char *pattern)
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 @@ minixfs3_ls(struct open_file *f, const char *pattern)
if (fs2h32(dp->mfsd_ino) == 0)
continue;
if (pattern && !fnmatch(dp->mfsd_name, pattern))
continue;
/* Compute the length of the name,
* We don't use strlen and strcpy, because original MFS
* code doesn't.
@ -877,45 +861,13 @@ minixfs3_ls(struct open_file *f, const char *pattern)
else
namlen = cp - (dp->mfsd_name);
n = alloc(sizeof *n + namlen);
if (!n) {
printf("%d: %s\n",
fs2h32(dp->mfsd_ino), dp->mfsd_name);
continue;
}
n->e_ino = fs2h32(dp->mfsd_ino);
strncpy(n->e_name, dp->mfsd_name, namlen);
n->e_name[namlen] = '\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;
lsadd(&names, pattern, dp->mfsd_name, namlen,
fs2h32(dp->mfsd_ino), "?");
}
fp->f_seekp += buf_size;
}
if (names) {
entry_t *p_names = names;
do {
n = p_names;
printf("%d: %s\n",
n->e_ino, n->e_name);
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

View File

@ -1,4 +1,4 @@
/* $NetBSD: nfs.c,v 1.47 2011/12/25 06:09:08 tsutsui Exp $ */
/* $NetBSD: nfs.c,v 1.48 2014/03/20 03:13:18 christos Exp $ */
/*-
* Copyright (c) 1993 John Brezak
@ -657,10 +657,10 @@ nfs_stat(struct open_file *f, struct stat *sb)
}
#if defined(LIBSA_ENABLE_LS_OP)
#include "ls.h"
__compactcall void
nfs_ls(struct open_file *f, const char *pattern)
{
printf("Currently ls command is unsupported by nfs\n");
return;
lsunsup("nfs");
}
#endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: nullfs.c,v 1.11 2011/12/25 06:09:08 tsutsui Exp $ */
/* $NetBSD: nullfs.c,v 1.12 2014/03/20 03:13:18 christos Exp $ */
/*-
* Copyright (c) 1993
@ -115,10 +115,10 @@ null_stat(struct open_file *f, struct stat *sb)
}
#if defined(LIBSA_ENABLE_LS_OP)
#include "ls.h"
__compactcall void
null_ls(struct open_file *f, const char *pattern)
{
printf("Currently ls command is unsupported by nullfs\n");
return;
lsunsup("nullfs");
}
#endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: tftp.c,v 1.34 2011/12/25 06:09:08 tsutsui Exp $ */
/* $NetBSD: tftp.c,v 1.35 2014/03/20 03:13:18 christos Exp $ */
/*
* Copyright (c) 1996
@ -430,11 +430,11 @@ tftp_stat(struct open_file *f, struct stat *sb)
}
#if defined(LIBSA_ENABLE_LS_OP)
#include "ls.h"
__compactcall void
tftp_ls(struct open_file *f, const char *pattern)
{
printf("Currently ls command is unsupported by tftp\n");
return;
lsunsup("tftp");
}
#endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: ufs.c,v 1.64 2013/10/20 17:17:30 christos Exp $ */
/* $NetBSD: ufs.c,v 1.65 2014/03/20 03:13:18 christos Exp $ */
/*-
* Copyright (c) 1993
@ -189,36 +189,6 @@ static void ffs_oldfscompat(struct fs *);
static int ffs_find_superblock(struct open_file *, struct fs *);
#endif
#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",
"FIFO",
"CHR",
0,
"DIR",
0,
"BLK",
0,
"REG",
0,
"LNK",
0,
"SOCK",
0,
"WHT"
};
#endif /* LIBSA_ENABLE_LS_OP */
#ifdef LIBSA_LFS
/*
@ -890,13 +860,34 @@ ufs_stat(struct open_file *f, struct stat *sb)
}
#if defined(LIBSA_ENABLE_LS_OP)
#include "ls.h"
static const char *const typestr[] = {
"unknown",
"FIFO",
"CHR",
0,
"DIR",
0,
"BLK",
0,
"REG",
0,
"LNK",
0,
"SOCK",
0,
"WHT"
};
__compactcall void
ufs_ls(struct open_file *f, const char *pattern)
{
struct file *fp = (struct file *)f->f_fsdata;
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.di_size) {
@ -931,46 +922,13 @@ ufs_ls(struct open_file *f, const char *pattern)
printf("bad dir entry\n");
goto out;
}
if (pattern && !fnmatch(dp->d_name, pattern))
continue;
n = alloc(sizeof *n + strlen(dp->d_name));
if (!n) {
printf("%d: %s (%s)\n",
dp->d_ino, dp->d_name, t);
continue;
}
n->e_ino = dp->d_ino;
n->e_type = dp->d_type;
strcpy(n->e_name, dp->d_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->d_name, strlen(dp->d_name),
dp->d_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);
}
lsprint(names);
out: lsfree(names);
}
#endif /* LIBSA_ENABLE_LS_OP */

View File

@ -1,4 +1,4 @@
/* $NetBSD: ustarfs.c,v 1.34 2011/12/25 06:09:08 tsutsui Exp $ */
/* $NetBSD: ustarfs.c,v 1.35 2014/03/20 03:13:18 christos Exp $ */
/* [Notice revision 2.2]
* Copyright (c) 1997, 1998 Avalon Computer Systems, Inc.
@ -539,10 +539,11 @@ ustarfs_stat(struct open_file *f, struct stat *sb)
#if defined(LIBSA_ENABLE_LS_OP)
#include "ls.h"
__compactcall void
ustarfs_ls(struct open_file *f, const char *pattern)
{
printf("Currently ls command is unsupported by ustarfs\n");
lsunsup("ustarfs");
return;
}
#endif