More stuff...

This commit is contained in:
Eric Andersen 1999-10-12 22:26:06 +00:00
parent 2ce1edcf54
commit 3cf52d1958
23 changed files with 2740 additions and 2938 deletions

File diff suppressed because it is too large Load Diff

View File

@ -16,7 +16,7 @@
//#define BB_DESCEND
#define BB_DF
#define BB_DMESG
//#define BB_DUTMP
#define BB_DUTMP
//#define BB_DYADIC
#define BB_FALSE
//#define BB_FDFLUSH
@ -24,7 +24,7 @@
#define BB_GREP
////#define BB_HALT
//#define BB_INIT
//#define BB_KILL
#define BB_KILL
////#define BB_LENGTH
//#define BB_LN
//#define BB_LOADKMAP
@ -44,10 +44,10 @@
//#define BB_POSTPROCESS
//#define BB_PRINTF
#define BB_PWD
//#define BB_REBOOT
#define BB_REBOOT
//#define BB_RM
//#define BB_RMDIR
//#define BB_SLEEP
#define BB_SLEEP
////#define BB_SWAPOFF
//#define BB_SWAPON
//#define BB_SYNC

28
cat.c
View File

@ -22,15 +22,27 @@
#include "internal.h"
#include <stdio.h>
const char cat_usage[] = "[file ...]";
static void print_file( FILE *file)
{
int c;
while ((c = getc(file)) != EOF)
putc(c, stdout);
fclose(file);
fflush(stdout);
}
extern int cat_more_main(int argc, char **argv)
{
int c;
FILE *file = stdin;
FILE *file;
if ( (argc < 2) || (**(argv+1) == '-') ) {
fprintf(stderr, "Usage: %s %s", *argv, cat_usage);
if (argc==1) {
print_file( stdin);
exit( TRUE);
}
if ( **(argv+1) == '-' ) {
fprintf(stderr, "Usage: cat [file ...]\n");
exit(FALSE);
}
argc--;
@ -42,11 +54,7 @@ extern int cat_more_main(int argc, char **argv)
perror(*argv);
exit(FALSE);
}
while ((c = getc(file)) != EOF)
putc(c, stdout);
fclose(file);
fflush(stdout);
print_file( file);
argc--;
argv++;
}

View File

@ -22,15 +22,27 @@
#include "internal.h"
#include <stdio.h>
const char cat_usage[] = "[file ...]";
static void print_file( FILE *file)
{
int c;
while ((c = getc(file)) != EOF)
putc(c, stdout);
fclose(file);
fflush(stdout);
}
extern int cat_more_main(int argc, char **argv)
{
int c;
FILE *file = stdin;
FILE *file;
if ( (argc < 2) || (**(argv+1) == '-') ) {
fprintf(stderr, "Usage: %s %s", *argv, cat_usage);
if (argc==1) {
print_file( stdin);
exit( TRUE);
}
if ( **(argv+1) == '-' ) {
fprintf(stderr, "Usage: cat [file ...]\n");
exit(FALSE);
}
argc--;
@ -42,11 +54,7 @@ extern int cat_more_main(int argc, char **argv)
perror(*argv);
exit(FALSE);
}
while ((c = getc(file)) != EOF)
putc(c, stdout);
fclose(file);
fflush(stdout);
print_file( file);
argc--;
argv++;
}

View File

