New timing infrastructure. There's a new function schedule_timer()
[u/mdw/putty] / unix / uxmisc.c
1 /*
2 * PuTTY miscellaneous Unix stuff
3 */
4
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <sys/time.h>
8 #include <sys/types.h>
9 #include <pwd.h>
10
11 #include "putty.h"
12
13 unsigned long getticks(void)
14 {
15 struct timeval tv;
16 gettimeofday(&tv, NULL);
17 /*
18 * We want to use milliseconds rather than microseconds,
19 * because we need a decent number of them to fit into a 32-bit
20 * word so it can be used for keepalives.
21 */
22 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
23 }
24
25 Filename filename_from_str(const char *str)
26 {
27 Filename ret;
28 strncpy(ret.path, str, sizeof(ret.path));
29 ret.path[sizeof(ret.path)-1] = '\0';
30 return ret;
31 }
32
33 const char *filename_to_str(const Filename *fn)
34 {
35 return fn->path;
36 }
37
38 int filename_equal(Filename f1, Filename f2)
39 {
40 return !strcmp(f1.path, f2.path);
41 }
42
43 int filename_is_null(Filename fn)
44 {
45 return !*fn.path;
46 }
47
48 #ifdef DEBUG
49 static FILE *debug_fp = NULL;
50
51 void dputs(char *buf)
52 {
53 if (!debug_fp) {
54 debug_fp = fopen("debug.log", "w");
55 }
56
57 write(1, buf, strlen(buf));
58
59 fputs(buf, debug_fp);
60 fflush(debug_fp);
61 }
62 #endif
63
64 char *get_username(void)
65 {
66 struct passwd *p;
67 uid_t uid = getuid();
68 char *user, *ret = NULL;
69
70 /*
71 * First, find who we think we are using getlogin. If this
72 * agrees with our uid, we'll go along with it. This should
73 * allow sharing of uids between several login names whilst
74 * coping correctly with people who have su'ed.
75 */
76 user = getlogin();
77 setpwent();
78 if (user)
79 p = getpwnam(user);
80 else
81 p = NULL;
82 if (p && p->pw_uid == uid) {
83 /*
84 * The result of getlogin() really does correspond to
85 * our uid. Fine.
86 */
87 ret = user;
88 } else {
89 /*
90 * If that didn't work, for whatever reason, we'll do
91 * the simpler version: look up our uid in the password
92 * file and map it straight to a name.
93 */
94 p = getpwuid(uid);
95 if (!p)
96 return NULL;
97 ret = p->pw_name;
98 }
99 endpwent();
100
101 return dupstr(ret);
102 }