summaryrefslogtreecommitdiffabout
path: root/src/util.c
blob: f254202243f7b61d98ab25466a47392b4ed39a7a (plain)
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
/* This file is part of GDBM, the GNU data base manager.
   Copyright (C) 1990-1991, 1993, 2007, 2011, 2013, 2017-2018 Free
   Software Foundation, Inc.

   GDBM 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 3, or (at your option)
   any later version.

   GDBM 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 GDBM. If not, see <http://www.gnu.org/licenses/>.    */

#include "gdbmtool.h"
#include <pwd.h>

char *
mkfilename (const char *dir, const char *file, const char *suf)
{
  char *tmp;
  size_t dirlen = strlen (dir);
  size_t suflen = suf ? strlen (suf) : 0;
  size_t fillen = strlen (file);
  size_t len;
  
  while (dirlen > 0 && dir[dirlen-1] == '/')
    dirlen--;

  len = dirlen + (dir[0] ? 1 : 0) + fillen + suflen;
  tmp = emalloc (len + 1);
  memcpy (tmp, dir, dirlen);
  if (dir[0])
    tmp[dirlen++] = '/';
  memcpy (tmp + dirlen, file, fillen);
  if (suf)
    memcpy (tmp + dirlen + fillen, suf, suflen);
  tmp[len] = 0;
  return tmp;
}

char *
tildexpand (char *s)
{
  if (s[0] == '~')
    {
      char *p = s + 1;
      size_t len = strcspn (p, "/");
      struct passwd *pw;

      if (len == 0)
	pw = getpwuid (getuid ());
      else
	{
	  char *user = emalloc (len + 1);
	  
	  memcpy (user, p, len);
	  user[len] = 0;
	  pw = getpwnam (user);
	  free (user);
	}
      if (pw)
	return mkfilename (pw->pw_dir, p + len + 1, NULL);
    }
  return estrdup (s);
}

int
vgetyn (const char *prompt, va_list ap)
{
  int state = 0;
  int c, resp;

  do
    {
      switch (state)
	{
	case 1:
	  if (c == ' ' || c == '\t')
	    continue;
	  resp = c;
	  state = 2;
	  /* fall through */
	case 2:
	  if (c == '\n')
	    {
	      switch (resp)
		{
		case 'y':
		case 'Y':
		  return 1;
		case 'n':
		case 'N':
		  return 0;
		default:
		  fprintf (stdout, "%s\n", _("Please, reply 'y' or 'n'"));
		}
	      state = 0;
	    } else
	    break;
	  
	case 0:
	  vfprintf (stdout, prompt, ap);
	  fprintf (stdout, " [y/n]?");
	  fflush (stdout);
	  state = 1;
	  break;
	}
    } while ((c = getchar ()) != EOF);
  exit (EXIT_USAGE);
}
	
int
getyn (const char *prompt, ...)
{
  va_list ap;
  int rc;
  
  va_start (ap, prompt);
  rc = vgetyn (prompt, ap);
  va_end (ap);
  return rc;
}
	

Return to:

Send suggestions and report system problems to the System administrator.