Category : Files from Magazines
Archive   : CUJ0894.ZIP
Filename : PSTRING.C

 
Output of file : PSTRING.C contained in archive : CUJ0894.ZIP
@CSOURCE6 = /*
* Module: pstring.c (string handling support)
* by [email protected]
*/
#include "pstring.h"
#include <>
#include <>
#include <>
/*
* The macros ALLOC and FREE hide away some casts
* (necessary to suppress compiler-warnings) and
* care for pre-ANSI/ISO-C free() which is not
* guaranteed to be safe for (void *)0-pointers.
*/
#define ALLOC(n, t) ((t *)malloc((n)*(sizeof(t))))
#if __STDC__ == 1
#define FREE(p) free((void *)p)
#else
#define FREE(p) if (p) free((void *)p)
#endif

/*
* Concatenate variable number of strings in space
* allocated from the heap. If no more space is
* available, the program is aborted.
*/
char *pstr_x(const char *str, ...) {
const char *cp; /* tmpry to step over args */
char *result; /* tmpry for return value */
va_list ap; /* arglist iterator */
size_t len = 1; /* total length of strs + 1 */

/*
* loop over args, sum length of all strs
*/
va_start(ap, str);
for (cp = str; cp; cp = va_arg(ap, const char *))
len += strlen(cp);
va_end(ap);
/*
* allocate space for all strs
*/
if ((result = ALLOC(len, char)) == (char *)0)
abort();
result[0] = '\0';
/*
* loop over args, concatenate all strs
*/
va_start(ap, str);
for (cp = str; cp; cp = va_arg(ap, const char *))
(void) strcat(result, cp);
va_end(ap);
return result;
}

/*
* Helper function for building temporary strings
* (see "TMPSTR"-macros in "pstring.h" for details).
* It effectively "schedules a free" delayed by one
* call to this function, so the caller does not have
* to care for saving the result of "strmake" in a
* variable and hand this to free later on.
* Note: With "(void) tmpstr((char *)0)" you may
* force to de-allocate the last temporary string
* build with one of the "TMPSTR"-macros.
*/
char *tmpstr(char *str) {
static char *laststr;
FREE(laststr);
return laststr = str;
}

/*
* He<%-2>lper function for copying to pointers initialized
<%0> * by calls to the "pstr_"-function and macros.
* N<%-2>ote: "pstrcpy" takes the ADDRESS of a char pointer<%0>
* as its first argument and automatic char pointer
* variables MUST be initialized with (char *)0 prior
* handing their address to this function the first
* time.
*/
void pstrcpy(char **destp, const char *src) {
FREE(*destp);
*destp = pstr_1(src);
}

/* End of File */



  3 Responses to “Category : Files from Magazines
Archive   : CUJ0894.ZIP
Filename : PSTRING.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/