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_DESCEND
#define BB_DF #define BB_DF
#define BB_DMESG #define BB_DMESG
//#define BB_DUTMP #define BB_DUTMP
//#define BB_DYADIC //#define BB_DYADIC
#define BB_FALSE #define BB_FALSE
//#define BB_FDFLUSH //#define BB_FDFLUSH
@ -24,7 +24,7 @@
#define BB_GREP #define BB_GREP
////#define BB_HALT ////#define BB_HALT
//#define BB_INIT //#define BB_INIT
//#define BB_KILL #define BB_KILL
////#define BB_LENGTH ////#define BB_LENGTH
//#define BB_LN //#define BB_LN
//#define BB_LOADKMAP //#define BB_LOADKMAP
@ -44,10 +44,10 @@
//#define BB_POSTPROCESS //#define BB_POSTPROCESS
//#define BB_PRINTF //#define BB_PRINTF
#define BB_PWD #define BB_PWD
//#define BB_REBOOT #define BB_REBOOT
//#define BB_RM //#define BB_RM
//#define BB_RMDIR //#define BB_RMDIR
//#define BB_SLEEP #define BB_SLEEP
////#define BB_SWAPOFF ////#define BB_SWAPOFF
//#define BB_SWAPON //#define BB_SWAPON
//#define BB_SYNC //#define BB_SYNC

28
cat.c
View File

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

View File

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

View File