@ -6,302 +6,211 @@
* The "dd" command, originally taken from sash.
*
* Permission to distribute this code under the GPL has been granted.
* Majorly modified, and bugs fixed for busybox by Erik Andersen <andersee@debian.org> <andersen@lineo.com>
* Mostly rewritten and bugs fixed for busybox by Erik Andersen <andersee@debian.org>
*/
#include "internal.h"
#ifdef BB_DD
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
const char dd_usage[] =
"Copy a file, converting and formatting according to options\n\
"Copy a file, converting and formatting according to options\n\
\n\
usage: [if=name] [of=name] [bs=n] [count=n]\n\
\tif=FILE\tread from FILE instead of stdin\n\
\tof=FILE\twrite to FILE instead of stout\n\
\tbs=n\tread and write N bytes at a time\n\
\tcount=n\tcopy only n input blocks\n\
\tskip=n\tskip n input blocks\n\
\n\
BYTES may be suffixed: by k for x1024, b for x512, and w for x2.\n";
#include <stdio.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#define PAR_NONE 0
#define PAR_IF 1
#define PAR_OF 2
#define PAR_BS 3
#define PAR_COUNT 4
typedef struct
{
const char * name;
int value;
} PARAM;
static const PARAM params[] =
{
{"if", PAR_IF},
{"of", PAR_OF},
{"bs", PAR_BS},
{"count", PAR_COUNT},
{NULL, PAR_NONE}
};
static long getNum(const char * cp);
extern int
dd_main (int argc, char **argv)
{
const char * str;
const PARAM * par;
const char * inFile;
const char * outFile;
char * cp;
int inFd;
int outFd;
int inCc=0;
int outCc;
int blockSize;
long count;
long intotal;
long outTotal;
unsigned char* buf;
unsigned char localBuf[BUF_SIZE];
inFile = NULL;
outFile = NULL;
blockSize = 512;
count = 1;
while (--argc > 0)
{
str = *++argv;
cp = strchr(str, '=');
if (cp == NULL)
{
fprintf(stderr, "Bad dd argument\n");
goto usage;
}
*cp++ = '\0';
for (par = params; par->name; par++)
{
if (strcmp(str, par->name) == 0)
break;
}
switch (par->value)
{
case PAR_IF:
if (inFile)
{
fprintf(stderr, "Multiple input files illegal\n");
goto usage;
}
//fprintf(stderr, "if=%s\n", cp);
inFile = cp;
break;
case PAR_OF:
if (outFile)
{
fprintf(stderr, "Multiple output files illegal\n");
goto usage;
}
//fprintf(stderr, "of=%s\n", cp);
outFile = cp;
break;
case PAR_BS:
blockSize = getNum(cp);
//fprintf(stderr, "bs=%d\n", blockSize);
if (blockSize <= 0)
{
fprintf(stderr, "Bad block size value\n");
goto usage;
}
break;
case PAR_COUNT:
count = getNum(cp);
//fprintf(stderr, "count=%ld\n", count);
if (count < 0)
{
fprintf(stderr, "Bad count value\n");
goto usage;
}
break;
default:
goto usage;
}
}
buf = localBuf;
if (blockSize > sizeof(localBuf))
{
buf = malloc(blockSize);
if (buf == NULL)
{
fprintf(stderr, "Cannot allocate buffer\n");
return 1;
}
}
intotal = 0;
outTotal = 0;
if (inFile == NULL)
inFd = STDIN;
else
inFd = open(inFile, 0);
if (inFd < 0)
{
perror(inFile);
if (buf != localBuf)
free(buf);
return 1;
}
if (outFile == NULL)
outFd = STDOUT;
else
outFd = creat(outFile, 0666);
if (outFd < 0)
{
perror(outFile);
close(inFd);
if (buf != localBuf)
free(buf);
return 1;
}
while ( outTotal < count*blockSize )
{
inCc = read(inFd, buf, blockSize);
if (inCc < 0) {
perror(inFile);
goto cleanup;
}
//fprintf(stderr, "read in =%d\n", inCc);
intotal += inCc;
cp = buf;
while ( intotal > outTotal )
{
if (outTotal+inCc > count*blockSize)
inCc=count*blockSize-outTotal;
outCc = write(outFd, cp, inCc);
if (outCc < 0)
{
perror(outFile);
goto cleanup;
}
//fprintf(stderr, "wrote out =%d\n", outCc);
inCc -= outCc;
cp += outCc;
outTotal += outCc;
//fprintf(stderr, "outTotal=%ld\n", outTotal);
}
}
if (inCc < 0)
perror(inFile);
cleanup:
close(inFd);
if (close(outFd) < 0)
perror(outFile);
if (buf != localBuf)
free(buf);
printf("%ld+%d records in\n", intotal / blockSize,
(intotal % blockSize) != 0);
printf("%ld+%d records out\n", outTotal / blockSize,
(outTotal % blockSize) != 0);
return 0;
usage:
fprintf(stderr, "%s", dd_usage);
return 1;
}
/*
* Read a number with a possible multiplier.
* Returns -1 if the number format is illegal.
*/
static long
getNum(const char * cp)
static long getNum (const char *cp)
{
long value;
long value;
if (!isDecimal(*cp))
return -1;
if (!isDecimal (*cp))
return -1;
value = 0;
value = 0;
while (isDecimal(*cp))
value = value * 10 + *cp++ - '0';
while (isDecimal (*cp))
value = value * 10 + *cp++ - '0';
switch (*cp++)
{
case 'k':
value *= 1024;
break;
switch (*cp++) {
case 'k':
value *= 1024;
break;
case 'b':
value *= 512;
break;
case 'b':
value *= 512;
break;
case 'w':
value *= 2;
break;
case '\0':
return value;
default:
return -1;
}
if (*cp)
return -1;
case 'w':
value *= 2;
break;
case '\0':
return value;
default:
return -1;
}
if (*cp)
return -1;
return value;
}
extern int dd_main (int argc, char **argv)
{
const char *inFile;
const char *outFile;
char *cp;
int inFd;
int outFd;
int inCc = 0;
int outCc;
int skipBlocks;
int blockSize;
long count;
long intotal;
long outTotal;
unsigned char *buf;
inFile = NULL;
outFile = NULL;
blockSize = 512;
skipBlocks = 0;
count = 1;
argc--;
argv++;
/* Parse any options */
while (argc) {
if (inFile == NULL && (strncmp("if", *argv, 2) == 0))
inFile=*argv;
else if (outFile == NULL && (strncmp("of", *argv, 2) == 0))
outFile=*argv;
else if (strncmp("count", *argv, 5) == 0) {
count = getNum (*argv);
if (count <= 0) {
fprintf (stderr, "Bad count value %ld\n", count);
goto usage;
}
}
else if (strncmp("bs", *argv, 2) == 0) {
blockSize = getNum(*argv);
if (blockSize <= 0) {
fprintf (stderr, "Bad block size value %d\n", blockSize);
goto usage;
}
}
else if (strncmp("skip", *argv, 4) == 0) {
skipBlocks = atoi( *argv);
if (skipBlocks <= 0) {
fprintf (stderr, "Bad skip value %d\n", skipBlocks);
goto usage;
}
}
else {
fprintf (stderr, "Got here. argv=%s\n", *argv);
goto usage;
argc--;
argv++;
}
}
if ( inFile == NULL || outFile == NULL)
goto usage;
buf = malloc (blockSize);
if (buf == NULL) {
fprintf (stderr, "Cannot allocate buffer\n");
return( FALSE);
}
intotal = 0;
outTotal = 0;
if (!inFile)
inFd = STDIN;
else
inFd = open (inFile, 0);
if (inFd < 0) {
perror (inFile);
free (buf);
return( FALSE);
}
if (!outFile)
outFd = STDOUT;
else
outFd = creat (outFile, 0666);
if (outFd < 0) {
perror (outFile);
close (inFd);
free (buf);
return( FALSE);
}
lseek(inFd, skipBlocks*blockSize, SEEK_SET);
while (outTotal < count * blockSize) {
inCc = read (inFd, buf, blockSize);
if (inCc < 0) {
perror (inFile);
goto cleanup;
}
intotal += inCc;
cp = buf;
while (intotal > outTotal) {
if (outTotal + inCc > count * blockSize)
inCc = count * blockSize - outTotal;
outCc = write (outFd, cp, inCc);
if (outCc < 0) {
perror (outFile);
goto cleanup;
}
inCc -= outCc;
cp += outCc;
outTotal += outCc;
}
}
if (inCc < 0)
perror (inFile);
cleanup:
close (inFd);
close (outFd);
free (buf);
printf ("%ld+%d records in\n", intotal / blockSize,
(intotal % blockSize) != 0);
printf ("%ld+%d records out\n", outTotal / blockSize,
(outTotal % blockSize) != 0);
exit( TRUE);
usage:
fprintf (stderr, "%s", dd_usage);
exit( FALSE);
}
#endif
/* END CODE */

View File

