Sponsored Content
Full Discussion: Problems with Strlen
Top Forums Programming Problems with Strlen Post 16234 by AtleRamsli on Wednesday 27th of February 2002 07:08:03 AM
Old 02-27-2002
I think you need to clarify a little more.
What kind of program is it?
What are cBuf[] and cInBuf[]?

In general, if you want to split an integer into its components for display, you must convert it to a string first
$ man sprintf

int n = 22;
int i;
char sbuf[6];
char tbuf[12];
/*
Especially when expanding a bufffer, make sure it holds 0's, otherwise strlen() will go amok
*/
memset(tbuf, 0, sizeof(tbuf));
/*
init should be done in general
*/
memset(sbuf, 0, sizeof(sbuf));

sprintf(sbuf,"%d", n);
/* sbuf[] now holds '2','2', 0 */

for (i = 0; sbuf[i]; i++)
{
tbuf[i*2] = sbuf[i]; /* for speed, you can shift here */
tbuf[i*2+1]='F';
}

/* Now, tbuf[] holds "2F2F", but somehow,
I don't think this is what you wanted?
*/


Atle
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

'make' problems (compliation problems?)

I'm trying to compile and install both most recent version of 'make' and the most recent version of 'openssh' on my Sparc20. I've run into the following problems... and I don't know what they mean. Can someone please help me resolve these issues? I'm using the 'make' version that was... (5 Replies)
Discussion started by: xyyz
5 Replies

2. Shell Programming and Scripting

Problem with the strlen function in ksh

Hello, Just a little problem with the ksh function : strlen I want to use this function in this little ksh program : while read line ; do TOTO=$line TOTONB=strlen($TOTO) echo $TOTONB (3 Replies)
Discussion started by: steiner
3 Replies

3. UNIX for Dummies Questions & Answers

Few problems

Hi how can i do this? 1) shell script which writes data and time on to a file if filesystem exceeds 70% of space. 2) make entry to cron table to run a script every 15 mins. and can anyone expplain or demonstrate the difference between variables used in inside a function and outside a... (3 Replies)
Discussion started by: vivekshankar
3 Replies

4. Programming

'strlen' of a constant string

In a declaration, I have: const char comment_begin = "<!--"; const char comment_end = "-->"; const int comment_begin_len = strlen(comment_begin); const int comment_end_len = strlen(comment_end); When I compile, I get the warnings: emhttpc.c:64: warning: initializer element is not... (10 Replies)
Discussion started by: cleopard
10 Replies

5. Programming

pointer arithmetic vs. strlen() & strnlen()?

I have been getting some flack recently for my use of strlen() and strnlen(). Honestly I have always just taken their functionality for granted as being the easiest way of getting the length of a string. Is it really so much better to do pointer arithmetic? What am I gaining besides more... (3 Replies)
Discussion started by: jjinno
3 Replies

6. UNIX for Dummies Questions & Answers

Problems with using less

Hello, I am having problems with using less on Linux version 2.6.18-92.1.17.el5 (brewbuilder@hs20-bc1-7.build.redhat.com) (gcc version 4.1.2 20071124 (Red Hat 4.1.2-42)). I am using csh but have the same problems on bash. If I pipe something to less it works perfectly i.e. cat file | less... (9 Replies)
Discussion started by: z1dane
9 Replies

7. Programming

strlen for UTF-8

