On Thu, Feb 09, 2012 at 12:15:32PM +0000, stateless wrote: > Hi all, > > Implemented yes(1), sync(1) and printenv(1). Source is attached, > haven't had time to write the manpage yet. > > Cheers, > stateless
These are slightly shorter and printenv() returns 1 when it can't find the environment variable. Thanks, Rob
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "util.h" int main(int argc, char **argv) { char *print_me; if(argc < 2){ print_me = "y"; }else{ char *arg, *p; int i, len = 1; for(i = 1; i < argc; i++) len += 1 + strlen(argv[i]); p = arg = malloc(len); if(!arg) eprintf("malloc():"); for(i = 1; i < argc; i++) p += sprintf(p, "%s%s", argv[i], argv[i + 1] ? " " : ""); print_me = arg; } for(;;) printf("%s\n", print_me); }
#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv, char **environ) { int ret = 0; if(argc <= 1){ char **ep; for(ep = environ; *ep; ep++) printf("%s\n", *ep); }else{ int i; for(i = 1; i < argc; i++){ char *env = getenv(argv[i]); if(env) printf("%s\n", env); else ret = 1; } } return ret; }