mirror of
https://github.com/samsonjs/csc360-a1-shell.git
synced 2026-03-25 08:45:52 +00:00
[c] Format code
This commit is contained in:
parent
94ca5e6024
commit
5a9cf1a0ab
10 changed files with 519 additions and 582 deletions
57
c/builtins.c
57
c/builtins.c
|
|
@ -20,39 +20,32 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#include "builtins.h"
|
||||
#include "main.h"
|
||||
#include "exec.h"
|
||||
#include "jobs.h"
|
||||
#include "main.h"
|
||||
#include "utils.h"
|
||||
|
||||
int builtin_bg ( int argc, char **argv )
|
||||
{
|
||||
if ( argc < 2 )
|
||||
{
|
||||
int builtin_bg(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
queue_message("bg: usage 'bg <command>'");
|
||||
queue_message(" runs <command> in the background");
|
||||
return -1;
|
||||
}
|
||||
|
||||
pid_t pid = exec_command(&argv[1], 1); /* &argv[1] skips 'bg' */
|
||||
if (pid > 0)
|
||||
{
|
||||
if (pid > 0) {
|
||||
job j = add_job(pid, &argv[1]);
|
||||
|
||||
char *message = (char *)myxmalloc(MSGLEN);
|
||||
snprintf (message, MSGLEN,
|
||||
"Running job " YELLOW "%i" WHITE " (pid %i) in background" CLEAR,
|
||||
j->id, pid);
|
||||
snprintf(message, MSGLEN, "Running job " YELLOW "%i" WHITE " (pid %i) in background" CLEAR, j->id, pid);
|
||||
queue_message(message);
|
||||
free(message);
|
||||
}
|
||||
return pid;
|
||||
}
|
||||
|
||||
int builtin_bgkill ( int argc, char **argv )
|
||||
{
|
||||
if ( argc != 2 )
|
||||
{
|
||||
int builtin_bgkill(int argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
queue_message("bgkill: usage 'bgkill <job number>'");
|
||||
queue_message(" type 'bglist' to see running jobs");
|
||||
return -1;
|
||||
|
|
@ -60,8 +53,7 @@ int builtin_bgkill ( int argc, char **argv )
|
|||
|
||||
int job_id = atoi(argv[1]);
|
||||
job j = job_with_id(job_id);
|
||||
if (!j)
|
||||
{
|
||||
if (!j) {
|
||||
queue_message(YELLOW "Invalid job number");
|
||||
queue_message("(type 'bglist' to see running jobs)");
|
||||
return -1;
|
||||
|
|
@ -73,30 +65,24 @@ int builtin_bgkill ( int argc, char **argv )
|
|||
return 1;
|
||||
}
|
||||
|
||||
int builtin_bglist ( void )
|
||||
{
|
||||
int builtin_bglist(void) {
|
||||
int num_jobs;
|
||||
if (!(num_jobs = get_num_jobs()))
|
||||
return 0;
|
||||
|
||||
job j;
|
||||
char *message = (char *)myxmalloc(MSGLEN);
|
||||
for (j = get_job_list(); j; j = j->next)
|
||||
{
|
||||
snprintf (message, MSGLEN,
|
||||
YELLOW "%i" WHITE ": (pid %i)" YELLOW " %s" CLEAR,
|
||||
j->id, j->pid, j->cmdline);
|
||||
for (j = get_job_list(); j; j = j->next) {
|
||||
snprintf(message, MSGLEN, YELLOW "%i" WHITE ": (pid %i)" YELLOW " %s" CLEAR, j->id, j->pid, j->cmdline);
|
||||
queue_message(message);
|
||||
}
|
||||
snprintf (message, MSGLEN,
|
||||
GREEN "Total: %i background job(s) running" CLEAR, num_jobs);
|
||||
snprintf(message, MSGLEN, GREEN "Total: %i background job(s) running" CLEAR, num_jobs);
|
||||
queue_message(message);
|
||||
free(message);
|
||||
return num_jobs;
|
||||
}
|
||||
|
||||
int builtin_cd ( int argc, char **argv )
|
||||
{
|
||||
int builtin_cd(int argc, char **argv) {
|
||||
static char *lastdir = NULL;
|
||||
char *dir, *pwd = getcwd(NULL, 0);
|
||||
|
||||
|
|
@ -105,20 +91,17 @@ int builtin_cd ( int argc, char **argv )
|
|||
|
||||
if (argc < 2) /* cd w/ no args acts like cd $HOME */
|
||||
dir = getenv("HOME");
|
||||
else
|
||||
{
|
||||
else {
|
||||
if (!strncmp(argv[1], "-", 1))
|
||||
dir = lastdir; /* cd - changes to previous dir */
|
||||
else
|
||||
dir = argv[1];
|
||||
}
|
||||
|
||||
if ( chdir (dir) < 0 )
|
||||
{ /* error */
|
||||
if (chdir(dir) < 0) { /* error */
|
||||
size_t len = strlen(dir);
|
||||
char *message = (char *)myxmalloc(len + MSGLEN);
|
||||
(void) snprintf (message, len + MSGLEN,
|
||||
RED "cd: %s: %s" CLEAR, strerror (errno), dir);
|
||||
(void)snprintf(message, len + MSGLEN, RED "cd: %s: %s" CLEAR, strerror(errno), dir);
|
||||
queue_message(message);
|
||||
free(message);
|
||||
return -1;
|
||||
|
|
@ -130,13 +113,9 @@ int builtin_cd ( int argc, char **argv )
|
|||
return 1;
|
||||
}
|
||||
|
||||
void builtin_clear ( void )
|
||||
{
|
||||
printf ("\033[2J");
|
||||
}
|
||||
void builtin_clear(void) { printf("\033[2J"); }
|
||||
|
||||
void builtin_pwd ( void )
|
||||
{
|
||||
void builtin_pwd(void) {
|
||||
char *pwd = getcwd(NULL, 0);
|
||||
queue_message(pwd);
|
||||
xfree(pwd);
|
||||
|
|
|
|||
33
c/exec.c
33
c/exec.c
|
|
@ -12,21 +12,19 @@
|
|||
/*#define _GNU_SOURCE*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h> /* waitpid */
|
||||
#include <sys/wait.h> /* waitpid */
|
||||
#include <unistd.h>
|
||||
|
||||
#include "exec.h"
|
||||
#include "utils.h"
|
||||
#include "main.h"
|
||||
#include "utils.h"
|
||||
|
||||
char *is_executable ( char *file )
|
||||
{
|
||||
if ( strchr (file, '/') )
|
||||
{ /* process absolute and relative paths directly */
|
||||
char *is_executable(char *file) {
|
||||
if (strchr(file, '/')) { /* process absolute and relative paths directly */
|
||||
if (access(file, X_OK) == 0)
|
||||
return strdup(file);
|
||||
else
|
||||
|
|
@ -37,8 +35,7 @@ char *is_executable ( char *file )
|
|||
int file_len = strlen(file);
|
||||
char *dir = strtok(path, ":");
|
||||
char *filename = NULL;
|
||||
while ( dir )
|
||||
{
|
||||
while (dir) {
|
||||
filename = (char *)myxmalloc(strlen(dir) + file_len + 2);
|
||||
sprintf(filename, "%s/%s", dir, file);
|
||||
if (access(filename, X_OK) == 0)
|
||||
|
|
@ -51,14 +48,12 @@ char *is_executable ( char *file )
|
|||
return filename;
|
||||
}
|
||||
|
||||
pid_t exec_command ( char **argv, int background )
|
||||
{
|
||||
pid_t exec_command(char **argv, int background) {
|
||||
int status;
|
||||
pid_t pid;
|
||||
char *filename;
|
||||
|
||||
if ( !(filename = is_executable (argv[0])) )
|
||||
{ /* error, not executable */
|
||||
if (!(filename = is_executable(argv[0]))) { /* error, not executable */
|
||||
char *msg = (char *)myxmalloc(MSGLEN);
|
||||
sprintf(msg, RED "%s: %s" CLEAR, argv[0], strerror(errno));
|
||||
queue_message(msg);
|
||||
|
|
@ -66,15 +61,12 @@ pid_t exec_command ( char **argv, int background )
|
|||
return -1;
|
||||
}
|
||||
|
||||
if ( (pid = fork()) > 0 )
|
||||
{ /* parent */
|
||||
if ((pid = fork()) > 0) { /* parent */
|
||||
if (background)
|
||||
waitpid(pid, &status, WNOHANG);
|
||||
else
|
||||
waitpid(pid, &status, 0);
|
||||
}
|
||||
else if ( pid == 0 )
|
||||
{ /* child */
|
||||
} else if (pid == 0) { /* child */
|
||||
/* kludge, briefly wait for the job to be created so that if the
|
||||
* command exits quickly, the job is found and removed. otherwise there
|
||||
* are zombie jobs erroneously lying around. note, this isn't guaranteed
|
||||
|
|
@ -84,13 +76,10 @@ pid_t exec_command ( char **argv, int background )
|
|||
execv(filename, argv);
|
||||
|
||||
/* if we get here there was an error, display it */
|
||||
printf (RED "\nCannot execute '%s' (%s)\n" CLEAR, argv[0],
|
||||
strerror(errno));
|
||||
printf(RED "\nCannot execute '%s' (%s)\n" CLEAR, argv[0], strerror(errno));
|
||||
free(filename);
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
else
|
||||
{ /* error, pid < 0 */
|
||||
} else { /* error, pid < 0 */
|
||||
queue_message(RED "Unable to fork(), uh oh..." CLEAR);
|
||||
}
|
||||
free(filename);
|
||||
|
|
|
|||
46
c/jobs.c
46
c/jobs.c
|
|
@ -24,15 +24,13 @@ static job job_list_head = NULL;
|
|||
static int num_jobs = 0;
|
||||
static int next_id = 1;
|
||||
|
||||
static int get_next_id ( void )
|
||||
{
|
||||
static int get_next_id(void) {
|
||||
while (job_with_id(next_id))
|
||||
next_id++;
|
||||
return next_id++;
|
||||
}
|
||||
|
||||
job add_job ( pid_t pid, char **argv )
|
||||
{
|
||||
job add_job(pid_t pid, char **argv) {
|
||||
job i, j = (job)myxmalloc(sizeof(struct job));
|
||||
j->id = get_next_id();
|
||||
j->pid = pid;
|
||||
|
|
@ -42,10 +40,8 @@ job add_job ( pid_t pid, char **argv )
|
|||
j->next = NULL;
|
||||
j->prev = NULL;
|
||||
|
||||
for (i = job_list_head; i && i->next; i = i->next)
|
||||
{ /* insert jobs in job_id order */
|
||||
if ( i->id > j->id )
|
||||
{ /* insert BEFORE i */
|
||||
for (i = job_list_head; i && i->next; i = i->next) { /* insert jobs in job_id order */
|
||||
if (i->id > j->id) { /* insert BEFORE i */
|
||||
if (DEBUG)
|
||||
printf("DEBUG: i=%i, i->next=%i, i->prev=%p\n", i->id, i->next->id, i->prev);
|
||||
j->next = i;
|
||||
|
|
@ -65,8 +61,7 @@ job add_job ( pid_t pid, char **argv )
|
|||
if (DEBUG)
|
||||
printf("DEBUG: i=%p, job_list_head=%p\n", i, job_list_head);
|
||||
job_list_head = j;
|
||||
}
|
||||
else if ( !i->next ) /* at the end, i->next == NULL */
|
||||
} else if (!i->next) /* at the end, i->next == NULL */
|
||||
{ /* at this point, append the new job to the end of the list */
|
||||
if (DEBUG)
|
||||
printf("DEBUG: i=%i\n", i->id);
|
||||
|
|
@ -79,12 +74,10 @@ job add_job ( pid_t pid, char **argv )
|
|||
return j;
|
||||
}
|
||||
|
||||
void delete_job ( job j )
|
||||
{
|
||||
void delete_job(job j) {
|
||||
next_id = MIN(next_id, j->id); /* prefer the lower id */
|
||||
|
||||
if ( j == job_list_head )
|
||||
{
|
||||
if (j == job_list_head) {
|
||||
if (j->next)
|
||||
job_list_head = j->next;
|
||||
else
|
||||
|
|
@ -102,17 +95,14 @@ void delete_job ( job j )
|
|||
num_jobs--;
|
||||
}
|
||||
|
||||
void free_job_list ( void )
|
||||
{
|
||||
void free_job_list(void) {
|
||||
while (job_list_head)
|
||||
delete_job(job_list_head);
|
||||
}
|
||||
|
||||
job job_with_id ( int job_id )
|
||||
{
|
||||
job job_with_id(int job_id) {
|
||||
job j;
|
||||
for (j = job_list_head; j; j = j->next)
|
||||
{
|
||||
for (j = job_list_head; j; j = j->next) {
|
||||
/* printf("DEBUG: id=%i, j=%p:%i:%i\n", job_id, j, j->id, j->pid); */
|
||||
if (j->id == job_id)
|
||||
return j;
|
||||
|
|
@ -120,11 +110,9 @@ job job_with_id ( int job_id )
|
|||
return NULL;
|
||||
}
|
||||
|
||||
job job_with_pid ( pid_t pid )
|
||||
{
|
||||
job job_with_pid(pid_t pid) {
|
||||
job j;
|
||||
for (j = job_list_head; j; j = j->next)
|
||||
{
|
||||
for (j = job_list_head; j; j = j->next) {
|
||||
printf("DEBUG: pid=%i, j=%p:%i:%i\n", pid, j, j->id, j->pid);
|
||||
if (j->pid == pid)
|
||||
return j;
|
||||
|
|
@ -132,12 +120,6 @@ job job_with_pid ( pid_t pid )
|
|||
return NULL;
|
||||
}
|
||||
|
||||
job get_job_list ( void )
|
||||
{
|
||||
return job_list_head;
|
||||
}
|
||||
job get_job_list(void) { return job_list_head; }
|
||||
|
||||
int get_num_jobs ( void )
|
||||
{
|
||||
return num_jobs;
|
||||
}
|
||||
int get_num_jobs(void) { return num_jobs; }
|
||||
|
|
|
|||
125
c/main.c
125
c/main.c
|
|
@ -11,26 +11,32 @@
|
|||
|
||||
/*#define _GNU_SOURCE*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <signal.h>
|
||||
/* This must be included before readline or FILE won't be defined. */
|
||||
#include <stdio.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <readline/history.h>
|
||||
#include <readline/readline.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
#include <unistd.h>
|
||||
#include <wordexp.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "jobs.h"
|
||||
#include "exec.h"
|
||||
#include "builtins.h"
|
||||
#include "exec.h"
|
||||
#include "jobs.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define MAX(a, b) (a) > (b) ? (a) : (b)
|
||||
#define PROMPT BLUE "%s" WHITE "%% " CLEAR /* looks like this: /path/to/somewhere% */
|
||||
#define INVALID_CHARS "&|;<>" /* for wordexp */
|
||||
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||
|
||||
/* looks like this: /path/to/somewhere% */
|
||||
#define PROMPT BLUE "%s" WHITE "%% " CLEAR
|
||||
|
||||
/* for wordexp */
|
||||
#define INVALID_CHARS "&|;<>"
|
||||
|
||||
struct message {
|
||||
char *data;
|
||||
|
|
@ -40,11 +46,11 @@ typedef struct message *message;
|
|||
|
||||
static message msg_queue_head = NULL; /* message queue */
|
||||
|
||||
void queue_message ( char *msg )
|
||||
{ /* queue messages so they don't mix with a running program's output
|
||||
/** queue messages so they don't mix with a running program's output
|
||||
* instead they're only displayed while waiting on input w/ readline
|
||||
* message m: freed in print_messages()
|
||||
*/
|
||||
void queue_message(char *msg) {
|
||||
message i, m = (message)myxmalloc(sizeof(struct message));
|
||||
m->data = strdup(msg);
|
||||
m->next = NULL;
|
||||
|
|
@ -57,11 +63,9 @@ void queue_message ( char *msg )
|
|||
i->next = m;
|
||||
}
|
||||
|
||||
void free_message_queue ( void )
|
||||
{
|
||||
void free_message_queue(void) {
|
||||
message m, n = msg_queue_head;
|
||||
while ( (m = n) )
|
||||
{
|
||||
while ((m = n)) {
|
||||
n = m->next;
|
||||
xfree(m->data);
|
||||
free(m);
|
||||
|
|
@ -69,8 +73,7 @@ void free_message_queue ( void )
|
|||
msg_queue_head = NULL;
|
||||
}
|
||||
|
||||
int print_messages ( void )
|
||||
{
|
||||
int print_messages(void) {
|
||||
if (!msg_queue_head)
|
||||
return 0;
|
||||
|
||||
|
|
@ -92,20 +95,17 @@ int print_messages ( void )
|
|||
return 1;
|
||||
}
|
||||
|
||||
char *strsignal ( int status )
|
||||
{ /* like strerror for waitpid */
|
||||
/* like strerror for waitpid */
|
||||
char *strsignal(int status) {
|
||||
char *str = (char *)myxmalloc(MSGLEN);
|
||||
if ( WIFEXITED(status) )
|
||||
{
|
||||
if (WIFEXITED(status)) {
|
||||
if (WEXITSTATUS(status)) /* non-zero exit status */
|
||||
sprintf(str, RED "exit %i", WEXITSTATUS(status));
|
||||
else /* clean exit */
|
||||
sprintf(str, GREEN "done");
|
||||
}
|
||||
if ( WIFSIGNALED(status) )
|
||||
{
|
||||
switch ( WTERMSIG(status) )
|
||||
{
|
||||
if (WIFSIGNALED(status)) {
|
||||
switch (WTERMSIG(status)) {
|
||||
case SIGTERM:
|
||||
sprintf(str, RED "terminated");
|
||||
break;
|
||||
|
|
@ -134,8 +134,8 @@ char *strsignal ( int status )
|
|||
return str;
|
||||
}
|
||||
|
||||
void child_state_changed ( int signum )
|
||||
{ /* handler for SIGCHLD when a child's state changes */
|
||||
/* handler for SIGCHLD when a child's state changes */
|
||||
void child_state_changed(int signum) {
|
||||
int status;
|
||||
pid_t pid;
|
||||
|
||||
|
|
@ -144,15 +144,12 @@ void child_state_changed ( int signum )
|
|||
|
||||
pid = waitpid(-1, &status, WNOHANG);
|
||||
job j = job_with_pid(pid);
|
||||
if (j)
|
||||
{
|
||||
if (j) {
|
||||
char *strstatus = strsignal(status);
|
||||
/* alert the user of the termination, and delete the job */
|
||||
size_t len = strlen(j->cmdline);
|
||||
char *msg = (char *)myxmalloc(len + MSGLEN);
|
||||
snprintf (msg, len + MSGLEN,
|
||||
YELLOW "%i" WHITE ": (pid %i) %s" YELLOW ": %s" CLEAR,
|
||||
j->id, j->pid, strstatus, j->cmdline);
|
||||
snprintf(msg, len + MSGLEN, YELLOW "%i" WHITE ": (pid %i) %s" YELLOW ": %s" CLEAR, j->id, j->pid, strstatus, j->cmdline);
|
||||
queue_message(msg);
|
||||
xfree(msg);
|
||||
xfree(strstatus);
|
||||
|
|
@ -160,8 +157,8 @@ void child_state_changed ( int signum )
|
|||
}
|
||||
}
|
||||
|
||||
char *get_prompt ( void )
|
||||
{ /* display the pwd in the prompt */
|
||||
/* display the pwd in the prompt */
|
||||
char *get_prompt(void) {
|
||||
char *pwd, *prompt;
|
||||
|
||||
pwd = getcwd(NULL, 0);
|
||||
|
|
@ -172,24 +169,19 @@ char *get_prompt ( void )
|
|||
return prompt;
|
||||
}
|
||||
|
||||
int cmd_matches ( char *trigger, char *cmd )
|
||||
{
|
||||
return !strcmp (trigger, cmd);
|
||||
}
|
||||
int cmd_matches(char *trigger, char *cmd) { return !strcmp(trigger, cmd); }
|
||||
|
||||
int handle_wordexp_result ( int result, char *cmd )
|
||||
{
|
||||
switch (result)
|
||||
{
|
||||
int handle_wordexp_result(int result, char *cmd) {
|
||||
switch (result) {
|
||||
case WRDE_BADCHAR:
|
||||
; /* gcc chokes if the int decl is first, lame */
|
||||
/* gcc chokes if the int decl is first, lame */
|
||||
;
|
||||
int invalid_char = strcspn(cmd, INVALID_CHARS);
|
||||
char *msg = (char *)myxmalloc(strlen(cmd) + MSGLEN);
|
||||
int i;
|
||||
for (i = 0; i < invalid_char; i++)
|
||||
*(msg + i) = ' ';
|
||||
sprintf (msg + invalid_char,
|
||||
RED "^ invalid character in column %i", invalid_char + 1);
|
||||
sprintf(msg + invalid_char, RED "^ invalid character in column %i", invalid_char + 1);
|
||||
queue_message(cmd);
|
||||
queue_message(msg);
|
||||
xfree(msg);
|
||||
|
|
@ -218,19 +210,8 @@ int handle_wordexp_result ( int result, char *cmd )
|
|||
return result;
|
||||
}
|
||||
|
||||
int main ( void )
|
||||
{
|
||||
signal (SIGCHLD, child_state_changed); /* catch SIGCHLD */
|
||||
|
||||
/* while waiting for input, display messasges */
|
||||
rl_event_hook = print_messages;
|
||||
|
||||
/* register clean-up function */
|
||||
atexit (free_job_list);
|
||||
atexit (free_message_queue);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
void repl_start(void) {
|
||||
for (;;) {
|
||||
char *prompt = get_prompt();
|
||||
char *cmd = readline(prompt);
|
||||
xfree(prompt);
|
||||
|
|
@ -240,10 +221,8 @@ int main ( void )
|
|||
|
||||
wordexp_t words;
|
||||
int result = wordexp(cmd, &words, WRDE_SHOWERR);
|
||||
if ( handle_wordexp_result (result, cmd) && words.we_wordc > 0 )
|
||||
{
|
||||
if (DEBUG)
|
||||
{
|
||||
if (handle_wordexp_result(result, cmd) && words.we_wordc > 0) {
|
||||
if (DEBUG) {
|
||||
int i;
|
||||
printf("DEBUG: args = { ");
|
||||
for (i = 0; i < words.we_wordc; i++)
|
||||
|
|
@ -263,13 +242,11 @@ int main ( void )
|
|||
builtin_clear();
|
||||
else if (cmd_matches("pwd", words.we_wordv[0]))
|
||||
builtin_pwd();
|
||||
else if ( cmd_matches ("exit", words.we_wordv[0]) )
|
||||
{ /* quick clean-up, then break */
|
||||
else if (cmd_matches("exit", words.we_wordv[0])) { /* quick clean-up, then break */
|
||||
wordfree(&words);
|
||||
free(cmd);
|
||||
break;
|
||||
}
|
||||
else /* default to trying to execute the cmd line */
|
||||
} else /* default to trying to execute the cmd line */
|
||||
exec_command(words.we_wordv, 0);
|
||||
|
||||
add_history(cmd); /* add to the readline history */
|
||||
|
|
@ -277,5 +254,19 @@ int main ( void )
|
|||
} /* if handle_wordexp_result (result) */
|
||||
free(cmd);
|
||||
} /* for (;;) */
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
signal(SIGCHLD, child_state_changed); /* catch SIGCHLD */
|
||||
|
||||
/* while waiting for input, display messasges */
|
||||
rl_event_hook = print_messages;
|
||||
|
||||
/* register clean-up function */
|
||||
atexit(free_job_list);
|
||||
atexit(free_message_queue);
|
||||
|
||||
repl_start();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
20
c/utils.c
20
c/utils.c
|
|
@ -9,17 +9,15 @@
|
|||
* $Id: utils.c 184 2006-01-29 08:53:30Z sjs $
|
||||
*/
|
||||
|
||||
#include "utils.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "utils.h"
|
||||
|
||||
char *array_cat ( char **array )
|
||||
{
|
||||
char *array_cat(char **array) {
|
||||
char *p = NULL, *str = NULL;
|
||||
int i, pos = 0;
|
||||
for (i = 0; array[i]; i++)
|
||||
{
|
||||
for (i = 0; array[i]; i++) {
|
||||
if (DEBUG)
|
||||
printf("DEBUG: array[%i]=%p:'%s'\n", i, array[i], array[i]);
|
||||
int len = strlen(array[i]);
|
||||
|
|
@ -36,8 +34,7 @@ char *array_cat ( char **array )
|
|||
return str;
|
||||
}
|
||||
|
||||
void free_array ( void **array )
|
||||
{
|
||||
void free_array(void **array) {
|
||||
int i = 0;
|
||||
|
||||
if (!array)
|
||||
|
|
@ -48,8 +45,7 @@ void free_array ( void **array )
|
|||
free(array);
|
||||
}
|
||||
|
||||
void *myxmalloc ( size_t size )
|
||||
{
|
||||
void *myxmalloc(size_t size) {
|
||||
void *ptr = malloc(size);
|
||||
if (ptr)
|
||||
return ptr;
|
||||
|
|
@ -58,8 +54,7 @@ void *myxmalloc ( size_t size )
|
|||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void *myxrealloc( void *ptr, size_t size )
|
||||
{
|
||||
void *myxrealloc(void *ptr, size_t size) {
|
||||
void *new_ptr = realloc(ptr, size);
|
||||
if (new_ptr)
|
||||
return new_ptr;
|
||||
|
|
@ -68,8 +63,7 @@ void *myxrealloc( void *ptr, size_t size )
|
|||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void **array_realloc ( void **array, size_t size )
|
||||
{
|
||||
void **array_realloc(void **array, size_t size) {
|
||||
void **ptr = realloc(array, size * sizeof(void *));
|
||||
if (ptr)
|
||||
return ptr;
|
||||
|
|
|
|||
|
|
@ -32,7 +32,9 @@ char *array_cat ( char **array );
|
|||
void *myxmalloc(size_t size);
|
||||
void *myxrealloc(void *ptr, size_t size);
|
||||
|
||||
#define xfree(ptr) if (ptr) free (ptr);
|
||||
#define xfree(ptr) \
|
||||
if (ptr) \
|
||||
free(ptr);
|
||||
|
||||
/* this takes n_elems of the original array, in case of failure it will
|
||||
* free_array (n_elems, array) before exiting */
|
||||
|
|
|
|||
Loading…
Reference in a new issue