diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2008-01-27 11:37:44 +0100 |
---|---|---|
committer | Tobias Klauser <tklauser@xenon.tklauser.home> | 2008-01-27 11:37:44 +0100 |
commit | 7e0f021a9aec35fd8e6725e87e3313b101d26f5e (patch) | |
tree | b1cacc4b24393f517aeb4610e9e1021f954307a8 /reference/C/EXAMPLES |
Initial import (2.0.2-6)2.0.2-6
Diffstat (limited to 'reference/C/EXAMPLES')
120 files changed, 5334 insertions, 0 deletions
diff --git a/reference/C/EXAMPLES/10to1.c b/reference/C/EXAMPLES/10to1.c new file mode 100644 index 0000000..74fea09 --- /dev/null +++ b/reference/C/EXAMPLES/10to1.c @@ -0,0 +1,54 @@ + +/******************************************************************* + * + * Purpose: Count down from 10 to 1 in steps of 1 + * Author: M J Leslie. + * Date: 02-Feb-94 + * + *******************************************************************/ + + /* version 1 */ + +#include <stdio.h> + +main() +{ + int a=10; + while (a >= 1) + { + printf("a is %2d\n",a); + a=a-1; + } +} + +/*******************************************************************/ + + /* version 2 */ + +main() +{ + a =11; + while (0 < (a = a - 1) ) + { + printf("a is %2d\n",a); + } +} + +/*******************************************************************/ + + /* version 3 */ + +main() +{ + /* + * This works because the 'while' loop + * will keep spinning while 'i' is TRUE. + * That is while 'i' is NOT equal to 0 + */ + + a=11; + while (i) + { + printf ("%i",i=i-1); + } +} diff --git a/reference/C/EXAMPLES/HEADER.html b/reference/C/EXAMPLES/HEADER.html new file mode 100644 index 0000000..1f2ca38 --- /dev/null +++ b/reference/C/EXAMPLES/HEADER.html @@ -0,0 +1,12 @@ +<title>Index of all Example Programs.</title> +<body bgcolor="#ffffcc"> +<hr> +<center> +<h1>Index of all Example Programs.</h1> +</center> +<hr> +<p> + +<! File descriptions are held in .htaccess > + +Here is the HTTP generated list of all Example programs. diff --git a/reference/C/EXAMPLES/Makefile.am b/reference/C/EXAMPLES/Makefile.am new file mode 100644 index 0000000..d045af2 --- /dev/null +++ b/reference/C/EXAMPLES/Makefile.am @@ -0,0 +1,5 @@ +EXTRA_DIST = $(wildcard *.html) $(wildcard *.c) $(wildcard *.h) $(wildcard *.gif) +docs_DATA = $(EXTRA_DIST) + +docsdir = $(kde_htmldir)/en/kdevelop/$(subdir) + diff --git a/reference/C/EXAMPLES/Makefile.in b/reference/C/EXAMPLES/Makefile.in new file mode 100644 index 0000000..331aa22 --- /dev/null +++ b/reference/C/EXAMPLES/Makefile.in @@ -0,0 +1,410 @@ +# KDE tags expanded automatically by am_edit - $Revision: 1.2 $ +# Makefile.in generated automatically by automake 1.5 from Makefile.am. + +# Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 +# Free Software Foundation, Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + +SHELL = @SHELL@ + +srcdir = @srcdir@ +top_srcdir = @top_srcdir@ +VPATH = @srcdir@ +prefix = @prefix@ +exec_prefix = @exec_prefix@ + +bindir = @bindir@ +sbindir = @sbindir@ +libexecdir = @libexecdir@ +datadir = @datadir@ +sysconfdir = @sysconfdir@ +sharedstatedir = @sharedstatedir@ +localstatedir = @localstatedir@ +libdir = @libdir@ +infodir = @infodir@ +mandir = @mandir@ +includedir = @includedir@ +oldincludedir = /usr/include +pkgdatadir = $(datadir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +top_builddir = ../../.. + +ACLOCAL = @ACLOCAL@ +AUTOCONF = @AUTOCONF@ +AUTOMAKE = @AUTOMAKE@ +AUTOHEADER = @AUTOHEADER@ + +INSTALL = @INSTALL@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_HEADER = $(INSTALL_DATA) +transform = @program_transform_name@ +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_alias = @build_alias@ +build_triplet = @build@ +host_alias = @host_alias@ +host_triplet = @host@ +target_alias = @target_alias@ +target_triplet = @target@ +AMTAR = @AMTAR@ +ARTSCCONFIG = @ARTSCCONFIG@ +AS = @AS@ +AUTODIRS = @AUTODIRS@ +AWK = @AWK@ +CC = @CC@ +CONF_FILES = @CONF_FILES@ +CPP = @CPP@ +CXX = @CXX@ +CXXCPP = @CXXCPP@ +DCOPIDL = @DCOPIDL@ +DCOPIDL2CPP = @DCOPIDL2CPP@ +DCOP_DEPENDENCIES = @DCOP_DEPENDENCIES@ +DEPDIR = @DEPDIR@ +DLLTOOL = @DLLTOOL@ +DPMSINC = @DPMSINC@ +DPMSLIB = @DPMSLIB@ +EXEEXT = @EXEEXT@ +GCJ = @GCJ@ +GCJFLAGS = @GCJFLAGS@ +GLINC = @GLINC@ +GLLIB = @GLLIB@ +GMSGFMT = @GMSGFMT@ +IDL = @IDL@ +IDL_DEPENDENCIES = @IDL_DEPENDENCIES@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +JAR = @JAR@ +JAVAC = @JAVAC@ +JAVAH = @JAVAH@ +JVMLIBS = @JVMLIBS@ +KDECONFIG = @KDECONFIG@ +KDE_CXXFLAGS = @KDE_CXXFLAGS@ +KDE_EXTRA_RPATH = @KDE_EXTRA_RPATH@ +KDE_INCLUDES = @KDE_INCLUDES@ +KDE_LDFLAGS = @KDE_LDFLAGS@ +KDE_PLUGIN = @KDE_PLUGIN@ +KDE_RPATH = @KDE_RPATH@ +KDE_USE_CLOSURE_FALSE = @KDE_USE_CLOSURE_FALSE@ +KDE_USE_CLOSURE_TRUE = @KDE_USE_CLOSURE_TRUE@ +KDE_USE_FINAL_FALSE = @KDE_USE_FINAL_FALSE@ +KDE_USE_FINAL_TRUE = @KDE_USE_FINAL_TRUE@ +KDE_XSL_STYLESHEET = @KDE_XSL_STYLESHEET@ +LIBCOMPAT = @LIBCOMPAT@ +LIBCRYPT = @LIBCRYPT@ +LIBDL = @LIBDL@ +LIBGEN = @LIBGEN@ +LIBJPEG = @LIBJPEG@ +LIBMICO = @LIBMICO@ +LIBOBJS = @LIBOBJS@ +LIBPNG = @LIBPNG@ +LIBPTHREAD = @LIBPTHREAD@ +LIBPYTHON = @LIBPYTHON@ +LIBQIMGIO = @LIBQIMGIO@ +LIBRESOLV = @LIBRESOLV@ +LIBSHADOW = @LIBSHADOW@ +LIBSM = @LIBSM@ +LIBSOCKET = @LIBSOCKET@ +LIBTIFF = @LIBTIFF@ +LIBTOOL = @LIBTOOL@ +LIBUCB = @LIBUCB@ +LIBUTIL = @LIBUTIL@ +LIBXINERAMA = @LIBXINERAMA@ +LIBZ = @LIBZ@ +LIB_KAB = @LIB_KAB@ +LIB_KDECORE = @LIB_KDECORE@ +LIB_KDEUI = @LIB_KDEUI@ +LIB_KFILE = @LIB_KFILE@ +LIB_KFM = @LIB_KFM@ +LIB_KFORMULA = @LIB_KFORMULA@ +LIB_KHTML = @LIB_KHTML@ +LIB_KIMGIO = @LIB_KIMGIO@ +LIB_KIO = @LIB_KIO@ +LIB_KPARTS = @LIB_KPARTS@ +LIB_KSPELL = @LIB_KSPELL@ +LIB_KSYCOCA = @LIB_KSYCOCA@ +LIB_KWRITE = @LIB_KWRITE@ +LIB_QT = @LIB_QT@ +LIB_SMB = @LIB_SMB@ +LIB_X11 = @LIB_X11@ +LN_S = @LN_S@ +MCOPIDL = @MCOPIDL@ +MEINPROC = @MEINPROC@ +MICO_INCLUDES = @MICO_INCLUDES@ +MICO_LDFLAGS = @MICO_LDFLAGS@ +MOC = @MOC@ +MSGFMT = @MSGFMT@ +NOOPT_CXXFLAGS = @NOOPT_CXXFLAGS@ +NOREPO = @NOREPO@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +PACKAGE = @PACKAGE@ +PAMLIBS = @PAMLIBS@ +PASSWDLIBS = @PASSWDLIBS@ +PYTHONINC = @PYTHONINC@ +PYTHONLIB = @PYTHONLIB@ +PYTHONMODDIR = @PYTHONMODDIR@ +QT_INCLUDES = @QT_INCLUDES@ +QT_LDFLAGS = @QT_LDFLAGS@ +RANLIB = @RANLIB@ +REPO = @REPO@ +SETUIDFLAGS = @SETUIDFLAGS@ +STRIP = @STRIP@ +TOPSUBDIRS = @TOPSUBDIRS@ +UIC = @UIC@ +UIC_TR = @UIC_TR@ +USER_INCLUDES = @USER_INCLUDES@ +USER_LDFLAGS = @USER_LDFLAGS@ +USE_EXCEPTIONS = @USE_EXCEPTIONS@ +USE_RTTI = @USE_RTTI@ +USE_THREADS = @USE_THREADS@ +VERSION = @VERSION@ +XGETTEXT = @XGETTEXT@ +XPMINC = @XPMINC@ +XPMLIB = @XPMLIB@ +X_EXTRA_LIBS = @X_EXTRA_LIBS@ +X_INCLUDES = @X_INCLUDES@ +X_LDFLAGS = @X_LDFLAGS@ +X_PRE_LIBS = @X_PRE_LIBS@ +all_includes = @all_includes@ +all_libraries = @all_libraries@ +am__include = @am__include@ +am__quote = @am__quote@ +idldir = @idldir@ +install_sh = @install_sh@ +jni_includes = @jni_includes@ +kde_appsdir = @kde_appsdir@ +kde_bindir = @kde_bindir@ +kde_confdir = @kde_confdir@ +kde_datadir = @kde_datadir@ +kde_htmldir = @kde_htmldir@ +kde_icondir = @kde_icondir@ +kde_includes = @kde_includes@ +kde_libraries = @kde_libraries@ +kde_libs_htmldir = @kde_libs_htmldir@ +kde_libs_prefix = @kde_libs_prefix@ +kde_locale = @kde_locale@ +kde_mimedir = @kde_mimedir@ +kde_moduledir = @kde_moduledir@ +kde_qtver = @kde_qtver@ +kde_servicesdir = @kde_servicesdir@ +kde_servicetypesdir = @kde_servicetypesdir@ +kde_sounddir = @kde_sounddir@ +kde_templatesdir = @kde_templatesdir@ +kde_wallpaperdir = @kde_wallpaperdir@ +micodir = @micodir@ +qt_includes = @qt_includes@ +qt_libraries = @qt_libraries@ +x_includes = @x_includes@ +x_libraries = @x_libraries@ + +EXTRA_DIST = $(wildcard *.html) $(wildcard *.c) $(wildcard *.h) $(wildcard *.gif) +docs_DATA = $(EXTRA_DIST) + +docsdir = $(kde_htmldir)/en/kdevelop/$(subdir) +subdir = reference/C/EXAMPLES +mkinstalldirs = $(SHELL) $(top_srcdir)/admin/mkinstalldirs +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +DIST_SOURCES = +DATA = $(docs_DATA) + +DIST_COMMON = Makefile.am Makefile.in +#>- all: all-am +#>+ 1 +all: docs-am all-am + +.SUFFIXES: + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +distclean-libtool: + -rm -f libtool +$(srcdir)/Makefile.in: Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4) +#>- cd $(top_srcdir) && \ +#>- $(AUTOMAKE) --gnu reference/C/EXAMPLES/Makefile +#>+ 3 + cd $(top_srcdir) && \ + $(AUTOMAKE) --gnu reference/C/EXAMPLES/Makefile + cd $(top_srcdir) && perl admin/am_edit reference/C/EXAMPLES/Makefile.in +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + cd $(top_builddir) && \ + CONFIG_HEADERS= CONFIG_LINKS= \ + CONFIG_FILES=$(subdir)/$@ $(SHELL) ./config.status +uninstall-info-am: +install-docsDATA: $(docs_DATA) + @$(NORMAL_INSTALL) + $(mkinstalldirs) $(DESTDIR)$(docsdir) + @list='$(docs_DATA)'; for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + f="`echo $$p | sed -e 's|^.*/||'`"; \ + echo " $(INSTALL_DATA) $$d$$p $(DESTDIR)$(docsdir)/$$f"; \ + $(INSTALL_DATA) $$d$$p $(DESTDIR)$(docsdir)/$$f; \ + done + +uninstall-docsDATA: + @$(NORMAL_UNINSTALL) + @list='$(docs_DATA)'; for p in $$list; do \ + f="`echo $$p | sed -e 's|^.*/||'`"; \ + echo " rm -f $(DESTDIR)$(docsdir)/$$f"; \ + rm -f $(DESTDIR)$(docsdir)/$$f; \ + done +tags: TAGS +TAGS: + + +#>- DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +#>+ 4 +KDE_DIST=do.c address.c basename.c first.c struct2.c extern2.c single_array.c copy_file.c var_func.c sprintf1.c README.html perl2.c atexit.c readch.c fgets.c strchr.c malloc.c sizeof1.c enum1.c qsort2.c fprintf.c linklst3.c statfs.c if.c funcpt2.c getchar.c setuid.c HEADER.html atof.c strtok.c popen.c const.c static.c pointer2_func.c sscanf.c ifndef.c user_name.c struct3.c continue.c union.c and.c time.c enum2.c system.c exec_one.c while.c fact1.c prog_name.c inc_dec.c goto.c funcpt3.c Master.c strtol.c password.c reverse.c printenv.c 10to1.c struct4.c pointer1_func.c switch.c clrscr.c exponent.c ljust1.c enum3.c strcpy.c rand.c function.c read_file.c fact2.c pointer_func.c strncpy.c strcmp.c sprintf.c stat.c for1.c if_else_endif.c enum4.c uname.c examples.html pipe1.c fork1.c sizeof.c kill.c command_line.c pointer4_func.c swap.c linklst1.c const2.c bit_shift.c lenstr.c or.c gets.c whiteball.gif strstr.c stdin.c struct1.c extern1.c macro.c for2.c keyboard.c enum5.c temp.c exec_two.c define.c pipe2.c fork2.c strpbrk.c qsort1.c convesc.c varlist.c modulo.c bsearch.c global.c linklst2.c is_examp.c pointer3_func.c dir.c funcpt1.c + +DISTFILES= $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) $(KDE_DIST) + + +top_distdir = ../../.. +distdir = $(top_distdir)/$(PACKAGE)-$(VERSION) + +distdir: $(DISTFILES) + @for file in $(DISTFILES); do \ + if test -f $$file; then d=.; else d=$(srcdir); fi; \ + dir=`echo "$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test "$$dir" != "$$file" && test "$$dir" != "."; then \ + $(mkinstalldirs) "$(distdir)/$$dir"; \ + fi; \ + if test -d $$d/$$file; then \ + cp -pR $$d/$$file $(distdir) \ + || exit 1; \ + else \ + test -f $(distdir)/$$file \ + || cp -p $$d/$$file $(distdir)/$$file \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(DATA) + +installdirs: + $(mkinstalldirs) $(DESTDIR)$(docsdir) + +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + `test -z '$(STRIP)' || \ + echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -rm -f Makefile $(CONFIG_CLEAN_FILES) stamp-h stamp-h[0-9]* + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +#>- clean: clean-am +#>+ 1 +clean: kde-rpo-clean clean-am + +clean-am: clean-generic clean-libtool mostlyclean-am + +distclean: distclean-am + +distclean-am: clean-am distclean-generic distclean-libtool + +dvi: dvi-am + +dvi-am: + +info: info-am + +info-am: + +install-data-am: install-docsDATA + +install-exec-am: + +install-info: install-info-am + +install-man: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-generic mostlyclean-libtool + +uninstall-am: uninstall-docsDATA uninstall-info-am + +.PHONY: all all-am check check-am clean clean-generic clean-libtool \ + distclean distclean-generic distclean-libtool distdir dvi \ + dvi-am info info-am install install-am install-data \ + install-data-am install-docsDATA install-exec install-exec-am \ + install-info install-info-am install-man install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-generic \ + mostlyclean-libtool uninstall uninstall-am uninstall-docsDATA \ + uninstall-info-am + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: + +#>+ 2 +docs-am: + +#>+ 6 +force-reedit: + cd $(top_srcdir) && \ + $(AUTOMAKE) --gnu reference/C/EXAMPLES/Makefile + cd $(top_srcdir) && perl admin/am_edit reference/C/EXAMPLES/Makefile.in + + +#>+ 2 +final: + $(MAKE) all-am +#>+ 2 +final-install: + $(MAKE) install-am +#>+ 2 +no-final: + $(MAKE) all-am +#>+ 2 +no-final-install: + $(MAKE) install-am +#>+ 3 +cvs-clean: + $(MAKE) -f $(top_srcdir)/admin/Makefile.common cvs-clean + +#>+ 3 +kde-rpo-clean: + -rm -f *.rpo diff --git a/reference/C/EXAMPLES/Master.c b/reference/C/EXAMPLES/Master.c new file mode 100644 index 0000000..7c082b6 --- /dev/null +++ b/reference/C/EXAMPLES/Master.c @@ -0,0 +1,14 @@ +/************************************************************************ + * + * Purpose: + * Author: M J Leslie + * Date: 26-Jan-94 + * + ************************************************************************/ + +#include <stdlib.h> + +main() +{ + +} diff --git a/reference/C/EXAMPLES/README.html b/reference/C/EXAMPLES/README.html new file mode 100644 index 0000000..c38d784 --- /dev/null +++ b/reference/C/EXAMPLES/README.html @@ -0,0 +1,30 @@ +<head> +<script language="JavaScript"> +</script> +</head> + +<p> + +<hr> +<p> +<center> +<table border=2 width="80%" bgcolor="ivory"> +<tr align=center> +<td width="25%"> +<a href="../cref.html"> Top</a> +</td><td width="25%"> +<a href="../master_index.html"> Master Index</a> +</td><td width="25%"> +<a href="../SYNTAX/keywords.html"> Keywords</a> +</td><td width="25%"> +<a href="../FUNCTIONS/funcref.htm"> Functions</a> +</td> +</tr> +</table> +</center> +<p> +<hr> +<address>Martin Leslie +</address><p> +</body> +</html> diff --git a/reference/C/EXAMPLES/address.c b/reference/C/EXAMPLES/address.c new file mode 100644 index 0000000..1fd6881 --- /dev/null +++ b/reference/C/EXAMPLES/address.c @@ -0,0 +1,25 @@ +/************************************************************************ + * + * Purpose: Demonstrate the Unary '&' and '*' operators. + * Unary & - Address of a variable. + * Unary * - Data at the address. + * + * These are not to be confused with their binary equivalents + * that perform bitwise AND and OR. + * + * Author: M J Leslie. + * Date: 16-Apr-94 + * + ************************************************************************/ + +main() +{ + int i=4; /* variable declaration */ + int* ptr; /* int pointer */ + + ptr = &i; /* 'ptr' now contains the + address of 'i' */ + + printf(" i is %d.\n", i); + printf("*ptr is %d.\n", *ptr); +} diff --git a/reference/C/EXAMPLES/and.c b/reference/C/EXAMPLES/and.c new file mode 100644 index 0000000..5844f61 --- /dev/null +++ b/reference/C/EXAMPLES/and.c @@ -0,0 +1,44 @@ +/**************************************************************************** + * + * Purpose: To show the effect of a bitwise AND (&) by converting + * an ASCII graphic extension into its character equivalent. + * Author: M.J. Leslie. + * Date: 04-Mar-95 + * + ****************************************************************************/ + +main() +{ + /* + * Hex E6 looks like this in binary + * + * 1110 0110 + * + * UNSIGNED is used to stop C using + * first 1 as an indicator of the sign + */ + unsigned char value='\xE6'; + + /* Hex 7F looks like this in binary + * + * 0111 1111 + */ + + printf("%2X %2X \n", value, (value & '\x7F')); + + /* A bitwise AND has the effect of + * filtering unwanted bits. + * + * 1110 0110 (E6) AND + * 0111 1111 (7F) + * --------- + * 0110 0110 (66) + */ +} +/**************************************************************************** + * + * Program results are: + * + * E6 66 + * + ****************************************************************************/ diff --git a/reference/C/EXAMPLES/atexit.c b/reference/C/EXAMPLES/atexit.c new file mode 100644 index 0000000..2371f2f --- /dev/null +++ b/reference/C/EXAMPLES/atexit.c @@ -0,0 +1,26 @@ + +/****************************************************************** + * + * Purpose: Program to demonstrate the use of atexit. + * Date: 17-Feb-97 + * Author: M J Leslie. + * Descrip: Regester a function to be executed when a program normally + * terminates. + * + ******************************************************************/ + +#include <stdlib.h> + +void End(void); + +main() +{ + atexit(End); + + printf("Program is about to end\n"); +} + +void End(void) +{ + printf("Program ended\n"); +} diff --git a/reference/C/EXAMPLES/atof.c b/reference/C/EXAMPLES/atof.c new file mode 100644 index 0000000..44ea3c7 --- /dev/null +++ b/reference/C/EXAMPLES/atof.c @@ -0,0 +1,44 @@ +/**************************************************************** + * + * Purpose: Program to demonstrate the 'atof' and 'gets' functions. + * Author: M J Leslie + * Date: 08/05/94 + * + ****************************************************************/ + +#include <stdio.h> /* puts gets */ +#include <stdlib.h> /* atof */ + +main() +{ + char str1[80], str2[80]; /* define a couple o' strings. */ + double result; /* Result of multiplication. */ + + puts ("This program will multiply two floating point numbers."); + puts ("Warning: The program will accept invalid data, and give"); + puts ("you crap results in return.\n"); + puts ("Please enter the first number."); + gets(str1); + + puts ("And the second."); + gets(str2); + + result = atof(str1) * atof(str2); + printf("Answer is %8.2f\n", result); +} + +/******************************************************************** + * + * Here is an example run. + * + * This program will multiply two floating point numbers. + * Warning: The program will accept invalid data, and give + * you crap results as a result. + * + * Please enter the first number. + * 20 + * And the second. + * .5 + * Answer is 10.00 + * + ********************************************************************/ diff --git a/reference/C/EXAMPLES/basename.c b/reference/C/EXAMPLES/basename.c new file mode 100644 index 0000000..fcecba7 --- /dev/null +++ b/reference/C/EXAMPLES/basename.c @@ -0,0 +1,51 @@ +/************************************************************************ + * + * Purpose: Extract the directory information from a file name. + * + * /etc/local/bin/fdmount --> fdmount + * + * This mimics the unix command 'basename'. + * + * Author: M J Leslie + * Date: 29-Feb-96 + * + ************************************************************************/ + +#include <string.h> + +char *basename(const char *FullName); + +main() +{ + char *FullName = "/usr/local/bin/fdmount"; + + printf("Full name is %s \n", FullName); + + printf("File name is %s \n", basename(FullName)); +} + +/************************************************************************/ + +char *basename(const char *FullName) +{ + static char *File; + + /* ... I guess DOS users will have to change the direction of + ... the slash. */ + + File = strrchr(FullName, '/'); + + /* ... If no slashes have been found, Return the full file name */ + + if (File == NULL) + { + File = FullName; + } + else + { + File++; + } + + return(File); +} + diff --git a/reference/C/EXAMPLES/bit_shift.c b/reference/C/EXAMPLES/bit_shift.c new file mode 100644 index 0000000..855a5fa --- /dev/null +++ b/reference/C/EXAMPLES/bit_shift.c @@ -0,0 +1,36 @@ +/*********************************************************************** + * + * Purpose: Show the 'shift right' operator. + * + * Author: M.J. Leslie. + * + * Date: 19-Mar-95 + * + ***********************************************************************/ + +main() +{ + unsigned int bytes=256; /* 00000000 00000000 00000000 10000000 */ + do + { + printf("%3d \n", bytes); + bytes >>= 1; /* 00000000 00000000 00000000 01000000 */ + } while (bytes); +} + +/*********************************************************************** + * + * Program results. + * ---------------- + * + * 256 + * 128 + * 64 + * 32 + * 16 + * 8 + * 4 + * 2 + * 1 + * + ***********************************************************************/ diff --git a/reference/C/EXAMPLES/bsearch.c b/reference/C/EXAMPLES/bsearch.c new file mode 100644 index 0000000..618f114 --- /dev/null +++ b/reference/C/EXAMPLES/bsearch.c @@ -0,0 +1,44 @@ +/************************************************************************ + * + * Purpose: To demonstrate the 'bsearch' function. + * + * Author: M.J. Leslie + * + * Date: 04-Jun-95 + * + ************************************************************************/ + +#include <stdlib.h> + +#define NUM 11 + +static int func (void *, void *); + +/************************************************************************/ + +main() +{ + /* List of numbers. */ + int numbers[NUM]={3,4,7,9,11,13,15,17,19,21,23}; + int key=22; /* number to find. */ + int * result; /* Result of 'bsearch' */ + + /* Search for 'key' in 'numbers'*/ + + result = bsearch (&key, numbers, NUM, sizeof(numbers[0]), (void *)func); + + printf("%d ", key); + (result) ? puts("found") : puts("not found"); +} + +/************************************************************************/ + +static int func (void *a, void *b) +{ + printf("%d %d \n", *(int *)a, *(int *)b); /* Diagnistics. */ + + /* Compare the two numbers */ + if (*(int *)a == *(int *)b) return(0); + if (*(int *)a < *(int *)b) return(-1); + return (1); +} diff --git a/reference/C/EXAMPLES/clrscr.c b/reference/C/EXAMPLES/clrscr.c new file mode 100644 index 0000000..a002dd3 --- /dev/null +++ b/reference/C/EXAMPLES/clrscr.c @@ -0,0 +1,22 @@ +/************************************************************************* + * + * Purpose: Clear the screen with VT100 escape codes. This can be done + * with conio.h on PCs - non standard code. Or curses.h, bit of + * a fag... + * Author: M.J. Leslie + * Date: 22-Jun-94 + * + ************************************************************************/ + +void clrscr(void); + +main() +{ + clrscr(); +} + +void clrscr(void) +{ + printf("\033[2J"); /* Clear the entire screen. */ + printf("\033[0;0f"); /* Move cursor to the top left hand corner */ +} diff --git a/reference/C/EXAMPLES/command_line.c b/reference/C/EXAMPLES/command_line.c new file mode 100644 index 0000000..40135cb --- /dev/null +++ b/reference/C/EXAMPLES/command_line.c @@ -0,0 +1,43 @@ +/******************************************************************** + * + * Purpose: Demonstrate getting parameters from the command line. + * Author: M J Leslie + * Date: 27-Mar-94 + * + ********************************************************************/ + +#include <stdio.h> + +main(int argc, char *argv[]) +{ + int count; + + /* Main takes two variables 'argc' is the number of parms on the + * command line and 'argv' is a pointer to each of the parameters. + * + * int argc -- integer number called 'argc' + * char *argv[] -- Character pointer array! + */ + + printf("%i parameters entered on the command line.\n", argc); + + /* + * progname argc = 1 + * progname parm1 parm2 argc = 3 + */ + + /* + * We take 1 from argc because + * the argv array starts at zero + * an ends at argc -1 + */ + + for ( count = 0; count <= argc -1 ; count++) + { + /* printf expects a pointer + * to the text + */ + + printf("parm %d is %s\n", count, argv[count]); + } +} diff --git a/reference/C/EXAMPLES/const.c b/reference/C/EXAMPLES/const.c new file mode 100644 index 0000000..d686d13 --- /dev/null +++ b/reference/C/EXAMPLES/const.c @@ -0,0 +1,17 @@ +/****************************************************************** + * + * Purpose: Program to demonstrate const + * Date: 03-Dec-96 + * Author: M J Leslie. + * Descrip: To be written. + * + ******************************************************************/ + +#include <stdio.h> + +main() +{ + const int Men=10; + + Men = 20; /* This will be failed by the compiler. */ +} diff --git a/reference/C/EXAMPLES/const2.c b/reference/C/EXAMPLES/const2.c new file mode 100644 index 0000000..249a071 --- /dev/null +++ b/reference/C/EXAMPLES/const2.c @@ -0,0 +1,33 @@ +/******************************************************************* + * + * Purpose: 'const' example. This code shows that making a + * variable 'const' only tells the compiler to not + * allow reassignment of the variable. The data is not + * in a special location, it can still be altered. + * + * This code may cause compiler warning messages + * but will compile and run. + * + * Author: M J Leslie. + * Date: 03-Mar-97 + * + *******************************************************************/ + +main() +{ + const int Men = 10; + int *Women = &Men; + + *Women = 20; + + printf("There are %d men\n", Men); + +} + +/******************************************************************* + * + * Result. + * + * There are 20 men + * + *******************************************************************/ diff --git a/reference/C/EXAMPLES/continue.c b/reference/C/EXAMPLES/continue.c new file mode 100644 index 0000000..9550129 --- /dev/null +++ b/reference/C/EXAMPLES/continue.c @@ -0,0 +1,32 @@ +/************************************************************************** + * + * Purpose: To filter some records. + * Demonstrates the 'continue', 'feof' & 'fgets' statemnts. + * Author: M J Leslie + * Date: 07-Jun-94 + * + *************************************************************************/ + +#include <stdio.h> + +main() +{ + char data[80]; /* Record read from the file. */ + FILE *ptr; /* Pointer to the file. FILE is a + structure defined in <stdio.h> */ + + /* Open the file - no error checking done */ + ptr = fopen("/etc/hosts","r"); + /* Read one record at a time, checking + for the End of File. EOF is defined + in <stdio.h> as -1 */ + + while (feof(ptr) == 0) + { + fgets(data, 80, ptr); /* Read next record */ + if (data[0] == '#') continue; /* filter out the comments */ + printf("%s",data); /* O/P the record to the screen */ + } + + fclose(ptr); /* Close the file. */ +} diff --git a/reference/C/EXAMPLES/convesc.c b/reference/C/EXAMPLES/convesc.c new file mode 100644 index 0000000..f79b11e --- /dev/null +++ b/reference/C/EXAMPLES/convesc.c @@ -0,0 +1,118 @@ + +/********************************************************************** + * + * Description: If a text string is created outside of C, the + * escape codes are not correctly stored. This + * routine will prepare them for C's use. + * + * Author : M.J. Leslie + * Date: 25-Feb-96 + * + *********************************************************************/ + +void mos_ConvertEscapeCode(char *String, char *Code); + +int main(int argc, char *argv[]) +{ + if (argc == 1) + { + puts("\n\tPlease provide a text string on the command line."); + } + else + { + printf("I/P string is: %s\n", argv[1]); + mos_ConvertEscapeCode(argv[1], "\\n"); + mos_ConvertEscapeCode(argv[1], "\\t"); + mos_ConvertEscapeCode(argv[1], "\\v"); + mos_ConvertEscapeCode(argv[1], "\\b"); + mos_ConvertEscapeCode(argv[1], "\\r"); + mos_ConvertEscapeCode(argv[1], "\\f"); + printf("O/P string is: %s\n", argv[1]); + } +} + +/********************************************************************** + * + * Purpose: To convert escape codes in text form into + * actual codes. + * + * I/P ------------------------------- + * | A | B | C | \ | n | D | E | F | + * ------------------------------- + * + * O/P ---------------------------- + * | A | B | C | \n | D | E | F | + * ---------------------------- + * + **********************************************************************/ + +void mos_ConvertEscapeCode(char *String, char *Code) +{ + char *Ptr1; + char *Ptr2; + char EscCode = ' '; + + /* ... Make sure the Code is long enough */ + + if (strlen(Code) == 2) + { + + /* ... Find the right escape code. */ + + switch(Code[1]) + { + case 'n': /* New line */ + EscCode = '\n'; + break; + case 't': /* Horizontal tab */ + EscCode = '\t'; + break; + case 'v': /* Vertical tab */ + EscCode = '\v'; + break; + case 'b': /* Backspace */ + EscCode = '\b'; + break; + case 'r': /* Return */ + EscCode = '\r'; + break; + case 'f': /* Form feed */ + EscCode = '\f'; + break; + default: + break; + } + + /* ... If the escape code has been found */ + + if (EscCode != ' ') + { + + /* ... Copy each character until the text code is found, + ... insert the escape code and copy the remaining chars. */ + + for (Ptr1=Ptr2=String; Ptr2 < (String+strlen(String)+1); Ptr1++, Ptr2++) + { + if ((*Ptr2 == '\\') && (*(Ptr2+1) == Code[1])) + { + *Ptr1 = EscCode; + Ptr2++; + } + else + { + *Ptr1 = *Ptr2; + } + } + } + } +} + + + + + + + + + + diff --git a/reference/C/EXAMPLES/copy_file.c b/reference/C/EXAMPLES/copy_file.c new file mode 100644 index 0000000..bf67e9d --- /dev/null +++ b/reference/C/EXAMPLES/copy_file.c @@ -0,0 +1,32 @@ +/************************************************************************ + * + * Purpose: To copy a file (Unix 'cp' command). + * Author: M J Leslie + * Date: 16-May-96 + * + ************************************************************************/ + +#include <stdio.h> + +main() +{ + int c; /* Character read/written between files */ + FILE *IPFile; /* Pointer to the I/P file. FILE is a + structure defined in <stdio.h> */ + FILE *OPFile; + + /* Open the file - no error checking done */ + IPFile = fopen("/etc/hosts","r"); + OPFile = fopen("/tmp/hosts","w"); + + /* Read one character at a time, checking + for the End of File. EOF is defined + in <stdio.h> as -1 */ + while ((c = fgetc(IPFile)) != EOF) + { + fputc(c, OPFile); /* O/P the character */ + } + + fclose(IPFile); /* Close the files. */ + fclose(OPFile); /* Close the files. */ +} diff --git a/reference/C/EXAMPLES/define.c b/reference/C/EXAMPLES/define.c new file mode 100644 index 0000000..259a3e8 --- /dev/null +++ b/reference/C/EXAMPLES/define.c @@ -0,0 +1,17 @@ +/********************************************************************* + * + * Purpose: Demonstrate the #define pre-processor. + * Author: M J Leslie + * Date: 26-Jun-94 + * + *********************************************************************/ + +#define EQ == + +main () +{ + /* the EQ is translated to == by + * the C pre-processor. COOL! + */ + if ( 5 EQ 5 ) printf("define works...\n"); +} diff --git a/reference/C/EXAMPLES/dir.c b/reference/C/EXAMPLES/dir.c new file mode 100644 index 0000000..c4012ed --- /dev/null +++ b/reference/C/EXAMPLES/dir.c @@ -0,0 +1,36 @@ +/************************************************************************ + * + * Purpose: List all files in the current directory. + * Author: M J Leslie + * Date: 15-Apr-95 + * + * Note: 1) This program uses NON ANSI STANDARD functions. You may + * not find them on your platform. + * 2) The file names are NOT stored in any particular order. + * + ************************************************************************/ + +#include <dirent.h> /* Directory information. */ + +main() +{ + DIR *dir_p; + struct dirent *dir_entry_p; + + /* Open the current directory */ + dir_p = opendir("."); + + /* read each entry until NULL. */ + + while( NULL != (dir_entry_p = readdir(dir_p))) + { + /* print the name of the file held in + * this directory entry. */ + + printf(" %s \n", dir_entry_p->d_name); + } + + /* Tidy up. */ + closedir(dir_p); +} + diff --git a/reference/C/EXAMPLES/do.c b/reference/C/EXAMPLES/do.c new file mode 100644 index 0000000..02b3d2c --- /dev/null +++ b/reference/C/EXAMPLES/do.c @@ -0,0 +1,26 @@ +/************************************************************************ + * + * Purpose: Program to demonstrate the 'do' statement. + * Author: M J Leslie + * Date: 26-Jan-94 + * + ************************************************************************/ + +#include <stdio.h> + +main() +{ + int i=1; /* Define an integer variable. */ + + /* + * The block is executed. Then the i <= 10 + * expression is evaluated. If TRUE the block + * is executed again. + */ + do + { + printf ("i is %i\n", i); + i++; + } while (i <= 10); + +} diff --git a/reference/C/EXAMPLES/enum1.c b/reference/C/EXAMPLES/enum1.c new file mode 100644 index 0000000..4467003 --- /dev/null +++ b/reference/C/EXAMPLES/enum1.c @@ -0,0 +1,25 @@ +/**************************************************************************** + * + * Enumeration example. Program will return the month in a year. + * I.E. It returns 9 for September. + * + ****************************************************************************/ + +main() +{ + /* + * Define a list of aliases + */ + enum months {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; + /* A A + | | + | | + | ------- list of aliases. + -------------- Enumeration tag. */ + + + enum months month; /* define 'month' variable of type 'months' */ + + printf("%d\n", month=Sep); /* Assign integer value via an alias + * This will return a 9 */ +} diff --git a/reference/C/EXAMPLES/enum2.c b/reference/C/EXAMPLES/enum2.c new file mode 100644 index 0000000..e978e74 --- /dev/null +++ b/reference/C/EXAMPLES/enum2.c @@ -0,0 +1,27 @@ +/**************************************************************************** + * + * Enumeration example. Program will return the number of days in a month + * I.E. returns 28 for Feburary. + * + ****************************************************************************/ +main() +{ + /* + * Define a list of aliases + */ + enum days {Jan=31, Feb=28, Mar=31, + Apr=30, May=31, Jun=30, + Jul=31, Aug=31, Sep=30, + Oct=31, Nov=30, Dec=31}; + /* A A + | | + | | + | ------- list of aliases. + -------------- Enumeration tag. */ + + + enum days month; /* define 'month' variable of type 'months' */ + + printf("%d\n", month=Feb); /* Assign integer value via an alias + * This will return 28 */ +} diff --git a/reference/C/EXAMPLES/enum3.c b/reference/C/EXAMPLES/enum3.c new file mode 100644 index 0000000..97b79b4 --- /dev/null +++ b/reference/C/EXAMPLES/enum3.c @@ -0,0 +1,36 @@ +/**************************************************************************** + * + * Enumeration example: This program will fail to compile because + * 'Alex' is in both enum lists. + * + ****************************************************************************/ +main() +{ + + enum People1 {Alex=0, Tracy, Kristian} Girls; + enum People2 {William=0, Martin, Alex} Boys; + /* A A + | | + | | + | ------- list of aliases. + -------------- Enumeration tag. */ + + + switch (Boys) + { + case William: + puts("William"); + break; + + case Martin: + puts("Martin"); + break; + + case Alex: + puts("Alex"); + break; + + default: + break; + } +} diff --git a/reference/C/EXAMPLES/enum4.c b/reference/C/EXAMPLES/enum4.c new file mode 100644 index 0000000..a07a3bf --- /dev/null +++ b/reference/C/EXAMPLES/enum4.c @@ -0,0 +1,17 @@ +/**************************************************************************** + * + * Enumeration example: This program will fail to compile because the + * preprocessor will change the FALSE to 1 on the enum statement.... + * + ****************************************************************************/ + +#define FALSE 1 + +main() +{ + + enum Boolian_t {FALSE=0, TRUE} Boolian; + + printf("False has a value of %d", FALSE); + printf(" True has a value of %d", TRUE); +} diff --git a/reference/C/EXAMPLES/enum5.c b/reference/C/EXAMPLES/enum5.c new file mode 100644 index 0000000..76f52f2 --- /dev/null +++ b/reference/C/EXAMPLES/enum5.c @@ -0,0 +1,27 @@ +/**************************************************************************** + * + * Enumeration example: This program will compile but the #define statement + * will cause FALSE and TRUE to have a value of 1 + * + ****************************************************************************/ + +enum Boolian_t {FALSE=0, TRUE} Boolian; + +#define FALSE 1 + +main() +{ + + enum Boolian_t Boolian; + + printf("False has a value of %d\n", FALSE); + printf(" True has a value of %d\n", TRUE); +} +/**************************************************************************** + * + * Results: + * + * False has a value of 1 + * True has a value of 1 + * + ****************************************************************************/ diff --git a/reference/C/EXAMPLES/examples.html b/reference/C/EXAMPLES/examples.html new file mode 100644 index 0000000..b86f7ee --- /dev/null +++ b/reference/C/EXAMPLES/examples.html @@ -0,0 +1,160 @@ +<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> +<html> +<head> + <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> + <meta name="GENERATOR" content="Mozilla/4.5 [en] (X11; I; Linux 2.0.36 i586) [Netscape]"> + <title>C Programming Examples</title> +<script language="JavaScript"> +</script> +</head> +<body bgcolor="#FFFFCC"> + +<hr> +<center> +<h1> +C Programming Examples</h1></center> + +<hr> +<p>All these examples have been tested on a PC running Linux and using +the 'gcc' compiler. You can extract the programs with the 'save' option +under 'file' and compile with your own compiler. +<dl compact> +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="first.c">Your first C program.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="if.c">if.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="while.c">while.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="do.c">do.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="for1.c">for </a>example 1.</dd> + +<br><a href="for2.c">for </a>A more advanced example. +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="switch.c">switch.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="function.c">function.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="global.c">Global and local variables.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="inc_dec.c">Increment & decrement.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +Pass <a href="command_line.c">command line</a> arguments.</dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="10to1.c">Print from 10 to 1 - three examples.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="read_file.c">Read a file - version 1.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="strcpy.c">manipulate strings.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="password.c">Using 'curses' to read a password.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="../X_EXAMPLES/Xref.html">Programs requiring X.</a> Unix users +only. +<hr></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="">Index of all example programs.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="../CONTRIB/OR_USING_C/">O'Reillys' Using C examples.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="../CONTRIB/OR_PRACTICAL_C/">O'Reillys' Practical C examples.</a></dd> + +<dt> +<img SRC="whiteball.gif" ALT="o" ></dt> + +<dd> +<a href="../CONTRIB/SNIP/">Bob Stouts 'Snippets'.</a></dd> +</dl> + +<hr> +<center><table BORDER=2 WIDTH="80%" BGCOLOR="#FFFFF0" > +<tr ALIGN=CENTER> +<td WIDTH="25%"><a href="../cref.html">Top</a></td> + +<td WIDTH="25%"><a href="../master_index.html">Master +Index</a></td> + +<td WIDTH="25%"><a href="../SYNTAX/keywords.html">Keywords</a></td> + +<td WIDTH="25%"><a href="../FUNCTIONS/funcref.htm">Functions</a></td> +</tr> +</table></center> + +<p> +<hr> +<address> +Martin Leslie</address> + +</body> +</html> diff --git a/reference/C/EXAMPLES/exec_one.c b/reference/C/EXAMPLES/exec_one.c new file mode 100644 index 0000000..9409aa7 --- /dev/null +++ b/reference/C/EXAMPLES/exec_one.c @@ -0,0 +1,13 @@ +/**************************************************************** + * + * Purpose: Program to demonstrate the 'execl' function. + * Author: M J Leslie + * Date: 21-Apr-96 + * + ****************************************************************/ + + +main(int argc, char *argv[]) +{ + execl("exec_two", "exec_two", (char *) 0); +} diff --git a/reference/C/EXAMPLES/exec_two.c b/reference/C/EXAMPLES/exec_two.c new file mode 100644 index 0000000..d1d48b5 --- /dev/null +++ b/reference/C/EXAMPLES/exec_two.c @@ -0,0 +1,14 @@ +/**************************************************************** + * + * Purpose: This program is executed by exec_one to demonstrate + * the execl function. + * Author: M J Leslie + * Date: 21-Apr-96 + * + ****************************************************************/ + + +main(int argc, char *argv[]) +{ + printf("This is %s\n", argv[0]); +} diff --git a/reference/C/EXAMPLES/exponent.c b/reference/C/EXAMPLES/exponent.c new file mode 100644 index 0000000..9967d08 --- /dev/null +++ b/reference/C/EXAMPLES/exponent.c @@ -0,0 +1,46 @@ +/************************************************************************** + * + * Purpose: Give the exponent of a number. + * Author: M. J. Leslie + * Date: 05-Apr-94 + * + **************************************************************************/ + +#include <stdio.h> + +int exponent(int); + +main(int argc,char *argv[]) +{ + int user_val; + char *progname; + + progname=argv[0]; + /* have we got one command line + parameter? */ + if (argc != 2) + { + printf("%s syntax is:\n", progname); + printf("\t%s num - where num is the number you ", progname); + printf("require the exponent of.\n"); + exit(); + } + /* y. Put it in a suitable variable */ + user_val = atoi(argv[1]); + /* get and O/P its exponent. */ + printf(" The exponent of %d is %d \n", user_val, exponent(user_val)); +} + +/************************************************************************ + Get the exponent of an integer number. +*************************************************************************/ + +int exponent(int input) +{ + int count, result=1; + /* 'result *= count' means 'result = result * count' + to fortran programmers... */ + for(count=1; count<=input; count++) result *= count; + + return result; +} diff --git a/reference/C/EXAMPLES/extern1.c b/reference/C/EXAMPLES/extern1.c new file mode 100644 index 0000000..d7d2eca --- /dev/null +++ b/reference/C/EXAMPLES/extern1.c @@ -0,0 +1,21 @@ +/************************************************************************ + * + * Purpose: Part one of a two part example showing the + * extern keyword in action. + * + * Author: M. J. Leslie + * + * Date: 24-Oct-95 + * + ************************************************************************/ + +void write_extern(void); + +extern int count; + +void write_extern(void) +{ + printf("count is %i\n", count); +} + + diff --git a/reference/C/EXAMPLES/extern2.c b/reference/C/EXAMPLES/extern2.c new file mode 100644 index 0000000..78d3f3e --- /dev/null +++ b/reference/C/EXAMPLES/extern2.c @@ -0,0 +1,19 @@ +/************************************************************************ + * + * Purpose: Part two of a two part example showing the + * extern keyword in action. + * + * Author: M. J. Leslie + * + * Date: 24-Oct-95 + * + ************************************************************************/ + +int count=5; + +main() +{ + write_extern(); +} + + diff --git a/reference/C/EXAMPLES/fact1.c b/reference/C/EXAMPLES/fact1.c new file mode 100644 index 0000000..0e3e2d2 --- /dev/null +++ b/reference/C/EXAMPLES/fact1.c @@ -0,0 +1,28 @@ +/************************************************************************** + * + * Purpose: generate the factorial of a number by iteration. + * Author: M. J. Leslie + * Date: 09-Apr-94 + * + **************************************************************************/ + +#include <stdio.h> + +int factorial(int num); + +main() +{ + int num; + puts ("This program will return the factorial of a number."); + printf("Please enter the number ==> " ); + scanf("%d", &num); + + printf(" %d! is %d\n",num, factorial(num) ); +} + +int factorial(int num) +{ + int count, ans=1; + for (count=1 ; count <= num; count++ ) ans *= count; + return ans; +} diff --git a/reference/C/EXAMPLES/fact2.c b/reference/C/EXAMPLES/fact2.c new file mode 100644 index 0000000..1c8a219 --- /dev/null +++ b/reference/C/EXAMPLES/fact2.c @@ -0,0 +1,29 @@ +/*************************************************************************** + * + * Purpose: generate the factorial of a number by recursion + * Author: M. J. Leslie + * Date: 09-Apr-94 + * + ***************************************************************************/ + +#include <stdio.h> + +int factorial(int num); + +main() +{ + int num; + puts ("This program will return the factorial of a number."); + printf("Please enter the number ==> " ); + scanf("%d", &num); + + printf(" %d! is %d\n",num, factorial(num) ); +} + +int factorial(int num) + { + int ans=1; + if (num == 1 ) return; + ans = num * factorial(num-1); + return ans; +} diff --git a/reference/C/EXAMPLES/fgets.c b/reference/C/EXAMPLES/fgets.c new file mode 100644 index 0000000..b87fc8e --- /dev/null +++ b/reference/C/EXAMPLES/fgets.c @@ -0,0 +1,28 @@ +/******************************************************************** + * + * Purpose: Program to demonstrate the 'fgets' function. + * The prog will count the number of lines in a file. + * The is a function of the UNIX command 'wc' + * Author: M J Leslie + * Date: 10-Apr-94 + * + ********************************************************************/ + +#include <stdio.h> + +#define LINE_LENGTH 80 + +main() +{ + FILE* fp; + char line[LINE_LENGTH]; + int count=0; + + fp=fopen("/home/DOC/C/c.html","r"); + /* Count up the lines here. */ + while ( fgets(line, LINE_LENGTH, fp) != NULL) count++; + + printf("File contains %d lines.\n", count); + + fclose(fp); +} diff --git a/reference/C/EXAMPLES/first.c b/reference/C/EXAMPLES/first.c new file mode 100644 index 0000000..9806f68 --- /dev/null +++ b/reference/C/EXAMPLES/first.c @@ -0,0 +1,14 @@ +/****************************************************************** + * + * Purpose: Show the basic structure of a C program + * Author: M J Leslie + * Date: 03-Feb-94 + * + ******************************************************************/ + +#include <stdio.h> + +main() +{ + puts ("your first C program"); +} diff --git a/reference/C/EXAMPLES/for1.c b/reference/C/EXAMPLES/for1.c new file mode 100644 index 0000000..87e8202 --- /dev/null +++ b/reference/C/EXAMPLES/for1.c @@ -0,0 +1,23 @@ +/************************************************************************** + * + * Purpose: Program to demonstrate the 'for' statement. + * Author: M J Leslie + * Date: 26/01/94 + * + **************************************************************************/ + +#include <stdio.h> + +main() +{ + int i; /* Define an integer */ + /* + * i=1 is executed the first time into the loop. + * i<=10 is then tested, if true, the block is executed. + * ++1 is the increment, before i<=10 is retested. + */ + for (i=1; i<=10; ++i) + { + printf ("loop counter = %i\n", i); + } +} diff --git a/reference/C/EXAMPLES/for2.c b/reference/C/EXAMPLES/for2.c new file mode 100644 index 0000000..8095113 --- /dev/null +++ b/reference/C/EXAMPLES/for2.c @@ -0,0 +1,37 @@ +/************************************************************************ + * + * Purpose: Program to demonstrate the 'for' statement. + * Author: M J Leslie + * Date: 08/04/94 + * + ************************************************************************/ + +#include <stdio.h> + +main() +{ + int i,j; /* Define integers */ + + /* 'i' and 'j' get initalised on the 'for'. + * Then they both are incremented and decremented + * before 'i' is tested. + */ + for (i=1, j=10; i<=10; ++i, --j) + { + printf (" i = %02d j = %02d\n", i, j); + } +} +/************************************************************************ + O/P will look like this: + + i = 01 j = 10 + i = 02 j = 09 + i = 03 j = 08 + i = 04 j = 07 + i = 05 j = 06 + i = 06 j = 05 + i = 07 j = 04 + i = 08 j = 03 + i = 09 j = 02 + i = 10 j = 01 +**************************************************************************/ diff --git a/reference/C/EXAMPLES/fork1.c b/reference/C/EXAMPLES/fork1.c new file mode 100644 index 0000000..daab203 --- /dev/null +++ b/reference/C/EXAMPLES/fork1.c @@ -0,0 +1,32 @@ +/**************************************************************** + * + * Purpose: Basic example of fork. + * Author: M J Leslie + * Date: 01 Apr 96 + * + ****************************************************************/ + +#include <unistd.h> + +main() +{ + + pid_t ForkPID; + + printf("Program start. \n"); + + ForkPID = fork(); /* Create a child and copy the parents + * parent data space, heap and stack. + */ + + /* ForkPID == -1 Fork failure. + * == 0 This is the child process. + * > 0 This is the parent process. The number given is the + * PID of the child. + */ + + printf("forkPID is %d \n", ForkPID); /* Child execution starts here. */ + + printf("Program end. \n"); +} + diff --git a/reference/C/EXAMPLES/fork2.c b/reference/C/EXAMPLES/fork2.c new file mode 100644 index 0000000..77eded1 --- /dev/null +++ b/reference/C/EXAMPLES/fork2.c @@ -0,0 +1,42 @@ +/**************************************************************** + * + * Purpose: Basic example of fork. + * Author: M J Leslie + * Date: 01 Apr 96 + * + ****************************************************************/ + +#include <unistd.h> + +int Value = 5; +main() +{ + + pid_t ForkPID; + + printf("Program start. \n"); + + ForkPID = fork(); /* Create a child and copy the parents + * parent data space, heap and stack. + */ + + /* ForkPID == -1 Fork failure. + * == 0 This is the child process. + * > 0 This is the parent process. The number given is the + * PID of the child. + */ + + if (ForkPID == 0) + { + Value += 4; + } + else + { + sleep (5); + } + printf("Value is %d\n", Value); + printf("forkPID is %d \n", ForkPID); + + printf("Program end. \n"); +} + diff --git a/reference/C/EXAMPLES/fprintf.c b/reference/C/EXAMPLES/fprintf.c new file mode 100644 index 0000000..1d86776 --- /dev/null +++ b/reference/C/EXAMPLES/fprintf.c @@ -0,0 +1,32 @@ +/************************************************************************ + * + * Purpose: To show fprintf in action. + * Author: M J Leslie + * Date: 15-May-96 + * Use: The program takes data from STDIN and sends it to a file. + * An example use would be. + * + * cat /etc/hosts | fprintf + * + ************************************************************************/ + +#include <stdio.h> + +main() +{ + + FILE *Ptr; + + char Line[256]; + + /* ... Open a file for output. */ + + Ptr = fopen("/tmp/OutputFile", "w"); + + while(gets(Line)) /* Get data from stdin */ + { + fprintf(Ptr, "%s\n", Line); /* Send data to file. */ + } + + fclose(Ptr); +} diff --git a/reference/C/EXAMPLES/funcpt1.c b/reference/C/EXAMPLES/funcpt1.c new file mode 100644 index 0000000..b73031a --- /dev/null +++ b/reference/C/EXAMPLES/funcpt1.c @@ -0,0 +1,40 @@ + +/************************************************************************ + * + * Purpose: 1. Define a pointer to a function. + * 2. Point at a function. + * 3. Execute the function. + * + * Author: M.J. Leslie + * + * Date: 04-Jun-95 + * + ************************************************************************/ + +int (*fpointer)(void); /* Define a pointer to a function */ + +int func1(void); /* Define a few functions. */ +int func2(void); + +main() +{ + fpointer = func1; /* Put the address of 'func1' in 'fpointer' */ + fpointer(); /* Execute 'func1' */ + + fpointer = func2; /* Repeat for 'func2' */ + fpointer(); +} + +/************************************************************************/ + +int func1(void) +{ + puts("martin was ere"); +} + +/************************************************************************/ + +int func2(void) +{ + puts("alex was ere"); +} diff --git a/reference/C/EXAMPLES/funcpt2.c b/reference/C/EXAMPLES/funcpt2.c new file mode 100644 index 0000000..6b04130 --- /dev/null +++ b/reference/C/EXAMPLES/funcpt2.c @@ -0,0 +1,40 @@ + +/************************************************************************ + * + * Purpose: 1. Define a pointer to a function with parameters. + * 2. Point at the function. + * 3. Execute the function passing parameters to it. + * + * Author: M.J. Leslie + * + * Date: 04-Jun-95 + * + ************************************************************************/ + +int (*fpointer)(int, int); /* Define a pointer to a function */ + +int add(int, int); /* Define a few functions. */ +int sub(int, int); + +main() +{ + fpointer = add; /* Put the address of 'add' in 'fpointer' */ + printf("%d \n", fpointer(4, 5)); /* Execute 'add' and print results */ + + fpointer = sub; /* Repeat for 'sub' */ + printf("%d \n", fpointer(6, 2)); +} + +/************************************************************************/ + +int add(int a, int b) +{ + return(a + b); +} + +/************************************************************************/ + +int sub(int a, int b) +{ + return(a - b); +} diff --git a/reference/C/EXAMPLES/funcpt3.c b/reference/C/EXAMPLES/funcpt3.c new file mode 100644 index 0000000..78a665c --- /dev/null +++ b/reference/C/EXAMPLES/funcpt3.c @@ -0,0 +1,54 @@ + +/************************************************************************ + * + * Purpose: 1. Define a pointer to a function with parameters. + * 2. Point at the function. + * 3. Execute the function passing char and string parameters to it. + * + * Author: M.J. Leslie + * + * Date: 04-Jun-95 + * + ************************************************************************/ + +char * (*fpointer)(char *, char); /* Define a pointer to a function */ + +char * prefix(char *, char); /* Define a few functions. */ +char * suffix(char *, char); + +main() +{ + fpointer = prefix; /* Put the address of 'add' in 'fpointer' */ + printf("%s ", fpointer("artin", 'M')); + + fpointer = suffix; /* Repeat for 'suffix' */ + printf("%s \n", fpointer("lesli", 'e')); +} + +/************************************************************************/ + +char * prefix(char *str, char c) +{ + char result[100]; + + result[0] = c; + strcpy(result+1, str); + + return(result); +} + +/************************************************************************/ + +char * suffix(char *str, char c) +{ + char result[100]; + int eol; + + strcpy(result, str); + + eol=strlen(result); + result[eol] = c; + result[eol+1] = '\0'; + + return(result); +} diff --git a/reference/C/EXAMPLES/function.c b/reference/C/EXAMPLES/function.c new file mode 100644 index 0000000..dbd0b35 --- /dev/null +++ b/reference/C/EXAMPLES/function.c @@ -0,0 +1,28 @@ +/************************************************************************ + * + * Purpose: Program to demonstrate functions. + * Author: M J Leslie. + * Date: 28-Feb-94 + * + ************************************************************************/ + +int add( int, int); /* Function declaration */ + +main() +{ + int i=1; + printf("i starts out life as %d.", i); + + i = add(1, 1); /* Function call */ + + printf(" And becomes %d after function is executed.\n", i); +} + +/************************************************************************/ + +int add( int a, int b) /* Function definition */ +{ + int c; + c = a + b; + return c; +} diff --git a/reference/C/EXAMPLES/getchar.c b/reference/C/EXAMPLES/getchar.c new file mode 100644 index 0000000..54d1448 --- /dev/null +++ b/reference/C/EXAMPLES/getchar.c @@ -0,0 +1,25 @@ +/************************************************************************ + * + * Purpose: Program to demonstrate the 'getchar' function. + * The prog will read data entered via the keyboard. And return + * the number of characters entered. + * Author: M. J. Leslie + * Date: 08-May-94 + * + ************************************************************************/ + +#include <stdio.h> + +#define RETURN '\n' /* \n == return in UNIX + \r == return in DOS */ + +main() +{ + int count=0; + puts("Please enter some text."); + /* Count the letters in the 'stdin' + buffer. */ + while ( getchar() != RETURN) count++; + + printf("You entered %d characters\n", count); +} diff --git a/reference/C/EXAMPLES/gets.c b/reference/C/EXAMPLES/gets.c new file mode 100644 index 0000000..0ca38d6 --- /dev/null +++ b/reference/C/EXAMPLES/gets.c @@ -0,0 +1,46 @@ +/************************************************************************** + * + * Purpose: Compare the action of gets and fgets reading data from STDIN. + * Author: M J Leslie + * Date: 22-Apr-95 + * + *************************************************************************/ + +#include <stdio.h> + +main() +{ + char buff[80]; /* Input buffer. */ + + /* Get data from the keyboard. */ + printf("\nplease enter text => "); + + gets(buff); + + printf("\nlength is %d #%s#\n", strlen(buff), buff); + + /* Get data from the keyboard. */ + printf("\nplease enter text => "); + + fgets(buff, sizeof(buff), stdin); + + printf("\nlength is %d #%s#\n\n",strlen(buff), buff); +} + +/************************************************************************* + * + * Example program run. + * -------------------- + * + * please enter text => abcd + * + * length is 4 #abcd# + * + * please enter text => abcd + * + * length is 5 #abcd + * # + * + *************************************************************************/ + + diff --git a/reference/C/EXAMPLES/global.c b/reference/C/EXAMPLES/global.c new file mode 100644 index 0000000..6ed7ca2 --- /dev/null +++ b/reference/C/EXAMPLES/global.c @@ -0,0 +1,28 @@ +/************************************************************************ + * + * Purpose: Demonstrate global and internal variables + * Author: M J Leslie + * date: 02-Feb-94 + * + ************************************************************************/ + +int counter = 0; /* global because we are outside + all blocks. */ +int func(void); + +main() +{ + counter++; /* global because it has not been + declared within this block */ + printf("counter is %2d before the call to func\n", counter); + + func(); /* call a function. */ + + printf("counter is %2d after the call to func\n", counter); +} + +int func(void) +{ + int counter = 10; /* local. */ + printf("counter is %2d within func\n", counter); +} diff --git a/reference/C/EXAMPLES/goto.c b/reference/C/EXAMPLES/goto.c new file mode 100644 index 0000000..812c41a --- /dev/null +++ b/reference/C/EXAMPLES/goto.c @@ -0,0 +1,36 @@ +/************************************************************************ + * + * Purpose: To perform a division checking for divide by zero. + * This program demonstrates the goto statement. + * + * Author: M.J. Leslie + * Date: 11-Feb-95 + * + ************************************************************************/ + +#include <stdlib.h> + +main() +{ + char data[100]; + double num1, num2; + + printf(" Please enter a number ==> " ); + gets(data); + num1 = atof(data); + + printf(" Please enter a number ==> " ); + gets(data); + num2 = atof(data); + + /* Stop a divide by zero with + * the goto statement. */ + if ( num2 == 0.0 ) goto end_prog; + + printf(" %4.2f divided by %4.2f is %4.2f\n", num1, num2, num1/num2); + + end_prog: + printf(" Program ended\n"); + +} + diff --git a/reference/C/EXAMPLES/if.c b/reference/C/EXAMPLES/if.c new file mode 100644 index 0000000..02cfd53 --- /dev/null +++ b/reference/C/EXAMPLES/if.c @@ -0,0 +1,29 @@ +/**************************************************************** + * + * Purpose: Program to demonstrate the 'if' statement. + * Author: M J Leslie + * Date: 26-Feb-94 + * + ****************************************************************/ + +#include <stdio.h> + +main() +{ + int i=1; /* Define an integer variable. */ + + /* + * i == 1 expression is evaluated. If TRUE the + * first block is executed. + * if i == 1 is FALSE the optional else block + * is executed */ + + if (i == 1) + { + puts ("i is equal to 1\n"); + } + else + { + puts("i is NOT equal to 1"); + } +} diff --git a/reference/C/EXAMPLES/if_else_endif.c b/reference/C/EXAMPLES/if_else_endif.c new file mode 100644 index 0000000..308d929 --- /dev/null +++ b/reference/C/EXAMPLES/if_else_endif.c @@ -0,0 +1,20 @@ +/* Purpose: Demonstrate #if, #else & #endif preprocessors. + * Author: M J Leslie + * Date: 28-Jun-94 + */ + +#define SWITCH 0 + + /* The #if can only perform + * INTEGER tests!!!! + */ +#if ( SWITCH == 1 ) + #define TEXT "Carabanger\n" +#else + #define TEXT "Hip Dude\n" +#endif + +main () +{ + printf(TEXT); +} diff --git a/reference/C/EXAMPLES/ifndef.c b/reference/C/EXAMPLES/ifndef.c new file mode 100644 index 0000000..5fee1da --- /dev/null +++ b/reference/C/EXAMPLES/ifndef.c @@ -0,0 +1,12 @@ +#include <stdio.h> + + /* Create NULL if NULL was not + * created by stdio.h */ +#ifndef NULL + #define NULL '\0' +#endif + +main() +{ + printf("%c \n", NULL); +} diff --git a/reference/C/EXAMPLES/inc_dec.c b/reference/C/EXAMPLES/inc_dec.c new file mode 100644 index 0000000..76ba776 --- /dev/null +++ b/reference/C/EXAMPLES/inc_dec.c @@ -0,0 +1,54 @@ +/************************************************************************* + * + * Purpose: Demonstrate the increment and decrement operators. + * + * Author: M.J. Leslie. + * + * Date: 07-Apr-94 + * + *************************************************************************/ +main() +{ + + /* + * ++i - i incremented before i is used. + * --i - i decremented before i is used. + * j++ - j is incremented AFTER j has been used. + * j-- - j is decremented AFTER j has been used. + */ + + int i=1,j=1; + + puts("\tDemo 1"); + printf("\t%d %d\n",++i, j++); /* O/P 2 1 */ + printf("\t%d %d\n",i, j); /* O/P 2 2 */ + + i=1;j=1; + + puts("\n\tDemo 2"); + printf("\t%d \n",i=j++); /* O/P 1 */ + printf("\t%d \n",i=++j); /* O/P 3 */ + + +/************************************************************************ + + This is a GOT YA + +*************************************************************************/ + + /* Consider this code */ + + i = 0; j = 0; + + puts("\n\tDemo 3"); + if ( (i++ == 1) && (j++ == 1)) puts("Some text"); + + /* Will i and j get incremented? The answer is NO! Because + * the expression in the left of '&&' resolves to false the + * compiler does NOT execute the expression on the right and + * so 'j' does not get executed!!!!! */ + + + printf("\t%d %d\n",i, j); /* O/P 1 0 */ + +} diff --git a/reference/C/EXAMPLES/is_examp.c b/reference/C/EXAMPLES/is_examp.c new file mode 100644 index 0000000..28430e1 --- /dev/null +++ b/reference/C/EXAMPLES/is_examp.c @@ -0,0 +1,49 @@ +/************************************************************************ + * + * Purpose: Program to demonstrate the following functions: + * isalpha, isdigit, isspace. + * The same principles apply to isalnum, iscntrl, isgraph, + * islower, isprint, ispunct, isupper, isxdigit + * + * Author: M. J. Leslie + * Date: 09-Mar-94 + * + ************************************************************************/ + +#include <stdio.h> /* printf */ +#include <ctype.h> /* isalpha isdigit isspace etc */ + +#define FALSE 0 +#define TRUE 1 + + /* function declarations */ +int char_type(char); + +main() +{ + char ch; + /* get a character from the keyboard */ + printf(" Please enter a charcater => "); + ch = getc(stdin); + + char_type(ch); /* Figure out the character type */ +} + +/**************************************************************** + decide the character type. +*****************************************************************/ +int char_type(char ch) +{ + /* returns non zero if A-Z or a-z */ + if ( isalpha(ch) != FALSE) + printf("%c is an Alpha character.\n",ch); + + /* returns non zero if 0-9 */ + if ( isdigit(ch) != FALSE) + printf("%c is a numeric character.\n",ch); + + /* returns non zero if a space, CR, Tab, NL FF */ + if ( isspace(ch) != FALSE) + printf("%c is white space\n", ch); + +} diff --git a/reference/C/EXAMPLES/keyboard.c b/reference/C/EXAMPLES/keyboard.c new file mode 100644 index 0000000..61ced2c --- /dev/null +++ b/reference/C/EXAMPLES/keyboard.c @@ -0,0 +1,51 @@ +/**************************************************************************** + * + * Purpose: Reset the keyboard from line mode to character mode and + * suppress the echo when a key is entered. This allow the + * program to get a character without the user pressing ENTER. + * + * This is for UNIX systems. DOS users have getch and getche. + * Unix programers can also use curses. + * + * Author: M.J. Leslie. + * Date: 30-Mar-97 + * + ****************************************************************************/ + +void SetKeyboard (void); +void ResetKeyboard(void); + +main() +{ + char Characters[1024]; + int Inc=0; + + memset(Characters, 0, 1024); + + printf("Press a load of characters (q=exit)\n"); + + SetKeyboard(); + + for(Inc = 0; (Characters[Inc] = getchar()) != 'q'; Inc++); + + ResetKeyboard(); + + printf("You Pressed %s\n", Characters); +} + +/* Set the keyboard. */ + +void SetKeyboard(void) +{ + system("stty raw -echo"); +} + +/* Return the keyboard to its original state. If this is not called + * before the program ends, the keyboard will remain in its reconfigured + * state. + */ + +void ResetKeyboard(void) +{ + system("stty -raw echo"); +} diff --git a/reference/C/EXAMPLES/kill.c b/reference/C/EXAMPLES/kill.c new file mode 100644 index 0000000..d54f41a --- /dev/null +++ b/reference/C/EXAMPLES/kill.c @@ -0,0 +1,16 @@ + + +main() +{ + int pid; + + /* ... Get the process ID of this program. */ + + getpid(pid); + + /* ... Check to see if this program is active. */ + + printf("R = %d\n", kill (pid, 0)); +} + + diff --git a/reference/C/EXAMPLES/lenstr.c b/reference/C/EXAMPLES/lenstr.c new file mode 100644 index 0000000..bf83bc0 --- /dev/null +++ b/reference/C/EXAMPLES/lenstr.c @@ -0,0 +1,37 @@ +/************************************************************************ + * + * Purpose: Find the length of a string. + * This duplicates the strlen function found in string.h + * Author: M J Leslie + * Date: 20-Mar-94 + * + ************************************************************************/ + +#include <stdio.h> + +int lenstr(const char *text); + +main() +{ + char kb_buf[80]; /* Keyboard buffer */ + + puts(" Please enter some text "); + printf("Text is %d characters long.\n", lenstr(gets(kb_buf))); +} + +/************************************************************************ + * + * Find the length of a string - does not check if we fall off + * the end of the text buffer. oops. + * + ************************************************************************/ + +int lenstr(const char *text) +{ + int count=-1; /* Character counter */ + + while(text[++count] != '\0') ; /* Serach for a null */ + + return(count); /* Return the position + * of the NULL-1 */ +} diff --git a/reference/C/EXAMPLES/linklst1.c b/reference/C/EXAMPLES/linklst1.c new file mode 100644 index 0000000..8e91b61 --- /dev/null +++ b/reference/C/EXAMPLES/linklst1.c @@ -0,0 +1,231 @@ + +/************************************************************************ + * + * Purpose: To demonstrate 'linked lists' This program will build a + * linked list and place data into it. When the data is exausted + * the contents of the list are O/P. + * + * This is a "First in First out" (FIFO) list. + * + * Author: M. J. Leslie + * + * Date: 11-May-95 + * + ************************************************************************/ + +#include <stdlib.h> /* malloc */ + +/************************************************************************/ + +struct x { /* Declare a structure */ + char name[20]; + int age; + struct x *next_rec; + }; + +/************************************************************************/ + +main() +{ + struct x *start_pointer; /* Define pointers to the structure */ + struct x *next_pointer; + + /* Create some data to be placed in the + * Linked list. */ + char *names[]= + { + "Martin", + "John ", + "Alex ", + "" + }; + + int ages[]={32, 43, 29, 0}; + + int count=0; /* General purpose counter. */ + + /*===================================================================* + = = + = Build a LINKED LIST and place data into it. = + = = + *===================================================================*/ + + /* Initalise 'start_pointer' by reserving + * memory and pointing to it */ + + start_pointer=(struct x *) malloc (sizeof (struct x)); + + /* Initalise 'next_pointer' to point + * to the same location. */ + next_pointer=start_pointer; + + /********************************************************************* + * + * start_pointer --- + * | + * | + * V + * ----------------- + * | | | | + * ----------------- + * A + * | + * | + * next_pointer --- + * + *********************************************************************/ + + /* Put some data into the reserved + * memory. */ + + strcpy(next_pointer->name, names[count]); + next_pointer->age = ages[count]; + + /********************************************************************* + * + * start_pointer --- + * | + * | + * V + * ----------------- + * |Martin | 32 | | + * ----------------- + * A + * | + * | + * next_pointer --- + * + *********************************************************************/ + + + /* Loop until all data has been read */ + + while ( ages[++count] != 0 ) + { + /* Reserve more memory and point to it */ + + next_pointer->next_rec=(struct x *) malloc (sizeof (struct x)); + + + /****************************************************************** + * + * start_pointer --- + * | + * | + * V + * ----------------- + * |Martin | 32 | | | + * ---------------|- + * A | + * | V + * | --------------- + * next_pointer --- | | + * --------------- + * + ******************************************************************/ + + next_pointer=next_pointer->next_rec; + + /****************************************************************** + * + * start_pointer --- + * | + * | + * V + * ----------------- + * |Martin | 32 | | | + * ---------------|- + * | + * V + * --------------- + * next_pointer --------------->| | + * --------------- + * + ******************************************************************/ + + strcpy(next_pointer->name, names[count]); + next_pointer->age = ages[count]; + + /****************************************************************** + * + * start_pointer --- + * | + * | + * V + * ----------------- + * |Martin | 32 | | | + * ---------------|- + * | + * V + * ---------------- + * next_pointer --------------->|John | 43 | | + * ---------------- + * + ******************************************************************/ + + } + + next_pointer->next_rec=NULL; + + /*===================================================================* + = = + = Traverse the linked list and O/P all the data within it. = + = = + *===================================================================*/ + + + next_pointer=start_pointer; + + /****************************************************************** + * + * start_pointer --- + * | + * | + * V + * ----------------- + * |Martin | 32 | | | + * ---------------|- + * A | + * | V + * | ---------------- + * next_pointer --- |John | 43 | | + * ---------------- + * + ******************************************************************/ + + while (next_pointer != NULL) + { + printf("%s ", next_pointer->name); + printf("%d \n", next_pointer->age); + next_pointer=next_pointer->next_rec; + + /****************************************************************** + * + * start_pointer --- + * | + * | + * V + * ----------------- + * |Martin | 32 | | | + * ---------------|- + * | + * V + * ---------------- + * next_pointer --------------->|John | 43 | | + * ---------------- + * + ******************************************************************/ + } + +} + +/************************************************************************ + * + * Program results. + * + * Martin 32 + * John 43 + * Alex 29 + * + ************************************************************************/ + diff --git a/reference/C/EXAMPLES/linklst2.c b/reference/C/EXAMPLES/linklst2.c new file mode 100644 index 0000000..d019454 --- /dev/null +++ b/reference/C/EXAMPLES/linklst2.c @@ -0,0 +1,112 @@ + +/************************************************************************ + * + * Purpose: To demonstrate 'linked lists' This program will build a + * linked list and place data into it. When the data is exausted + * the contents of the list are O/P. + * + * This is a "First in First out" (FIFO) list. + * + * Author: M. J. Leslie + * + * Date: 11-May-95 + * + ************************************************************************/ + +#include <stdlib.h> /* malloc */ + +/************************************************************************/ + +struct x { /* Declare a structure */ + char name[20]; + int age; + struct x *next_rec; +}; + +/************************************************************************/ + +main() +{ + struct x *start_pointer; /* Define pointers to the structure */ + struct x *next_pointer; + + /* Create some data to be placed in the + * Linked list. */ + char *names[]= + { + "Martin", + "John ", + "Alex ", + "" + }; + + int ages[]={32, 43, 29, 0}; + + int count=0; /* General purpose counter. */ + + /*===================================================================* + = = + = Build a LINKED LIST and place data into it. = + = = + *===================================================================*/ + + /* Initalise 'start_pointer' by reserving + * memory and pointing to it */ + + start_pointer=(struct x *) malloc (sizeof (struct x)); + + /* Initalise 'next_pointer' to point + * to the same location. */ + next_pointer=start_pointer; + + /* Put some data into the reserved + * memory. */ + + strcpy(next_pointer->name, names[count]); + next_pointer->age = ages[count]; + + + /* Loop until all data has been read */ + + while ( ages[++count] != 0 ) + { + /* Reserve more memory and point to it */ + + next_pointer->next_rec=(struct x *) malloc (sizeof (struct x)); + + next_pointer=next_pointer->next_rec; + + + strcpy(next_pointer->name, names[count]); + next_pointer->age = ages[count]; + } + + next_pointer->next_rec=NULL; + + /*===================================================================* + = = + = Traverse the linked list and O/P all the data within it. = + = = + *===================================================================*/ + + + next_pointer=start_pointer; + + while (next_pointer != NULL) + { + printf("%s ", next_pointer->name); + printf("%d \n", next_pointer->age); + next_pointer=next_pointer->next_rec; + } +} + +/************************************************************************ + * + * Program results. + * + * Martin 32 + * John 43 + * Alex 29 + * + ************************************************************************/ + diff --git a/reference/C/EXAMPLES/linklst3.c b/reference/C/EXAMPLES/linklst3.c new file mode 100644 index 0000000..78e01ad --- /dev/null +++ b/reference/C/EXAMPLES/linklst3.c @@ -0,0 +1,115 @@ + +/************************************************************************ + * + * Purpose: To demonstrate 'linked lists' This program will build a + * linked list and place data into it. When the data is exausted + * the contents of the list are O/P. + * This example shows the use of 'typedef' on linked lists. + * + * This is a "First in First out" (FIFO) list. + * + * Author: M. J. Leslie + * + * Date: 11-May-95 + * + ************************************************************************/ + +#include <stdlib.h> /* malloc */ + +/************************************************************************/ + + /* Declare a structure and give it a + * data type name with 'typdef' */ +typedef struct x { + char name[20]; + int age; + struct x *next_rec; +} linklist; + +/************************************************************************/ + +main() +{ + linklist *start_pointer; /* Define pointers to the structure */ + linklist *next_pointer; + + /* Create some data to be placed in the + * Linked list. */ + char *names[]= + { + "Martin", + "John ", + "Alex ", + "" + }; + + int ages[]={32, 43, 29, 0}; + + int count=0; /* General purpose counter. */ + + /*===================================================================* + = = + = Build a LINKED LIST and place data into it. = + = = + *===================================================================*/ + + /* Initalise 'start_pointer' by reserving + * memory and pointing to it */ + + start_pointer=(linklist *) malloc (sizeof (linklist)); + + /* Initalise 'next_pointer' to point + * to the same location. */ + next_pointer=start_pointer; + + /* Put some data into the reserved + * memory. */ + + strcpy(next_pointer->name, names[count]); + next_pointer->age = ages[count]; + + + /* Loop until all data has been read */ + + while ( ages[++count] != 0 ) + { + /* Reserve more memory and point to it */ + + next_pointer->next_rec=(linklist *) malloc (sizeof (linklist)); + + next_pointer=next_pointer->next_rec; + + + strcpy(next_pointer->name, names[count]); + next_pointer->age = ages[count]; + } + + next_pointer->next_rec=NULL; + + /*===================================================================* + = = + = Traverse the linked list and O/P all the data within it. = + = = + *===================================================================*/ + + + next_pointer=start_pointer; + + while (next_pointer != NULL) + { + printf("%s ", next_pointer->name); + printf("%d \n", next_pointer->age); + next_pointer=next_pointer->next_rec; + } +} + +/************************************************************************ + * + * Program results. + * + * Martin 32 + * John 43 + * Alex 29 + * + ************************************************************************/ + diff --git a/reference/C/EXAMPLES/ljust1.c b/reference/C/EXAMPLES/ljust1.c new file mode 100644 index 0000000..88d1329 --- /dev/null +++ b/reference/C/EXAMPLES/ljust1.c @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <ctype.h> + +void ljust(char *String); + +main() +{ + char Str[]=" Martin "; + + puts("Function to left justify text."); + + printf("*%s*\n", Str); + + ljust(Str); + + printf("*%s*\n", Str); +} + +/*****************************************************************/ + +void ljust( + char *String) +{ + int Pos; + + /* ... Keep looping until a non space + ... character appears in the first byte. */ + + while(isspace(String[0])) + { + /* ... Move all the characters left one place. */ + + for(Pos=0; Pos < strlen(String)-1; Pos++) + { + String[Pos] = String[Pos+1]; + } + + String[strlen(String)-1] = ' '; + } +} diff --git a/reference/C/EXAMPLES/macro.c b/reference/C/EXAMPLES/macro.c new file mode 100644 index 0000000..05fe12c --- /dev/null +++ b/reference/C/EXAMPLES/macro.c @@ -0,0 +1,22 @@ +/**************************************************************** + * + * Purpose: To demonstrate macros + * Author: M.J. Leslie + * Date: 17-Oct-94 + * + ****************************************************************/ + +#define SQUARE(x) x*x + +main() +{ + int value=3; + + printf("%d \n", SQUARE(value)); +} +/**************************************************************** + * + * answer will be 9 + * + ****************************************************************/ + diff --git a/reference/C/EXAMPLES/malloc.c b/reference/C/EXAMPLES/malloc.c new file mode 100644 index 0000000..7c082b6 --- /dev/null +++ b/reference/C/EXAMPLES/malloc.c @@ -0,0 +1,14 @@ +/************************************************************************ + * + * Purpose: + * Author: M J Leslie + * Date: 26-Jan-94 + * + ************************************************************************/ + +#include <stdlib.h> + +main() +{ + +} diff --git a/reference/C/EXAMPLES/modulo.c b/reference/C/EXAMPLES/modulo.c new file mode 100644 index 0000000..7d10986 --- /dev/null +++ b/reference/C/EXAMPLES/modulo.c @@ -0,0 +1,24 @@ + +/************************************************************************ + * + * Purpose: Program to demonstrate the '%' operator. + * Author: M J Leslie + * Date: 19-Mar-94 + * + ************************************************************************/ + +#include <stdio.h> + +main() +{ + int i,j; + /* The modulo is the remainder of + after an integer division */ + + printf("14 modulo 7 (14%%7) is %i\n", 14%7); /* answer is 0 */ + printf("15 modulo 7 (15%%7) is %i\n", 15%7); /* answer is 1 */ + + i=10; j=6; + + printf("%i modulo %i (%i%%%i) is %i\n",i,j,i,j,i%j); /* answer is 4 */ +} diff --git a/reference/C/EXAMPLES/or.c b/reference/C/EXAMPLES/or.c new file mode 100644 index 0000000..1541830 --- /dev/null +++ b/reference/C/EXAMPLES/or.c @@ -0,0 +1,41 @@ +/**************************************************************************** + * + * Purpose: To show the effect of a bitwise OR (|) by converting + * an ASCII character to its graphic extension equivalent. + * Author: M.J. Leslie. + * Date: 04-Mar-95 + * + ****************************************************************************/ + +main() +{ + /* Hex 66 (f) looks like this in binary + * + * 0110 0110 + */ + unsigned char value='\x66'; + + /* Hex 80 looks like this in binary + * + * 1000 0000 + */ + + printf("%2X %2X \n", value, (value | (unsigned char)'\x80')); + + /* A bitwise OR has the effect of + * adding wanted bits. + * + * 0110 0110 (66) OR + * 1000 0000 (80) + * --------- + * 1110 0110 (E6) + */ +} + +/**************************************************************************** + * + * Program results are: + * + * 66 E6 + * + ****************************************************************************/ diff --git a/reference/C/EXAMPLES/password.c b/reference/C/EXAMPLES/password.c new file mode 100644 index 0000000..c0d91f6 --- /dev/null +++ b/reference/C/EXAMPLES/password.c @@ -0,0 +1,33 @@ +/************************************************************************ + * + * Purpose: To read and verify a password. + * Compile: gcc password.c -lcurses + * Notes: To read text from the keyboard on a UNIX system without + * it being echoed to the screen you can use noecho() and + * getch() from curses.h In DOS you can use getch() in conio.h + * Author: M. J. Leslie + * Date: 12-Mar-94 + * + ************************************************************************/ + +#include <curses.h> + +main() +{ + int i; + char buffer[80]; /* work buffer */ + + initscr(); /* initialize the screen */ + + printw("Please enter a password => "); /* update screen image */ + refresh(); /* Update screen with screen image */ + noecho(); /* Suppress echo to the screen */ + /* Read characters until C/R */ + while((buffer[i] = getch()) != '\n') i++; + + printw("\nPassword is %s - press return to continue.",buffer); + refresh(); + getch(); + + endwin(); /* Shut down curses */ +} diff --git a/reference/C/EXAMPLES/perl2.c b/reference/C/EXAMPLES/perl2.c new file mode 100644 index 0000000..edbde2e --- /dev/null +++ b/reference/C/EXAMPLES/perl2.c @@ -0,0 +1,106 @@ +/******************************************************************** + * + * Date: 11-May-97 + * + * Purpose: Imbed PERL code in C. + * + * Notes: This code is based on the examples found in the + * Perl man pages. + * + * Compile: To compile this code you will need a starement simular too: + * + * g++ perl2.c -L/usr/lib/perl5/i586-linux/5.003/CORE + * -I/usr/lib/perl5/i586-linux/5.003/CORE + * -lperl + * -lm + * + * This program uses the C++ compiler as there seem to be problems + * With the header files on Linux when using GCC. + * + ********************************************************************/ + +#include <stdio.h> +#include <EXTERN.h> +#include <perl.h> + +/********************************************************************/ + +PerlInterpreter *my_perl; + +void PerlInit(char **env); +int PerlEval(char *string); +void PerlEnd (void); + +/********************************************************************/ + +main ( + int argc, + char **argv, + char **env) +{ + unsigned int Length; + + // ... Initalise. + + PerlInit(env); + + // ... Some basic Perl statements + + PerlEval("$a = 3;"); + PerlEval("$a = $a+2;"); + + // ... Extract an integer perl value into the C code. + + printf("a = %d\n", SvIV(perl_get_sv("a", FALSE))); + + PerlEval("$a = $a+2.5;"); + + // ... Extract a float perl value into the C code. + + printf("a = %f5.2\n", SvNV(perl_get_sv("a", FALSE))); + + // ... Play with strings. + + PerlEval("$a = 'rekcaH lreP rehtonA tsuJ';"); + PerlEval("$a = reverse($a); "); + + // ... Extract a string perl value into the C code. + + printf("a = %s\n", SvPV(perl_get_sv("a", FALSE), Length)); + + printf("%d\n", Length); + + // ... Tidy up. + + PerlEnd(); +} + +/********************************************************************/ + +void PerlInit( char **env) +{ + char *Embedding[] = { "", "-e", "sub _eval_ { eval $_[0] }" }; + + my_perl = perl_alloc(); + perl_construct( my_perl ); + + perl_parse(my_perl, NULL, 3, Embedding, env); +} + +/********************************************************************/ + +int PerlEval(char *string) +{ + char *argv[2]; + argv[0] = string; + argv[1] = NULL; + perl_call_argv("_eval_", 0, argv); +} + +/********************************************************************/ + +void PerlEnd(void) +{ + perl_destruct(my_perl); + perl_free(my_perl); +} diff --git a/reference/C/EXAMPLES/pipe1.c b/reference/C/EXAMPLES/pipe1.c new file mode 100644 index 0000000..7ddd1bf --- /dev/null +++ b/reference/C/EXAMPLES/pipe1.c @@ -0,0 +1,93 @@ +/**************************************************************** + * + * Purpose: Basic example of pipe. + * Read and write fixed length records across a pipe. + * This is about a simple as they come... + * + * Author: M J Leslie + * + * Date: 17 Apr 96 + * + ****************************************************************/ + +#include <sys/types.h> +#include <unistd.h> /* pipe. */ +#include <signal.h> + +void Child (pid_t Handle); +void Parent (pid_t Handle); + +main() +{ + + pid_t Pid; + int fd[2]; + + pipe(fd); /* Create two file descriptors */ + + Pid = fork(); + + if ( Pid == 0) /* Child */ + { + close(fd[0]); + Child(fd[1]); + puts("Child end"); + } + else /* Parent. */ + { + close(fd[1]); + Parent(fd[0]); + puts("Parent end"); + } +} + +/**************************************************************** + * + * The Child sends data to the parent. + * + ****************************************************************/ + +void Child(pid_t Handle) +{ + char Buff[]="Martin 1 abcdefghijklmnop "; + + write(Handle, Buff, strlen(Buff)+1); + + Buff[7] = '2'; + write(Handle, Buff, strlen(Buff)+1); + + Buff[7] = '3'; + write(Handle, Buff, strlen(Buff)+1); + + Buff[7] = '4'; + write(Handle, Buff, strlen(Buff)+1); + + close(Handle); +} + +/**************************************************************** + * + * Read the data sent by the child. + * + ****************************************************************/ + + +void Parent(pid_t Handle) +{ + + char Buff[50]; + + /* ... Read EXACTLY the number of bytes sent. + ... 0 is returned when the pipe is closed by the child. */ + + while (read(Handle,Buff, 27) > 0) + { + printf("%s\n", Buff); + } + +} + + + + + diff --git a/reference/C/EXAMPLES/pipe2.c b/reference/C/EXAMPLES/pipe2.c new file mode 100644 index 0000000..726c5e1 --- /dev/null +++ b/reference/C/EXAMPLES/pipe2.c @@ -0,0 +1,103 @@ +/**************************************************************** + * + * Purpose: Basic example of pipe. + * Read and write variable length records across a pipe. + * + * Author: M J Leslie + * + * Date: 17 Apr 96 + * + ****************************************************************/ + +#include <sys/types.h> +#include <unistd.h> /* pipe. */ +#include <signal.h> + +void Child (pid_t Handle); +void Parent (pid_t Handle); + +main() +{ + + pid_t Pid; + int fd[2]; + + pipe(fd); /* Create two file descriptors */ + + Pid = fork(); + + if ( Pid == 0) /* Child */ + { + close(fd[0]); + Child(fd[1]); + puts("Child end"); + } + else /* Parent */ + { + close(fd[1]); + Parent(fd[0]); + puts("Parent end"); + } +} + +/**************************************************************** + * + * The Child sends data to the parent. + * + ****************************************************************/ + +void Child(pid_t Handle) +{ + int Len; + + char Buff[50]="Bass Beer"; + + Len = strlen(Buff)+1; + write(Handle, &Len, sizeof(Len)); + write(Handle, Buff, Len); + + strcpy(Buff, "Wild times."); + Len = strlen(Buff)+1; + write(Handle, &Len, sizeof(Len)); + write(Handle, Buff, Len); + + strcpy(Buff, "Alex was ere."); + Len = strlen(Buff)+1; + write(Handle, &Len, sizeof(Len)); + write(Handle, Buff, Len); + + strcpy(Buff, "Bon Jovi rules the world."); + Len = strlen(Buff)+1; + write(Handle, &Len, sizeof(Len)); + write(Handle, Buff, Len); + + close(Handle); +} + +/**************************************************************** + * + * Read the data sent by the child. + * + ****************************************************************/ + +void Parent(pid_t Handle) +{ + + int Len; + char Buff[50]; + + /* ... Perform two reads. THe first gets the length of the data + ... the second gets the actual data. */ + + while (read(Handle, &Len, sizeof(Len)) > 0) + { + read(Handle,Buff, Len); + printf("%s\n", Buff); + } + +} + + + + + diff --git a/reference/C/EXAMPLES/pointer1_func.c b/reference/C/EXAMPLES/pointer1_func.c new file mode 100644 index 0000000..69abe37 --- /dev/null +++ b/reference/C/EXAMPLES/pointer1_func.c @@ -0,0 +1,36 @@ +/************************************************************************ + * + * Purpose: Program to demonstrate passing a pointer to an array + * to a function. + * Author: M J Leslie. + * Date: 14-Apr-94 + * + ************************************************************************/ +#define I_SIZE 2 + +void add(int*); /* Function declaration */ + +main() + { + int i[I_SIZE]={4,6}; /* array declaration */ + int count=0; + + for (count=0;count<I_SIZE;count++) + printf("i starts out life as %d.\n", i[count]); + + add(i); /* Function call */ + + for (count=0;count<I_SIZE;count++) \ + printf(" i is now %d.\n", i[count]); + + } + +/************************************************************************/ + +void add(int *ptr) /* Function definition */ + { + ++*ptr; /* Add 1 to the first element in + the array */ + ++*(ptr+1); /* And the second element */ + return; + } diff --git a/reference/C/EXAMPLES/pointer2_func.c b/reference/C/EXAMPLES/pointer2_func.c new file mode 100644 index 0000000..19ca3d2 --- /dev/null +++ b/reference/C/EXAMPLES/pointer2_func.c @@ -0,0 +1,51 @@ +/* + * Purpose: Program to demonstrate passing an int array to a function. + * Author: M J Leslie. + * Date: 14-Apr-94 + */ + +void add(int swap[3][2]); /* Function declaration */ +void display(int array[3][2]); /* Function declaration */ + +main() +{ + int i[3][2]= + { + {1,2}, /* array declaration */ + {3,4}, + {5,6} + }; + + display(i); /* i is a pointer */ + + add(i); + + display(i); +} + +/***************************************************************/ + +void add(int swap[3][2]) /* Function definition */ +{ + int temp,i; + + for (i=0; i<3; i++) + { + temp = swap[i][0]; + swap[i][0] = swap[i][1]; + swap[i][1] = temp; + } + return; +} + +/***************************************************************/ + +void display(int array[3][2]) /* Function definition */ +{ + int count=0,count1=0; + + for (count=0;count<3;count++) + for (count1=0;count1<2;count1++) + printf("%d ", array[count][count1]); + puts(""); +} diff --git a/reference/C/EXAMPLES/pointer3_func.c b/reference/C/EXAMPLES/pointer3_func.c new file mode 100644 index 0000000..b507810 --- /dev/null +++ b/reference/C/EXAMPLES/pointer3_func.c @@ -0,0 +1,29 @@ +/* + * Purpose: Program to demonstrate passing apointer to a character + * string to a function. + * Author: M J Leslie. + * Date: 21-Apr-94 + */ + +int function1(char * array); + +main() +{ + char array1[10]="987654321"; /* one less so the \0 will fit */ + + function1(array1); /* call function */ + + printf("%s\n", array1); /* O/P the altered array. '5' will + * have been changed to 'x' */ +} + +/************************************************************************/ + +function1(char * array) +{ + printf("%s\n", array); /* printf expects a pointer. */ + + array +=4; /* Modify the pointer. */ + *array = 'x'; /* Modify the data pointed to + * by 'array' */ +} diff --git a/reference/C/EXAMPLES/pointer4_func.c b/reference/C/EXAMPLES/pointer4_func.c new file mode 100644 index 0000000..37c14b1 --- /dev/null +++ b/reference/C/EXAMPLES/pointer4_func.c @@ -0,0 +1,21 @@ +int set(char *items[]); + +main() +{ + char *items[]={"apple", "pear", "banana", "grape"}; + + set (items); + +} + +int set(char *items[]) +{ + printf ("/t%s\n", items[2]); +} + +/********************************************************************** + * + * Result is + * banana + * + **********************************************************************/ diff --git a/reference/C/EXAMPLES/pointer_func.c b/reference/C/EXAMPLES/pointer_func.c new file mode 100644 index 0000000..37e1bfd --- /dev/null +++ b/reference/C/EXAMPLES/pointer_func.c @@ -0,0 +1,35 @@ +/************************************************************************ + * + * Purpose: Program to demonstrate passing a pointer to a scalar to + * a function. + * Author: M J Leslie. + * Date: 11-Apr-94 + * + ************************************************************************/ + +void add(int*); /* Function declaration */ + +main() +{ + int i=4; /* variable declaration */ + int* ptr; /* int pointer */ + + ptr = &i; /* 'ptr' now contains the + address of 'i' */ + + printf("i starts out life as %d.\n", i); + printf(" *ptr is %d.\n", *ptr); + + add(ptr); /* Function call */ + + printf(" i is now %d.\n", i); +} + +/************************************************************************/ + +void add(int *ptr) /* Function definition */ +{ + ++*ptr; /* Add 1 to the value + * pointed too by 'ptr' */ + return; +} diff --git a/reference/C/EXAMPLES/popen.c b/reference/C/EXAMPLES/popen.c new file mode 100644 index 0000000..4df7a5c --- /dev/null +++ b/reference/C/EXAMPLES/popen.c @@ -0,0 +1,25 @@ +/* + * Purpose: Program to demonstrate the popen function. + * + * to do: Check that the 'popen' was successfull. + * + * Author: M J Leslie. + * Date: 08-Jan-94 +*/ + +#include <stdio.h> + +main() +{ + FILE *fp; + char line[130]; /* line of data from unix command*/ + + fp = popen("ls -l", "r"); /* Issue the command. */ + + /* Read a line */ + while ( fgets( line, sizeof line, fp)) + { + printf("%s", line); + } + pclose(fp); +} diff --git a/reference/C/EXAMPLES/printenv.c b/reference/C/EXAMPLES/printenv.c new file mode 100644 index 0000000..fc7f9da --- /dev/null +++ b/reference/C/EXAMPLES/printenv.c @@ -0,0 +1,24 @@ +/****************************************************************** + * + * Purpose: Program to demonstrate the 'environ' variable + * by emulating the Unix printenv command. + * Date: 21-May-96 + * Author: M J Leslie. + * Descrip: 'environ' is declared in stdlib.h and points to + * a list of environmental variables. + * + ******************************************************************/ + +#include <stdlib.h> + +main() +{ + int Count; + char *Line; + + while(Line = *(environ+Count)) + { + printf("%s \n", Line); + Count++; + } +} diff --git a/reference/C/EXAMPLES/prog_name.c b/reference/C/EXAMPLES/prog_name.c new file mode 100644 index 0000000..fcf18c9 --- /dev/null +++ b/reference/C/EXAMPLES/prog_name.c @@ -0,0 +1,19 @@ +/************************************************************************ + * + * Purpose: Display the name of the program. + * To do: Stip any directory information away so we are left with + * with the file name only. + * Author: M.J.Leslie + * Date: 09-Jan-95 + * + ************************************************************************/ + +main(int argc, char *argv[]) +{ + char *prog=argv[0]; + + /* prog_name points to the same location + * as argv[0] + */ + printf("Program is called %s\n", prog); +} diff --git a/reference/C/EXAMPLES/qsort1.c b/reference/C/EXAMPLES/qsort1.c new file mode 100644 index 0000000..b078ab4 --- /dev/null +++ b/reference/C/EXAMPLES/qsort1.c @@ -0,0 +1,87 @@ +/************************************************************************ + * + * Purpose: To sort numbers held in an array. + * Author: M.J.Leslie + * Date: 30-Nov-94 + * + ************************************************************************/ + +/********** Preprocessor ***********************************************/ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +/********** Functions ***************************************************/ + +void display_nums(int *, int); +int comp_nums(const int *, const int *); + +/********** main ********************************************************/ + +main() +{ + int numbers[]= /* Numbers to be sorted. */ + {43,76,23,1,100,56,23,99,33,654}; + + int how_many=10; /* Number of numbers entered */ + + + puts("\nThese are the unsorted numbers\n"); + display_nums(numbers, how_many); + + /* SORT the numbers held in + * 'numbers'. */ + qsort( + numbers, /* Pointer to elements */ + how_many, /* Number of elements */ + sizeof(int), /* size of one element. */ + (void *)comp_nums /* Pointer to comparison function */ + ); + + puts("\nThese are the sorted numbers\n"); + display_nums(numbers, how_many); +} + +/************************************************************************ + * + * comp_nums: Compare two numbers. + * + ************************************************************************/ + +int comp_nums(const int *num1, const int *num2) +{ + if (*num1 < *num2) return -1; + if (*num1 == *num2) return 0; + if (*num1 > *num2) return 1; +} + +/************************************************************************ + * + * display_nums: Display the numbers + * + ************************************************************************/ + +void display_nums(int *array, int count) +{ + /* Print all the elements in + * the array. */ + while ( count-- ) + { + printf("%d ",*array); + array++; + } + puts(""); +} + +/******* The Results **************************************************** + * + * These are the unsorted numbers + * + * 43 76 23 1 100 56 23 99 33 654 + * + * These are the sorted numbers + * + * 1 23 23 33 43 56 76 99 100 654 + * + ************************************************************************/ diff --git a/reference/C/EXAMPLES/qsort2.c b/reference/C/EXAMPLES/qsort2.c new file mode 100644 index 0000000..d169261 --- /dev/null +++ b/reference/C/EXAMPLES/qsort2.c @@ -0,0 +1,107 @@ +/************************************************************************ + * + * Purpose: To read numbers entered by the user and sort them. + * Author: M.J.Leslie + * Date: 30-Nov-94 + * + ************************************************************************/ + +/********** Preprocessor ***********************************************/ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +/********** Functions ***************************************************/ + +int read_nums(int *); +void display_nums(int *); +int comp_nums(const int *, const int *); + +/********** main ********************************************************/ + +main() +{ + int numbers[30]; /* Numbers to be sorted. */ + int how_many=0; /* Number of numbers entered */ + + /* Get the numbers */ + how_many = read_nums(numbers); + + puts("\nThese are the unsorted numbers\n"); + display_nums(numbers); + + /* SORT the numbers held in + * 'numbers'. */ + qsort( + numbers, /* Pointer to elements */ + how_many, /* Number of elements */ + sizeof(int), /* size of one element. */ + (void *)comp_nums /* Pointer to comparison function */ + ); + + puts("\nThese are the sorted numbers\n"); + display_nums(numbers); +} + +/************************************************************************ + * + * comp_nums: Compare two numbers. + * + ************************************************************************/ + +int comp_nums(const int *num1, const int *num2) +{ + if (*num1 < *num2) return -1; + if (*num1 == *num2) return 0; + if (*num1 > *num2) return 1; +} + +/************************************************************************ + * + * readnums: read numbers from the keyboard and put them into an array. + * + ************************************************************************/ + +int read_nums(int *array) +{ + + char data[80]; /* Data entered by the user */ + int inc=0; + + puts("\n\tPlease enter some integer numbers"); + puts("\tno checking is made on the data you enter...."); + printf("\t==> "); + + gets(data); + /* Assume we have valid data. + * Put it into an array. */ + *(array+inc) = atoi(strtok(data," ")); + inc++; + + while ( *(array+inc) = atoi(strtok(NULL," "))) inc++; + + /* Return the number of items + * stored in 'array'. */ + return inc++; +} + +/************************************************************************ + * + * display_nums: Display the numbers + * + ************************************************************************/ + +void display_nums(int *array) +{ + /* NULL is a pointer to 0, + * so it must be cast to an + * integer */ + while ( *array != (int)NULL) + { + printf("%d ",*array); + array++; + } + + puts(""); +} diff --git a/reference/C/EXAMPLES/rand.c b/reference/C/EXAMPLES/rand.c new file mode 100644 index 0000000..2967c8a --- /dev/null +++ b/reference/C/EXAMPLES/rand.c @@ -0,0 +1,29 @@ +/********************************************************************* + * + * Purpose: Demonstrate the 'srand' and 'rand' functions + * Author: M.J. Leslie. + * Date: 11=Nov-94 + * + *********************************************************************/ + +#include <time.h> +#include <stdlib.h> + +main() +{ + int rolls=4; + /* This looks DISCUSTING! + * time returns a different value + * on every execution. And so + * changes the value passed to + * srand. See CAST + * for an explanation of + * (unsigned int) and (time_t) */ + srand((unsigned int)time((time_t *)NULL)); + + /* Roll the dice. */ + while(rolls--) + { + printf("Dice value is %d\n", (rand()%6)+1); + } +} diff --git a/reference/C/EXAMPLES/read_file.c b/reference/C/EXAMPLES/read_file.c new file mode 100644 index 0000000..721d6a1 --- /dev/null +++ b/reference/C/EXAMPLES/read_file.c @@ -0,0 +1,32 @@ +/************************************************************************ + * + * Purpose: To display a file on the screen (Unix 'cat' command). + * Version 1 + * + * to do: Read file name from command line. + * + * Author: M J Leslie + * Date: 06-Feb-94 + * + ************************************************************************/ + +#include <stdio.h> + +main() +{ + int c; /* Character read from the file. */ + FILE *ptr; /* Pointer to the file. FILE is a + structure defined in <stdio.h> */ + + /* Open the file - no error checking done */ + ptr = fopen("/etc/hosts","r"); + /* Read one character at a time, checking + for the End of File. EOF is defined + in <stdio.h> as -1 */ + while ((c = fgetc(ptr)) != EOF) + { + printf("%c",c); /* O/P the character to the screen */ + } + + fclose(ptr); /* Close the file. */ +} diff --git a/reference/C/EXAMPLES/readch.c b/reference/C/EXAMPLES/readch.c new file mode 100644 index 0000000..8287efd --- /dev/null +++ b/reference/C/EXAMPLES/readch.c @@ -0,0 +1,25 @@ +/* + * Purpose: Program to demonstrate the 'getchar' function. + * The prog will read data entered via the keyboard. And return + * the number of characters entered. + * Author: M J Leslie + * Date: 08-May-94 + */ + +#include <stdio.h> +#include <unistd.h> + +int readch(void); + +main() +{ + printf("%c",readch()+1); +} + +int readch(void) +{ + char ch; + + read(0, &ch, 1); + return (ch); +} diff --git a/reference/C/EXAMPLES/reverse.c b/reference/C/EXAMPLES/reverse.c new file mode 100644 index 0000000..9532e93 --- /dev/null +++ b/reference/C/EXAMPLES/reverse.c @@ -0,0 +1,30 @@ +/********************************************************************* + * + * Purpose: Reverse characters in a string. + * Author: K&R + * Date: + * + *********************************************************************/ + +void reverse(char s[]); + +main() +{ + char text[80]="martin"; + + printf("string is %s\n", text); + reverse(text); + printf("string is %s\n", text); +} + +void reverse(char s[]) +{ + int c, i, j; + + for (i=0, j=strlen(s)-1; i < j;i++, j--) + { + c = s[i]; + s[i] = s [j]; + s[j] = c; + } +} diff --git a/reference/C/EXAMPLES/setuid.c b/reference/C/EXAMPLES/setuid.c new file mode 100644 index 0000000..bce4a3d --- /dev/null +++ b/reference/C/EXAMPLES/setuid.c @@ -0,0 +1,47 @@ +#include <sys/types.h> +#include <unistd.h> +main() +{ + + /* Identify the person logged in (real ID). */ + + printf("getuid = %d\n", getuid()); + + /* Get the ID in use by this process (effective ID). + * Normally this is the same as above but if the program is: + * + * o Owned by root (chown root progname). + * o Had 'Set owner' set in the permissions (chmod u+s progname). + * o The disk is not mounted 'nosetuid'. + * + * We can use 'setuid' and 'seteuid'. + */ + + printf("geteuid = %d\n", geteuid()); + + (!geteuid()) + ? + puts("setuid//setruid/seteuid will work.") + : + puts("setuid/setruid/seteuid will fail."); + + /* 0 = OK + * -1 = Fail + */ +/* + printf("seteuid return code = %d\n", seteuid(3570)); + + printf("getuid = %d\n", getuid()); + printf("geteuid = %d\n", geteuid()); + + printf("seteuid return code = %d\n", seteuid(0)); + + printf("getuid = %d\n", getuid()); + printf("geteuid = %d\n", geteuid()); +*/ + + printf("setuid return code = %d\n", setuid(4261)); + + printf("getuid = %d\n", getuid()); + printf("geteuid = %d\n", geteuid()); +} diff --git a/reference/C/EXAMPLES/single_array.c b/reference/C/EXAMPLES/single_array.c new file mode 100644 index 0000000..70a3761 --- /dev/null +++ b/reference/C/EXAMPLES/single_array.c @@ -0,0 +1,30 @@ +int function1(char* array); + +main() +{ + /* The array MUST be initalised + otherwise the pointer will not + be defined. */ + /* We have one less so the \0 + will fit */ + char array1[10]="987654321"; + + function1(array1); /* Pass the pointer to the array */ + + printf("%s\n", array1); /* O/P the altered array. */ + +} + +/************************************************************************/ + +function1(char * array) +{ + /* Pass a pointer to the string.*/ + printf("%s\n", array); + + array +=4; /* Modify the pointer. */ + *array = 'x'; /* Modify the data pointed to + by 'array' */ + +} + diff --git a/reference/C/EXAMPLES/sizeof.c b/reference/C/EXAMPLES/sizeof.c new file mode 100644 index 0000000..869bd5b --- /dev/null +++ b/reference/C/EXAMPLES/sizeof.c @@ -0,0 +1,63 @@ +/**************************************************************************** + * + * Purpose: Find out the size of the different data types + * Author: M J Leslie + * Date: 27-feb-94 + * + ****************************************************************************/ + +#include <stdio.h> +main() +{ + /* + * int/char are 'type specifiers' + * short/long are 'type qualifiers' + */ + int i; + + printf(" short int is %2d bytes \n", sizeof(short int)); + printf(" int is %2d bytes \n", sizeof(int)); + printf(" int * is %2d bytes \n", sizeof(int *)); + printf(" long int is %2d bytes \n", sizeof(long int)); + printf(" long int * is %2d bytes \n", sizeof(long int *)); + printf(" signed int is %2d bytes \n", sizeof(signed int)); + printf(" unsigned int is %2d bytes \n", sizeof(unsigned int)); + printf("\n"); + printf(" float is %2d bytes \n", sizeof(float)); + printf(" float * is %2d bytes \n", sizeof(float *)); + printf(" double is %2d bytes \n", sizeof(double)); + printf(" double * is %2d bytes \n", sizeof(double *)); + printf(" long double is %2d bytes \n", sizeof(long double)); + printf("\n"); + printf(" signed char is %2d bytes \n", sizeof(signed char)); + printf(" char is %2d bytes \n", sizeof(char)); + printf(" char * is %2d bytes \n", sizeof(char *)); + printf("unsigned char is %2d bytes \n", sizeof(unsigned char)); +} + + +/************************************************************* + * + * The O/P from this program is + * + * short int is 2 bytes + * int is 4 bytes + * int * is 4 bytes + * long int is 4 bytes + * long int * is 4 bytes + * signed int is 4 bytes + * unsigned int is 4 bytes + * + * float is 4 bytes + * float * is 4 bytes + * double is 8 bytes + * double * is 4 bytes + * long double is 12 bytes + * + * signed char is 1 bytes + * char is 1 bytes + * char * is 4 bytes + * unsigned char is 1 bytes + * + * + **************************************************************/ diff --git a/reference/C/EXAMPLES/sizeof1.c b/reference/C/EXAMPLES/sizeof1.c new file mode 100644 index 0000000..b8ff882 --- /dev/null +++ b/reference/C/EXAMPLES/sizeof1.c @@ -0,0 +1,29 @@ +/*************************************************************** + * + * Purpose: Find out the size of the different data objects + * Author: M J Leslie + * Date: 03-Sep-94 + * + ***************************************************************/ + +#include <stdio.h> + + +main() +{ + char array[10]; + struct s + { + int a; + float b; + } structure; + + + printf(" array is %i\n", sizeof array); + printf(" struct is %i\n", sizeof structure); +} + +/***************************************************************** + * array is 10 + * struct is 8 + *****************************************************************/ diff --git a/reference/C/EXAMPLES/sprintf.c b/reference/C/EXAMPLES/sprintf.c new file mode 100644 index 0000000..2009529 --- /dev/null +++ b/reference/C/EXAMPLES/sprintf.c @@ -0,0 +1,28 @@ +/*********************************************************************** + * + * Purpose: Program to demonstrate the 'sprintf' statement. This example + * is a bit lame as the same effect can be seen with a 'printf'. + * But, it does show a string being built and passed into a + * function. + * Author: M J Leslie + * Date: 10-Jul-94 + * + ************************************************************************/ + +#include <stdio.h> + +main() +{ + int i=1; /* Define an integer variable. */ + char message[80]; /* Text string */ + + /* format text and put into 'message' this a great + * improvement over using 'strcpy' and 'strcat' to + * build a text string. + */ + sprintf (message, "i is %i", i); + /* I may be stating the obvious but a '\0' is + * put on the end of the string. */ + + puts(message); /* Display message */ +} diff --git a/reference/C/EXAMPLES/sprintf1.c b/reference/C/EXAMPLES/sprintf1.c new file mode 100644 index 0000000..85a8163 --- /dev/null +++ b/reference/C/EXAMPLES/sprintf1.c @@ -0,0 +1,25 @@ +/*********************************************************************** + * + * Purpose: Program to demonstrate the * flag limiting a string filed width + * in a 'sprintf' statement. + * + * This example shows an alternative way of performing a strncpy. + * + * Author: M J Leslie + * Date: 18-Mar-98 + * + ************************************************************************/ + +#include <stdio.h> + +main() +{ + char Dest[10] = ""; + char Src[] = "Ski on Whistler Mountain"; + /* Populate Dest */ + memset (Dest, 0, sizeof(Dest)); + sprintf (Dest, "%.*s", sizeof(Dest)-1, Src); + /* Show the results. */ + printf("Src string : %s\n", Src); + printf("Dest string: %s\n", Dest); +} diff --git a/reference/C/EXAMPLES/sscanf.c b/reference/C/EXAMPLES/sscanf.c new file mode 100644 index 0000000..672a152 --- /dev/null +++ b/reference/C/EXAMPLES/sscanf.c @@ -0,0 +1,31 @@ + +#include <stdio.h> + +main() +{ + char Host[64]; + char User[64]; + char *Buff = "Jobname=job1 Hostname=arnamul User=leslim Time=11:15"; + /* <----------> <-----> <---------> <--------> + * | | | | + * | ------------ | | + * | | ------------------ V + * | | | NULL + * V V V */ + sscanf (Buff, "%*s Hostname=%s %s", Host, User); + + printf("Host is %s\n", Host); + printf("User is %s\n", User); + exit(0); +} + +/************************************************************************ + * + * Job Output is: + * + * Host is arnamul + * User is User=leslim + * + ************************************************************************/ + + diff --git a/reference/C/EXAMPLES/stat.c b/reference/C/EXAMPLES/stat.c new file mode 100644 index 0000000..6afeb8c --- /dev/null +++ b/reference/C/EXAMPLES/stat.c @@ -0,0 +1,87 @@ +/************************************************************************ + * + * Purpose: Program to demonstrate the 'stat' function. + * The program will show the modification and access times + * plus the size of a file. + * + * Author: M J Leslie + * + * Date: 01-Jun-95 MJL Minor bug fixed. + * 13-Apr-95 MJL Inital Release + * + * Notes: This function is only available to Unix systems. If you + * are on a DOS machine, this will not compile. + * + * Compile: Change the #define (below) to a suitable file name on your + * system. The file name is not passed on the command line + * as I did not want to complicate the example. + * + ************************************************************************/ + + +#include <sys/stat.h> /* declare the 'stat' structure */ +#include <sys/types.h> +#include <unistd.h> +#include <stdio.h> /* printf */ +#include <time.h> + +#define FILENAME "martin" /* PUT YOUR FILE NAME HERE */ + +/************************************************************************/ + +char * format_time(time_t cal_time); + +void file_stat(char * filename); + +/************************************************************************/ + +main() +{ + file_stat(FILENAME); +} + +/************************************************************************/ + +void file_stat(char * filename) +{ + struct stat stat_p; /* 'stat_p' is a pointer to a structure + * of type 'stat'. */ + + /* Get stats for file and place them in + * the structure. */ + if ( -1 == stat (filename, &stat_p)) + { + printf(" Error occoured attempting to stat %s\n", filename); + exit(0); + } + /* Print a few structure members. */ + + printf("Stats for %s \n", filename); + + printf("Modify time is %s", format_time(stat_p.st_mtime)); + + /* Access time does not get updated + if the filesystem is NFS mounted! */ + + printf("Access time is %s", format_time(stat_p.st_atime)); + + printf("File size is %d bytes\n", stat_p.st_size); +} + +/************************************************************************/ + +char * format_time(time_t cal_time) +{ + struct tm *time_struct; + static char string[30]; + /* Put the calendar time into a structure + * if type 'tm'. */ + time_struct=localtime(&cal_time); + + /* Build a formatted date from the + * structure. */ + strftime(string, sizeof string, "%h %e %H:%M\n", time_struct); + + /* Return the date/time */ + return(string); +} diff --git a/reference/C/EXAMPLES/statfs.c b/reference/C/EXAMPLES/statfs.c new file mode 100644 index 0000000..f5a41a8 --- /dev/null +++ b/reference/C/EXAMPLES/statfs.c @@ -0,0 +1,117 @@ + +/************************************************************************ + * + * Purpose: Program to demonstrate the 'statfs' function. + * The program will show the number of free blocks + * on the /tmp filesystem. + * + * Author: M J Leslie + * + * Date: 09-Nov-97 MJL Inital Release + * + * Notes: This function is only available on older unix systems. + * SVR4 systems replaced statfs with statvfs. + * + ************************************************************************/ + +#include <sys/types.h> +#include <sys/vfs.h> + +#define UNDEF -1 +#define OK 0 +#define FAIL 1 + +void GetFileSystemStat( + const char *Path, /* I Path to a file on the file system to be queried */ + long *BSize, /* O Size of blocks on this file system */ + long *Blocks, /* O Total no. of blocks on this file system */ + long *BFree, /* O No. of blocks still available to an ordinary user */ + long *Files, /* O Maximum no. of files possible on this file system */ + long *FFree, /* O No. of free file nodes (ie max - currently allocated) */ + int *Status); /* O Returned status of this function call: + OK File system parameters found + FAIL Unable to determine file system + parameters */ + +main() +{ + long BSize; + long Blocks; + long BFree; + long Files; + long FFree; + int Status; + + GetFileSystemStat("/tmp", /* I Path to a file on the file system to be queried */ + &BSize, /* O Size of blocks on this file system */ + &Blocks, /* O Total no. of blocks on this file system */ + &BFree, /* O No. of blocks still available to an ordinary user */ + &Files, /* O Maximum no. of files possible on this file system */ + &FFree, /* O No. of free file nodes (ie max - currently allocated) */ + &Status); /* O REturned status of this function call: + OK File system parameters found + FAIL */ + + printf("Free Blocks = %d\n", BFree); + +} + + +/* PUBLIC FUNCTION + ******************************************************************************** + ** + ** mos_GetFileSystemStat + ** + ** DESCRIPTION + ** + ** Gets information on the file-system on which a particular file resides + ** + ** IMPLEMENTATION NOTES + ** + ** On some file systems (eg NFS mounted partitions) it may not be possible + ** to determine all the required parameters. In these cases the returned + ** value will be set to MOS_UNDEF. + ** + ******************************************************************************** +*/ + +void GetFileSystemStat( + const char *Path, /* I Path to a file on the file system to be queried */ + long *BSize, /* O Size of blocks on this file system */ + long *Blocks, /* O Total no. of blocks on this file system */ + long *BFree, /* O No. of blocks still available to an ordinary user */ + long *Files, /* O Maximum no. of files possible on this file system */ + long *FFree, /* O No. of free file nodes (ie max - currently allocated) */ + int *Status) /* O returned status of this function call: + OK File system parameters found + FAIL Unable to determine file system + parameters */ + +{ + struct statfs FSBuf; /* structure to contain file system information */ + + int locStatus; /* local status value */ + + locStatus = statfs(Path, &FSBuf); + + if (locStatus != 0) + { + *BSize = UNDEF; + *Blocks = UNDEF; + *BFree = UNDEF; + *Files = UNDEF; + *FFree = UNDEF; + + *Status = FAIL; + } + else + { + *BSize = FSBuf.f_bsize; + *Blocks = FSBuf.f_blocks; + *BFree = FSBuf.f_bavail; + *Files = FSBuf.f_files; + *FFree = FSBuf.f_ffree; + + *Status = OK; + } +} diff --git a/reference/C/EXAMPLES/static.c b/reference/C/EXAMPLES/static.c new file mode 100644 index 0000000..7017397 --- /dev/null +++ b/reference/C/EXAMPLES/static.c @@ -0,0 +1,47 @@ +/* + * Author: M.J Leslie. + * Purpose: To demonstrate the 'static' storage class + */ + +void func1(void); + +static count=10; /* Global variable - static is the default */ + +main() +{ + while (count--) func1(); + +} + +/***************************************************************************/ + +void func1(void) +{ + /* 'thingy' is local to 'func1' - it is + * only initalised at run time. Its value + * is NOT reset on every invocation of + * 'func1' + */ + static thingy=5; + thingy++; + printf(" thingy is %d and count is %d\n", thingy, count); +} + + +/************************************************************************** + +Program )/P looks like this: + + thingy is 6 and count is 9 + thingy is 7 and count is 8 + thingy is 8 and count is 7 + thingy is 9 and count is 6 + thingy is 10 and count is 5 + thingy is 11 and count is 4 + thingy is 12 and count is 3 + thingy is 13 and count is 2 + thingy is 14 and count is 1 + thingy is 15 and count is 0 + +**************************************************************************/ + diff --git a/reference/C/EXAMPLES/stdin.c b/reference/C/EXAMPLES/stdin.c new file mode 100644 index 0000000..9ceadb9 --- /dev/null +++ b/reference/C/EXAMPLES/stdin.c @@ -0,0 +1,30 @@ +/************************************************************************ + * + * Purpose: To read data fron 'stdin' (by default the keyboard) + * + * Author: M. J. Leslie + * + * Date: 01-Sep-95 + * + ************************************************************************/ + +#include <stdio.h> + +void read_stdin(void); + +main() +{ + read_stdin(); +} + +/************************************************************************/ + +void read_stdin(void) +{ + char line[256]; + + while (gets(line)) + { + printf("%s\n", line); + } +} diff --git a/reference/C/EXAMPLES/strchr.c b/reference/C/EXAMPLES/strchr.c new file mode 100644 index 0000000..37bfde3 --- /dev/null +++ b/reference/C/EXAMPLES/strchr.c @@ -0,0 +1,38 @@ +/************************************************************************* + * + * Purpose: To find a # in a string. If found, it is removed along all text + * to the right of it. + * Author: Dave Doolin + * Date: 03-Jun-95 + * + * + * Notes: This is NOT a complete program. It is provided as a function + * for inclusion into your code. + * + *************************************************************************/ + +#include <stdio.h> +#include <string.h> /* For the `strchr' synonym for `index'. */ + + +/* + * FIND_COMMENT will replace the octothorpe ("#") with a null character to + * get rid of comments in a line. Later need to add `stopchar' for more + * flexibility, instead of just using "#". Need also to find a way to + * collapse initial spaces and tabs, in case I want to clean up the line as + * well. + */ + +void find_comment(char *tline) +{ + char *comment; + + comment = strchr(tline, '#'); + if (comment != 0) /* If STRCHR returns an address for # then */ + *comment = '\0'; /* stick a null character at that address. */ + + comment = strchr(tline, '\n');/* Replace \n with \0 for consistency. */ + if (comment != 0) + *comment = '\0'; +} + diff --git a/reference/C/EXAMPLES/strcmp.c b/reference/C/EXAMPLES/strcmp.c new file mode 100644 index 0000000..ba36a6a --- /dev/null +++ b/reference/C/EXAMPLES/strcmp.c @@ -0,0 +1,57 @@ +/****************************************************************** + * + * Purpose: Program to demonstrate the use of strcmp. + * Date: 05-Dec-96 + * Author: M J Leslie. + * Descrip: The standard strcmp returns 0 when the strings match + * and indicates which string is lexigraphically greater. + * Every time I have used strcmp, I have never been interested + * in which string is greater and always frustrated that the + * return code is inverted. This function tidys things up. + * + ******************************************************************/ + +#include <string.h> +#include <stdio.h> + +#define TRUE 1 +#define FALSE 0 + +int StringCompare(char *s1, char *s2); + +main() +{ + char One[] = "Bartman"; + char Two[] = "Batman"; + + int Ret; + + Ret = StringCompare(One, Two); + + if (Ret == TRUE) + { + puts("The Strings match"); + } + else + { + puts("The Strings do not match"); + } +} + +/**************************************************************/ + +int StringCompare(char *s1, char *s2) +{ + int Ret; + + if (strcmp(s1, s2)) + { + Ret = 0; + } + else + { + Ret = 1; + } + + return (Ret); +} diff --git a/reference/C/EXAMPLES/strcpy.c b/reference/C/EXAMPLES/strcpy.c new file mode 100644 index 0000000..3580e9f --- /dev/null +++ b/reference/C/EXAMPLES/strcpy.c @@ -0,0 +1,25 @@ +/* + * Purpose: Program to demonstrate the 'strcpy' function. + * Author: M J Leslie + * Date: 12-Mar-94 +*/ + +#include <string.h> /* strcpy */ + +main() +{ + char text1[20]="martin"; /* string buffer */ + char text2[20]="leslie"; /* string buffer */ + + printf (" original string contents are: %s\n", text1); + /* Copy text2 into text1. + If text1 is smaller that text2 + it will probably overwrite + something! */ + strcpy(text1, text2); + printf (" new string contents are: %s\n", text1); + + strcpy(text1, "linux"); + printf (" final string contents are: %s\n", text1); + +} diff --git a/reference/C/EXAMPLES/strncpy.c b/reference/C/EXAMPLES/strncpy.c new file mode 100644 index 0000000..c6c0054 --- /dev/null +++ b/reference/C/EXAMPLES/strncpy.c @@ -0,0 +1,48 @@ + +/**************************************************************** + * + * Purpose: Program to demonstrate the 'strncpy' function. + * Author: M J Leslie + * Date: 03-Feb-96 + * + ****************************************************************/ + +#include <string.h> /* strcpy */ + +void SafeCopy(char *Dest, int DestSize, char *Source); + +main() +{ + char Text1[20]="Tracy Sorrell"; /* string buffer */ + char Text2[10]="Martin"; /* string buffer */ + + printf (" Original string contents are: %s\n", Text2); + + SafeCopy(Text2, sizeof(Text2), Text1); + + printf (" New string contents are: %s\n", Text2); + + strcpy(Text2, "Alex"); + + printf (" Final string contents are: %s\n", Text2); + +} + +/****************************************************************/ + +void SafeCopy( + char *Dest, /* Destination buffer. */ + int DestSize, + char *Source) /* Source data. */ +{ + + /* ... Copy 'Source' into 'Dest'. + * ... 'Dest' is padded with NULLs if 'Source' is smaller.. */ + + strncpy(Dest, Source, DestSize); + + /* ... Safety net! Add the NULL just in case 'Source' is larger + * ... than 'Dest'. */ + + Dest[DestSize-1] = '\0'; +} diff --git a/reference/C/EXAMPLES/strpbrk.c b/reference/C/EXAMPLES/strpbrk.c new file mode 100644 index 0000000..9c33f7a --- /dev/null +++ b/reference/C/EXAMPLES/strpbrk.c @@ -0,0 +1,48 @@ + +/* + * field.c + * + * Dave Doolin 12 May, 1995 + * + * Turns miscellaneous field separators into just a space separating tokens for + * easy parsing by SSCANF. Eventually, the character separators and + * replacement character will be passed in as strings. + * + */ + +#include <stdio.h> +#include <string.h> +#include <strings.h> + +#define LINE_BUF 100 + +void find_comment(char *); + +main() +{ + char line[LINE_BUF]; + char *sep; + int var1, var2; + + while (fgets(line, LINE_BUF, stdin)) + { + + /* + * Check this out: Since SEP is a pointer to type char, when line is + * assigned to sep, really the first address is assigned to sep. LINE + * is the address of the start of the string. In contrast, LINE[0] + * is the first character of the string. + */ + + sep = line; + + while (sep != 0) + { + sep = strpbrk(line, ";.&:,"); + if (sep != 0) + *sep = ' '; + } + fputs(line, stdout); + } + return 0; +} diff --git a/reference/C/EXAMPLES/strstr.c b/reference/C/EXAMPLES/strstr.c new file mode 100644 index 0000000..5e9a048 --- /dev/null +++ b/reference/C/EXAMPLES/strstr.c @@ -0,0 +1,23 @@ +/***************************************************************** + * + * Purpose: Program to demonstrate the 'strstr' function. + * Author: M J Leslie + * Date: 18-Jun-94 + * + *****************************************************************/ + +#include <stdio.h> +#include <string.h> + +main() +{ + char string[]="string to search"; + char test[]="sear"; + + /* strstr returns a pointer into 'string' + * if 'test' is found' if not found, NULL + * is returned. */ + + if (strstr(string, test)) puts("String found"); + +} diff --git a/reference/C/EXAMPLES/strtok.c b/reference/C/EXAMPLES/strtok.c new file mode 100644 index 0000000..23b5d30 --- /dev/null +++ b/reference/C/EXAMPLES/strtok.c @@ -0,0 +1,47 @@ +/***************************************************************** + * + * Purpose: Program to demonstrate the 'strtok' function. + * Author: M J Leslie + * Date: 23-Apr-94 + * + ****************************************************************/ + +#include <stdio.h> +#include <string.h> + +main() +{ + /* Copy the constant into the memory + * pinted to by 'test_string' */ + char test_string[50]="string to split up"; + + /* if 'test_string' is declared as below and the program will give a + * 'Segmentation fault' This is because test_string' is pointing + * to a constant i.e. somethin that cant be changed. + + char *test_string="string to split up"; */ + + char *sub_string; + + /* Extract first string */ + printf("%s\n", strtok(test_string, " ")); + + /* Extract remaining + * strings */ + while ( (sub_string=strtok(NULL, " ")) != NULL) + { + printf("%s\n", sub_string); + } +} +/***************************************************************** + * + * Program O/P will look like this... + * + * string + * to + * split + * up + * + *****************************************************************/ + + diff --git a/reference/C/EXAMPLES/strtol.c b/reference/C/EXAMPLES/strtol.c new file mode 100644 index 0000000..4fbbc16 --- /dev/null +++ b/reference/C/EXAMPLES/strtol.c @@ -0,0 +1,81 @@ +/**************************************************************** + * + * Purpose: To show examples of the strtol function. + * Author: M.J. Leslie + * Date: 06-Nov-94 + * + ****************************************************************/ + +#include <stdlib.h> + +main() +{ + char num[10]; + + /* Test a valid number */ + strcpy(num,"13"); + + printf("%s(Oct) is %i(Dec)\n", num, strtol(num, NULL, 8)); + printf("%s(Dec) is %i(Dec)\n", num, strtol(num, NULL, 10)); + printf("%s(hex) is %i(Dec)\n", num, strtol(num, NULL, 16)); + + puts("----------------------------------"); + + /* Test a slightly valid number + * Returns the same results as + * above. */ + strcpy(num, "13hzcd"); + + printf("%s(Oct) is %i(Dec)\n", num, strtol(num, NULL, 8)); + printf("%s(Dec) is %i(Dec)\n", num, strtol(num, NULL, 10)); + printf("%s(hex) is %i(Dec)\n", num, strtol(num, NULL, 16)); + + puts("----------------------------------"); + + /* Test an invalid number + * Returns ZERO */ + strcpy(num, "hzcd"); + + printf("%s(Oct) is %i(Dec)\n", num, strtol(num, NULL, 8)); + printf("%s(Dec) is %i(Dec)\n", num, strtol(num, NULL, 10)); + printf("%s(hex) is %i(Dec)\n", num, strtol(num, NULL, 16)); + + + puts("----------------------------------"); + + /* Test 0 base. + * This will look at the number + * and decide the base for its self! + */ + strcpy(num, "13"); + printf("%s is %i(Dec)\n", num, strtol(num, NULL, 0)); + + strcpy(num, "013"); + printf("%s is %i(Dec)\n", num, strtol(num, NULL, 0)); + + strcpy(num, "0x13"); + printf("%s is %i(Dec)\n", num, strtol(num, NULL, 0)); + +} + +/**************************************************************** + * + * Results of the program: + * + * 13(Oct) is 11(Dec) + * 13(Dec) is 13(Dec) + * 13(hex) is 19(Dec) + * ---------------------------------- + * 13hzcd(Oct) is 11(Dec) + * 13hzcd(Dec) is 13(Dec) + * 13hzcd(hex) is 19(Dec) + * ---------------------------------- + * hzcd(Oct) is 0(Dec) + * hzcd(Dec) is 0(Dec) + * hzcd(hex) is 0(Dec) + * ---------------------------------- + * 13 is 13(Dec) + * 013 is 11(Dec) + * 0x13 is 19(Dec) + * + ****************************************************************/ diff --git a/reference/C/EXAMPLES/struct1.c b/reference/C/EXAMPLES/struct1.c new file mode 100644 index 0000000..119c2ac --- /dev/null +++ b/reference/C/EXAMPLES/struct1.c @@ -0,0 +1,36 @@ +/************************************************************************ + * + * Purpose: Demonstrate structures. + * Author: M.J. Leslie. + * Date: 13-June-94 + * + * + ************************************************************************/ + +#include <stdio.h> + +main () +{ + /* Declare the structure. */ + struct { char name[20]; int age;} record ; + + /* Put some values into the + * structure. */ + strcpy(record.name, "Joe Brown"); + record.age = 21; + + /* Display the contents of the + * structure. */ + + printf("name is %s \nage is %d \n", record.name, record.age); +} + +/************************************************************************ + * + * Here is the result. + * + * name is Joe Brown + * age is 21 + * + ************************************************************************/ + diff --git a/reference/C/EXAMPLES/struct2.c b/reference/C/EXAMPLES/struct2.c new file mode 100644 index 0000000..e38cc7f --- /dev/null +++ b/reference/C/EXAMPLES/struct2.c @@ -0,0 +1,27 @@ +/* + * Date 13-Jun-94 + * + * Pass 'struct' elements to a function. + */ +#include <stdio.h> + +void display(char *name, int age); + +/************************************************************************/ + +main () +{ + struct { char name[20]; int age;} record; + + strcpy(record.name, "Joe Brown"); + record.age = 21; + + display (record.name, record.age); +} + +/************************************************************************/ + +void display(char *name, int age) +{ + printf("name is %s \nage is %d \n", name, age); +} diff --git a/reference/C/EXAMPLES/struct3.c b/reference/C/EXAMPLES/struct3.c new file mode 100644 index 0000000..f40e7ba --- /dev/null +++ b/reference/C/EXAMPLES/struct3.c @@ -0,0 +1,45 @@ +/************************************************************************ + * + * Purpose: Passing a copy of a structure to a function. + * Author: M.J. Leslie + * Date: 13-Jun-94 + * + ************************************************************************/ + +#include <stdio.h> + /* Declare a structure. It MUST + * be declared before its use in + * function declarations. + */ +struct record_format { char name[20]; int age;}; + +void display(struct record_format); /* function declaration */ + +/************************************************************************/ + +main () +{ + struct record_format record; + + strcpy(record.name, "Joe Brown"); + record.age = 21; + + display (record); + + /* + * Print the contents of the structure. + * The original value of record.age has been + * retained. + */ + printf("name is %s \nage is %d \n", record.name, record.age); + +} + +/************************************************************************/ + +void display(struct record_format rec) +{ + printf("name is %s \nage is %d \n", rec.name, rec.age); + rec.age=31; /* change the value of a structure + * member. */ +} diff --git a/reference/C/EXAMPLES/struct4.c b/reference/C/EXAMPLES/struct4.c new file mode 100644 index 0000000..552dcd7 --- /dev/null +++ b/reference/C/EXAMPLES/struct4.c @@ -0,0 +1,56 @@ +/************************************************************************ + * + * Purpose: Create an array of structures. + * Author: M.J. Leslie + * Date: 09-May-95 + * + ************************************************************************/ + +#include <stdio.h> + /* Declare a structure. It MUST + * be declared before use. + */ +struct record_format +{ + char name[20]; + int age; +}; + +/************************************************************************/ + +main () +{ + int count=0; + + struct record_format record[]= + { + {"Joe Brown", 21}, + {"James Dean", 34}, + {"David Williams", 54}, + {"Neil Harrison", 62}, + {"EOF", -1} + }; + + /* + * Print the contents of the structure. + */ + while( record[count].age != -1) + { + printf("name is %s \tage is %d \n", record[count].name, record[count].age); + count++; + } +} + +/************************************************************************ + * + * Program will produce: + * + * name is Joe Brown age is 21 + * name is James Dean age is 34 + * name is David Williams age is 54 + * name is Neil Harrison age is 62 + * + *************************************************************************/ + + + diff --git a/reference/C/EXAMPLES/swap.c b/reference/C/EXAMPLES/swap.c new file mode 100644 index 0000000..63ac18b --- /dev/null +++ b/reference/C/EXAMPLES/swap.c @@ -0,0 +1,26 @@ +/****************************************************************** + * + * Purpose: Program to demonstrate the use of ^ (XOR). + * Date: 03-Dec-96 + * Author: M J Leslie. + * Descrip: The contents of two variables are swapped without + * the use of a temorary variable + * + ******************************************************************/ + +#include <stdio.h> + +main() +{ + + int One = 20; + int Two = 12; + + printf("One = %d Two = %d\n", One, Two); + + One ^= Two; + Two ^= One; + One ^= Two; + + printf("One = %d Two = %d\n", One, Two); +} diff --git a/reference/C/EXAMPLES/switch.c b/reference/C/EXAMPLES/switch.c new file mode 100644 index 0000000..0e232da --- /dev/null +++ b/reference/C/EXAMPLES/switch.c @@ -0,0 +1,37 @@ +/************************************************************************** + * + * Purpose: Program to demonstrate the 'switch/case' structure. + * Method: Prog looks at the number of parameters passed to it and + * tells the user how many its got. + * Author: M J Leslie + * Date: 09-Apr-94 + * + **************************************************************************/ + +main(int argc, char *argv[]) +{ + + switch (argc) /* Switch evaluates an expression (argc) */ + { + /* If expression resolves to 1, jump here */ + case 1: + puts("Only the command was entered."); + break; /* break - cases the execution to jump + out of the 'switch' block. */ + + /* If expression resolves to 2, jump here */ + case 2: + puts("Command plus one parm entered"); + break; + + /* If expression resolves to 3, jump here */ + case 3: + puts("Command plus two parm entered"); + break; + + /* Any other value jumps here. */ + default: + printf("Command plus %d parms entered\n", argc-1); + break; + } +} diff --git a/reference/C/EXAMPLES/system.c b/reference/C/EXAMPLES/system.c new file mode 100644 index 0000000..c3c3cc7 --- /dev/null +++ b/reference/C/EXAMPLES/system.c @@ -0,0 +1,18 @@ +/************************************************************************ + * + * Purpose: To test the system function. + * + * Author: M J Leslie + * Date: 23-Oct-95 + * + ************************************************************************/ + +#include <stdlib.h> + +main() +{ + /* Execute an system command and wait + * for it to end. + * DO NOT ATTEMPT TO RUN INERACTIVE CMDS*/ + system("ls -l"); +} diff --git a/reference/C/EXAMPLES/temp.c b/reference/C/EXAMPLES/temp.c new file mode 100644 index 0000000..2bb7568 --- /dev/null +++ b/reference/C/EXAMPLES/temp.c @@ -0,0 +1,29 @@ +/* +Purpose: Find out the size of the different data types +Author: M J Leslie +Date: 27-feb-94 +*/ + +#include <stdio.h> +main() +{ + /* + int/char are 'type specifiers' + short/long are 'type qualifiers' + */ + int i; + + printf(" short int is %2d bytes \n", sizeof(short int)); + printf(" int is %2d bytes \n", sizeof(int)); + printf(" long int is %2d bytes \n", sizeof(long int)); + printf(" signed int is %2d bytes \n", sizeof(signed int)); + printf(" unsigned int is %2d bytes \n", sizeof(unsigned int)); + printf("\n"); + printf(" float is %2d bytes \n", sizeof(float)); + printf(" double is %2d bytes \n", sizeof(double)); + printf(" long double is %2d bytes \n", sizeof(long double)); + printf("\n"); + printf(" signed char is %2d bytes \n", sizeof(signed char)); + printf(" char is %2d bytes \n", sizeof(char)); + printf("unsigned char is %2d bytes \n", sizeof(unsigned char)); +} diff --git a/reference/C/EXAMPLES/time.c b/reference/C/EXAMPLES/time.c new file mode 100644 index 0000000..067318f --- /dev/null +++ b/reference/C/EXAMPLES/time.c @@ -0,0 +1,65 @@ +/************************************************************************ + * + * Purpose: To play with the time.h functions. + * + * Author: M.J. Leslie + * + * Date: 18-Mar-95 + * + ************************************************************************/ + +#include <stdio.h> /* NULL */ +#include <time.h> /* ctime, asctime */ + +main() +{ + time_t now; /* define 'now'. time_t is probably + * a typedef */ + + /* Calender time is the number of + * seconds since 1/1/1970 */ + + now = time((time_t *)NULL); /* Get the system time and put it + * into 'now' as 'calender time' */ + + printf("%s", ctime(&now)); /* Format data in 'now' + * NOTE that 'ctime' inserts a + * '\n' */ + + /*********************************************************************/ + + /* Here is another way to extract the time/date information */ + + time(&now); + + printf("%s", ctime(&now)); /* Format data in 'now' */ + + /*********************************************************************/ + + { + struct tm *l_time; + + l_time = localtime(&now); /* Convert 'calender time' to + * 'local time' - return a pointer + * to the 'tm' structure. localtime + * reserves the storage for us. */ + printf("%s", asctime(l_time)); + } + + /*********************************************************************/ + + time(&now); + printf("%s", asctime(localtime( &now ))); + + /*********************************************************************/ + + { + struct tm *l_time; + char string[20]; + + time(&now); + l_time = localtime(&now); + strftime(string, sizeof string, "%d-%b-%y\n", l_time); + printf("%s", string); + } +} diff --git a/reference/C/EXAMPLES/uname.c b/reference/C/EXAMPLES/uname.c new file mode 100644 index 0000000..8cee6cc --- /dev/null +++ b/reference/C/EXAMPLES/uname.c @@ -0,0 +1,34 @@ +/************************************************************************ + * + * Description: Try out the 'uname' function. + * Author: M.J. Leslie + * Date: 28-12-94 + * + ************************************************************************/ + +#include <sys/utsname.h> /* Header for 'uname' */ + +main() +{ + struct utsname uname_pointer; + + uname(&uname_pointer); + + printf("System name - %s \n", uname_pointer.sysname); + printf("Nodename - %s \n", uname_pointer.nodename); + printf("Release - %s \n", uname_pointer.release); + printf("Version - %s \n", uname_pointer.version); + printf("Machine - %s \n", uname_pointer.machine); + printf("Domain name - %s \n", uname_pointer.domainname); +} + +/*********************************************************************** + * + * system name - Linux + * Nodename - norma2 + * Release - 1.1.54 + * Version - #5 Sat Dec 3 15:08:21 GMT 1994 + * Machine - i486 + * Domain name - (none) + * + ***********************************************************************/ diff --git a/reference/C/EXAMPLES/union.c b/reference/C/EXAMPLES/union.c new file mode 100644 index 0000000..597d8b4 --- /dev/null +++ b/reference/C/EXAMPLES/union.c @@ -0,0 +1,5 @@ +/* + * Example to be done ..... + */ + + diff --git a/reference/C/EXAMPLES/user_name.c b/reference/C/EXAMPLES/user_name.c new file mode 100644 index 0000000..4a7421a --- /dev/null +++ b/reference/C/EXAMPLES/user_name.c @@ -0,0 +1,41 @@ + +/************************************************************************ + * + * Purpose: Get the users real name with the 'getpwuid' function. + * Notes: This is a UNIX only program. It will not work on DOS machines + * as 'getpwuid' gets its information from /etc/passwd. + * Author: M J Leslie + * Date: 13-Jan-95 + * + ************************************************************************/ + +#include <pwd.h> /* getpwuid */ +#include <sys/types.h> + +/************************************************************************/ + +void user_name(void); + +/************************************************************************/ + +main() +{ + user_name(); +} + +/************************************************************************/ + +void user_name(void) +{ + /* See the getpwuid man page + * for a description of the + * structure. */ + struct passwd *passwd; + /* Get the uid of the running + * process and use it to get + * a record from /etc/passwd + */ + passwd=getpwuid(getuid()); + + printf("Users Real name is %s\n", passwd->pw_gecos); +} diff --git a/reference/C/EXAMPLES/var_func.c b/reference/C/EXAMPLES/var_func.c new file mode 100644 index 0000000..4784d92 --- /dev/null +++ b/reference/C/EXAMPLES/var_func.c @@ -0,0 +1,56 @@ +/************************************************************************ + * + * Purpose: Program to demonstrate functions that have a variable + * number of parameters. + * Author: M J Leslie. + * Date: 28-Mar-94 + * + ************************************************************************/ + +#include <stdio.h> +#include <stdarg.h> /* va_list, va_arg, va_end */ + +int set(char *item, int num, ...); /* Declare the function. */ + +/************************************************************************/ + +main() +{ + char *item="pear"; + + int Ret; + + Ret = set (item,4, "apple", "pear", "banana", "grape"); + + if (Ret) + { + printf ("%s found\n", item); + } + else + { + printf("%s not found\n", item); + } +} + +/************************************************************************/ + +int set(char *item, int num, ...) +{ + va_list ap; /* define 'ap' It acts as a + * pointer to the undefined + * variables. */ + int Ret=0; + int Inc=0; /* Assume the worst. */ + va_start(ap, num); /* seed 'ap' */ + + do + { + if ( item == va_arg(ap, char *)) + { + Ret = 1; + } + } while ( Ret==0 && ++Inc < num); + + va_end(ap); /* tidy up. */ + return (Ret); +} diff --git a/reference/C/EXAMPLES/varlist.c b/reference/C/EXAMPLES/varlist.c new file mode 100644 index 0000000..5b6360b --- /dev/null +++ b/reference/C/EXAMPLES/varlist.c @@ -0,0 +1,43 @@ +/************************************************************************ + * + * Purpose: To show ... in action. This operator (?) allows us to pass + * a variable number of parameters to a function, 'printf' + * uses ... + * Author: M.J. Leslie + * Date: 09-Jul-94 + * + ************************************************************************/ + +#include <stdio.h> +#include <stdarg.h> /* va_list, va_arg, va_end */ + +int set(char *item, int, num, ...); + +/************************************************************************/ + +main() + { + char *item="pear"; + + (set (item,4, "apple", "pear", "banana", "grape") ) + ? printf ("%s found\n", item) : printf("%s not found\n", item); + + } + +/************************************************************************/ + +int set(char *item, int num, ...) + { + va_list ap; /* define 'ap' It acts as a pointer + * to the undefined variables. */ + int ok=0; + int inc=0; + va_start(ap, num); /* seed 'ap' */ + + do { + if ( item == va_arg(ap, char *)) ok=1; + } while ( ok==0 && ++inc < num); + + va_end(ap); /* tidy up. */ + return (ok); + } diff --git a/reference/C/EXAMPLES/while.c b/reference/C/EXAMPLES/while.c new file mode 100644 index 0000000..71230ce --- /dev/null +++ b/reference/C/EXAMPLES/while.c @@ -0,0 +1,25 @@ +/****************************************************************** + * + * Purpose: Program to demonstrate the 'while' statement. + * Date: 20-Feb-94 + * Author: M J Leslie. + * + ******************************************************************/ + +#include <stdio.h> + +main() +{ + int i=1; /* Define an integer variable. */ + + /* + * i <= 10 expression is evaluated. If TRUE the + * block is executed. + */ + + while (i <= 10) + { + printf ("i is %i\n", i); + i++; + } +} diff --git a/reference/C/EXAMPLES/whiteball.gif b/reference/C/EXAMPLES/whiteball.gif Binary files differnew file mode 100644 index 0000000..d2c09a1 --- /dev/null +++ b/reference/C/EXAMPLES/whiteball.gif |