@ -6,302 +6,211 @@
* The "dd" command, originally taken from sash. * The "dd" command, originally taken from sash.
* *
* Permission to distribute this code under the GPL has been granted. * 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" #include "internal.h"
#ifdef BB_DD #include <stdio.h>
#include <fcntl.h>
#include <errno.h>
const char dd_usage[] = 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\ \n\
usage: [if=name] [of=name] [bs=n] [count=n]\n\ usage: [if=name] [of=name] [bs=n] [count=n]\n\
\tif=FILE\tread from FILE instead of stdin\n\ \tif=FILE\tread from FILE instead of stdin\n\
\tof=FILE\twrite to FILE instead of stout\n\ \tof=FILE\twrite to FILE instead of stout\n\
\tbs=n\tread and write N bytes at a time\n\ \tbs=n\tread and write N bytes at a time\n\
\tcount=n\tcopy only n input blocks\n\ \tcount=n\tcopy only n input blocks\n\
\tskip=n\tskip n input blocks\n\
\n\ \n\
BYTES may be suffixed: by k for x1024, b for x512, and w for x2.\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. * Read a number with a possible multiplier.
* Returns -1 if the number format is illegal. * Returns -1 if the number format is illegal.
*/ */
static long static long getNum (const char *cp)
getNum(const char * cp)
{ {
long value; long value;
if (!isDecimal(*cp)) if (!isDecimal (*cp))
return -1; return -1;
value = 0; value = 0;
while (isDecimal(*cp)) while (isDecimal (*cp))
value = value * 10 + *cp++ - '0'; value = value * 10 + *cp++ - '0';
switch (*cp++) switch (*cp++) {
{ case 'k':
case 'k': value *= 1024;
value *= 1024; break;
break;
case 'b': case 'b':
value *= 512; value *= 512;
break; break;
case 'w': case 'w':
value *= 2; value *= 2;
break; break;
case '\0':
return value;
default:
return -1;
}
if (*cp)
return -1;
case '\0':
return value; 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 "internal.h"
#include <stdio.h> #include <stdio.h>
#include <dirent.h>
const char pwd_usage[] = "Print the current directory.\n"; const char pwd_usage[] = "Print the current directory.\n";
extern int extern int
pwd_main(int argc, char * * argv) pwd_main(int argc, char * * argv)
{ {
char buf[1024]; char buf[NAME_MAX];
if ( getcwd(buf, sizeof(buf)) == NULL ) { if ( getcwd(buf, sizeof(buf)) == NULL ) {
perror("get working directory"); perror("get working directory");

View File

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

443
dd.c
View File

@ -6,302 +6,211 @@
* The "dd" command, originally taken from sash. * The "dd" command, originally taken from sash.
* *
* Permission to distribute this code under the GPL has been granted. * 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" #include "internal.h"
#ifdef BB_DD #include <stdio.h>
#include <fcntl.h>
#include <errno.h>
const char dd_usage[] = 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\ \n\
usage: [if=name] [of=name] [bs=n] [count=n]\n\ usage: [if=name] [of=name] [bs=n] [count=n]\n\
\tif=FILE\tread from FILE instead of stdin\n\ \tif=FILE\tread from FILE instead of stdin\n\
\tof=FILE\twrite to FILE instead of stout\n\ \tof=FILE\twrite to FILE instead of stout\n\
\tbs=n\tread and write N bytes at a time\n\ \tbs=n\tread and write N bytes at a time\n\
\tcount=n\tcopy only n input blocks\n\ \tcount=n\tcopy only n input blocks\n\
\tskip=n\tskip n input blocks\n\
\n\ \n\
BYTES may be suffixed: by k for x1024, b for x512, and w for x2.\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. * Read a number with a possible multiplier.
* Returns -1 if the number format is illegal. * Returns -1 if the number format is illegal.
*/ */
static long static long getNum (const char *cp)
getNum(const char * cp)
{ {
long value; long value;
if (!isDecimal(*cp)) if (!isDecimal (*cp))
return -1; return -1;
value = 0; value = 0;
while (isDecimal(*cp)) while (isDecimal (*cp))
value = value * 10 + *cp++ - '0'; value = value * 10 + *cp++ - '0';
switch (*cp++) switch (*cp++) {
{ case 'k':
case 'k': value *= 1024;
value *= 1024; break;
break;
case 'b': case 'b':
value *= 512; value *= 512;
break; break;
case 'w': case 'w':
value *= 2; value *= 2;
break; break;
case '\0':
return value;
default:
return -1;
}
if (*cp)
return -1;
case '\0':
return value; 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 */

127
dmesg.c
View File

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

55
dutmp.c
View File

@ -9,39 +9,44 @@
* *
* made against libc6 * made against libc6
*/ */
#include "internal.h" #include "internal.h"
#include <stdio.h> #include <stdio.h>
#include <utmp.h> #include <utmp.h>
const char dutmp_usage[] = "dutmp\n" const char dutmp_usage[] = "dutmp\n"
"\n" "\n"
"\tDump file or stdin utmp file format to stdout, pipe delimited.\n" "\tDump file or stdin utmp file format to stdout, pipe delimited.\n"
"\tdutmp /var/run/utmp\n"; "\tdutmp /var/run/utmp\n";
extern int extern int dutmp_fn (int argc, char **argv)
dutmp_fn(const struct FileInfo * i)
{ {
FILE * f = stdin; FILE *f = stdin;
struct utmp * ut = (struct utmp *) malloc(sizeof(struct utmp) ); struct utmp ut;
if ( i ) if ((argc < 2) || (**(argv + 1) == '-')) {
if (! (f = fopen(i->source, "r"))) { fprintf (stderr, "Usage: %s %s\n", *argv, dutmp_usage);
name_and_error(i->source); exit (FALSE);
return 1; }
}
while (fread (ut, 1, sizeof(struct utmp), f)) { if ( **(++argv) == 0 ) {
//printf("%d:%d:%s:%s:%s:%s:%d:%d:%ld:%ld:%ld:%x\n", f = fopen (*(++argv), "r");
printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n", if (f < 0 ) {
ut->ut_type, ut->ut_pid, ut->ut_line, perror (*argv);
ut->ut_id, ut->ut_user, ut->ut_host, exit (FALSE);
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);
}
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" #include "internal.h"
#ifdef BB_GREP
#include <stdio.h> #include <stdio.h>
#include <dirent.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) 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 */ /* END CODE */

146
grep.c
View File

@ -11,7 +11,6 @@
*/ */
#include "internal.h" #include "internal.h"
#ifdef BB_GREP
#include <stdio.h> #include <stdio.h>
#include <dirent.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) 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 */ /* END CODE */

View File

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

@ -9,39 +9,44 @@
* *
* made against libc6 * made against libc6
*/ */
#include "internal.h" #include "internal.h"
#include <stdio.h> #include <stdio.h>
#include <utmp.h> #include <utmp.h>
const char dutmp_usage[] = "dutmp\n" const char dutmp_usage[] = "dutmp\n"
"\n" "\n"
"\tDump file or stdin utmp file format to stdout, pipe delimited.\n" "\tDump file or stdin utmp file format to stdout, pipe delimited.\n"
"\tdutmp /var/run/utmp\n"; "\tdutmp /var/run/utmp\n";
extern int extern int dutmp_fn (int argc, char **argv)
dutmp_fn(const struct FileInfo * i)
{ {
FILE * f = stdin; FILE *f = stdin;
struct utmp * ut = (struct utmp *) malloc(sizeof(struct utmp) ); struct utmp ut;
if ( i ) if ((argc < 2) || (**(argv + 1) == '-')) {
if (! (f = fopen(i->source, "r"))) { fprintf (stderr, "Usage: %s %s\n", *argv, dutmp_usage);
name_and_error(i->source); exit (FALSE);
return 1; }
}
while (fread (ut, 1, sizeof(struct utmp), f)) { if ( **(++argv) == 0 ) {
//printf("%d:%d:%s:%s:%s:%s:%d:%d:%ld:%ld:%ld:%x\n", f = fopen (*(++argv), "r");
printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n", if (f < 0 ) {
ut->ut_type, ut->ut_pid, ut->ut_line, perror (*argv);
ut->ut_id, ut->ut_user, ut->ut_host, exit (FALSE);
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);
}
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 "internal.h"
#include <stdio.h> #include <stdio.h>
#include <signal.h> #include <signal.h>
const char more_usage[] = "[file ...]"; 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) extern int more_main(int argc, char **argv)
{ {
int c, lines=0; int c, lines=0, input;
int next_page=0, rows = 24; int next_page=0, rows = 24;
#ifdef ERASE_STUFF #ifdef BB_MORE_TERM
int cols=79; int cols;
struct winsize win;
#endif #endif
struct stat st; struct stat st;
FILE *file = stdin; FILE *file = stdin;
if ( strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0 ) { if ( strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0 ) {
fprintf(stderr, "Usage: %s %s", *argv, more_usage); fprintf(stderr, "Usage: %s %s", *argv, more_usage);
return(FALSE); exit(FALSE);
} }
argc--; argc--;
argv++; argv++;
@ -48,23 +69,47 @@ extern int more_main(int argc, char **argv)
file = fopen(*argv, "r"); file = fopen(*argv, "r");
if (file == NULL) { if (file == NULL) {
perror("Can't open file"); perror("Can't open file");
return(FALSE); exit(FALSE);
} }
fstat(fileno(file), &st); fstat(fileno(file), &st);
fprintf(stderr, "hi\n"); 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) { while ((c = getc(file)) != EOF) {
if ( next_page ) { if ( next_page ) {
int len=0; int len=0;
next_page = 0; next_page = 0;
lines=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 )), (int) (100*( (double) ftell(file) / (double) st.st_size )),
st.st_size); st.st_size,
#ifdef BB_MORE_TERM
""
#else
"\n"
#endif
);
fflush(stdout); fflush(stdout);
getc( stdin); input = getc( stdin);
#ifdef ERASE_STUFF
/* Try to erase the "More" message */ #ifdef BB_MORE_TERM
/* Erase the "More" message */
while(len-- > 0) while(len-- > 0)
putc('\b', stdout); putc('\b', stdout);
while(len++ < cols) while(len++ < cols)
@ -73,7 +118,12 @@ extern int more_main(int argc, char **argv)
putc('\b', stdout); putc('\b', stdout);
fflush(stdout); fflush(stdout);
#endif #endif
} }
if (input=='q')
goto end;
if (input==' ' && c == '\n' )
next_page = 1;
if ( c == '\n' && ++lines == (rows + 1) ) if ( c == '\n' && ++lines == (rows + 1) )
next_page = 1; next_page = 1;
putc(c, stdout); putc(c, stdout);
@ -84,7 +134,10 @@ extern int more_main(int argc, char **argv)
argc--; argc--;
argv++; argv++;
} }
return(TRUE); end:
#ifdef BB_MORE_TERM
gotsig(0);
#endif
exit(TRUE);
} }

View File

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

View File

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

1722
tar.c

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,6 @@
#include <linux/unistd.h> #include <linux/unistd.h>
#include <stdio.h> #include <stdio.h>
#include <getopt.h>
#define __NR_klog __NR_syslog #define __NR_klog __NR_syslog
@ -22,74 +21,78 @@
#include <sys/klog.h> #include <sys/klog.h>
#define klog klogctl #define klog klogctl
#else #else
static inline _syscall3(int,klog,int,type,char *,b,int,len) static inline _syscall3 (int, klog, int, type, char *, b, int, len)
#endif /* __GLIBC__ */ #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]; char buf[4096];
int i; int i;
int n; int n;
int c; int level = 0;
int level = 0; int lastc;
int lastc; int cmd = 3;
int cmd = 3;
while ((c = getopt( argc, argv, "cn:" )) != EOF) { argc--;
switch (c) { argv++;
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;
if (argc > 1) {
fprintf(stderr, "%s\n", dmesg_usage);
exit(1);
}
if (cmd == 8) { /* Parse any options */
n = klog( cmd, NULL, level ); while (argc && **argv == '-') {
if (n < 0) { while (*++(*argv))
perror( "klog" ); switch (**argv) {
exit( 1 ); case 'c':
} cmd = 4;
exit( 0 ); break;
} case 'n':
cmd = 8;
if (--argc == 0)
goto end;
level = atoi (*(++argv));
--argc;
++argv;
break;
default:
goto end;
}
}
n = klog( cmd, buf, sizeof( buf ) ); if (cmd == 8) {
if (n < 0) { n = klog (cmd, NULL, level);
perror( "klog" ); if (n < 0) {
exit( 1 ); perror ("klog");
} exit (FALSE);
}
exit (TRUE);
}
lastc = '\n'; n = klog (cmd, buf, sizeof (buf));
for (i = 0; i < n; i++) { if (n < 0) {
if ((i == 0 || buf[i - 1] == '\n') && buf[i] == '<') { perror ("klog");
i++; exit (FALSE);
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++; i++;
if (buf[i] == '>') while (buf[i] >= '0' && buf[i] <= '9')
i++; i++;
} if (buf[i] == '>')
lastc = buf[i]; i++;
putchar( lastc ); }
} lastc = buf[i];
if (lastc != '\n') putchar (lastc);
putchar( '\n' ); }
return 0; 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 "internal.h"
#include <stdio.h> #include <stdio.h>
#include <signal.h> #include <signal.h>
const char more_usage[] = "[file ...]"; 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) extern int more_main(int argc, char **argv)
{ {
int c, lines=0; int c, lines=0, input;
int next_page=0, rows = 24; int next_page=0, rows = 24;
#ifdef ERASE_STUFF #ifdef BB_MORE_TERM
int cols=79; int cols;
struct winsize win;
#endif #endif
struct stat st; struct stat st;
FILE *file = stdin; FILE *file = stdin;
if ( strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0 ) { if ( strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0 ) {
fprintf(stderr, "Usage: %s %s", *argv, more_usage); fprintf(stderr, "Usage: %s %s", *argv, more_usage);
return(FALSE); exit(FALSE);
} }
argc--; argc--;
argv++; argv++;
@ -48,23 +69,47 @@ extern int more_main(int argc, char **argv)
file = fopen(*argv, "r"); file = fopen(*argv, "r");
if (file == NULL) { if (file == NULL) {
perror("Can't open file"); perror("Can't open file");
return(FALSE); exit(FALSE);
} }
fstat(fileno(file), &st); fstat(fileno(file), &st);
fprintf(stderr, "hi\n"); 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) { while ((c = getc(file)) != EOF) {
if ( next_page ) { if ( next_page ) {
int len=0; int len=0;
next_page = 0; next_page = 0;
lines=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 )), (int) (100*( (double) ftell(file) / (double) st.st_size )),
st.st_size); st.st_size,
#ifdef BB_MORE_TERM
""
#else
"\n"
#endif
);
fflush(stdout); fflush(stdout);
getc( stdin); input = getc( stdin);
#ifdef ERASE_STUFF
/* Try to erase the "More" message */ #ifdef BB_MORE_TERM
/* Erase the "More" message */
while(len-- > 0) while(len-- > 0)
putc('\b', stdout); putc('\b', stdout);
while(len++ < cols) while(len++ < cols)
@ -73,7 +118,12 @@ extern int more_main(int argc, char **argv)
putc('\b', stdout); putc('\b', stdout);
fflush(stdout); fflush(stdout);
#endif #endif
} }
if (input=='q')
goto end;
if (input==' ' && c == '\n' )
next_page = 1;
if ( c == '\n' && ++lines == (rows + 1) ) if ( c == '\n' && ++lines == (rows + 1) )
next_page = 1; next_page = 1;
putc(c, stdout); putc(c, stdout);
@ -84,7 +134,10 @@ extern int more_main(int argc, char **argv)
argc--; argc--;
argv++; argv++;
} }
return(TRUE); end:
#ifdef BB_MORE_TERM
gotsig(0);
#endif
exit(TRUE);
} }