bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Identical function names in bash source code


From: Peng Yu
Subject: Identical function names in bash source code
Date: Sat, 5 Jan 2019 08:19:38 -0600

Hi,

It is not uncommon to see the same name is used to defined functions
in different .c files in bash source code.

For example, sh_single_quote is defined in both lib/readline/shell.c
and lib/sh/shquote.c with the exact same signature. The two pieces of
code are slightly different. Do they do the exact same things or do
something different?

In either case, is having the same name for different functions a good
practice? This will make the linked binary dependent on the order of
the corresponding .a files specified. Or if linked via .o files, then
one function will shadow the others. See 1) and 2) below for minimal
working examples. Neither cases seem to be good and could be avoided
easily by giving the functions unique names.

So, should such functions with the same name be named differently? Thanks.

// lib/readline/shell.c
/* Does shell-like quoting using single quotes. */
char *
sh_single_quote (char *string)
{
  register int c;
  char *result, *r, *s;

  result = (char *)xmalloc (3 + (4 * strlen (string)));
  r = result;
  *r++ = '\'';

  for (s = string; s && (c = *s); s++)
    {
      *r++ = c;

      if (c == '\'')
  {
    *r++ = '\\';  /* insert escaped single quote */
    *r++ = '\'';
    *r++ = '\'';  /* start new quoted string */
  }
    }

  *r++ = '\'';
  *r = '\0';

  return (result);
}

// lib/sh/shquote.c
/* Return a new string which is the single-quoted version of STRING.
   Used by alias and trap, among others. */
char *
sh_single_quote (string)
     const char *string;
{
  register int c;
  char *result, *r;
  const char *s;

  result = (char *)xmalloc (3 + (4 * strlen (string)));
  r = result;

  if (string[0] == '\'' && string[1] == 0)
    {
      *r++ = '\\';
      *r++ = '\'';
      *r++ = 0;
      return result;
    }

  *r++ = '\'';

  for (s = string; s && (c = *s); s++)
    {
      *r++ = c;

      if (c == '\'')
  {
    *r++ = '\\';  /* insert escaped single quote */
    *r++ = '\'';
    *r++ = '\'';  /* start new quoted string */
  }
    }

  *r++ = '\'';
  *r = '\0';

  return (result);
}

##### 1 ##########
==> libprint1.c <==
// vim: set noexpandtab tabstop=2:
#include <stdio.h>

void print() {
  puts("Hello World1!\n");
}

==> libprint2.c <==
// vim: set noexpandtab tabstop=2:
#include <stdio.h>

void print() {
  puts("Hello World2!\n");
}

==> main.c <==
// vim: set noexpandtab tabstop=2:

void print();
int main() {
  print();
  return 0;
}

==> main.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
gcc -g -Wall -pedantic -c -o libprint1.o libprint1.c
gcc -g -Wall -pedantic -c -o libprint2.o libprint2.c
ar cr libprint1.a libprint1.o
ar cr libprint2.a libprint2.o
gcc -g -Wall -pedantic -c -o main.o main.c
gcc -o main.exe main.o libprint1.a libprint2.a
./main.exe
gcc -o main.exe main.o libprint2.a libprint1.a
./main.exe
$ ./main.sh
gcc -g -Wall -pedantic -c -o libprint1.o libprint1.c
gcc -g -Wall -pedantic -c -o libprint2.o libprint2.c
ar cr libprint1.a libprint1.o
ar cr libprint2.a libprint2.o
gcc -g -Wall -pedantic -c -o main.o main.c
gcc -o main.exe main.o libprint1.a libprint2.a
./main.exe
Hello World1!

gcc -o main.exe main.o libprint2.a libprint1.a
./main.exe
Hello World2!

##### 2 ##########
==> libprint1.c <==
// vim: set noexpandtab tabstop=2:
#include <stdio.h>

void print() {
  puts("Hello World1!\n");
}

==> libprint2.c <==
// vim: set noexpandtab tabstop=2:
#include <stdio.h>

void print() {
  puts("Hello World2!\n");
}

==> main.c <==
// vim: set noexpandtab tabstop=2:

void print();
int main() {
  print();
  return 0;
}

==> main.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
gcc -g -Wall -pedantic -c -o libprint1.o libprint1.c
gcc -g -Wall -pedantic -c -o libprint2.o libprint2.c
ar cr libprint.a libprint1.o libprint2.o
gcc -g -Wall -pedantic -c -o main.o main.c
gcc -o main.exe main.o libprint.a
./main.exe
ar cr libprint.a libprint2.o libprint1.o
gcc -o main.exe main.o libprint.a
./main.exe

$ ./main.sh
gcc -g -Wall -pedantic -c -o libprint1.o libprint1.c
gcc -g -Wall -pedantic -c -o libprint2.o libprint2.c
ar cr libprint.a libprint1.o libprint2.o
gcc -g -Wall -pedantic -c -o main.o main.c
gcc -o main.exe main.o libprint.a
./main.exe
Hello World1!

ar cr libprint.a libprint2.o libprint1.o
gcc -o main.exe main.o libprint.a
./main.exe
Hello World1!

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]