File: nicknames.c

package info (click to toggle)
yp-tools 4.2.3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,292 kB
  • sloc: ansic: 4,192; sh: 1,081; makefile: 62; sed: 16
file content (153 lines) | stat: -rw-r--r-- 3,193 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Copyright (C) 1998, 1999 Thorsten Kukuk
   This file is part of the yp-tools.
   Author: Thorsten Kukuk <kukuk@suse.de>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <ctype.h>
#include <locale.h>
#include <libintl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "nicknames.h"

#ifndef _
#define _(String) gettext (String)
#endif

#ifndef N_
#define N_(String) String
#endif

struct ypalias
{
  char *alias, *name;
  struct ypalias *next;
};

static struct ypalias *ypaliases = NULL;

static void
load_nicknames (void)
{
  FILE *fp;
  char *line;
  size_t len;
  int i = 0;

  /* Open the nickname file.  */
  fp = fopen (NICKNAMEFILE, "r");
  if (fp == NULL)
    {
      fprintf (stderr, _("nickname file %s does not exist.\n"), NICKNAMEFILE);
      return;
    }

  line = NULL;
  len = 0;
  do
    {
      struct ypalias *ptr;
      ssize_t n;
      char *cp;

      n = getline (&line, &len, fp);
      if (n < 0)
        break;
      ++i;
      if (line[n - 1] == '\n')
        line[n - 1] = '\0';
      cp = strchr (line, '#');
      if (cp != NULL)
        *cp = '\0';

      /* If the line is blank it is ignored.  */
      if (line[0] == '\0')
        continue;

      /* Each line contains an alias and the full name */
      cp = line;
      while (!isspace ((int)*cp) && *cp != '\0')
	++cp;
      if (*cp == '\0')
	{
	  fprintf (stderr, _("Bogus entry in line %d: %s\n"), i, line);
	  continue;
	}
      *cp = '\0';
      ++cp;
      while (isspace ((int)*cp) && *cp != '\0')
	++cp;
      if (*cp == '\0')
	{
	  fprintf (stderr, _("Bogus entry in line %d: %s\n"), i, line);
	  continue;
	}
      ptr = calloc (1, sizeof (struct ypalias));
      ptr->next = ypaliases;
      ptr->alias = strdup (line);
      ptr->name = strdup (cp);
      ypaliases = ptr;
   }
  while (!feof (fp));

  /* Free the buffer.  */
  free (line);
  /* Close configuration file.  */
  fclose (fp);
}

const char *
getypalias (const char *alias)
{
  struct ypalias *ptr;

  if (ypaliases == NULL)
    load_nicknames ();

  ptr = ypaliases;

  while (ptr != NULL)
    {
      if (strcmp (alias, ptr->alias) == 0)
	return ptr->name;
      ptr = ptr->next;
    }
  return alias;
}

void
print_nicknames (void)
{
  struct ypalias *ptr;

  if (ypaliases == NULL)
    load_nicknames ();

  ptr = ypaliases;

  while (ptr != NULL)
    {
      fprintf (stdout, _("Use \"%s\"\tfor map \"%s\"\n"), ptr->alias, ptr->name);
      ptr = ptr->next;
    }
}