@ -1,12 +1,13 @@
#include "internal.h"
#include <stdio.h>
#include <dirent.h>
const char pwd_usage[] = "Print the current directory.\n";
extern int
pwd_main(int argc, char * * argv)
{
char buf[1024];
char buf[NAME_MAX];
if ( getcwd(buf, sizeof(buf)) == NULL ) {
perror("get working directory");

View File

@ -1,15 +1,20 @@
#include "internal.h"
#include <stdio.h>
const char sleep_usage[] = "sleep seconds\n"
"\n"
"\tPause program execution for the given number of seconds.\n";
const char sleep_usage[] = " NUMBER\n"
"Pause for NUMBER seconds.\n";
extern int
sleep_main(struct FileInfo * i, int argc, char * * argv)
sleep_main(int argc, char * * argv)
{
if ( sleep(atoi(argv[1])) != 0 )
return -1;
else
return 0;
if ( (argc < 2) || (**(argv+1) == '-') ) {
fprintf(stderr, "Usage: %s %s", *argv, sleep_usage);
exit(FALSE);
}
if ( sleep(atoi(*(++argv))) != 0 ) {
perror( "sleep");
exit (FALSE);
} else
exit (TRUE);
}

441
dd.c
View File

@ -6,302 +6,211 @@
* The "dd" command, originally taken from sash.
*
* Permission to distribute this code under the GPL has been granted.
* Majorly modified, and bugs fixed for busybox by Erik Andersen <andersee@debian.org> <andersen@lineo.com>
* Mostly rewritten and bugs fixed for busybox by Erik Andersen <andersee@debian.org>
*/
#include "internal.h"
#ifdef BB_DD
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
const char dd_usage[] =
"Copy a file, converting and formatting according to options\n\
"Copy a file, converting and formatting according to options\n\
\n\
usage: [if=name] [of=name] [bs=n] [count=n]\n\
\tif=FILE\tread from FILE instead of stdin\n\
\tof=FILE\twrite to FILE instead of stout\n\
\tbs=n\tread and write N bytes at a time\n\
\tcount=n\tcopy only n input blocks\n\
\tskip=n\tskip n input blocks\n\
\n\
BYTES may be suffixed: by k for x1024, b for x512, and w for x2.\n";
#include <stdio.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#define PAR_NONE 0
#define PAR_IF 1
#define PAR_OF 2
#define PAR_BS 3
#define PAR_COUNT 4
typedef struct
{
const char * name;
int value;
} PARAM;
static const PARAM params[] =
{
{"if", PAR_IF},
{"of", PAR_OF},
{"bs", PAR_BS},
{"count", PAR_COUNT},
{NULL, PAR_NONE}
};
static long getNum(const char * cp);
extern int
dd_main (int argc, char **argv)
{
const char * str;
const PARAM * par;
const char * inFile;
const char * outFile;
char * cp;
int inFd;
int outFd;
int inCc=0;
int outCc;
int blockSize;
long count;
long intotal;
long outTotal;
unsigned char* buf;
unsigned char localBuf[BUF_SIZE];
inFile = NULL;
outFile = NULL;
blockSize = 512;
count = 1;
while (--argc > 0)
{
str = *++argv;
cp = strchr(str, '=');
if (cp == NULL)
{
fprintf(stderr, "Bad dd argument\n");
goto usage;
}
*cp++ = '\0';
for (par = params; par->name; par++)
{
if (strcmp(str, par->name) == 0)
break;
}
switch (par->value)
{
case PAR_IF:
if (inFile)
{
fprintf(stderr, "Multiple input files illegal\n");
goto usage;
}
//fprintf(stderr, "if=%s\n", cp);
inFile = cp;
break;
case PAR_OF:
if (outFile)
{
fprintf(stderr, "Multiple output files illegal\n");
goto usage;
}
//fprintf(stderr, "of=%s\n", cp);
outFile = cp;
break;
case PAR_BS:
blockSize = getNum(cp);
//fprintf(stderr, "bs=%d\n", blockSize);
if (blockSize <= 0)
{
fprintf(stderr, "Bad block size value\n");
goto usage;
}
break;
case PAR_COUNT:
count = getNum(cp);
//fprintf(stderr, "count=%ld\n", count);
if (count < 0)
{
fprintf(stderr, "Bad count value\n");
goto usage;
}
break;
default:
goto usage;
}
}
buf = localBuf;
if (blockSize > sizeof(localBuf))
{
buf = malloc(blockSize);
if (buf == NULL)
{
fprintf(stderr, "Cannot allocate buffer\n");
return 1;
}
}
intotal = 0;
outTotal = 0;
if (inFile == NULL)
inFd = STDIN;
else
inFd = open(inFile, 0);
if (inFd < 0)
{
perror(inFile);
if (buf != localBuf)
free(buf);
return 1;
}
if (outFile == NULL)
outFd = STDOUT;
else
outFd = creat(outFile, 0666);
if (outFd < 0)
{
perror(outFile);
close(inFd);
if (buf != localBuf)
free(buf);
return 1;
}
while ( outTotal < count*blockSize )
{
inCc = read(inFd, buf, blockSize);
if (inCc < 0) {
perror(inFile);
goto cleanup;
}
//fprintf(stderr, "read in =%d\n", inCc);
intotal += inCc;
cp = buf;
while ( intotal > outTotal )
{
if (outTotal+inCc > count*blockSize)
inCc=count*blockSize-outTotal;
outCc = write(outFd, cp, inCc);
if (outCc < 0)
{
perror(outFile);
goto cleanup;
}
//fprintf(stderr, "wrote out =%d\n", outCc);
inCc -= outCc;
cp += outCc;
outTotal += outCc;
//fprintf(stderr, "outTotal=%ld\n", outTotal);
}
}
if (inCc < 0)
perror(inFile);
cleanup:
close(inFd);
if (close(outFd) < 0)
perror(outFile);
if (buf != localBuf)
free(buf);
printf("%ld+%d records in\n", intotal / blockSize,
(intotal % blockSize) != 0);
printf("%ld+%d records out\n", outTotal / blockSize,
(outTotal % blockSize) != 0);
return 0;
usage:
fprintf(stderr, "%s", dd_usage);
return 1;
}
/*
* Read a number with a possible multiplier.
* Returns -1 if the number format is illegal.
*/
static long
getNum(const char * cp)
static long getNum (const char *cp)
{
long value;
long value;
if (!isDecimal(*cp))
return -1;
if (!isDecimal (*cp))
return -1;
value = 0;
value = 0;
while (isDecimal(*cp))
value = value * 10 + *cp++ - '0';
while (isDecimal (*cp))
value = value * 10 + *cp++ - '0';
switch (*cp++)
{
case 'k':
value *= 1024;
break;
switch (*cp++) {
case 'k':
value *= 1024;
break;
case 'b':
value *= 512;
break;
case 'b':
value *= 512;
break;
case 'w':
value *= 2;
break;
case '\0':
return value;
default:
return -1;
}
if (*cp)
return -1;
case 'w':
value *= 2;
break;
case '\0':
return value;
default:
return -1;
}
if (*cp)
return -1;
return value;
}
extern int dd_main (int argc, char **argv)
{
const char *inFile;
const char *outFile;
char *cp;
int inFd;
int outFd;
int inCc = 0;
int outCc;
int skipBlocks;
int blockSize;
long count;
long intotal;
long outTotal;
unsigned char *buf;
inFile = NULL;
outFile = NULL;
blockSize = 512;
skipBlocks = 0;
count = 1;
argc--;
argv++;
/* Parse any options */
while (argc) {
if (inFile == NULL && (strncmp("if", *argv, 2) == 0))
inFile=*argv;
else if (outFile == NULL && (strncmp("of", *argv, 2) == 0))
outFile=*argv;
else if (strncmp("count", *argv, 5) == 0) {
count = getNum (*argv);
if (count <= 0) {
fprintf (stderr, "Bad count value %ld\n", count);
goto usage;
}
}
else if (strncmp("bs", *argv, 2) == 0) {
blockSize = getNum(*argv);
if (blockSize <= 0) {
fprintf (stderr, "Bad block size value %d\n", blockSize);
goto usage;
}
}
else if (strncmp("skip", *argv, 4) == 0) {
skipBlocks = atoi( *argv);
if (skipBlocks <= 0) {
fprintf (stderr, "Bad skip value %d\n", skipBlocks);
goto usage;
}
}
else {
fprintf (stderr, "Got here. argv=%s\n", *argv);
goto usage;
argc--;
argv++;
}
}
if ( inFile == NULL || outFile == NULL)
goto usage;
buf = malloc (blockSize);
if (buf == NULL) {
fprintf (stderr, "Cannot allocate buffer\n");
return( FALSE);
}
intotal = 0;
outTotal = 0;
if (!inFile)
inFd = STDIN;
else
inFd = open (inFile, 0);
if (inFd < 0) {
perror (inFile);
free (buf);
return( FALSE);
}
if (!outFile)
outFd = STDOUT;
else
outFd = creat (outFile, 0666);
if (outFd < 0) {
perror (outFile);
close (inFd);
free (buf);
return( FALSE);
}
lseek(inFd, skipBlocks*blockSize, SEEK_SET);
while (outTotal < count * blockSize) {
inCc = read (inFd, buf, blockSize);
if (inCc < 0) {
perror (inFile);
goto cleanup;
}
intotal += inCc;
cp = buf;
while (intotal > outTotal) {
if (outTotal + inCc > count * blockSize)
inCc = count * blockSize - outTotal;
outCc = write (outFd, cp, inCc);
if (outCc < 0) {
perror (outFile);
goto cleanup;
}
inCc -= outCc;
cp += outCc;
outTotal += outCc;
}
}
if (inCc < 0)
perror (inFile);
cleanup:
close (inFd);
close (outFd);
free (buf);
printf ("%ld+%d records in\n", intotal / blockSize,
(intotal % blockSize) != 0);
printf ("%ld+%d records out\n", outTotal / blockSize,
(outTotal % blockSize) != 0);
exit( TRUE);
usage:
fprintf (stderr, "%s", dd_usage);
exit( FALSE);
}
#endif
/* END CODE */

125
dmesg.c
View File

@ -14,7 +14,6 @@
#include <linux/unistd.h>
#include <stdio.h>
#include <getopt.h>
#define __NR_klog __NR_syslog
@ -22,74 +21,78 @@
#include <sys/klog.h>
#define klog klogctl
#else
static inline _syscall3(int,klog,int,type,char *,b,int,len)
#endif /* __GLIBC__ */
static inline _syscall3 (int, klog, int, type, char *, b, int, len)
#endif /* __GLIBC__ */
const char dmesg_usage[] = "dmesg";
int
dmesg_main(int argc, char * * argv)
static const char dmesg_usage[] = "dmesg [-c] [-n level]\n";
int dmesg_main (int argc, char **argv)
{
char buf[4096];
int i;
int n;
int c;
int level = 0;
int lastc;
int cmd = 3;
char buf[4096];
int i;
int n;
int level = 0;
int lastc;
int cmd = 3;
while ((c = getopt( argc, argv, "cn:" )) != EOF) {
switch (c) {
case 'c':
cmd = 4;
break;
case 'n':
cmd = 8;
level = atoi(optarg);
break;
case '?':
default:
fprintf(stderr, "%s\n", dmesg_usage);
exit(1);
}
}
argc -= optind;
argv += optind;
argc--;
argv++;
if (argc > 1) {
fprintf(stderr, "%s\n", dmesg_usage);
exit(1);
}
/* Parse any options */
while (argc && **argv == '-') {
while (*++(*argv))
switch (**argv) {
case 'c':
cmd = 4;
break;
case 'n':
cmd = 8;
if (--argc == 0)
goto end;
level = atoi (*(++argv));
--argc;
++argv;
break;
default:
goto end;
}
}
if (cmd == 8) {
n = klog( cmd, NULL, level );
if (n < 0) {
perror( "klog" );
exit( 1 );
}
exit( 0 );
}
if (cmd == 8) {
n = klog (cmd, NULL, level);
if (n < 0) {
perror ("klog");
exit (FALSE);
}
exit (TRUE);
}
n = klog( cmd, buf, sizeof( buf ) );
if (n < 0) {
perror( "klog" );
exit( 1 );
}
n = klog (cmd, buf, sizeof (buf));
if (n < 0) {
perror ("klog");
exit (FALSE);
}
lastc = '\n';
for (i = 0; i < n; i++) {
if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
i++;
while (buf[i] >= '0' && buf[i] <= '9')
lastc = '\n';
for (i = 0; i < n; i++) {
if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
i++;
if (buf[i] == '>')
i++;
}
lastc = buf[i];
putchar( lastc );
}
if (lastc != '\n')
putchar( '\n' );
return 0;
while (buf[i] >= '0' && buf[i] <= '9')
i++;
if (buf[i] == '>')
i++;
}
lastc = buf[i];
putchar (lastc);
}
if (lastc != '\n')
putchar ('\n');
exit (TRUE);
end:
fprintf (stderr, "Usage: %s\n", dmesg_usage);
exit (FALSE);
}