My OS (Debian) and gcc use the UTF-8 locale. This code says that the char size is 1 byte but the size of 'a' is really 4 bytes. int main(void) { setlocale(LC_ALL, "en_US.UTF-8"); printf("Char size: %i\nSize of char 'a': %i\nSize of Euro sign '€': %i\nLength of Euro sign: %i\n",... (8 Replies)
Discussion started by: cyler
8 Replies

8. Shell Programming and Scripting

If/then problems

#! /bin/bash # ask what the user would like to do CMD=$CMD MBA=$MB RS=$RS CT=$CT echo echo -n "What would you like to do?? REMEMBER WHEN PROGRAMMING ICP's TO SELECT CORRECT COMMAND ACCORDING TO NECCESSARY TYPE CODE! Please enter a command ct = program ctek ... (5 Replies)
Discussion started by: tdalyman
5 Replies

9. Programming

Segment fault related to strlen.S

Hello, This function was copied into my code, which was compiled without error/warning, but when executed there is always Segmentation fault at the end after the output (which seems correct!): void get_hashes(unsigned int hash, unsigned char *in) { unsigned char *str = in; int pos =... (7 Replies)
Discussion started by: yifangt
7 Replies

10. Shell Programming and Scripting

PHP: declared variables, strlen vs isset

greetings, pretty new to php and i think i might be missing some fundamental limitation of isset. i have two php scripts below that are executed by crond, one using --host X and one that does not. and below that are three different attempts at generating a command line that will be executed. the... (8 Replies)
Discussion started by: crimso
8 Replies
SBUF(9) 						   BSD Kernel Developer's Manual						   SBUF(9)

NAME
sbuf, sbuf_new, sbuf_new_auto, sbuf_clear, sbuf_setpos, sbuf_bcat, sbuf_bcopyin, sbuf_bcpy, sbuf_cat, sbuf_copyin, sbuf_cpy, sbuf_printf, sbuf_vprintf, sbuf_putc, sbuf_trim, sbuf_overflowed, sbuf_finish, sbuf_data, sbuf_len, sbuf_done, sbuf_delete -- safe string formatting SYNOPSIS
#include <sys/types.h> #include <sys/sbuf.h> struct sbuf * sbuf_new(struct sbuf *s, char *buf, int length, int flags); struct sbuf * sbuf_new_auto(); void sbuf_clear(struct sbuf *s); int sbuf_setpos(struct sbuf *s, int pos); int sbuf_bcat(struct sbuf *s, const void *buf, size_t len); int sbuf_bcopyin(struct sbuf *s, const void *uaddr, size_t len); int sbuf_bcpy(struct sbuf *s, const void *buf, size_t len); int sbuf_cat(struct sbuf *s, const char *str); int sbuf_copyin(struct sbuf *s, const void *uaddr, size_t len); int sbuf_cpy(struct sbuf *s, const char *str); int sbuf_printf(struct sbuf *s, const char *fmt, ...); int sbuf_vprintf(struct sbuf *s, const char *fmt, va_list ap); int sbuf_putc(struct sbuf *s, int c); int sbuf_trim(struct sbuf *s); int sbuf_overflowed(struct sbuf *s); void sbuf_finish(struct sbuf *s); char * sbuf_data(struct sbuf *s); int sbuf_len(struct sbuf *s); int sbuf_done(struct sbuf *s); void sbuf_delete(struct sbuf *s); DESCRIPTION
The sbuf family of functions allows one to safely allocate, construct and release bounded null-terminated strings in kernel space. Instead of arrays of characters, these functions operate on structures called sbufs, defined in <sys/sbuf.h>. The sbuf_new() function initializes the sbuf pointed to by its first argument. If that pointer is NULL, sbuf_new() allocates a struct sbuf using malloc(9). The buf argument is a pointer to a buffer in which to store the actual string; if it is NULL, sbuf_new() will allocate one using malloc(9). The length is the initial size of the storage buffer. The fourth argument, flags, may be comprised of the following flags: SBUF_FIXEDLEN The storage buffer is fixed at its initial size. Attempting to extend the sbuf beyond this size results in an overflow con- dition. SBUF_AUTOEXTEND This indicates that the storage buffer may be extended as necessary, so long as resources allow, to hold additional data. Note that if buf is not NULL, it must point to an array of at least length characters. The result of accessing that array directly while it is in use by the sbuf is undefined. The sbuf_new_auto() function is a shortcut for creating a completely dynamic sbuf. It is the equivalent of calling sbuf_new() with values NULL, NULL, 0, and SBUF_AUTOEXTEND. The sbuf_delete() function clears the sbuf and frees any memory allocated for it. There must be a call to sbuf_delete() for every call to sbuf_new(). Any attempt to access the sbuf after it has been deleted will fail. The sbuf_clear() function invalidates the contents of the sbuf and resets its position to zero. The sbuf_setpos() function sets the sbuf's end position to pos, which is a value between zero and one less than the size of the storage buf- fer. This effectively truncates the sbuf at the new position. The sbuf_bcat() function appends the first len bytes from the buffer buf to the sbuf. The sbuf_bcopyin() function copies len bytes from the specified userland address into the sbuf. The sbuf_bcpy() function replaces the contents of the sbuf with the first len bytes from the buffer buf. The sbuf_cat() function appends the NUL-terminated string str to the sbuf at the current position. The sbuf_copyin() function copies a NUL-terminated string from the specified userland address into the sbuf. If the len argument is non- zero, no more than len characters (not counting the terminating NUL) are copied; otherwise the entire string, or as much of it as can fit in the sbuf, is copied. The sbuf_cpy() function replaces the contents of the sbuf with those of the NUL-terminated string str. This is equivalent to calling sbuf_cat() with a fresh sbuf or one which position has been reset to zero with sbuf_clear() or sbuf_setpos(). The sbuf_printf() function formats its arguments according to the format string pointed to by fmt and appends the resulting string to the sbuf at the current position. The sbuf_vprintf() function behaves the same as sbuf_printf() except that the arguments are obtained from the variable-length argument list ap. The sbuf_putc() function appends the character c to the sbuf at the current position. The sbuf_trim() function removes trailing whitespace from the sbuf. The sbuf_overflowed() function returns a non-zero value if the sbuf overflowed. The sbuf_finish() function null-terminates the sbuf and marks it as finished, which means that it may no longer be modified using sbuf_setpos(), sbuf_cat(), sbuf_cpy(), sbuf_printf() or sbuf_putc(). The sbuf_data() and sbuf_len() functions return the actual string and its length, respectively; sbuf_data() only works on a finished sbuf. sbuf_done() returns non-zero if the sbuf is finished. NOTES
If an operation caused an sbuf to overflow, most subsequent operations on it will fail until the sbuf is finished using sbuf_finish() or reset using sbuf_clear(), or its position is reset to a value between 0 and one less than the size of its storage buffer using sbuf_setpos(), or it is reinitialized to a sufficiently short string using sbuf_cpy(). RETURN VALUES
The sbuf_new() function returns NULL if it failed to allocate a storage buffer, and a pointer to the new sbuf otherwise. The sbuf_setpos() function returns -1 if pos was invalid, and zero otherwise. The sbuf_cat(), sbuf_cpy(), sbuf_printf(), sbuf_putc(), and sbuf_trim() functions all return -1 if the buffer overflowed, and zero otherwise. The sbuf_overflowed() function returns a non-zero value if the buffer overflowed, and zero otherwise. The sbuf_data() and sbuf_len() functions return NULL and -1, respectively, if the buffer overflowed. The sbuf_copyin() function returns -1 if copying string from userland failed, and number of bytes copied otherwise. SEE ALSO
printf(3), strcat(3), strcpy(3), copyin(9), copyinstr(9), printf(9) HISTORY
The sbuf family of functions first appeared in FreeBSD 4.4. AUTHORS
The sbuf family of functions was designed by Poul-Henning Kamp <phk@FreeBSD.org> and implemented by Dag-Erling Smorgrav <des@FreeBSD.org>. Additional improvements were suggested by Justin T. Gibbs <gibbs@FreeBSD.org>. Auto-extend support added by Kelly Yancey <kbyanc@FreeBSD.org>. This manual page was written by Dag-Erling Smorgrav <des@FreeBSD.org>. BSD
May 17, 2009 BSD
All times are GMT -4. The time now is 02:16 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy