File: whoami.c

package info (click to toggle)
sharutils 1%3A4.2-6
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,436 kB
  • ctags: 587
  • sloc: ansic: 6,313; perl: 1,742; makefile: 563; sh: 495; pascal: 293; sed: 93
file content (118 lines) | stat: -rw-r--r-- 2,866 bytes parent folder | download | duplicates (5)
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
/* Find out who I am & where I am.
   Copyright (C) 1994, 1995 Free Software Foundation, Inc.

   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.
*/

#include "system.h"

/* Size of buffers holding strings.  */
#define STRING_BUFFER_SIZE 64

#include <pwd.h>

/* On Ultrix 2.x, <utsname.h> uses SYS_NMLEN, which is only defined in
   <limits.h>.  Sigh!  */

#ifdef HAVE_UNAME
# ifdef HAVE_LIMITS_H
#  include <limits.h>
# endif
# include <sys/utsname.h>
# include <time.h>
#else
# include <sys/time.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#ifndef _POSIX_VERSION
struct passwd *getpwuid ();
#endif

/*----------------.
| Get user name.  |
`----------------*/

static const char *
get_username ()
{
  struct passwd *passwd;

  passwd = getpwuid (getuid ());
  endpwent ();
  if (passwd == NULL)
    return "???";
  return passwd->pw_name;
}

/*------------------------------------------------------.
| Do uname, gethostname, or read file (/etc/systemid).  |
`------------------------------------------------------*/

static const char *
get_hostname ()
{
#if HAVE_UNAME

  static struct utsname hostname_buffer;

  uname (&hostname_buffer);
  return hostname_buffer.nodename;

#else
# if HAVE_ETC_SYSTEMID

  FILE *fpsid = fopen ("/etc/systemid", "r");
  static char buffer[STRING_BUFFER_SIZE];

  if (!fpsid)
    return "???";
  fgets (buffer, sizeof (buffer), fpsid);
  fclose (fpsid);
  buffer[strlen (buffer) - 1] = 0;
  return buffer;

# else

  static char hostname_buffer[STRING_BUFFER_SIZE];

  gethostname (hostname_buffer, sizeof (hostname_buffer));
  return hostname_buffer;

# endif
#endif
}

/*------------------------------------------------------------------.
| Fill BUFFER with a string representing the username and hostname  |
| separated by an `@' sign, and return a pointer to the constructed |
| string.  If BUFFER is NULL, use an internal buffer instead.	    |
`------------------------------------------------------------------*/

char *
get_submitter (buffer)
     char *buffer;
{
  static char static_buffer[STRING_BUFFER_SIZE];

  if (!buffer)
    buffer = static_buffer;
  strcpy (buffer, get_username ());
  strcat (buffer, "@");
  return strcat (buffer, get_hostname ());
}