Latest and greatest.

-Erik
This commit is contained in:
Eric Andersen 1999-10-12 15:42:48 +00:00
parent f811e07b07
commit 2ce1edcf54
19 changed files with 255 additions and 305 deletions

View File

@ -1,3 +1,6 @@
0.28
mini-netcat (mnc) rewritten.
0.27 0.27
Mount now supports -a, and -t auto. Mount now supports -a, and -t auto.
Mount now updates mtab correctly for 'ro'. Mount now updates mtab correctly for 'ro'.

View File

@ -189,7 +189,7 @@ int main(int argc, char **argv)
} }
a++; a++;
} }
return (busybox_main(argc, argv)); exit (busybox_main(argc, argv));
} }
@ -216,8 +216,7 @@ int busybox_main(int argc, char **argv)
fprintf(stderr, "\nCurrently defined functions:\n"); fprintf(stderr, "\nCurrently defined functions:\n");
while (a->name != 0) { while (a->name != 0) {
col+=fprintf(stderr, "%s%s", ((col==0)? "\t":", "), a->name); col+=fprintf(stderr, "%s%s", ((col==0)? "\t":", "), (a++)->name);
a++;
if (col>60) { if (col>60) {
fprintf(stderr, ",\n"); fprintf(stderr, ",\n");
col=0; col=0;

View File

@ -189,7 +189,7 @@ int main(int argc, char **argv)
} }
a++; a++;
} }
return (busybox_main(argc, argv)); exit (busybox_main(argc, argv));
} }
@ -216,8 +216,7 @@ int busybox_main(int argc, char **argv)
fprintf(stderr, "\nCurrently defined functions:\n"); fprintf(stderr, "\nCurrently defined functions:\n");
while (a->name != 0) { while (a->name != 0) {
col+=fprintf(stderr, "%s%s", ((col==0)? "\t":", "), a->name); col+=fprintf(stderr, "%s%s", ((col==0)? "\t":", "), (a++)->name);
a++;
if (col>60) { if (col>60) {
fprintf(stderr, ",\n"); fprintf(stderr, ",\n");
col=0; col=0;

View File

@ -35,7 +35,7 @@
//#define BB_MKDIR //#define BB_MKDIR
//#define BB_MKNOD //#define BB_MKNOD
////#define BB_MKSWAP ////#define BB_MKSWAP
//#define BB_MNC #define BB_MNC
//#define BB_MONADIC //#define BB_MONADIC
#define BB_MORE #define BB_MORE
#define BB_MOUNT #define BB_MOUNT
@ -43,7 +43,7 @@
#define BB_MV #define BB_MV
//#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

8
cat.c
View File

@ -29,9 +29,9 @@ extern int cat_more_main(int argc, char **argv)
int c; int c;
FILE *file = stdin; FILE *file = stdin;
if (argc < 2) { if ( (argc < 2) || (**(argv+1) == '-') ) {
fprintf(stderr, "Usage: %s %s", *argv, cat_usage); fprintf(stderr, "Usage: %s %s", *argv, cat_usage);
return(FALSE); exit(FALSE);
} }
argc--; argc--;
argv++; argv++;
@ -40,7 +40,7 @@ extern int cat_more_main(int argc, char **argv)
file = fopen(*argv, "r"); file = fopen(*argv, "r");
if (file == NULL) { if (file == NULL) {
perror(*argv); perror(*argv);
return(FALSE); exit(FALSE);
} }
while ((c = getc(file)) != EOF) while ((c = getc(file)) != EOF)
putc(c, stdout); putc(c, stdout);
@ -50,5 +50,5 @@ extern int cat_more_main(int argc, char **argv)
argc--; argc--;
argv++; argv++;
} }
return(TRUE); exit(TRUE);
} }

12
chown.c
View File

