Back to index

glibc  2.9
Defines | Functions | Variables
isomac.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Defines

#define _GNU_SOURCE   1
#define HEADER_MAX   256
#define NUMBER_OF_HEADERS   (sizeof header / sizeof *header)
#define NUMBER_OF_PREFIXES   (sizeof prefix / sizeof *prefix)
#define NUMBER_OF_SUFFIXES   (sizeof suffix / sizeof *suffix)
#define NUMBER_OF_MACROS   (sizeof macros / sizeof *macros)

Functions

static char * xstrndup (const char *, size_t)
static const char ** get_null_defines (void)
static int check_header (const char *, const char **)
int main (int argc, char *argv[])

Variables

static const char * macrofile
static char * header []
static char * prefix []
static char * suffix []
static char * macros []
static const char fmt []
char * CC
char * INC

Define Documentation

#define _GNU_SOURCE   1

Definition at line 71 of file isomac.c.

#define HEADER_MAX   256

Definition at line 78 of file isomac.c.

#define NUMBER_OF_HEADERS   (sizeof header / sizeof *header)

Definition at line 171 of file isomac.c.

#define NUMBER_OF_MACROS   (sizeof macros / sizeof *macros)

Definition at line 174 of file isomac.c.

#define NUMBER_OF_PREFIXES   (sizeof prefix / sizeof *prefix)

Definition at line 172 of file isomac.c.

#define NUMBER_OF_SUFFIXES   (sizeof suffix / sizeof *suffix)

Definition at line 173 of file isomac.c.


Function Documentation

static int check_header ( const char *  file_name,
const char **  except 
) [static]

Definition at line 338 of file isomac.c.

