74eb15680f3b6ef728aacc4c9e8f856b13dfa3ee
[u/mdw/putty] / unix / uxmisc.c
1 /*
2 * PuTTY miscellaneous Unix stuff
3 */
4
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <sys/time.h>
10 #include <sys/types.h>
11 #include <pwd.h>
12
13 #include "putty.h"
14
15 long tickcount_offset = 0;
16
17 unsigned long getticks(void)
18 {
19 struct timeval tv;
20 gettimeofday(&tv, NULL);
21 /*
22 * We want to use milliseconds rather than microseconds,
23 * because we need a decent number of them to fit into a 32-bit
24 * word so it can be used for keepalives.
25 */
26 return tv.tv_sec * 1000 + tv.tv_usec / 1000 + tickcount_offset;
27 }
28
29 Filename filename_from_str(const char *str)
30 {
31 Filename ret;
32 strncpy(ret.path, str, sizeof(ret.path));
33 ret.path[sizeof(ret.path)-1] = '\0';
34 return ret;
35 }
36
37 const char *filename_to_str(const Filename *fn)
38 {
39 return fn->path;
40 }
41
42 int filename_equal(Filename f1, Filename f2)
43 {
44 return !strcmp(f1.path, f2.path);
45 }
46
47 int filename_is_null(Filename fn)
48 {
49 return !*fn.path;
50 }
51
52 #ifdef DEBUG
53 static FILE *debug_fp = NULL;
54
55 void dputs(char *buf)
56 {
57 if (!debug_fp) {
58 debug_fp = fopen("debug.log", "w");
59 }
60
61 write(1, buf, strlen(buf));
62
63 fputs(buf, debug_fp);
64 fflush(debug_fp);
65 }
66 #endif
67
68 char *get_username(void)
69 {
70 struct passwd *p;
71 uid_t uid = getuid();
72 char *user, *ret = NULL;
73
74 /*
75 * First, find who we think we are using getlogin. If this
76 * agrees with our uid, we'll go along with it. This should
77 * allow sharing of uids between several login names whilst
78 * coping correctly with people who have su'ed.
79 */
80 user = getlogin();
81 setpwent();
82 if (user)
83 p = getpwnam(user);
84 else
85 p = NULL;
86 if (p && p->pw_uid == uid) {
87 /*
88 * The result of getlogin() really does correspond to
89 * our uid. Fine.
90 */
91 ret = user;
92 } else {
93 /*
94 * If that didn't work, for whatever reason, we'll do
95 * the simpler version: look up our uid in the password
96 * file and map it straight to a name.
97 */
98 p = getpwuid(uid);
99 if (!p)
100 return NULL;
101 ret = p->pw_name;
102 }
103 endpwent();
104
105 return dupstr(ret);
106 }
107
108 /*
109 * Display the fingerprints of the PGP Master Keys to the user.
110 * (This is here rather than in uxcons because it's appropriate even for
111 * Unix GUI apps.)
112 */
113 void pgp_fingerprints(void)
114 {
115 fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
116 "be used to establish a trust path from this executable to another\n"
117 "one. See the manual for more information.\n"
118 "(Note: these fingerprints have nothing to do with SSH!)\n"
119 "\n"
120 "PuTTY Master Key (RSA), 1024-bit:\n"
121 " " PGP_RSA_MASTER_KEY_FP "\n"
122 "PuTTY Master Key (DSA), 1024-bit:\n"
123 " " PGP_DSA_MASTER_KEY_FP "\n", stdout);
124 }
125
126 /*
127 * Set FD_CLOEXEC on a file descriptor
128 */
129 int cloexec(int fd) {
130 int fdflags;
131
132 fdflags = fcntl(fd, F_GETFD);
133 if (fdflags == -1) return -1;
134 return fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
135 }