@ -66,7 +66,7 @@ int chown_main(int argc, char **argv)
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "Usage: %s %s", *argv, fprintf(stderr, "Usage: %s %s", *argv,
(chownApp==TRUE)? chown_usage : chgrp_usage); (chownApp==TRUE)? chown_usage : chgrp_usage);
return( FALSE); exit( FALSE);
} }
invocationName=*argv; invocationName=*argv;
argc--; argc--;
@ -80,7 +80,7 @@ int chown_main(int argc, char **argv)
break; break;
default: default:
fprintf(stderr, "Unknown option: %c\n", **argv); fprintf(stderr, "Unknown option: %c\n", **argv);
return( FALSE); exit( FALSE);
} }
argc--; argc--;
argv++; argv++;
@ -95,7 +95,7 @@ int chown_main(int argc, char **argv)
grp = getgrnam(groupName); grp = getgrnam(groupName);
if (grp == NULL) { if (grp == NULL) {
fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName); fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);
return( FALSE); exit( FALSE);
} }
gid = grp->gr_gid; gid = grp->gr_gid;
@ -104,7 +104,7 @@ int chown_main(int argc, char **argv)
pwd = getpwnam(*argv); pwd = getpwnam(*argv);
if (pwd == NULL) { if (pwd == NULL) {
fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv); fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
return( FALSE); exit( FALSE);
} }
uid = pwd->pw_uid; uid = pwd->pw_uid;
} }
@ -112,11 +112,11 @@ int chown_main(int argc, char **argv)
/* Ok, ready to do the deed now */ /* Ok, ready to do the deed now */
if (argc <= 1) { if (argc <= 1) {
fprintf(stderr, "%s: too few arguments", invocationName); fprintf(stderr, "%s: too few arguments", invocationName);
return( FALSE); exit( FALSE);
} }
while (argc-- > 1) { while (argc-- > 1) {
argv++; argv++;
recursiveAction( *argv, recursiveFlag, TRUE, fileAction, fileAction); recursiveAction( *argv, recursiveFlag, TRUE, fileAction, fileAction);
} }
return(TRUE); exit(TRUE);
} }

View File

@ -20,8 +20,9 @@
*/ */
#include "internal.h" #include "internal.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <errno.h>
static const char chroot_usage[] = "NEWROOT [COMMAND...]\n" static const char chroot_usage[] = "NEWROOT [COMMAND...]\n"
@ -31,18 +32,17 @@ static const char chroot_usage[] = "NEWROOT [COMMAND...]\n"
int chroot_main(int argc, char **argv) int chroot_main(int argc, char **argv)
{ {
if (argc < 2) { if ( (argc < 2) || (**(argv+1) == '-') ) {
fprintf(stderr, "Usage: %s %s", *argv, chroot_usage); fprintf(stderr, "Usage: %s %s", *argv, chroot_usage);
return( FALSE); exit( FALSE);
} }
argc--; argc--;
argv++; argv++;
fprintf(stderr, "new root: %s\n", *argv);
if (chroot (*argv) || (chdir ("/"))) { if (chroot (*argv) || (chdir ("/"))) {
perror("cannot chroot"); fprintf(stderr, "chroot: cannot change root directory to %s: %s\n",
return( FALSE); *argv, strerror(errno));
exit( FALSE);
} }
argc--; argc--;
@ -56,10 +56,10 @@ int chroot_main(int argc, char **argv)
prog = getenv ("SHELL"); prog = getenv ("SHELL");
if (!prog) if (!prog)
prog = "/bin/sh"; prog = "/bin/sh";
fprintf(stderr, "no command. running: %s\n", prog);
execlp (prog, prog, NULL); execlp (prog, prog, NULL);
} }
perror("cannot exec"); fprintf(stderr, "chroot: cannot execute %s: %s\n",
return(FALSE); *argv, strerror(errno));
exit( FALSE);
} }

View File

@ -27,5 +27,5 @@ extern int
clear_main(int argc, char** argv) clear_main(int argc, char** argv)
{ {
printf("\033[H\033[J"); printf("\033[H\033[J");
return 0; exit( TRUE);
} }

View File

@ -27,5 +27,5 @@ extern int
clear_main(int argc, char** argv) clear_main(int argc, char** argv)
{ {
printf("\033[H\033[J"); printf("\033[H\033[J");
return 0; exit( TRUE);
} }

View File

