xref: /openbsd-src/usr.sbin/tcpdump/savestr.c (revision 6ad041cbfa64be66bf4b7dd964b245da8a241da1)
1*6ad041cbSmmcc /*	$OpenBSD: savestr.c,v 1.10 2015/11/16 00:16:39 mmcc Exp $	*/
2cf4e9b47Sho 
3da08816cSjakob /*
4da08816cSjakob  * Copyright (c) 1997
5da08816cSjakob  *	The Regents of the University of California.  All rights reserved.
6da08816cSjakob  *
7da08816cSjakob  * Redistribution and use in source and binary forms, with or without
8da08816cSjakob  * modification, are permitted provided that: (1) source code distributions
9da08816cSjakob  * retain the above copyright notice and this paragraph in its entirety, (2)
10da08816cSjakob  * distributions including binary code include the above copyright notice and
11da08816cSjakob  * this paragraph in its entirety in the documentation or other materials
12da08816cSjakob  * provided with the distribution, and (3) all advertising materials mentioning
13da08816cSjakob  * features or use of this software display the following acknowledgement:
14da08816cSjakob  * ``This product includes software developed by the University of California,
15da08816cSjakob  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16da08816cSjakob  * the University nor the names of its contributors may be used to endorse
17da08816cSjakob  * or promote products derived from this software without specific prior
18da08816cSjakob  * written permission.
19da08816cSjakob  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20da08816cSjakob  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21da08816cSjakob  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22da08816cSjakob  */
23da08816cSjakob 
24da08816cSjakob #include <sys/types.h>
25da08816cSjakob 
26da08816cSjakob #include <stdio.h>
27da08816cSjakob #include <stdlib.h>
28a310b12dSjakob #include <string.h>
29da08816cSjakob 
30da08816cSjakob #ifdef HAVE_OS_PROTO_H
31da08816cSjakob #include "os-proto.h"
32da08816cSjakob #endif
33da08816cSjakob 
34da08816cSjakob #include "savestr.h"
35da08816cSjakob 
36da08816cSjakob /* A replacement for strdup() that cuts down on malloc() overhead */
37da08816cSjakob char *
savestr(const char * str)38*6ad041cbSmmcc savestr(const char *str)
39da08816cSjakob {
40*6ad041cbSmmcc 	size_t size;
41*6ad041cbSmmcc 	char *p;
42da08816cSjakob 	static char *strptr = NULL;
43cf0d875fSmillert 	static size_t strsize = 0;
44da08816cSjakob 
45da08816cSjakob 	size = strlen(str) + 1;
46da08816cSjakob 	if (size > strsize) {
47da08816cSjakob 		strsize = 1024;
48da08816cSjakob 		if (strsize < size)
49da08816cSjakob 			strsize = size;
50323ce4b6Sderaadt 		strptr = malloc(strsize);
51da08816cSjakob 		if (strptr == NULL) {
5296a8bdbaSmillert 			fprintf(stderr, "savestr: malloc\n");
53da08816cSjakob 			exit(1);
54da08816cSjakob 		}
55da08816cSjakob 	}
56cf0d875fSmillert 	(void)strlcpy(strptr, str, size);
57da08816cSjakob 	p = strptr;
58da08816cSjakob 	strptr += size;
59da08816cSjakob 	strsize -= size;
60da08816cSjakob 	return (p);
61da08816cSjakob }
62