UnixReview.com
June 2006
Listing 6: lcase.c
/* lcase - convert string to lowercase */ #include <config.h> #if defined (HAVE_UNISTD_H) # include <unistd.h> #endif #include "bashansi.h" #include <stdio.h> #include <errno.h> #include "builtins.h" #include "shell.h" #include "bashgetopt.h" #if !defined (errno) extern int errno; #endif extern char *strerror (); lcase_builtin (list) WORD_LIST *list; { int n = 0; int ch; char *string; char *var = NULL; reset_internal_getopt (); while ((ch = internal_getopt (list, "p:")) != -1) switch(ch) { case 'p': var = list_optarg; break; default: builtin_usage(); return (EX_USAGE); } list = loptend; if (list == 0 || list->next) { builtin_usage (); return (EX_USAGE); } if (no_options (list)) return (EX_USAGE); string = list->word->word; while ( string[n] ) { string[n] = tolower(string[n]); ++n; } if ( var ) bind_variable (var, string, 0); else printf ("%s\n", string); return (EXECUTION_SUCCESS); } char *lcase_doc[] = { "The STRING is converted to lower case and either:", " stored in the variable supplied with -p", " or", " printed to stdout if no variable is given", (char *)NULL }; struct builtin lcase_struct = { "lcase", /* builtin name */ lcase_builtin, /* function implementing the builtin */ BUILTIN_ENABLED, /* initial flags for builtin */ lcase_doc, /* array of long documentation strings. */ "lcase [-p VAR] STRING", /* usage synopsis */ 0 /* reserved for internal use */ };