@ -29,9 +29,9 @@ extern int cat_more_main(int argc, char **argv)
int c; int c;
FILE *file = stdin; FILE *file = stdin;
if (argc < 2) { if ( (argc < 2) || (**(argv+1) == '-') ) {
fprintf(stderr, "Usage: %s %s", *argv, cat_usage); fprintf(stderr, "Usage: %s %s", *argv, cat_usage);
return(FALSE); exit(FALSE);
} }
argc--; argc--;
argv++; argv++;
@ -40,7 +40,7 @@ extern int cat_more_main(int argc, char **argv)
file = fopen(*argv, "r"); file = fopen(*argv, "r");
if (file == NULL) { if (file == NULL) {
perror(*argv); perror(*argv);
return(FALSE); exit(FALSE);
} }
while ((c = getc(file)) != EOF) while ((c = getc(file)) != EOF)
putc(c, stdout); putc(c, stdout);
@ -50,5 +50,5 @@ extern int cat_more_main(int argc, char **argv)
argc--; argc--;
argv++; argv++;
} }
return(TRUE); exit(TRUE);
} }

View File

@ -66,7 +66,7 @@ int chown_main(int argc, char **argv)
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "Usage: %s %s", *argv, fprintf(stderr, "Usage: %s %s", *argv,
(chownApp==TRUE)? chown_usage : chgrp_usage); (chownApp==TRUE)? chown_usage : chgrp_usage);
return( FALSE); exit( FALSE);
} }
invocationName=*argv; invocationName=*argv;
argc--; argc--;
@ -80,7 +80,7 @@ int chown_main(int argc, char **argv)
break; break;
default: default:
fprintf(stderr, "Unknown option: %c\n", **argv); fprintf(stderr, "Unknown option: %c\n", **argv);
return( FALSE); exit( FALSE);
} }
argc--; argc--;
argv++; argv++;
@ -95,7 +95,7 @@ int chown_main(int argc, char **argv)
grp = getgrnam(groupName); grp = getgrnam(groupName);
if (grp == NULL) { if (grp == NULL) {
fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName); fprintf(stderr, "%s: Unknown group name: %s\n", invocationName, groupName);
return( FALSE); exit( FALSE);
} }
gid = grp->gr_gid; gid = grp->gr_gid;
@ -104,7 +104,7 @@ int chown_main(int argc, char **argv)
pwd = getpwnam(*argv); pwd = getpwnam(*argv);
if (pwd == NULL) { if (pwd == NULL) {
fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv); fprintf(stderr, "%s: Unknown user name: %s\n", invocationName, *argv);
return( FALSE); exit( FALSE);
} }
uid = pwd->pw_uid; uid = pwd->pw_uid;
} }
@ -112,11 +112,11 @@ int chown_main(int argc, char **argv)
/* Ok, ready to do the deed now */ /* Ok, ready to do the deed now */
if (argc <= 1) { if (argc <= 1) {
fprintf(stderr, "%s: too few arguments", invocationName); fprintf(stderr, "%s: too few arguments", invocationName);
return( FALSE); exit( FALSE);
} }
while (argc-- > 1) { while (argc-- > 1) {
argv++; argv++;
recursiveAction( *argv, recursiveFlag, TRUE, fileAction, fileAction); recursiveAction( *argv, recursiveFlag, TRUE, fileAction, fileAction);
} }
return(TRUE); exit(TRUE);
} }

View File

@ -20,8 +20,9 @@
*/ */
#include "internal.h" #include "internal.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <errno.h>
static const char chroot_usage[] = "NEWROOT [COMMAND...]\n" static const char chroot_usage[] = "NEWROOT [COMMAND...]\n"
@ -31,18 +32,17 @@ static const char chroot_usage[] = "NEWROOT [COMMAND...]\n"
int chroot_main(int argc, char **argv) int chroot_main(int argc, char **argv)
{ {
if (argc < 2) { if ( (argc < 2) || (**(argv+1) == '-') ) {
fprintf(stderr, "Usage: %s %s", *argv, chroot_usage); fprintf(stderr, "Usage: %s %s", *argv, chroot_usage);
return( FALSE); exit( FALSE);
} }
argc--; argc--;
argv++; argv++;
fprintf(stderr, "new root: %s\n", *argv);
if (chroot (*argv) || (chdir ("/"))) { if (chroot (*argv) || (chdir ("/"))) {
perror("cannot chroot"); fprintf(stderr, "chroot: cannot change root directory to %s: %s\n",
return( FALSE); *argv, strerror(errno));
exit( FALSE);
} }
argc--; argc--;
@ -56,10 +56,10 @@ int chroot_main(int argc, char **argv)
prog = getenv ("SHELL"); prog = getenv ("SHELL");
if (!prog) if (!prog)
prog = "/bin/sh"; prog = "/bin/sh";
fprintf(stderr, "no command. running: %s\n", prog);
execlp (prog, prog, NULL); execlp (prog, prog, NULL);
} }
perror("cannot exec"); fprintf(stderr, "chroot: cannot execute %s: %s\n",
return(FALSE); *argv, strerror(errno));
exit( FALSE);
} }

