New timing infrastructure. There's a new function schedule_timer()
[u/mdw/putty] / unix / uxmisc.c
CommitLineData
f7f27309 1/*
2 * PuTTY miscellaneous Unix stuff
3 */
4
5#include <stdio.h>
d0912d1f 6#include <unistd.h>
f7f27309 7#include <sys/time.h>
799dfcfa 8#include <sys/types.h>
9#include <pwd.h>
f7f27309 10
9a30e26b 11#include "putty.h"
12
f7f27309 13unsigned long getticks(void)
14{
15 struct timeval tv;
16 gettimeofday(&tv, NULL);
17 /*
39934deb 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.
f7f27309 21 */
39934deb 22 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
f7f27309 23}
9a30e26b 24
9fab77dc 25Filename filename_from_str(const char *str)
9a30e26b 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
9fab77dc 33const char *filename_to_str(const Filename *fn)
9a30e26b 34{
9fab77dc 35 return fn->path;
9a30e26b 36}
37
38int filename_equal(Filename f1, Filename f2)
39{
40 return !strcmp(f1.path, f2.path);
41}
42
43int filename_is_null(Filename fn)
44{
45 return !*fn.path;
46}
d0912d1f 47
48#ifdef DEBUG
49static FILE *debug_fp = NULL;
50
51void 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
799dfcfa 63
64char *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}