ref: 03961e94972a0419db48886a1067158d9ca3d85b
parent: 4b2ef597e4d4f5e7f05045e55db76dd4fabbe7f5
author: cbagwell <cbagwell>
date: Sun Aug 29 22:25:38 EDT 2004
Use strdup for ft->filename and ft->comment fields. Add cross platform strdup.
--- a/configure
+++ b/configure
@@ -3910,7 +3910,8 @@
-for ac_func in getopt strerror memmove rand strcasecmp
+
+for ac_func in getopt strerror memmove rand strcasecmp strdup
do
as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
echo "$as_me:$LINENO: checking for $ac_func" >&5
--- a/configure.in
+++ b/configure.in
@@ -126,7 +126,7 @@
dnl Checks for library functions.
-AC_CHECK_FUNCS(getopt strerror memmove rand strcasecmp)
+AC_CHECK_FUNCS(getopt strerror memmove rand strcasecmp strdup)
dnl Test for Ogg Vorbis libraries.
--- a/src/misc.c
+++ b/src/misc.c
@@ -340,6 +340,20 @@
}
#endif
+#ifndef HAVE_STRDUP
+/*
+ * Portable strdup() function
+ */
+char *strdup(const char *s)
+{
+ char *dups;
+
+ dups = (char *)malloc(strlen(s+1));
+ strcpy(dups, s);
+ return s;
+}
+#endif
+
#ifndef HAVE_RAND
/*
* Portable random generator as defined by ANSI C Standard.
--- a/src/sox.c
+++ b/src/sox.c
@@ -190,8 +190,7 @@
}
}
- ft = (ft_t)malloc(sizeof(struct st_soundstream));
- st_initformat(ft);
+ ft = st_initformat();
doopts(ft, argc, argv);
@@ -206,7 +205,7 @@
{
if (optind < argc)
{
- ft->filename = argv[optind];
+ ft->filename = strdup(argv[optind]);
optind++;
copy_input(ft);
@@ -217,7 +216,7 @@
{
if (optind < argc && writing)
{
- ft->filename = argv[optind];
+ ft->filename = strdup(argv[optind]);
optind++;
}
else
@@ -314,10 +313,9 @@
/* Default the input comment to the filename if not set from
* command line.
- * FIXME: Should be a memory copy, not a pointer asignment.
*/
if (!ft->comment)
- ft->comment = ft->filename;
+ ft->comment = strdup(ft->filename);
input_count++;
}
--- a/src/st.h
+++ b/src/st.h
@@ -251,7 +251,7 @@
int st_checkeffect(char *);
int st_updateeffect(eff_t, ft_t, ft_t, int);
int st_gettype(ft_t);
-void st_initformat(ft_t ft);
+ft_t st_initformat(void);
void st_copyformat(ft_t, ft_t);
int st_checkformat(ft_t);
int st_parsesamples(st_rate_t rate, char *str, st_size_t *samples, char def);
--- a/src/st_i.h
+++ b/src/st_i.h
@@ -49,6 +49,10 @@
int strcasecmp(const char *s1, const char *s2);
#endif
+#ifndef HAVE_STRDUP
+char *strdup(const char *s);
+#endif
+
#ifndef HAVE_RAND
int rand(void);
void srand(unsigned int seed);
--- a/src/stconfig.h.in
+++ b/src/stconfig.h.in
@@ -87,6 +87,9 @@
/* Define to 1 if you have the `strcasecmp' function. */
#undef HAVE_STRCASECMP
+/* Define to 1 if you have the `strdup' function. */
+#undef HAVE_STRDUP
+
/* Define to 1 if you have the `strerror' function. */
#undef HAVE_STRERROR
--- a/src/util.c
+++ b/src/util.c
@@ -334,66 +334,49 @@
* File format routines
*/
-void st_initformat(ft_t ft)
+ft_t st_initformat(void)
{
- ft->filename = 0;
- ft->filetype = 0;
- ft->fp = 0;
+ ft_t ft;
- ft->info.rate = 0;
+ ft = (ft_t)calloc(sizeof(struct st_soundstream), 1);
+
ft->info.size = -1;
ft->info.encoding = -1;
ft->info.channels = -1;
- ft->comment = 0;
- ft->swap = 0;
-
- /* FIXME: This should zero out the reset of the structures */
+ return ft;
}
void st_copyformat(ft_t ft, ft_t ft2)
{
- int noise = 0, i;
- double factor;
+ int i;
+ double factor;
- if (ft2->info.rate == 0) {
- ft2->info.rate = ft->info.rate;
- noise = 1;
- }
- if (ft2->info.size == -1) {
- ft2->info.size = ft->info.size;
- noise = 1;
- }
- if (ft2->info.encoding == -1) {
- ft2->info.encoding = ft->info.encoding;
- noise = 1;
- }
- if (ft2->info.channels == -1) {
- ft2->info.channels = ft->info.channels;
- noise = 1;
- }
+ if (ft2->info.rate == 0)
+ ft2->info.rate = ft->info.rate;
+ if (ft2->info.size == -1)
+ ft2->info.size = ft->info.size;
+ if (ft2->info.encoding == -1)
+ ft2->info.encoding = ft->info.encoding;
+ if (ft2->info.channels == -1)
+ ft2->info.channels = ft->info.channels;
- /* FIXME: Do not copy pointers! This should be at least
- * a malloc+strcpy.
- */
- if (ft2->comment == NULL) {
- ft2->comment = ft->comment;
- noise = 1;
- }
+ if (ft2->comment == NULL && ft->comment != NULL)
+ ft2->comment = strdup(ft->comment);
- /*
- * copy loop info, resizing appropriately
- * it's in samples, so # channels don't matter
- */
- factor = (double) ft2->info.rate / (double) ft->info.rate;
- for(i = 0; i < ST_MAX_NLOOPS; i++) {
- ft2->loops[i].start = ft->loops[i].start * factor;
- ft2->loops[i].length = ft->loops[i].length * factor;
- ft2->loops[i].count = ft->loops[i].count;
- ft2->loops[i].type = ft->loops[i].type;
- }
- /* leave SMPTE # alone since it's absolute */
- ft2->instr = ft->instr;
+ /*
+ * copy loop info, resizing appropriately
+ * it's in samples, so # channels don't matter
+ */
+ factor = (double) ft2->info.rate / (double) ft->info.rate;
+ for(i = 0; i < ST_MAX_NLOOPS; i++) {
+ ft2->loops[i].start = ft->loops[i].start * factor;
+ ft2->loops[i].length = ft->loops[i].length * factor;
+ ft2->loops[i].count = ft->loops[i].count;
+ ft2->loops[i].type = ft->loops[i].type;
+ }
+ /* leave SMPTE # alone since it's absolute */
+ ft2->instr = ft->instr;
}
/* check that all settings have been given */