View File

@ -91,7 +91,7 @@ extern int cp_main(int argc, char **argv)
if (argc < 3) { if (argc < 3) {
fprintf(stderr, "Usage: %s", cp_usage); fprintf(stderr, "Usage: %s", cp_usage);
return (FALSE); exit (FALSE);
} }
argc--; argc--;
argv++; argv++;
@ -129,13 +129,13 @@ extern int cp_main(int argc, char **argv)
if ((argc > 3) && !dirFlag) { if ((argc > 3) && !dirFlag) {
fprintf(stderr, "%s: not a directory\n", destName); fprintf(stderr, "%s: not a directory\n", destName);
return (FALSE); exit (FALSE);
} }
while (argc-- >= 2) { while (argc-- >= 2) {
srcName = *(argv++); srcName = *(argv++);
return recursiveAction(srcName, recursiveFlag, followLinks, exit( recursiveAction(srcName, recursiveFlag, followLinks,
fileAction, fileAction); fileAction, fileAction));
} }
return( TRUE); exit( TRUE);
} }

View File

@ -1,3 +1,24 @@
/*
* Mini date implementation for busybox
*
* Copyright (C) 1999 by Erik Andersen <andersee@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "internal.h" #include "internal.h"
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
@ -5,7 +26,6 @@
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <stdio.h> #include <stdio.h>
#include <getopt.h>
/* This 'date' command supports only 2 time setting formats, /* This 'date' command supports only 2 time setting formats,
@ -14,25 +34,12 @@
an RFC 822 complient date output for shell scripting an RFC 822 complient date output for shell scripting
mail commands */ mail commands */
const char date_usage[] = "date [-uR] [+FORMAT|+%f] [ [-s|-d] MMDDhhmm[[CC]YY]\n" const char date_usage[] = "Usage: date [OPTION]... [+FORMAT]\n"
"| [[[[CCYY.]MM.DD-]hh:mm[:ss]]]] ]"; " or: date [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n"
"Display the current time in the given FORMAT, or set the system date.\n"
//static const char date_usage[] = "Usage: date [OPTION]... [+FORMAT]\n" "\nOptions:\n\t-R\t\toutput RFC-822 compliant date string\n"
//"or: date [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n" "\t-s\t\tset time described by STRING\n"
//"Display the current time in the given FORMAT, or set the system date.\n"; "\t-u\t\tprint or set Coordinated Universal Time\n";
static struct option const long_options[] =
{
{"date", required_argument, NULL, 'd'},
/* {"rfc-822", no_argument, NULL, 'R'},
{"set", required_argument, NULL, 's'},
{"uct", no_argument, NULL, 'u'},
{"utc", no_argument, NULL, 'u'},
{"universal", no_argument, NULL, 'u'}, */
{NULL, 0, NULL, 0}
};
/* Input parsing code is always bulky - used heavy duty libc stuff as /* Input parsing code is always bulky - used heavy duty libc stuff as
@ -53,7 +60,7 @@ date_conv_time(struct tm *tm_time, const char *t_string) {
if(nr < 4 || nr > 5) { if(nr < 4 || nr > 5) {
fprintf(stderr, "date: invalid date `%s'\n", t_string); fprintf(stderr, "date: invalid date `%s'\n", t_string);
exit(1); exit( FALSE);
} }
/* correct for century - minor Y2K problem here? */ /* correct for century - minor Y2K problem here? */
@ -147,15 +154,15 @@ date_conv_ftime(struct tm *tm_time, const char *t_string) {
fprintf(stderr, "date: invalid date `%s'\n", t_string); fprintf(stderr, "date: invalid date `%s'\n", t_string);
exit(1); exit( FALSE);
} }
void void
date_err(void) { date_err(void) {
fprintf(stderr, "date: only one date argument can be given at a time.\n"); fprintf (stderr, "%s\n", date_usage);
exit(1); exit( FALSE);
} }
int int
@ -164,82 +171,56 @@ date_main(int argc, char * * argv)
char *date_str = NULL; char *date_str = NULL;
char *date_fmt = NULL; char *date_fmt = NULL;
char *t_buff; char *t_buff;
int i;
int set_time = 0; int set_time = 0;
int rfc822 = 0; int rfc822 = 0;
int utc = 0; int utc = 0;
int use_arg = 0; int use_arg = 0;
int n_args;
time_t tm; time_t tm;
struct tm tm_time; struct tm tm_time;
char optc;
/* Interpret command line args */ /* Interpret command line args */
i = --argc;
argv++;
while ((optc = getopt_long (argc, argv, "d:Rs:u", long_options, NULL)) while (i > 0 && **argv) {
!= EOF) { if (**argv == '-') {
switch (optc) { while (i>0 && *++(*argv)) switch (**argv) {
case 0: case 'R':
break; rfc822 = 1;
break;
case 'R': case 's':
rfc822 = 1; set_time = 1;
break; if(date_str != NULL) date_err();
date_str = optarg;
case 's': break;
set_time = 1; case 'u':
if(date_str != NULL) date_err(); utc = 1;
date_str = optarg; if (putenv ("TZ=UTC0") != 0) {
break; fprintf(stderr,"date: memory exhausted\n");
exit( FALSE);
case 'u': }
utc = 1; /* Look ma, no break. Don't fix it either. */
if (putenv ("TZ=UTC0") != 0) { case 'd':
fprintf(stderr,"date: memory exhausted\n"); use_arg = 1;
return(1); if(date_str != NULL) date_err();
} date_str = optarg;
#if LOCALTIME_CACHE break;
tzset (); case '-':
#endif break; date_err();
}
case 'd': } else {
use_arg = 1; if ( (date_fmt == NULL) && (strcmp(*argv, "+")==0) )
if(date_str != NULL) date_err(); date_fmt=*argv;
date_str = optarg; else if (date_str == NULL) {
break; set_time = 1;
date_str=*argv;
default: } else {
fprintf(stderr, "Usage: %s", date_usage); date_err();
break; }
}
i--;
argv++;
} }
}
n_args = argc - optind;
while (n_args--){
switch(argv[optind][0]) {
case '+':
/* Date format strings */
if(date_fmt != NULL) {
fprintf(stderr, "date: only one date format can be given.\n");
return(1);
}
date_fmt = &argv[optind][1];
break;
case '\0':
break;
default:
/* Anything left over must be a date string to set the time */
set_time = 1;
if(date_str != NULL) date_err();
date_str = argv[optind];
break;
}
optind++;
}
/* Now we have parsed all the information except the date format /* Now we have parsed all the information except the date format
@ -267,14 +248,14 @@ date_main(int argc, char * * argv)
tm = mktime(&tm_time); tm = mktime(&tm_time);
if (tm < 0 ) { if (tm < 0 ) {
fprintf(stderr, "date: invalid date `%s'\n", date_str); fprintf(stderr, "date: invalid date `%s'\n", date_str);
exit(1); exit( FALSE);
} }
/* if setting time, set it */ /* if setting time, set it */
if(set_time) { if(set_time) {
if( stime(&tm) < 0) { if( stime(&tm) < 0) {
fprintf(stderr, "date: can't set date.\n"); fprintf(stderr, "date: can't set date.\n");
exit(1); exit( FALSE);
} }
} }
} }
@ -292,7 +273,7 @@ date_main(int argc, char * * argv)
} else if ( *date_fmt == '\0' ) { } else if ( *date_fmt == '\0' ) {
/* Imitate what GNU 'date' does with NO format string! */ /* Imitate what GNU 'date' does with NO format string! */
printf ("\n"); printf ("\n");
return(0); exit( TRUE);
} }
/* Handle special conversions */ /* Handle special conversions */
@ -306,6 +287,7 @@ date_main(int argc, char * * argv)
strftime(t_buff, 200, date_fmt, &tm_time); strftime(t_buff, 200, date_fmt, &tm_time);
printf("%s\n", t_buff); printf("%s\n", t_buff);
return(0); exit( TRUE);
} }

