str_wrap function
| From: | chagenbu at wso dot williams dot edu | Date: | Thu, 27 Apr 2000 19:27:13 +0000 |
| Subject: | str_wrap function | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-14112@lists.php.net to get a copy of this message | ||
I'm trying to convert some donated c code into a str_wrap function for php4.
Here's my current code:
/* {{{ proto string str_wrap(string text, int linelength, int breakchar) */
PHP_FUNCTION(str_wrap)
{
zval **ztext, **zlinelength, **zbreakchar;
char *text;
int linelength, breakchar;
int i=0, l=0, le=0;
if (ARG_COUNT(ht) != 3 || zend_get_parameters_ex(3, &ztext, &zlinelength,
&zbreakchar) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(ztext);
text = estrndup((*ztext)->value.str.val,
(*ztext)->value.str.len);
convert_to_long_ex(zlinelength);
convert_to_long_ex(zbreakchar);
linelength = Z_LVAL_PP(zlinelength);
breakchar = Z_LVAL_PP(zbreakchar);
while (text[i] != '\0') {
/* prescan line to see if it is greater than linelength */
l = 0;
while (text[i+l] != '\n') {
if (text[i+l] == '\0') break;
l++;
}
le = l;
if ((l > linelength) && (text[i+l] != '\0')) {
/* needs breaking; work backwards to find previous word */
l = linelength;
while(l > 0) {
if (text[i+l] == ' ') {
text[i+l] = breakchar;
break;
}
l--;
}
}
i += 1 + (l ? le : l);
}
RETVAL_STRINGL(text, (*ztext)->value.str.val, 1);
efree(text);
}
/* }}} */
And here's the backtrace I get when I try and run it:
Program received signal SIGSEGV, Segmentation fault.
0x400fe557 in memcpy () from /lib/libc.so.6
(gdb) bt
#0 0x400fe557 in memcpy () from /lib/libc.so.6
#1 0x4022362b in fetch_simple_variable_ex () at zend_compile.c:175
#2 0x402b4d31 in php_if_chunk_split () at string.c:870
#3 0x4022e56c in execute () at ./zend_execute.c:924
#4 0x40216d12 in cfgparse ()
at /home/chuck/code/php4/./configuration-parser.y:345
#5 0x4024686c in php_if_apache_note () at php_apache.c:114
#6 0x402471f4 in php_if_getallheaders () at php_apache.c:114
#7 0x40247242 in php_if_getallheaders () at php_apache.c:114
#8 0x8053e74 in ap_invoke_handler (r=0x80f6724) at http_config.c:508
#9 0x806271c in process_request_internal (r=0x80f6724) at http_request.c:1214
#10 0x8062778 in ap_process_request (r=0x80f6724) at http_request.c:1230
#11 0x805c509 in child_main (child_num_arg=0) at http_main.c:4122
#12 0x805c69c in make_child (s=0x8099f44, slot=0, now=956863548)
at http_main.c:4235
#13 0x805c7b9 in startup_children (number_to_start=5) at http_main.c:4317
#14 0x805cc6b in standalone_main (argc=2, argv=0xbffff6f4) at http_main.c:4605
#15 0x805d31d in main (argc=2, argv=0xbffff6f4) at http_main.c:4933
Anyone know right off the bat what I'm doing wrong/have suggestions?
-chuck