From cce891bb05118330ca609ac989491ad8a2fa7f71 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Sat, 5 Nov 2005 16:43:21 +0000 Subject: [PATCH 1/1] 2005-11-05 Carl Worth * src/Makefile.am: Add ttt-board.[ch] and x.[ch] * src/ttt-board.h: * src/ttt-board.c: (ttt_board_init), (ttt_board_init_from_string), (ttt_board_to_string), (ttt_board_write): Add some stub functions for Richard and Kevin to have some practice implementing. Functions to represent a board and to go to/from a string. * src/x.h: * src/x.c: (xasprintf), (xvasprintf), (xpipe), (xfork), (xmalloc), (xcalloc), (xrealloc), (xfdopen), (xstrdup), (xfwrite): Several utility functions that make system calls, check the result, and exit on any error. This will simplify the error checking needed in programs using these functions. --- ChangeLog | 18 ++++++ src/Makefile.am | 4 +- src/ttt-board.c | 72 +++++++++++++++++++++ src/ttt-board.h | 30 +++++++++ src/x.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++++ src/x.h | 52 +++++++++++++++ 6 files changed, 338 insertions(+), 1 deletion(-) create mode 100644 src/ttt-board.c create mode 100644 src/ttt-board.h create mode 100644 src/x.c create mode 100644 src/x.h diff --git a/ChangeLog b/ChangeLog index 63169bb..eedf81b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2005-11-05 Carl Worth + + * src/Makefile.am: Add ttt-board.[ch] and x.[ch] + + * src/ttt-board.h: + * src/ttt-board.c: (ttt_board_init), (ttt_board_init_from_string), + (ttt_board_to_string), (ttt_board_write): Add some stub functions + for Richard and Kevin to have some practice + implementing. Functions to represent a board and to go to/from a + string. + + * src/x.h: + * src/x.c: (xasprintf), (xvasprintf), (xpipe), (xfork), (xmalloc), + (xcalloc), (xrealloc), (xfdopen), (xstrdup), (xfwrite): Several + utility functions that make system calls, check the result, and + exit on any error. This will simplify the error checking needed in + programs using these functions. + 2005-11-05 Carl Worth * src/Makefile.am: Break things up for separate ttt-client and diff --git a/src/Makefile.am b/src/Makefile.am index 9b52b44..2e7ae73 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,7 +8,9 @@ ttt_common_sources = \ ttt-command.c \ ttt-command.h \ ttt-error.c \ - ttt-error.h + ttt-error.h \ + x.c \ + x.h ttt_client_SOURCES = \ $(ttt_common_sources) \ diff --git a/src/ttt-board.c b/src/ttt-board.c new file mode 100644 index 0000000..9e9db98 --- /dev/null +++ b/src/ttt-board.c @@ -0,0 +1,72 @@ +/* ttt.c - client-server tic-tac-toe game + * + * Copyright © 2005 Carl Worth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Author: Carl Worth + */ + +#include "ttt-board.h" + +/* Initialize an empty board. */ +void +ttt_board_init (ttt_board_t *board) +{ + /* XXX: NYI */ +} + +/* Initialize a board from its string representation. + * + * Returns: TTT_BOARD_SUCCESS or TTT_BOARD_INVALID_BOARD if the board + * string could not be properly parsed. + */ +void +ttt_board_init_from_string (ttt_board_t *board, + const char *s) +{ + /* XXX: NYI */ +} + +/* Return the string representation of a board. + * + * The return value is a malloc()ed string that should be free()ed + * when no longer needed. + * + * Errors: If out-of-memory occurs, this function will not return. + */ +char * +ttt_board_to_string (ttt_board_t *board) +{ + /* XXX: NYI */ +} + +/* Write a string representation of a board to the provided file. + * + * Errors: If out-of-memory or a file IO error occurs, this function + * will not return. + */ +void +ttt_board_write (ttt_board_t *board, FILE *file) +{ + char *s; + + s = ttt_board_to_string (board); + + xfwrite (s, 1, strlen (s) + 1, file); + + free (s); +} + diff --git a/src/ttt-board.h b/src/ttt-board.h new file mode 100644 index 0000000..dd2b6eb --- /dev/null +++ b/src/ttt-board.h @@ -0,0 +1,30 @@ +/* ttt.c - client-server tic-tac-toe game + * + * Copyright © 2005 Carl Worth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Author: Carl Worth + */ + +#ifndef _TTT_BOARD_H_ +#define _TTT_BOARD_H_ + +typedef struct _ttt_board { + /* XXX: Fill this out with appropriate fields. */ +} ttt_board_t; + +#endif + diff --git a/src/x.c b/src/x.c new file mode 100644 index 0000000..4062294 --- /dev/null +++ b/src/x.c @@ -0,0 +1,163 @@ +/* x.c - Some wrappers for various functions to check and exit on out-of-memory + * + * Copyright © 2005 Carl Worth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Author: Carl Worth + */ + +#include "ttt.h" + +#include + +void +xasprintf (char **strp, const char *fmt, ...) +{ + va_list ap; + + va_start (ap, fmt); + xvasprintf (strp, fmt, ap); + va_end (ap); +} + +void +xvasprintf (char **strp, const char *fmt, va_list ap) +{ + int ret; + + ret = vasprintf (strp, fmt, ap); + + if (ret == -1) { + fprintf (stderr, "Error: vasprintf failed: Out of memory? Aborting.\n"); + exit (1); + } +} + +void +xpipe (int filedes[2]) +{ + int ret; + + ret = pipe (filedes); + + if (ret == -1) { + fprintf (stderr, "Error: pipe failed: %s. Aborting.\n", strerror (errno)); + exit (1); + } +} + +pid_t +xfork (void) +{ + pid_t pid; + + pid = fork (); + + if (pid == -1) { + fprintf (stderr, "Error: fork failed: %s. Aborting.\n", strerror (errno)); + exit (1); + } + + return pid; +} + +void * +xmalloc (size_t size) +{ + void *ret; + + ret = malloc (size); + + if (ret == NULL) { + fprintf (stderr, "Error: malloc failed. Out of memory. Aborting.\n"); + exit (1); + } + + return ret; +} + +void * +xcalloc (size_t nmemb, size_t size) +{ + void *ret; + + ret = calloc (nmemb, size); + + if (ret == NULL) { + fprintf (stderr, "Error: calloc failed. Out of memory. Aborting.\n"); + exit (1); + } + + return ret; +} + +void * +xrealloc (void *ptr, size_t size) +{ + void *ret; + + ret = realloc (ptr, size); + + if (ret == NULL) { + fprintf (stderr, "Error: realloc failed: Out of memory. Aborting.\n"); + exit (1); + } + + return ret; +} + +FILE * +xfdopen (int filedes, const char *mode) +{ + FILE *ret; + + ret = fdopen (filedes, mode); + + if (ret == NULL) { + fprintf (stderr, "Error: fdopen failed: %s. Aborting.\n", strerror (errno)); + exit (1); + } + + return ret; +} + +char * +xstrdup (const char *s) +{ + char *ret; + + ret = strdup (s); + + if (ret == NULL) { + fprintf (stderr, "Error: strdup failed: %s. Aborting.\n", + strerror (errno)); + exit (1); + } + + return ret; +} + +void +xfwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + int ret; + + ret = fwrite (ptr, size, nmemb, stream); + if (ret != nmemb) { + fprintf (stderr, "Error: error occured during fwrite. Aborting.\n"); + exit (1); + } +} diff --git a/src/x.h b/src/x.h new file mode 100644 index 0000000..52c3acd --- /dev/null +++ b/src/x.h @@ -0,0 +1,52 @@ +/* x.c - Some wrappers for various functions to check and exit on out-of-memory + * + * Copyright © 2005 Carl Worth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Author: Carl Worth + */ + +#ifndef _X_H_ +#define _X_H_ + +void +xasprintf (char **strp, const char *fmt, ...) WDO_PRINTF_FORMAT(2, 3); + +void +xvasprintf (char **strp, const char *fmt, va_list ap); + +void +xpipe (int filedes[2]); + +pid_t +xfork (void); + +void * +xmalloc (size_t size); + +void * +xcalloc (size_t nmemb, size_t size); + +void * +xrealloc (void *ptr, size_t size); + +FILE * +xfdopen (int filedes, const char *mode); + +char * +xstrdup (const char *s); + +#endif /* _X_H_ */ -- 2.43.0