View File

@ -4,15 +4,15 @@
const char pwd_usage[] = "Print the current directory.\n"; const char pwd_usage[] = "Print the current directory.\n";
extern int extern int
pwd_main(struct FileInfo * i, int argc, char * * argv) pwd_main(int argc, char * * argv)
{ {
char buf[1024]; char buf[1024];
if ( getcwd(buf, sizeof(buf)) == NULL ) { if ( getcwd(buf, sizeof(buf)) == NULL ) {
name_and_error("get working directory"); perror("get working directory");
return 1; exit( FALSE);
} }
printf("%s\n", buf); printf("%s\n", buf);
return 0; exit( TRUE);
} }

10
cp.c
View File

@ -91,7 +91,7 @@ extern int cp_main(int argc, char **argv)
if (argc < 3) { if (argc < 3) {
fprintf(stderr, "Usage: %s", cp_usage); fprintf(stderr, "Usage: %s", cp_usage);
return (FALSE); exit (FALSE);
} }
argc--; argc--;
argv++; argv++;
@ -129,13 +129,13 @@ extern int cp_main(int argc, char **argv)
if ((argc > 3) && !dirFlag) { if ((argc > 3) && !dirFlag) {
fprintf(stderr, "%s: not a directory\n", destName); fprintf(stderr, "%s: not a directory\n", destName);
return (FALSE); exit (FALSE);
} }
while (argc-- >= 2) { while (argc-- >= 2) {
srcName = *(argv++); srcName = *(argv++);
return recursiveAction(srcName, recursiveFlag, followLinks, exit( recursiveAction(srcName, recursiveFlag, followLinks,
fileAction, fileAction); fileAction, fileAction));
} }
return( TRUE); exit( TRUE);
} }

