2018-06-18 03:13:02 +08:00
|
|
|
/* fileman.c -- A tiny application which demonstrates how to use the
|
|
|
|
GNU Readline library. This application interactively allows users
|
|
|
|
to manipulate files and their modes.
|
|
|
|
|
|
|
|
NOTE: this was taken from the GNU Readline documentation and ported
|
|
|
|
to libedit. A command to output the history list was added.
|
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
*/
|
2018-06-18 03:13:02 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "editline.h"
|
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
void *xmalloc(size_t size);
|
|
|
|
void too_dangerous(char *caller);
|
|
|
|
void initialize_readline();
|
|
|
|
int execute_line(char *line);
|
|
|
|
int valid_argument(char *caller, char *arg);
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
typedef int rl_icpfunc_t(char *);
|
2018-06-18 03:13:02 +08:00
|
|
|
|
|
|
|
/* The names of functions that actually do the manipulation. */
|
2018-12-26 02:37:42 +08:00
|
|
|
int com_list(char *);
|
|
|
|
int com_view(char *);
|
|
|
|
int com_history(char *);
|
2018-06-18 03:13:02 +08:00
|
|
|
int com_rename(char *);
|
|
|
|
int com_stat(char *);
|
|
|
|
int com_pwd(char *);
|
|
|
|
int com_delete(char *);
|
|
|
|
int com_help(char *);
|
|
|
|
int com_cd(char *);
|
|
|
|
int com_quit(char *);
|
|
|
|
|
|
|
|
/* A structure which contains information on the commands this program
|
|
|
|
can understand. */
|
|
|
|
|
|
|
|
typedef struct {
|
2018-12-26 02:37:42 +08:00
|
|
|
char *name; /* User printable name of the function. */
|
|
|
|
rl_icpfunc_t *func; /* Function to call to do the job. */
|
|
|
|
char *doc; /* Documentation for this function. */
|
2018-06-18 03:13:02 +08:00
|
|
|
} COMMAND;
|
|
|
|
|
|
|
|
COMMAND commands[] = {
|
2018-12-26 02:37:42 +08:00
|
|
|
{"cd", com_cd, "Change to directory DIR"},
|
|
|
|
{"delete", com_delete, "Delete FILE"},
|
|
|
|
{"help", com_help, "Display this text"},
|
|
|
|
{"?", com_help, "Synonym for `help'"},
|
|
|
|
{"list", com_list, "List files in DIR"},
|
|
|
|
{"ls", com_list, "Synonym for `list'"},
|
|
|
|
{"pwd", com_pwd, "Print the current working directory"},
|
|
|
|
{"quit", com_quit, "Quit using Fileman"},
|
|
|
|
{"rename", com_rename, "Rename FILE to NEWNAME"},
|
|
|
|
{"stat", com_stat, "Print out statistics on FILE"},
|
|
|
|
{"view", com_view, "View the contents of FILE"},
|
|
|
|
{"history", com_history, "List editline history"},
|
|
|
|
{(char *)NULL, (rl_icpfunc_t *) NULL, (char *)NULL}
|
2018-06-18 03:13:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Forward declarations. */
|
2018-12-26 02:37:42 +08:00
|
|
|
char *stripwhite();
|
|
|
|
COMMAND *find_command();
|
2018-06-18 03:13:02 +08:00
|
|
|
|
|
|
|
/* The name of this program, as taken from argv[0]. */
|
|
|
|
char *progname;
|
|
|
|
|
|
|
|
/* When non-zero, this means the user is done using this program. */
|
|
|
|
int done;
|
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
char *dupstr(char *s)
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
char *r;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
r = xmalloc(strlen(s) + 1);
|
|
|
|
strcpy(r, s);
|
|
|
|
return (r);
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
int main(int argc __attribute__ ((__unused__)), char **argv)
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
char *line, *s;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
progname = argv[0];
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
setlocale(LC_CTYPE, "");
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
initialize_readline(); /* Bind our completer. */
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
/* Loop reading and executing lines until the user quits. */
|
|
|
|
for (; done == 0;) {
|
|
|
|
line = readline("FileMan: ");
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
if (!line)
|
|
|
|
break;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
/* Remove leading and trailing whitespace from the line.
|
|
|
|
Then, if there is anything left, add it to the history list
|
|
|
|
and execute it. */
|
|
|
|
s = stripwhite(line);
|
2018-06-18 03:13:02 +08:00
|
|
|
#if 0
|
2018-12-26 02:37:42 +08:00
|
|
|
if (*s) {
|
|
|
|
|
|
|
|
char *expansion;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = history_expand(s, &expansion);
|
|
|
|
if (result < 0 || result == 2) {
|
|
|
|
fprintf(stderr, "%s\n", expansion);
|
|
|
|
} else {
|
|
|
|
add_history(expansion);
|
|
|
|
execute_line(expansion);
|
|
|
|
}
|
|
|
|
free(expansion);
|
|
|
|
}
|
2018-06-18 03:13:02 +08:00
|
|
|
#else
|
2018-12-26 02:37:42 +08:00
|
|
|
execute_line(s);
|
2018-06-18 03:13:02 +08:00
|
|
|
#endif
|
2018-12-26 02:37:42 +08:00
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
exit(0);
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
return 0;
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute a command line. */
|
2018-12-26 02:37:42 +08:00
|
|
|
int execute_line(char *line)
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
register int i;
|
|
|
|
COMMAND *command;
|
|
|
|
char *word;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
/* Isolate the command word. */
|
|
|
|
i = 0;
|
|
|
|
while (line[i] && isspace(line[i]))
|
|
|
|
i++;
|
|
|
|
word = line + i;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
while (line[i] && !isspace(line[i]))
|
|
|
|
i++;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
if (line[i])
|
|
|
|
line[i++] = '\0';
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
command = find_command(word);
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
if (!command) {
|
|
|
|
fprintf(stderr, "%s: No such command for FileMan.\n", word);
|
|
|
|
return (-1);
|
|
|
|
}
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
/* Get argument to command, if any. */
|
|
|
|
while (isspace(line[i]))
|
|
|
|
i++;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
word = line + i;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
/* Call the function. */
|
|
|
|
return ((*(command->func)) (word));
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Look up NAME as the name of a command, and return a pointer to that
|
|
|
|
command. Return a NULL pointer if NAME isn't a command name. */
|
2018-12-26 02:37:42 +08:00
|
|
|
COMMAND *find_command(char *name)
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
register int i;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
for (i = 0; commands[i].name; i++)
|
|
|
|
if (strcmp(name, commands[i].name) == 0)
|
|
|
|
return (&commands[i]);
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
return ((COMMAND *) NULL);
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Strip whitespace from the start and end of STRING. Return a pointer
|
|
|
|
into STRING. */
|
2018-12-26 02:37:42 +08:00
|
|
|
char *stripwhite(char *string)
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
register char *s, *t;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
for (s = string; isspace(*s); s++) ;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
if (*s == 0)
|
|
|
|
return (s);
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
t = s + strlen(s) - 1;
|
|
|
|
while (t > s && isspace(*t))
|
|
|
|
t--;
|
|
|
|
*++t = '\0';
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
return s;
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* **************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* Interface to Readline Completion */
|
|
|
|
/* */
|
|
|
|
/* **************************************************************** */
|
|
|
|
|
|
|
|
char *command_generator(const char *, int);
|
|
|
|
char **fileman_completion(const char *, int, int);
|
|
|
|
|
|
|
|
/* Tell the GNU Readline library how to complete. We want to try to
|
|
|
|
complete on command names if this is the first word in the line, or
|
|
|
|
on filenames if not. */
|
2018-12-26 02:37:42 +08:00
|
|
|
void initialize_readline()
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
/* Allow conditional parsing of the ~/.inputrc file. */
|
|
|
|
rl_readline_name = "FileMan";
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
/* Tell the completer that we want a crack first. */
|
|
|
|
rl_attempted_completion_function = fileman_completion;
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Attempt to complete on the contents of TEXT. START and END
|
|
|
|
bound the region of rl_line_buffer that contains the word to
|
|
|
|
complete. TEXT is the word to complete. We can use the entire
|
|
|
|
contents of rl_line_buffer in case we want to do some simple
|
|
|
|
parsing. Returnthe array of matches, or NULL if there aren't any. */
|
2018-12-26 02:37:42 +08:00
|
|
|
char **fileman_completion(const char *text, int start, int end
|
|
|
|
__attribute__ ((__unused__)))
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
char **matches;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
matches = (char **)NULL;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
/* If this word is at the start of the line, then it is a command
|
|
|
|
to complete. Otherwise it is the name of a file in the current
|
|
|
|
directory. */
|
|
|
|
if (start == 0)
|
|
|
|
matches = rl_completion_matches(text, command_generator);
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
return (matches);
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Generator function for command completion. STATE lets us
|
|
|
|
know whether to start from scratch; without any state
|
|
|
|
(i.e. STATE == 0), then we start at the top of the list. */
|
2018-12-26 02:37:42 +08:00
|
|
|
char *command_generator(text, state)
|
|
|
|
const char *text;
|
|
|
|
int state;
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
static int list_index, len;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
/* If this is a new word to complete, initialize now. This
|
|
|
|
includes saving the length of TEXT for efficiency, and
|
|
|
|
initializing the index variable to 0. */
|
|
|
|
if (!state) {
|
|
|
|
list_index = 0;
|
|
|
|
len = strlen(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the next name which partially matches from the
|
|
|
|
command list. */
|
|
|
|
while ((name = commands[list_index].name)) {
|
|
|
|
list_index++;
|
|
|
|
|
|
|
|
if (strncmp(name, text, len) == 0)
|
|
|
|
return (dupstr(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no names matched, then return NULL. */
|
|
|
|
return ((char *)NULL);
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* **************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* FileMan Commands */
|
|
|
|
/* */
|
|
|
|
/* **************************************************************** */
|
|
|
|
|
|
|
|
/* String to pass to system (). This is for the LIST, VIEW and RENAME
|
|
|
|
commands. */
|
|
|
|
static char syscom[1024];
|
|
|
|
|
|
|
|
/* List the file(s) named in arg. */
|
2018-12-26 02:37:42 +08:00
|
|
|
int com_list(char *arg)
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
if (!arg)
|
|
|
|
arg = "";
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
sprintf(syscom, "ls -FClg %s", arg);
|
|
|
|
return (system(syscom));
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
int com_view(char *arg)
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
if (!valid_argument("view", arg))
|
|
|
|
return 1;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
sprintf(syscom, "more %s", arg);
|
|
|
|
return (system(syscom));
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
int com_history(char *arg __attribute__ ((__unused__)))
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
const char *he;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
/* rewind history */
|
|
|
|
while (el_prev_hist()) ;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
for (he = el_next_hist(); he != NULL; he = el_next_hist())
|
|
|
|
printf("%s\n", he);
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
return 0;
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
int com_rename(char *arg __attribute__ ((__unused__)))
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
too_dangerous("rename");
|
|
|
|
return (1);
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
int com_stat(char *arg)
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
struct stat finfo;
|
|
|
|
|
|
|
|
if (!valid_argument("stat", arg))
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
if (stat(arg, &finfo) == -1) {
|
|
|
|
perror(arg);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Statistics for `%s':\n", arg);
|
|
|
|
|
|
|
|
printf("%s has %ld link%s, and is %lld byte%s in length.\n", arg,
|
|
|
|
(long)finfo.st_nlink,
|
|
|
|
(finfo.st_nlink == 1) ? "" : "s",
|
|
|
|
(long long)finfo.st_size, (finfo.st_size == 1) ? "" : "s");
|
|
|
|
printf("Inode Last Change at: %s", ctime(&finfo.st_ctime));
|
|
|
|
printf(" Last access at: %s", ctime(&finfo.st_atime));
|
|
|
|
printf(" Last modified at: %s", ctime(&finfo.st_mtime));
|
|
|
|
return (0);
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
int com_delete(char *arg __attribute__ ((__unused__)))
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
too_dangerous("delete");
|
|
|
|
return (1);
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Print out help for ARG, or for all of the commands if ARG is
|
|
|
|
not present. */
|
2018-12-26 02:37:42 +08:00
|
|
|
int com_help(char *arg)
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
register int i;
|
|
|
|
int printed = 0;
|
|
|
|
|
|
|
|
for (i = 0; commands[i].name; i++) {
|
|
|
|
if (!*arg || (strcmp(arg, commands[i].name) == 0)) {
|
|
|
|
printf("%s\t\t%s.\n", commands[i].name,
|
|
|
|
commands[i].doc);
|
|
|
|
printed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!printed) {
|
|
|
|
printf("No commands match `%s'. Possibilties are:\n", arg);
|
|
|
|
|
|
|
|
for (i = 0; commands[i].name; i++) {
|
|
|
|
/* Print in six columns. */
|
|
|
|
if (printed == 6) {
|
|
|
|
printed = 0;
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s\t", commands[i].name);
|
|
|
|
printed++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (printed)
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
return (0);
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Change to the directory ARG. */
|
2018-12-26 02:37:42 +08:00
|
|
|
int com_cd(char *arg)
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
if (chdir(arg) == -1) {
|
|
|
|
perror(arg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
com_pwd("");
|
|
|
|
return (0);
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Print out the current working directory. */
|
2018-12-26 02:37:42 +08:00
|
|
|
int com_pwd(char *ignore __attribute__ ((__unused__)))
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
char dir[1024], *s;
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
s = (char *)getcwd(dir, sizeof(dir) - 1);
|
|
|
|
if (s == 0) {
|
|
|
|
printf("Error getting pwd: %s\n", dir);
|
|
|
|
return 1;
|
|
|
|
}
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
printf("Current directory is %s\n", dir);
|
|
|
|
return 0;
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The user wishes to quit using this program. Just set DONE
|
|
|
|
non-zero. */
|
2018-12-26 02:37:42 +08:00
|
|
|
int com_quit(char *arg __attribute__ ((__unused__)))
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
done = 1;
|
|
|
|
return (0);
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Function which tells you that you can't do this. */
|
2018-12-26 02:37:42 +08:00
|
|
|
void too_dangerous(char *caller)
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
fprintf(stderr, "%s: Too dangerous for me to distribute.\n", caller);
|
|
|
|
fprintf(stderr, "Write it yourself.\n");
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return non-zero if ARG is a valid argument for CALLER,
|
|
|
|
else print an error message and return zero. */
|
2018-12-26 02:37:42 +08:00
|
|
|
int valid_argument(char *caller, char *arg)
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
if (!arg || !*arg) {
|
|
|
|
fprintf(stderr, "%s: Argument required.\n", caller);
|
|
|
|
return (0);
|
|
|
|
}
|
2018-06-18 03:13:02 +08:00
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
return (1);
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
void *xmalloc(size_t size)
|
2018-06-18 03:13:02 +08:00
|
|
|
{
|
2018-12-26 02:37:42 +08:00
|
|
|
register void *value = (void *)malloc(size);
|
|
|
|
if (value == 0)
|
|
|
|
fprintf(stderr, "virtual memory exhausted");
|
|
|
|
return value;
|
2018-06-18 03:13:02 +08:00
|
|
|
}
|
|
|
|
|
2018-12-26 02:37:42 +08:00
|
|
|
/**
|
|
|
|
* Local Variables:
|
|
|
|
* c-file-style: "k&r"
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
*/
|