53
dutmp.c
View File

@ -14,34 +14,39 @@
#include <stdio.h>
#include <utmp.h>
const char dutmp_usage[] = "dutmp\n"
"\n"
"\tDump file or stdin utmp file format to stdout, pipe delimited.\n"
"\tdutmp /var/run/utmp\n";
const char dutmp_usage[] = "dutmp\n"
"\n"
"\tDump file or stdin utmp file format to stdout, pipe delimited.\n"
"\tdutmp /var/run/utmp\n";
extern int
dutmp_fn(const struct FileInfo * i)
extern int dutmp_fn (int argc, char **argv)
{
FILE * f = stdin;
struct utmp * ut = (struct utmp *) malloc(sizeof(struct utmp) );
FILE *f = stdin;
struct utmp ut;
if ( i )
if (! (f = fopen(i->source, "r"))) {
name_and_error(i->source);
return 1;
}
if ((argc < 2) || (**(argv + 1) == '-')) {
fprintf (stderr, "Usage: %s %s\n", *argv, dutmp_usage);
exit (FALSE);
}
while (fread (ut, 1, sizeof(struct utmp), f)) {
//printf("%d:%d:%s:%s:%s:%s:%d:%d:%ld:%ld:%ld:%x\n",
printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n",
ut->ut_type, ut->ut_pid, ut->ut_line,
ut->ut_id, ut->ut_user, ut->ut_host,
ut->ut_exit.e_termination, ut->ut_exit.e_exit,
ut->ut_session,
ut->ut_tv.tv_sec, ut->ut_tv.tv_usec,
ut->ut_addr);
}
if ( **(++argv) == 0 ) {
f = fopen (*(++argv), "r");
if (f < 0 ) {
perror (*argv);
exit (FALSE);
}
}
return 0;
while (fread (&ut, 1, sizeof (struct utmp), f)) {
// printf("%d:%d:%s:%s:%s:%s:%d:%d:%ld:%ld:%ld:%x\n",
printf ("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n",
ut.ut_type, ut.ut_pid, ut.ut_line,
ut.ut_id, ut.ut_user, ut.ut_host,
ut.ut_exit.e_termination, ut.ut_exit.e_exit,
ut.ut_session,
ut.ut_tv.tv_sec, ut.ut_tv.tv_usec, ut.ut_addr);
}
exit (TRUE);
}

View File

@ -11,7 +11,6 @@
*/
#include "internal.h"
#ifdef BB_GREP
#include <stdio.h>
#include <dirent.h>
@ -31,7 +30,76 @@ const char grep_usage[] =
static int search (const char *string, const char *word, int ignoreCase);
/*
* See if the specified word is found in the specified string.
*/
static int search (const char *string, const char *word, int ignoreCase)
{
const char *cp1;
const char *cp2;
int len;
int lowFirst;
int ch1;
int ch2;
len = strlen (word);
if (!ignoreCase) {
while (TRUE) {
string = strchr (string, word[0]);
if (string == NULL)
return FALSE;
if (memcmp (string, word, len) == 0)
return TRUE;
string++;
}
}
/*
* Here if we need to check case independence.
* Do the search by lower casing both strings.
*/
lowFirst = *word;
if (isupper (lowFirst))
lowFirst = tolower (lowFirst);
while (TRUE) {
while (*string && (*string != lowFirst) &&
(!isupper (*string) || (tolower (*string) != lowFirst))) {
string++;
}
if (*string == '\0')
return FALSE;
cp1 = string;
cp2 = word;
do {
if (*cp2 == '\0')
return TRUE;
ch1 = *cp1++;
if (isupper (ch1))
ch1 = tolower (ch1);
ch2 = *cp2++;
if (isupper (ch2))
ch2 = tolower (ch2);
}
while (ch1 == ch2);
string++;
}
return (TRUE);
}
extern int grep_main (int argc, char **argv)
@ -122,76 +190,6 @@ extern int grep_main (int argc, char **argv)
}
/*
* See if the specified word is found in the specified string.
*/
static int search (const char *string, const char *word, int ignoreCase)
{
const char *cp1;
const char *cp2;
int len;
int lowFirst;
int ch1;
int ch2;
len = strlen (word);
if (!ignoreCase) {
while (TRUE) {
string = strchr (string, word[0]);
if (string == NULL)
return FALSE;
if (memcmp (string, word, len) == 0)
return TRUE;
string++;
}
}
/*
* Here if we need to check case independence.
* Do the search by lower casing both strings.
*/
lowFirst = *word;
if (isupper (lowFirst))
lowFirst = tolower (lowFirst);
while (TRUE) {
while (*string && (*string != lowFirst) &&
(!isupper (*string) || (tolower (*string) != lowFirst))) {
string++;
}
if (*string == '\0')
return FALSE;
cp1 = string;
cp2 = word;
do {
if (*cp2 == '\0')
return TRUE;
ch1 = *cp1++;
if (isupper (ch1))
ch1 = tolower (ch1);
ch2 = *cp2++;
if (isupper (ch2))
ch2 = tolower (ch2);
}
while (ch1 == ch2);
string++;
}
return (TRUE);
}
#endif
/* END CODE */

