util.c

00001 /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> - All rights reserved
00002  *
00003  * Redistribution and use in source and binary forms, with or without
00004  * modification, are permitted provided that the following conditions are met:
00005  *
00006  * Redistributions of source code must retain the above copyright notice, this
00007  * list of conditions and the following disclaimer.
00008  *
00009  * Redistributions in binary form must reproduce the above copyright notice,
00010  * this list of conditions and the following disclaimer in the documentation
00011  * and/or other materials provided with the distribution.
00012  *
00013  * Neither the name of the original program's authors nor the names of its
00014  * contributors may be used to endorse or promote products derived from this
00015  * software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <stdio.h>
00031 #include <str.h>
00032 #include <stdarg.h>
00033 #include <stdlib.h>
00034 #include <stdarg.h>
00035 
00036 #include "config.h"
00037 #include "errors.h"
00038 #include "util.h"
00039 
00040 extern volatile int cli_errno;
00041 
00042 /* Count and return the # of elements in an array */
00043 unsigned int cli_count_args(char **args)
00044 {
00045         unsigned int i;
00046 
00047         for (i=0; args[i] != NULL; i++);
00048         return i;
00049 }
00050 
00051 /* (re)allocates memory to store the current working directory, gets
00052  * and updates the current working directory, formats the prompt
00053  * string */
00054 unsigned int cli_set_prompt(cliuser_t *usr)
00055 {
00056         usr->cwd = (char *) realloc(usr->cwd, PATH_MAX);
00057         if (NULL == usr->cwd) {
00058                 cli_error(CL_ENOMEM, "Can not allocate cwd");
00059                 cli_errno = CL_ENOMEM;
00060                 return 1;
00061         }
00062         if (!getcwd(usr->cwd, PATH_MAX)) 
00063                 snprintf(usr->cwd, PATH_MAX, "(unknown)");
00064 
00065         if (usr->prompt)
00066                 free(usr->prompt);
00067         asprintf(&usr->prompt, "%s # ", usr->cwd);
00068 
00069         return 0;
00070 }
00071 
00072 

Generated on Thu Jun 2 07:45:41 2011 for HelenOS/USB by  doxygen 1.4.7