{
  char line[BUFSIZ], *command;
  FILE *input;
  int result = 0;

  command = malloc (sizeof fmt + strlen (file_name) + 2 * strlen (CC)
                  + strlen (INC) + strlen (macrofile));

  if (command == NULL)
    {
      puts ("No more memory.");
      exit (1);
    }

  puts (file_name);
  sprintf (command, fmt, file_name, CC, INC, CC, macrofile);

  if (system (command))
    {
      puts ("system() returned nonzero");
      result = 1;
    }
  free (command);
  input = fopen (macrofile, "r");

  if (input == NULL)
    {
      printf ("Could not read %s: ", macrofile);
      perror (NULL);
      return 1;
    }

  while (fgets (line, sizeof line, input) != NULL)
    {
      int i, okay = 0;
      size_t endmac;
      const char **cpp;
      if (strlen (line) < 9 || line[7] != ' ')
       { /* "#define A" */
         printf ("Malformed input, expected '#define MACRO'\ngot '%s'\n",
                line);
         result = 1;
         continue;
       }
      for (i = 0; i < NUMBER_OF_PREFIXES; ++i)
       {
         if (!strncmp (line+8, prefix[i], strlen (prefix[i]))) {
           ++okay;
           break;
         }
       }
      if (okay)
       continue;
      for (i = 0; i < NUMBER_OF_MACROS; ++i)
       {
         if (!strncmp (line + 8, macros[i], strlen (macros[i])))
           {
             ++okay;
             break;
           }
       }
      if (okay)
       continue;
      /* Find next char after the macro identifier; this can be either
        a space or an open parenthesis.  */
      endmac = strcspn (line + 8, " (");
      if (line[8+endmac] == '\0')
       {
         printf ("malformed input, expected '#define MACRO VALUE'\n"
                "got '%s'\n", line);
         result = 1;
         continue;
       }
      for (i = 0; i < NUMBER_OF_SUFFIXES; ++i)
       {
         size_t len = strlen (suffix[i]);
         if (!strncmp (line + 8 + endmac - len, suffix[i], len))
           {
             ++okay;
             break;
           }
       }
      if (okay)
       continue;
      if (except != NULL)
       for (cpp = except; *cpp != NULL; ++cpp)
         {
           size_t len = strlen (*cpp);
           if (!strncmp (line + 8, *cpp, len) && isspace (line[8 + len]))
             {
              ++okay;
              break;
             }
         }
      if (!okay)
       {
         fputs (line, stdout);
         result = 2;
       }
    }
  fclose (input);
  remove (macrofile);

  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static const char ** get_null_defines ( void  ) [static]

Definition at line 242 of file isomac.c.

{
  char line[BUFSIZ], *command;
  char **result = NULL;
  size_t result_len = 0;
  size_t result_max = 0;
  FILE *input;
  int first = 1;

  macrofile = tmpnam (NULL);

  command = malloc (sizeof fmt + sizeof "/dev/null" + 2 * strlen (CC)
                  + strlen (INC) + strlen (macrofile));

  if (command == NULL)
    {
      puts ("No more memory.");
      exit (1);
    }

  sprintf (command, fmt, "/dev/null", CC, INC, CC, macrofile);

  if (system (command))
    {
      puts ("system() returned nonzero");
      return NULL;
    }
  free (command);
  input = fopen (macrofile, "r");

  if (input == NULL)
    {
      printf ("Could not read %s: ", macrofile);
      perror (NULL);
      return NULL;
    }

  while (fgets (line, sizeof line, input) != NULL)
    {
      int i, okay = 0;
      size_t endmac;
      char *start, *end;
      if (strlen (line) < 9 || line[7] != ' ')
       { /* "#define A" */
         printf ("Malformed input, expected '#define MACRO'\ngot '%s'\n",
                line);
         continue;
       }
      if (line[8] == '_')
       /* It's a safe identifier.  */
       continue;
      if (result_len == result_max)
       {
         result_max += 10;
         result = realloc (result, result_max * sizeof (char **));
         if (result == NULL)
           {
             puts ("No more memory.");
             exit (1);
           }
       }
      start = &line[8];
      for (end = start + 1; !isspace (*end) && *end != '\0'; ++end)
       ;
      result[result_len] = xstrndup (start, end - start);

      if (strcmp (result[result_len], "NOT_IN_libc") != 0)
       {
         if (first)
           {
             fputs ("The following identifiers will be ignored since the compiler defines them\nby default:\n", stdout);
             first = 0;
           }
         puts (result[result_len]);
       }
      ++result_len;
    }
  if (result_len == result_max)
    {
      result_max += 1;
      result = realloc (result, result_max * sizeof (char **));
      if (result == NULL)
       {
         puts ("No more memory.");
         exit (1);
       }
    }
  result[result_len] = NULL;
  fclose (input);
  remove (macrofile);

  return (const char **) result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 194 of file isomac.c.

{
  int h;
  int result = 0;
  const char **ignore_list;

  CC = argc > 1 ? argv[1] : "gcc";
  INC = argc > 2 ? argv[2] : "";

  if (system (NULL) == 0)
    {
      puts ("Sorry, no command processor.");
      return EXIT_FAILURE;
    }

  /* First get list of symbols which are defined by the compiler.  */
  ignore_list = get_null_defines ();

  fputs ("Tested files:\n", stdout);

  for (h = 0; h < NUMBER_OF_HEADERS; ++h)
    {
      char file_name[HEADER_MAX];
      sprintf (file_name, "%s.h", header[h]);
      result |= check_header (file_name, ignore_list);
    }

  /* The test suite should return errors but for now this is not
     practical.  Give a warning and ask the user to correct the bugs.  */
  return result;
}

Here is the call graph for this function:

static char * xstrndup ( const char *  s,
size_t  n 
) [static]

Definition at line 228 of file isomac.c.

{
  size_t len = n;
  char *new = malloc (len + 1);

  if (new == NULL)
    return NULL;

  new[len] = '\0';
  return memcpy (new, s, len);
}

Here is the caller graph for this function:


Variable Documentation

char* CC

Definition at line 185 of file isomac.c.

const char fmt[] [static]
Initial value:
"\
echo \"#include <%s>\" |\
%s -E -dM -ansi -pedantic %s -D_LIBC -D_ISOMAC -DNOT_IN_libc -I. \
-isystem `%s --print-prog-name=include` - 2> /dev/null > %s"

Definition at line 178 of file isomac.c.

char* header[] [static]
Initial value:
{
  "assert", "ctype", "errno", "float", "iso646", "limits", "locale",
  "math", "setjmp", "signal", "stdarg", "stddef", "stdio", "stdlib",
  "string", "time", "wchar", "wctype"
}

Definition at line 83 of file isomac.c.

char* INC

Definition at line 187 of file isomac.c.

const char* macrofile [static]

Definition at line 80 of file isomac.c.

char* macros[] [static]

Definition at line 105 of file isomac.c.

char* prefix[] [static]
Initial value:
{
  "_", "E", "is", "str", "mem", "SIG", "FLT_", "DBL_", "LDBL_",
  "LC_", "wmem", "wcs"
}

Definition at line 91 of file isomac.c.

char* suffix[] [static]
Initial value:
{
  "_MAX", "_MIN"
}

Definition at line 99 of file isomac.c.