I need idas how to clean up this function.


 
Thread Tools Search this Thread
Top Forums Programming I need idas how to clean up this function.
# 1  
Old 06-22-2007
I need idas how to clean up this function.

OpenBSD complains when it sees this function in my program

Code:
/*This function takes the string "test\n" and returns the string "test\n\test\ntest\n"
ENTROPY = 1024
*/
void *build_string(int count, char **strarr)
{
  int k;
  char *new;;
  size_t max;

  if(count == 0) {
    exit(EXIT_FAILURE);
  }

  if(strarr == 0) {
    fprintf(stderr, "illegal division \n");
    exit(EXIT_FAILURE);
  }

  max = ENTROPY/(strlen(strarr[count-1])+1);

  /*I probably should have used calloc() */
  if( ( new=malloc(max* (strlen(strarr[count-1]))+1) ) == NULL){
    return NULL;
  }

  /*memset(new, 0, max*strlen(strarr[count-1])+1);*/

  for(k=0; k < max; k++){
    strcat(new, strarr[count-1]);
  }
  return new;
}

OpenBSD complains that strcat() is almost always misused and says that I should instead use strlcat(). However, strlcat() isn't available on the FreeBSD box. Should I just roll my own version of strlcat()? Or is there a better way to concate the strings in this case? Also, I don't think I'm checking strarr correctly in this case.

Last edited by reborg; 06-23-2007 at 06:11 AM.. Reason: sexist remarks not welcome here
# 2  
Old 06-24-2007
Or use the stncat(), strncat(), snprintf() etc family..

However the reason the strlcat() etc functions exists is there are some implementations of the strncat() etc set don't include the null terminator if the string tries to exceed the boundary length.
# 3  
Old 06-24-2007
Okay, I decided to quit being up 48 hours and then start writing some crazy code. Instead, I've opted to sleep and only write semi-crazy code. How about using memcpy()? Maybe something like this

Code:
static void *xmalloc (size_t size)
{

  if(size == 0) {
    exit(EXIT_FAILURE);
  }

  void *value = malloc (size);
  if (value == NULL)
    return NULL;
  return value;
}

static void *build_string(char *s)
{
  int k;
  char *start;
  size_t len;  
  size_t max; 

  len = strlen(s);

  if(len == 0) {
    fprintf(stderr,"Zero length");
    exit(EXIT_FAILURE);
  }

  max = ENTROPY/len;

  char *p = xmalloc((max*len) + 1);

  if(p == NULL){
    fprintf(stderr, "Out of memory\n");
    return NULL;
  }

  start = p;
  for(k=0; k < max; k++){
    memcpy(p, s, len);
    p += len;
  }
   
  return start;
}

I haven't tested this on the site that is running OpenBSD because the adminstrator decided to put a perm ban on my IP block.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[bash] wanted: function with a clean way for multiple return values

Hi, I have a small part of a project which is done as a bash script. bash was selected as an portability issue that works out of the box. In this script I have an exec shell-function, a wrapper around arbitrary commands. I want to have STDOUT, as an addon STDERR and the EXIT-CODE of a specified... (5 Replies)
Discussion started by: stomp
5 Replies

2. UNIX for Advanced & Expert Users

Clean up Scripts

Hi i have a perl script that i use to clean up empty folders on our server. I need to make a amendment to this to exclude certain folders. Folders are invisible to end users but must not be cleaned up by this script. folders i need protecting have unique names .Highres .HighresF .Lowres... (0 Replies)
Discussion started by: treds
0 Replies

3. Shell Programming and Scripting

How to clean this script?

Hello guys, this script partially works but it's still pretty ugly and, moreover, if the month is jan/feb/mar... it doesn't work at all. Could anyone say me how to correct, cut and clean a little bit? #!/usr/bin/ksh egrep -v -e "^\s*#" /file/permission | awk '{ print $1 }' | sort | uniq... (3 Replies)
Discussion started by: gogol_bordello
3 Replies

4. AIX

How to clean PV id?

When I run command: >chdev -l hdisk1 -a pv=clear It shows Method error (/etc/methods/chgdisk): 0514-062 Cannot perform the requested function because the specified device is busy. run: #>fuser -kxuc /dev/raw1 /dev/raw1: How to clean PV id? (4 Replies)
Discussion started by: rainbow_bean
4 Replies

5. UNIX for Dummies Questions & Answers

Help me clean this up

UNIX amature/novice. I've written a script (it works) that needs to be cleaned up. I'm searching for a way to perform a "go to , perform this then return" section of code. The books I have and searches i"ve done so far do not show me how to do this. I've pasted in the current code below. I've... (1 Reply)
Discussion started by: scanner248
1 Replies

6. Shell Programming and Scripting

clean /etc/hosts

hi all, i need to write a script. it should be able to clean the /etc/hosts file like this: first field is the ip (what else ;)) second field is the fqdn (full qualified domain name) third field is the hostname the fourth to n'th field is the rest. i hope you understand what is needed?... (5 Replies)
Discussion started by: DukeNuke2
5 Replies

7. UNIX for Advanced & Expert Users

Clean File

BeginDate 07/01/06 End: 07/31/06 Cust: A02991 - Burnham 0002000 5,829,773 145.3 0009701 4,043,850 267.3 2005000 286,785.13 100.0 BeginDate 07/01/06 End: 07/31/06 Cust: A01239 - East Track PSE Index A 0009902 317,356.82 890.2 0020021 ... (5 Replies)
Discussion started by: kris01752
5 Replies

8. Shell Programming and Scripting

clean up script

I have a script which would monitor a given directory and delete any files which are older than 10 days. I was going to set the 10 crob jobs to perform this operation for 10 different directories (some are actually sub-directories), but my boss doesn't like that idea, so I need to do that in one... (1 Reply)
Discussion started by: mpang_
1 Replies

9. UNIX for Advanced & Expert Users

Clean an LV out of the ODM

I recently had a disk crash and was not able to clean the dump lv off the disk. Now trying to create new lvdump I am running into errors. My question is how do I remove the old lv out of the ODM? I have tried going through smit and also just rmlv and it cannot find the lv. Yet when I run lslv on... (0 Replies)
Discussion started by: Wamland
0 Replies

10. AIX

Directory clean up

I want to write a script in ksh that will delete file that are more than 2 weeks old. (4 Replies)
Discussion started by: jango
4 Replies
Login or Register to Ask a Question