146
grep.c
View File

@ -11,7 +11,6 @@
*/
#include "internal.h"
#ifdef BB_GREP
#include <stdio.h>
#include <dirent.h>
@ -31,7 +30,76 @@ const char grep_usage[] =
static int search (const char *string, const char *word, int ignoreCase);
/*
* See if the specified word is found in the specified string.
*/
static int search (const char *string, const char *word, int ignoreCase)
{
const char *cp1;
const char *cp2;
int len;
int lowFirst;
int ch1;
int ch2;
len = strlen (word);
if (!ignoreCase) {
while (TRUE) {
string = strchr (string, word[0]);
if (string == NULL)
return FALSE;
if (memcmp (string, word, len) == 0)
return TRUE;
string++;
}
}
/*
* Here if we need to check case independence.
* Do the search by lower casing both strings.
*/
lowFirst = *word;
if (isupper (lowFirst))
lowFirst = tolower (lowFirst);
while (TRUE) {
while (*string && (*string != lowFirst) &&
(!isupper (*string) || (tolower (*string) != lowFirst))) {
string++;
}
if (*string == '\0')
return FALSE;
cp1 = string;
cp2 = word;
do {
if (*cp2 == '\0')
return TRUE;
ch1 = *cp1++;
if (isupper (ch1))
ch1 = tolower (ch1);
ch2 = *cp2++;
if (isupper (ch2))
ch2 = tolower (ch2);
}
while (ch1 == ch2);
string++;
}
return (TRUE);
}
extern int grep_main (int argc, char **argv)
@ -122,76 +190,6 @@ extern int grep_main (int argc, char **argv)
}
/*
* See if the specified word is found in the specified string.
*/
static int search (const char *string, const char *word, int ignoreCase)
{
const char *cp1;
const char *cp2;
int len;
int lowFirst;
int ch1;
int ch2;
len = strlen (word);
if (!ignoreCase) {
while (TRUE) {
string = strchr (string, word[0]);
if (string == NULL)
return FALSE;
if (memcmp (string, word, len) == 0)
return TRUE;
string++;
}
}
/*
* Here if we need to check case independence.
* Do the search by lower casing both strings.
*/
lowFirst = *word;
if (isupper (lowFirst))
lowFirst = tolower (lowFirst);
while (TRUE) {
while (*string && (*string != lowFirst) &&
(!isupper (*string) || (tolower (*string) != lowFirst))) {
string++;
}
if (*string == '\0')
return FALSE;
cp1 = string;
cp2 = word;
do {
if (*cp2 == '\0')
return TRUE;
ch1 = *cp1++;
if (isupper (ch1))
ch1 = tolower (ch1);
ch2 = *cp2++;
if (isupper (ch2))
ch2 = tolower (ch2);
}
while (ch1 == ch2);
string++;
}
return (TRUE);
}
#endif
/* END CODE */

View File

@ -1,12 +1,8 @@
#include "internal.h"
#include <signal.h>
const char reboot_usage[] = "reboot\n"
"\n\t"
"\treboot the system.\n";
extern int
reboot_main(struct FileInfo * i, int argc, char * * argv)
reboot_main(int argc, char ** argv)
{
return kill(1, SIGUSR2);
exit( kill(1, SIGUSR2));
}

203
kill.c
View File

@ -4,137 +4,134 @@
#include <unistd.h>
#include <signal.h>
const char kill_usage[] = "kill [-signal] process-id [process-id ...]\n";
const char kill_usage[] = "kill [-signal] process-id [process-id ...]\n";
struct signal_name {
const char * name;
int number;
const char *name;
int number;
};
const struct signal_name signames[] = {
{ "HUP", SIGHUP },
{ "INT", SIGINT },
{ "QUIT", SIGQUIT },
{ "ILL", SIGILL },
{ "TRAP", SIGTRAP },
{ "ABRT", SIGABRT },
{"HUP", SIGHUP},
{"INT", SIGINT},
{"QUIT", SIGQUIT},
{"ILL", SIGILL},
{"TRAP", SIGTRAP},
{"ABRT", SIGABRT},
#ifndef __alpha__
{ "IOT", SIGIOT },
{"IOT", SIGIOT},
#endif
#if defined(sparc) || defined(__alpha__)
{ "EMT", SIGEMT },
{"EMT", SIGEMT},
#else
{ "BUS", SIGBUS },
{"BUS", SIGBUS},
#endif
{ "FPE", SIGFPE },
{ "KILL", SIGKILL },
{"FPE", SIGFPE},
{"KILL", SIGKILL},
#if defined(sparc) || defined(__alpha__)
{ "BUS", SIGBUS },
{"BUS", SIGBUS},
#else
{ "USR1", SIGUSR1 },
{"USR1", SIGUSR1},
#endif
{ "SEGV", SIGSEGV },
{"SEGV", SIGSEGV},
#if defined(sparc) || defined(__alpha__)
{ "SYS", SIGSYS },
{"SYS", SIGSYS},
#else
{ "USR2", SIGUSR2 },
{"USR2", SIGUSR2},
#endif
{ "PIPE", SIGPIPE },
{ "ALRM", SIGALRM },
{ "TERM", SIGTERM },
{"PIPE", SIGPIPE},
{"ALRM", SIGALRM},
{"TERM", SIGTERM},
#if defined(sparc) || defined(__alpha__)
{ "URG", SIGURG },
{ "STOP", SIGSTOP },
{ "TSTP", SIGTSTP },
{ "CONT", SIGCONT },
{ "CHLD", SIGCHLD },
{ "TTIN", SIGTTIN },
{ "TTOU", SIGTTOU },
{ "IO", SIGIO },
{"URG", SIGURG},
{"STOP", SIGSTOP},
{"TSTP", SIGTSTP},
{"CONT", SIGCONT},
{"CHLD", SIGCHLD},
{"TTIN", SIGTTIN},
{"TTOU", SIGTTOU},
{"IO", SIGIO},
# ifndef __alpha__
{ "POLL", SIGIO },
{"POLL", SIGIO},
# endif
{ "XCPU", SIGXCPU },
{ "XFSZ", SIGXFSZ },
{ "VTALRM", SIGVTALRM },
{ "PROF", SIGPROF },
{ "WINCH", SIGWINCH },
{"XCPU", SIGXCPU},
{"XFSZ", SIGXFSZ},
{"VTALRM", SIGVTALRM},
{"PROF", SIGPROF},
{"WINCH", SIGWINCH},
# ifdef __alpha__
{ "INFO", SIGINFO },
{"INFO", SIGINFO},
# else
{ "LOST", SIGLOST },
{"LOST", SIGLOST},
# endif
{ "USR1", SIGUSR1 },
{ "USR2", SIGUSR2 },
{"USR1", SIGUSR1},
{"USR2", SIGUSR2},
#else
{ "STKFLT", SIGSTKFLT },
{ "CHLD", SIGCHLD },
{ "CONT", SIGCONT },
{ "STOP", SIGSTOP },
{ "TSTP", SIGTSTP },
{ "TTIN", SIGTTIN },
{ "TTOU", SIGTTOU },
{ "URG", SIGURG },
{ "XCPU", SIGXCPU },
{ "XFSZ", SIGXFSZ },
{ "VTALRM", SIGVTALRM },
{ "PROF", SIGPROF },
{ "WINCH", SIGWINCH },
{ "IO", SIGIO },
{ "POLL", SIGPOLL },
{ "PWR", SIGPWR },
{ "UNUSED", SIGUNUSED },
{"STKFLT", SIGSTKFLT},
{"CHLD", SIGCHLD},
{"CONT", SIGCONT},
{"STOP", SIGSTOP},
{"TSTP", SIGTSTP},
{"TTIN", SIGTTIN},
{"TTOU", SIGTTOU},
{"URG", SIGURG},
{"XCPU", SIGXCPU},
{"XFSZ", SIGXFSZ},
{"VTALRM", SIGVTALRM},
{"PROF", SIGPROF},
{"WINCH", SIGWINCH},
{"IO", SIGIO},
{"POLL", SIGPOLL},
{"PWR", SIGPWR},
{"UNUSED", SIGUNUSED},
#endif
{ 0, 0 }
{0, 0}
};
extern int
kill_main(struct FileInfo * i, int argc, char * * argv)
extern int kill_main (int argc, char **argv)
{
int had_error = 0;
int sig = SIGTERM;
if ( argv[1][0] == '-' ) {
if ( argv[1][1] >= '0' && argv[1][1] <= '9' ) {
sig = atoi(&argv[1][1]);
if ( sig < 0 || sig >= NSIG ) {
usage(kill_usage);
exit(-1);
}
}
else {
const struct signal_name * s = signames;
for ( ; ; ) {
if ( strcmp(s->name, &argv[1][1]) == 0 ) {
sig = s->number;
break;
}
s++;
if ( s->name == 0 ) {
usage(kill_usage);
exit(-1);
}
}
}
argv++;
argc--;
int had_error = 0;
int sig = SIGTERM;
}
while ( argc > 1 ) {
int pid;
if ( argv[1][0] < '0' || argv[1][0] > '9' ) {
usage(kill_usage);
exit(-1);
if (argv[1][0] == '-') {
if (argv[1][1] >= '0' && argv[1][1] <= '9') {
sig = atoi (&argv[1][1]);
if (sig < 0 || sig >= NSIG)
goto end;
} else {
const struct signal_name *s = signames;
for (;;) {
if (strcmp (s->name, &argv[1][1]) == 0) {
sig = s->number;
break;
}
pid = atoi(argv[1]);
if ( kill(pid, sig) != 0 ) {
had_error = 1;
perror(argv[1]);
}
argv++;
argc--;
s++;
if (s->name == 0)
goto end;
}
}
if ( had_error )
return -1;
else
return 0;
argv++;
argc--;
}
while (argc > 1) {
int pid;
if (argv[1][0] < '0' || argv[1][0] > '9')
goto end;
pid = atoi (argv[1]);
if (kill (pid, sig) != 0) {
had_error = 1;
perror (argv[1]);
}
argv++;
argc--;
}
if (had_error) {
end:
fprintf(stderr, "Usage: %s\n", kill_usage);
exit ( FALSE);
}
exit (TRUE);
}

View File

@ -14,34 +14,39 @@
#include <stdio.h>
#include <utmp.h>
const char dutmp_usage[] = "dutmp\n"
"\n"
"\tDump file or stdin utmp file format to stdout, pipe delimited.\n"
"\tdutmp /var/run/utmp\n";
const char dutmp_usage[] = "dutmp\n"
"\n"
"\tDump file or stdin utmp file format to stdout, pipe delimited.\n"
"\tdutmp /var/run/utmp\n";
extern int
dutmp_fn(const struct FileInfo * i)
extern int dutmp_fn (int argc, char **argv)
{
FILE * f = stdin;
struct utmp * ut = (struct utmp *) malloc(sizeof(struct utmp) );
FILE *f = stdin;
struct utmp ut;
if ( i )
if (! (f = fopen(i->source, "r"))) {
name_and_error(i->source);
return 1;
}
if ((argc < 2) || (**(argv + 1) == '-')) {
fprintf (stderr, "Usage: %s %s\n", *argv, dutmp_usage);
exit (FALSE);
}
while (fread (ut, 1, sizeof(struct utmp), f)) {
//printf("%d:%d:%s:%s:%s:%s:%d:%d:%ld:%ld:%ld:%x\n",
printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n",
ut->ut_type, ut->ut_pid, ut->ut_line,
ut->ut_id, ut->ut_user, ut->ut_host,
ut->ut_exit.e_termination, ut->ut_exit.e_exit,
ut->ut_session,
ut->ut_tv.tv_sec, ut->ut_tv.tv_usec,
ut->ut_addr);
}
if ( **(++argv) == 0 ) {
f = fopen (*(++argv), "r");
if (f < 0 ) {
perror (*argv);
exit (FALSE);
}
}
return 0;
while (fread (&ut, 1, sizeof (struct utmp), f)) {
// printf("%d:%d:%s:%s:%s:%s:%d:%d:%ld:%ld:%ld:%x\n",
printf ("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n",
ut.ut_type, ut.ut_pid, ut.ut_line,
ut.ut_id, ut.ut_user, ut.ut_host,
ut.ut_exit.e_termination, ut.ut_exit.e_exit,
ut.ut_session,
ut.ut_tv.tv_sec, ut.ut_tv.tv_usec, ut.ut_addr);
}
exit (TRUE);
}

79
more.c
View File

@ -19,27 +19,48 @@
*
*/
/* Turning this off makes things a bit smaller (and less pretty) */
#define BB_MORE_TERM
#include "internal.h"
#include <stdio.h>
#include <signal.h>
const char more_usage[] = "[file ...]";
//#define ERASE_STUFF
#ifdef BB_MORE_TERM
#include <termios.h>
#include <signal.h>
#include <sys/ioctl.h>
FILE *cin;
struct termios initial_settings, new_settings;
void gotsig(int sig) {
tcsetattr(fileno(cin), TCSANOW, &initial_settings);
exit( TRUE);
}
#endif
extern int more_main(int argc, char **argv)
{
int c, lines=0;
int c, lines=0, input;
int next_page=0, rows = 24;
#ifdef ERASE_STUFF
int cols=79;
#ifdef BB_MORE_TERM
int cols;
struct winsize win;
#endif
struct stat st;
FILE *file = stdin;
if ( strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0 ) {
fprintf(stderr, "Usage: %s %s", *argv, more_usage);
return(FALSE);
exit(FALSE);
}
argc--;
argv++;
@ -48,23 +69,47 @@ extern int more_main(int argc, char **argv)
file = fopen(*argv, "r");
if (file == NULL) {
perror("Can't open file");
return(FALSE);
exit(FALSE);
}
fstat(fileno(file), &st);
fprintf(stderr, "hi\n");
#ifdef BB_MORE_TERM
cin = fopen("/dev/tty", "r");
tcgetattr(fileno(cin),&initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
tcsetattr(fileno(cin), TCSANOW, &new_settings);
(void) signal(SIGINT, gotsig);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &win);
if (win.ws_row > 4) rows = win.ws_row - 2;
if (win.ws_col > 0) cols = win.ws_col - 1;
#endif
while ((c = getc(file)) != EOF) {
if ( next_page ) {
int len=0;
next_page = 0;
lines=0;
len = fprintf(stdout, "--More-- (%d%% of %ld bytes)",
len = fprintf(stdout, "--More-- (%d%% of %ld bytes)%s",
(int) (100*( (double) ftell(file) / (double) st.st_size )),
st.st_size);
st.st_size,
#ifdef BB_MORE_TERM
""
#else
"\n"
#endif
);
fflush(stdout);
getc( stdin);
#ifdef ERASE_STUFF
/* Try to erase the "More" message */
input = getc( stdin);
#ifdef BB_MORE_TERM
/* Erase the "More" message */
while(len-- > 0)
putc('\b', stdout);
while(len++ < cols)
@ -73,7 +118,12 @@ extern int more_main(int argc, char **argv)
putc('\b', stdout);
fflush(stdout);
#endif
}
if (input=='q')
goto end;
if (input==' ' && c == '\n' )
next_page = 1;
if ( c == '\n' && ++lines == (rows + 1) )
next_page = 1;
putc(c, stdout);
@ -84,7 +134,10 @@ extern int more_main(int argc, char **argv)
argc--;
argv++;
}
return(TRUE);
end:
#ifdef BB_MORE_TERM
gotsig(0);
#endif
exit(TRUE);
}

View File

@ -4,137 +4,134 @@
#include <unistd.h>
#include <signal.h>
const char kill_usage[] = "kill [-signal] process-id [process-id ...]\n";
const char kill_usage[] = "kill [-signal] process-id [process-id ...]\n";
struct signal_name {
const char * name;
int number;
const char *name;
int number;
};
const struct signal_name signames[] = {
{ "HUP", SIGHUP },
{ "INT", SIGINT },
{ "QUIT", SIGQUIT },
{ "ILL", SIGILL },
{ "TRAP", SIGTRAP },
{ "ABRT", SIGABRT },
{"HUP", SIGHUP},
{"INT", SIGINT},
{"QUIT", SIGQUIT},
{"ILL", SIGILL},
{"TRAP", SIGTRAP},
{"ABRT", SIGABRT},
#ifndef __alpha__
{ "IOT", SIGIOT },
{"IOT", SIGIOT},
#endif
#if defined(sparc) || defined(__alpha__)
{ "EMT", SIGEMT },
{"EMT", SIGEMT},
#else
{ "BUS", SIGBUS },
{"BUS", SIGBUS},
#endif
{ "FPE", SIGFPE },
{ "KILL", SIGKILL },
{"FPE", SIGFPE},
{"KILL", SIGKILL},
#if defined(sparc) || defined(__alpha__)
{ "BUS", SIGBUS },
{"BUS", SIGBUS},
#else
{ "USR1", SIGUSR1 },
{"USR1", SIGUSR1},
#endif
{ "SEGV", SIGSEGV },
{"SEGV", SIGSEGV},
#if defined(sparc) || defined(__alpha__)
{ "SYS", SIGSYS },
{"SYS", SIGSYS},
#else
{ "USR2", SIGUSR2 },
{"USR2", SIGUSR2},
#endif
{ "PIPE", SIGPIPE },
{ "ALRM", SIGALRM },
{ "TERM", SIGTERM },
{"PIPE", SIGPIPE},
{"ALRM", SIGALRM},
{"TERM", SIGTERM},
#if defined(sparc) || defined(__alpha__)
{ "URG", SIGURG },
{ "STOP", SIGSTOP },
{ "TSTP", SIGTSTP },
{ "CONT", SIGCONT },
{ "CHLD", SIGCHLD },
{ "TTIN", SIGTTIN },
{ "TTOU", SIGTTOU },
{ "IO", SIGIO },
{"URG", SIGURG},
{"STOP", SIGSTOP},
{"TSTP", SIGTSTP},
{"CONT", SIGCONT},
{"CHLD", SIGCHLD},
{"TTIN", SIGTTIN},
{"TTOU", SIGTTOU},
{"IO", SIGIO},
# ifndef __alpha__
{ "POLL", SIGIO },
{"POLL", SIGIO},
# endif
{ "XCPU", SIGXCPU },
{ "XFSZ", SIGXFSZ },
{ "VTALRM", SIGVTALRM },
{ "PROF", SIGPROF },
{ "WINCH", SIGWINCH },
{"XCPU", SIGXCPU},
{"XFSZ", SIGXFSZ},
{"VTALRM", SIGVTALRM},
{"PROF", SIGPROF},
{"WINCH", SIGWINCH},
# ifdef __alpha__
{ "INFO", SIGINFO },
{"INFO", SIGINFO},
# else
{ "LOST", SIGLOST },
{"LOST", SIGLOST},
# endif
{ "USR1", SIGUSR1 },
{ "USR2", SIGUSR2 },
{"USR1", SIGUSR1},
{"USR2", SIGUSR2},
#else
{ "STKFLT", SIGSTKFLT },
{ "CHLD", SIGCHLD },
{ "CONT", SIGCONT },
{ "STOP", SIGSTOP },
{ "TSTP", SIGTSTP },
{ "TTIN", SIGTTIN },
{ "TTOU", SIGTTOU },
{ "URG", SIGURG },
{ "XCPU", SIGXCPU },
{ "XFSZ", SIGXFSZ },
{ "VTALRM", SIGVTALRM },
{ "PROF", SIGPROF },
{ "WINCH", SIGWINCH },
{ "IO", SIGIO },
{ "POLL", SIGPOLL },
{ "PWR", SIGPWR },
{ "UNUSED", SIGUNUSED },
{"STKFLT", SIGSTKFLT},
{"CHLD", SIGCHLD},
{"CONT", SIGCONT},
{"STOP", SIGSTOP},
{"TSTP", SIGTSTP},
{"TTIN", SIGTTIN},
{"TTOU", SIGTTOU},
{"URG", SIGURG},
{"XCPU", SIGXCPU},
{"XFSZ", SIGXFSZ},
{"VTALRM", SIGVTALRM},
{"PROF", SIGPROF},
{"WINCH", SIGWINCH},
{"IO", SIGIO},
{"POLL", SIGPOLL},
{"PWR", SIGPWR},
{"UNUSED", SIGUNUSED},
#endif
{ 0, 0 }
{0, 0}
};
extern int
kill_main(struct FileInfo * i, int argc, char * * argv)
extern int kill_main (int argc, char **argv)
{
int had_error = 0;
int sig = SIGTERM;
if ( argv[1][0] == '-' ) {
if ( argv[1][1] >= '0' && argv[1][1] <= '9' ) {
sig = atoi(&argv[1][1]);
if ( sig < 0 || sig >= NSIG ) {
usage(kill_usage);
exit(-1);
}
}
else {
const struct signal_name * s = signames;
for ( ; ; ) {
if ( strcmp(s->name, &argv[1][1]) == 0 ) {
sig = s->number;
break;
}
s++;
if ( s->name == 0 ) {
usage(kill_usage);
exit(-1);
}
}
}
argv++;
argc--;
int had_error = 0;
int sig = SIGTERM;
}
while ( argc > 1 ) {
int pid;
if ( argv[1][0] < '0' || argv[1][0] > '9' ) {
usage(kill_usage);
exit(-1);
if (argv[1][0] == '-') {
if (argv[1][1] >= '0' && argv[1][1] <= '9') {
sig = atoi (&argv[1][1]);
if (sig < 0 || sig >= NSIG)
goto end;
} else {
const struct signal_name *s = signames;
for (;;) {
if (strcmp (s->name, &argv[1][1]) == 0) {
sig = s->number;
break;
}
pid = atoi(argv[1]);
if ( kill(pid, sig) != 0 ) {
had_error = 1;
perror(argv[1]);
}
argv++;
argc--;
s++;
if (s->name == 0)
goto end;
}
}
if ( had_error )
return -1;
else
return 0;
argv++;
argc--;
}
while (argc > 1) {
int pid;
if (argv[1][0] < '0' || argv[1][0] > '9')
goto end;
pid = atoi (argv[1]);
if (kill (pid, sig) != 0) {
had_error = 1;
perror (argv[1]);
}
argv++;
argc--;
}
if (had_error) {
end:
fprintf(stderr, "Usage: %s\n", kill_usage);
exit ( FALSE);
}
exit (TRUE);
}

3
pwd.c
View File

@ -1,12 +1,13 @@
#include "internal.h"
#include <stdio.h>
#include <dirent.h>
const char pwd_usage[] = "Print the current directory.\n";
extern int
pwd_main(int argc, char * * argv)
{
char buf[1024];
char buf[NAME_MAX];
if ( getcwd(buf, sizeof(buf)) == NULL ) {
perror("get working directory");

View File

@ -1,12 +1,8 @@
#include "internal.h"
#include <signal.h>
const char reboot_usage[] = "reboot\n"
"\n\t"
"\treboot the system.\n";
extern int
reboot_main(struct FileInfo * i, int argc, char * * argv)
reboot_main(int argc, char ** argv)
{
return kill(1, SIGUSR2);
exit( kill(1, SIGUSR2));
}

21
sleep.c
View File

@ -1,15 +1,20 @@
#include "internal.h"
#include <stdio.h>
const char sleep_usage[] = "sleep seconds\n"
"\n"
"\tPause program execution for the given number of seconds.\n";
const char sleep_usage[] = " NUMBER\n"
"Pause for NUMBER seconds.\n";
extern int
sleep_main(struct FileInfo * i, int argc, char * * argv)
sleep_main(int argc, char * * argv)
{
if ( sleep(atoi(argv[1])) != 0 )
return -1;
else
return 0;
if ( (argc < 2) || (**(argv+1) == '-') ) {
fprintf(stderr, "Usage: %s %s", *argv, sleep_usage);
exit(FALSE);
}
if ( sleep(atoi(*(++argv))) != 0 ) {
perror( "sleep");
exit (FALSE);
} else
exit (TRUE);
}

1702
tar.c

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,6 @@
#include <linux/unistd.h>
#include <stdio.h>
#include <getopt.h>
#define __NR_klog __NR_syslog
@ -22,74 +21,78 @@
#include <sys/klog.h>
#define klog klogctl
#else
static inline _syscall3(int,klog,int,type,char *,b,int,len)
#endif /* __GLIBC__ */
static inline _syscall3 (int, klog, int, type, char *, b, int, len)
#endif /* __GLIBC__ */
const char dmesg_usage[] = "dmesg";
int
dmesg_main(int argc, char * * argv)
static const char dmesg_usage[] = "dmesg [-c] [-n level]\n";
int dmesg_main (int argc, char **argv)
{
char buf[4096];
int i;
int n;
int c;
int level = 0;
int lastc;
int cmd = 3;
char buf[4096];
int i;
int n;
int level = 0;
int lastc;
int cmd = 3;
while ((c = getopt( argc, argv, "cn:" )) != EOF) {
switch (c) {
case 'c':
cmd = 4;
break;
case 'n':
cmd = 8;
level = atoi(optarg);
break;
case '?':
default:
fprintf(stderr, "%s\n", dmesg_usage);
exit(1);
}
}
argc -= optind;
argv += optind;
argc--;
argv++;
if (argc > 1) {
fprintf(stderr, "%s\n", dmesg_usage);
exit(1);
}
/* Parse any options */
while (argc && **argv == '-') {
while (*++(*argv))
switch (**argv) {
case 'c':
cmd = 4;
break;
case 'n':
cmd = 8;
if (--argc == 0)
goto end;
level = atoi (*(++argv));
--argc;
++argv;
break;
default:
goto end;
}
}
if (cmd == 8) {
n = klog( cmd, NULL, level );
if (n < 0) {
perror( "klog" );
exit( 1 );
}
exit( 0 );
}
if (cmd == 8) {
n = klog (cmd, NULL, level);
if (n < 0) {
perror ("klog");
exit (FALSE);
}
exit (TRUE);
}
n = klog( cmd, buf, sizeof( buf ) );
if (n < 0) {
perror( "klog" );
exit( 1 );
}
n = klog (cmd, buf, sizeof (buf));
if (n < 0) {
perror ("klog");
exit (FALSE);
}
lastc = '\n';
for (i = 0; i < n; i++) {
if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
i++;
while (buf[i] >= '0' && buf[i] <= '9')
lastc = '\n';
for (i = 0; i < n; i++) {
if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') {
i++;
if (buf[i] == '>')
i++;
}
lastc = buf[i];
putchar( lastc );
}
if (lastc != '\n')
putchar( '\n' );
return 0;
while (buf[i] >= '0' && buf[i] <= '9')
i++;
if (buf[i] == '>')
i++;
}
lastc = buf[i];
putchar (lastc);
}
if (lastc != '\n')
putchar ('\n');
exit (TRUE);
end:
fprintf (stderr, "Usage: %s\n", dmesg_usage);
exit (FALSE);
}

View File

@ -19,27 +19,48 @@
*
*/
/* Turning this off makes things a bit smaller (and less pretty) */
#define BB_MORE_TERM
#include "internal.h"
#include <stdio.h>
#include <signal.h>
const char more_usage[] = "[file ...]";
//#define ERASE_STUFF
#ifdef BB_MORE_TERM
#include <termios.h>
#include <signal.h>
#include <sys/ioctl.h>
FILE *cin;
struct termios initial_settings, new_settings;
void gotsig(int sig) {
tcsetattr(fileno(cin), TCSANOW, &initial_settings);
exit( TRUE);
}
#endif
extern int more_main(int argc, char **argv)
{
int c, lines=0;
int c, lines=0, input;
int next_page=0, rows = 24;
#ifdef ERASE_STUFF
int cols=79;
#ifdef BB_MORE_TERM
int cols;
struct winsize win;
#endif
struct stat st;
FILE *file = stdin;
if ( strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0 ) {
fprintf(stderr, "Usage: %s %s", *argv, more_usage);
return(FALSE);
exit(FALSE);
}
argc--;
argv++;
@ -48,23 +69,47 @@ extern int more_main(int argc, char **argv)
file = fopen(*argv, "r");
if (file == NULL) {
perror("Can't open file");
return(FALSE);
exit(FALSE);
}
fstat(fileno(file), &st);
fprintf(stderr, "hi\n");
#ifdef BB_MORE_TERM
cin = fopen("/dev/tty", "r");
tcgetattr(fileno(cin),&initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
tcsetattr(fileno(cin), TCSANOW, &new_settings);
(void) signal(SIGINT, gotsig);
ioctl(STDOUT_FILENO, TIOCGWINSZ, &win);
if (win.ws_row > 4) rows = win.ws_row - 2;
if (win.ws_col > 0) cols = win.ws_col - 1;
#endif
while ((c = getc(file)) != EOF) {
if ( next_page ) {
int len=0;
next_page = 0;
lines=0;
len = fprintf(stdout, "--More-- (%d%% of %ld bytes)",
len = fprintf(stdout, "--More-- (%d%% of %ld bytes)%s",
(int) (100*( (double) ftell(file) / (double) st.st_size )),
st.st_size);
st.st_size,
#ifdef BB_MORE_TERM
""
#else
"\n"
#endif
);
fflush(stdout);
getc( stdin);
#ifdef ERASE_STUFF
/* Try to erase the "More" message */
input = getc( stdin);
#ifdef BB_MORE_TERM
/* Erase the "More" message */
while(len-- > 0)
putc('\b', stdout);
while(len++ < cols)
@ -73,7 +118,12 @@ extern int more_main(int argc, char **argv)
putc('\b', stdout);
fflush(stdout);
#endif
}
if (input=='q')
goto end;
if (input==' ' && c == '\n' )
next_page = 1;
if ( c == '\n' && ++lines == (rows + 1) )
next_page = 1;
putc(c, stdout);
@ -84,7 +134,10 @@ extern int more_main(int argc, char **argv)
argc--;
argv++;
}
return(TRUE);
end:
#ifdef BB_MORE_TERM
gotsig(0);
#endif
exit(TRUE);
}