Fixing the ARG macros and adding nice.

Thanks "Galos, David" <galosd83@students.rowan.edu>!
This commit is contained in:
Christoph Lohmann 2013-05-29 20:52:39 +02:00
parent f648c7fa71
commit c9aac796c8
3 changed files with 58 additions and 22 deletions

View File

@ -41,6 +41,7 @@ SRC = \
mkdir.c \ mkdir.c \
mkfifo.c \ mkfifo.c \
mv.c \ mv.c \
nice.c \
nl.c \ nl.c \
nohup.c \ nohup.c \
paste.c \ paste.c \

55
arg.h Normal file
View File

@ -0,0 +1,55 @@
/*
* Copy me if you can.
* by 20h
*/
#ifndef __ARG_H__
#define __ARG_H__
extern char *argv0;
#define USED(x) ((void)(x))
/* use main(int argc, char *argv[]) */
#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
argv[0] && argv[0][1]\
&& argv[0][0] == '-';\
argc--, argv++) {\
char _argc;\
char **_argv;\
int brk;\
if (argv[0][1] == '-' && argv[0][2] == '\0') {\
argv++;\
argc--;\
break;\
}\
for (brk = 0, argv[0]++, _argv = argv;\
argv[0][0] && !brk;\
argv[0]++) {\
if (_argv != argv)\
break;\
_argc = argv[0][0];\
switch (_argc)
#define ARGEND }\
USED(_argc);\
}\
USED(argv);\
USED(argc);
#define ARGC() _argc
#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\
((x), abort(), (char *)0) :\
(brk = 1, (argv[0][1] != '\0')?\
(&argv[0][1]) :\
(argc--, argv++, argv[0])))
#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\
(char *)0 :\
(brk = 1, (argv[0][1] != '\0')?\
(&argv[0][1]) :\
(argc--, argv++, argv[0])))
#endif

24
util.h
View File

@ -1,5 +1,7 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include "arg.h"
#define UTF8_POINT(c) (((c) & 0xc0) != 0x80) #define UTF8_POINT(c) (((c) & 0xc0) != 0x80)
#define MIN(x,y) ((x) < (y) ? (x) : (y)) #define MIN(x,y) ((x) < (y) ? (x) : (y))
@ -7,28 +9,6 @@
#define LEN(x) (sizeof (x) / sizeof *(x)) #define LEN(x) (sizeof (x) / sizeof *(x))
#define ARGBEGIN \
{ \
if(!argv0) \
argv0 = argv[0]; \
for(argc--, argv++; argv[0] && argv[0][0] == '-' && argv[0][1] != '\0'; argc--, argv++) { \
if(argv[0][1] == '-' && argv[0][2] == '\0') { /* -- signifies end of flags */ \
argc--; argv++; \
break; \
} \
for(argv[0]++; *argv[0] != '\0'; argv[0]++) \
switch(*argv[0])
#define ARGEND \
} \
}
#define ARGC() (*argv[0])
#define ARGF() (*argv[0] ? argv[0] \
: argv[1] ? (argc--, *++argv) : NULL)
#define EARGF(x) (*argv[0] ? argv[0] \
: argv[1] ? (argc--, *++argv) : ((x), abort(), NULL))
extern char *argv0; extern char *argv0;
char *agetcwd(void); char *agetcwd(void);