172
date.c
View File

@ -1,3 +1,24 @@
/*
* Mini date implementation for busybox
*
* Copyright (C) 1999 by Erik Andersen <andersee@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "internal.h" #include "internal.h"
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
@ -5,7 +26,6 @@
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <stdio.h> #include <stdio.h>
#include <getopt.h>
/* This 'date' command supports only 2 time setting formats, /* This 'date' command supports only 2 time setting formats,
@ -14,25 +34,12 @@
an RFC 822 complient date output for shell scripting an RFC 822 complient date output for shell scripting
mail commands */ mail commands */
const char date_usage[] = "date [-uR] [+FORMAT|+%f] [ [-s|-d] MMDDhhmm[[CC]YY]\n" const char date_usage[] = "Usage: date [OPTION]... [+FORMAT]\n"
"| [[[[CCYY.]MM.DD-]hh:mm[:ss]]]] ]"; " or: date [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n"
"Display the current time in the given FORMAT, or set the system date.\n"
//static const char date_usage[] = "Usage: date [OPTION]... [+FORMAT]\n" "\nOptions:\n\t-R\t\toutput RFC-822 compliant date string\n"
//"or: date [OPTION] [MMDDhhmm[[CC]YY][.ss]]\n" "\t-s\t\tset time described by STRING\n"
//"Display the current time in the given FORMAT, or set the system date.\n"; "\t-u\t\tprint or set Coordinated Universal Time\n";
static struct option const long_options[] =
{
{"date", required_argument, NULL, 'd'},
/* {"rfc-822", no_argument, NULL, 'R'},
{"set", required_argument, NULL, 's'},
{"uct", no_argument, NULL, 'u'},
{"utc", no_argument, NULL, 'u'},
{"universal", no_argument, NULL, 'u'}, */
{NULL, 0, NULL, 0}
};
/* Input parsing code is always bulky - used heavy duty libc stuff as /* Input parsing code is always bulky - used heavy duty libc stuff as
@ -53,7 +60,7 @@ date_conv_time(struct tm *tm_time, const char *t_string) {
if(nr < 4 || nr > 5) { if(nr < 4 || nr > 5) {
fprintf(stderr, "date: invalid date `%s'\n", t_string); fprintf(stderr, "date: invalid date `%s'\n", t_string);
exit(1); exit( FALSE);
} }
/* correct for century - minor Y2K problem here? */ /* correct for century - minor Y2K problem here? */
@ -147,15 +154,15 @@ date_conv_ftime(struct tm *tm_time, const char *t_string) {
fprintf(stderr, "date: invalid date `%s'\n", t_string); fprintf(stderr, "date: invalid date `%s'\n", t_string);
exit(1); exit( FALSE);
} }
void void
date_err(void) { date_err(void) {
fprintf(stderr, "date: only one date argument can be given at a time.\n"); fprintf (stderr, "%s\n", date_usage);
exit(1); exit( FALSE);
} }
int int
@ -164,82 +171,56 @@ date_main(int argc, char * * argv)
char *date_str = NULL; char *date_str = NULL;
char *date_fmt = NULL; char *date_fmt = NULL;
char *t_buff; char *t_buff;
int i;
int set_time = 0; int set_time = 0;
int rfc822 = 0; int rfc822 = 0;
int utc = 0; int utc = 0;
int use_arg = 0; int use_arg = 0;
int n_args;
time_t tm; time_t tm;
struct tm tm_time; struct tm tm_time;
char optc;
/* Interpret command line args */ /* Interpret command line args */
i = --argc;
argv++;
while ((optc = getopt_long (argc, argv, "d:Rs:u", long_options, NULL)) while (i > 0 && **argv) {
!= EOF) { if (**argv == '-') {
switch (optc) { while (i>0 && *++(*argv)) switch (**argv) {
case 0: case 'R':
break; rfc822 = 1;
break;
case 'R': case 's':
rfc822 = 1; set_time = 1;
break; if(date_str != NULL) date_err();
date_str = optarg;
case 's': break;
set_time = 1; case 'u':
if(date_str != NULL) date_err(); utc = 1;
date_str = optarg; if (putenv ("TZ=UTC0") != 0) {
break; fprintf(stderr,"date: memory exhausted\n");
exit( FALSE);
case 'u': }
utc = 1; /* Look ma, no break. Don't fix it either. */
if (putenv ("TZ=UTC0") != 0) { case 'd':
fprintf(stderr,"date: memory exhausted\n"); use_arg = 1;
return(1); if(date_str != NULL) date_err();
} date_str = optarg;
#if LOCALTIME_CACHE break;
tzset (); case '-':
#endif break; date_err();
}
case 'd': } else {
use_arg = 1; if ( (date_fmt == NULL) && (strcmp(*argv, "+")==0) )
if(date_str != NULL) date_err(); date_fmt=*argv;
date_str = optarg; else if (date_str == NULL) {
break; set_time = 1;
date_str=*argv;
default: } else {
fprintf(stderr, "Usage: %s", date_usage); date_err();
break; }
}
i--;
argv++;
} }
}
n_args = argc - optind;
while (n_args--){
switch(argv[optind][0]) {
case '+':
/* Date format strings */
if(date_fmt != NULL) {
fprintf(stderr, "date: only one date format can be given.\n");
return(1);
}
date_fmt = &argv[optind][1];
break;
case '\0':
break;
default:
/* Anything left over must be a date string to set the time */
set_time = 1;
if(date_str != NULL) date_err();
date_str = argv[optind];
break;
}
optind++;
}
/* Now we have parsed all the information except the date format /* Now we have parsed all the information except the date format
@ -267,14 +248,14 @@ date_main(int argc, char * * argv)
tm = mktime(&tm_time); tm = mktime(&tm_time);
if (tm < 0 ) { if (tm < 0 ) {
fprintf(stderr, "date: invalid date `%s'\n", date_str); fprintf(stderr, "date: invalid date `%s'\n", date_str);
exit(1); exit( FALSE);
} }
/* if setting time, set it */ /* if setting time, set it */
if(set_time) { if(set_time) {
if( stime(&tm) < 0) { if( stime(&tm) < 0) {
fprintf(stderr, "date: can't set date.\n"); fprintf(stderr, "date: can't set date.\n");
exit(1); exit( FALSE);
} }
} }
} }
@ -292,7 +273,7 @@ date_main(int argc, char * * argv)
} else if ( *date_fmt == '\0' ) { } else if ( *date_fmt == '\0' ) {
/* Imitate what GNU 'date' does with NO format string! */ /* Imitate what GNU 'date' does with NO format string! */
printf ("\n"); printf ("\n");
return(0); exit( TRUE);
} }
/* Handle special conversions */ /* Handle special conversions */
@ -306,6 +287,7 @@ date_main(int argc, char * * argv)
strftime(t_buff, 200, date_fmt, &tm_time); strftime(t_buff, 200, date_fmt, &tm_time);
printf("%s\n", t_buff); printf("%s\n", t_buff);
return(0); exit( TRUE);
} }

79
mnc.c
View File

@ -3,9 +3,11 @@
0.0.1 6K It works. 0.0.1 6K It works.
0.0.2 5K Smaller and you can also check the exit condition if you wish. 0.0.2 5K Smaller and you can also check the exit condition if you wish.
0.0.3 Uses select()
19980918 Busy Boxed! Dave Cinege 19980918 Busy Boxed! Dave Cinege
19990512 Uses Select. Charles P. Wright
19990513 Fixes stdin stupidity and uses buffers. Charles P. Wright
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -35,26 +37,24 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#define BUFSIZE 100
const char mnc_usage[] = const char mnc_usage[] =
"mini-netcat 0.0.1 -- Open pipe to IP:port\n" "mini-netcat 0.0.3 -- Open pipe to IP:port\n"
"\tmnc [IP] [port]\n"; "\tmnc [IP] [port]\n";
int int
mnc_main(struct FileInfo * i, int argc, char **argv) mnc_main(int argc, char **argv)
{ {
int sfd; int sfd;
int result; int result;
int len; int len;
int pid; char ch[BUFSIZE];
char ch;
struct sockaddr_in address; struct sockaddr_in address;
struct hostent *hostinfo; struct hostent *hostinfo;
#ifdef SELECT
fd_set readfds, testfds; fd_set readfds, testfds;
#endif
sfd = socket(AF_INET, SOCK_STREAM, 0); sfd = socket(AF_INET, SOCK_STREAM, 0);
@ -78,7 +78,6 @@ mnc_main(struct FileInfo * i, int argc, char **argv)
exit(2); exit(2);
} }
#ifdef SELECT
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_SET(sfd, &readfds); FD_SET(sfd, &readfds);
FD_SET(fileno(stdin), &readfds); FD_SET(fileno(stdin), &readfds);
@ -86,6 +85,7 @@ mnc_main(struct FileInfo * i, int argc, char **argv)
while(1) while(1)
{ {
int fd; int fd;
int ofd;
int nread; int nread;
testfds = readfds; testfds = readfds;
@ -101,48 +101,33 @@ mnc_main(struct FileInfo * i, int argc, char **argv)
{ {
if(FD_ISSET(fd,&testfds)) if(FD_ISSET(fd,&testfds))
{ {
ioctl(fd, FIONREAD, &nread); int trn = 0;
int rn;
if (nread == 0) ioctl(fd, FIONREAD, &nread);
exit(0);
if(fd == sfd) if(fd == sfd)
{ {
read(sfd, &ch, 1); if (nread == 0)
write(fileno(stdout), &ch, 1); exit(0);
} ofd = fileno(stdout);
else }
{ else
read(fileno(stdin), &ch, 1); {
write(sfd, &ch, 1); ofd = sfd;
} }
}
}
}
#else
pid = fork();
if (!pid)
{
int retval;
retval = 1;
while(retval == 1)
{
retval = read(fileno(stdin), &ch, 1);
write(sfd, &ch, 1);
}
}
else
{
int retval;
retval = 1;
while(retval == 1)
{
retval = read(sfd, &ch, 1);
write(fileno(stdout), &ch, 1);
}
}
exit(0);
#endif do
{
rn = (BUFSIZE < nread - trn) ? BUFSIZE : nread - trn;
trn += rn;
read(fd, ch, rn);
write(ofd, ch, rn);
}
while (trn < nread);
}
}
}
} }

8
pwd.c
View File

@ -4,15 +4,15 @@
const char pwd_usage[] = "Print the current directory.\n"; const char pwd_usage[] = "Print the current directory.\n";
extern int extern int
pwd_main(struct FileInfo * i, int argc, char * * argv) pwd_main(int argc, char * * argv)
{ {
char buf[1024]; char buf[1024];
if ( getcwd(buf, sizeof(buf)) == NULL ) { if ( getcwd(buf, sizeof(buf)) == NULL ) {
name_and_error("get working directory"); perror("get working directory");
return 1; exit( FALSE);
} }
printf("%s\n", buf); printf("%s\n", buf);
return 0; exit( TRUE);
} }