2 * PuTTY miscellaneous Unix stuff
11 #include <sys/types.h>
16 long tickcount_offset = 0;
18 unsigned long getticks(void)
21 gettimeofday(&tv, NULL);
23 * We want to use milliseconds rather than microseconds,
24 * because we need a decent number of them to fit into a 32-bit
25 * word so it can be used for keepalives.
27 return tv.tv_sec * 1000 + tv.tv_usec / 1000 + tickcount_offset;
30 Filename *filename_from_str(const char *str)
32 Filename *ret = snew(Filename);
33 ret->path = dupstr(str);
37 Filename *filename_copy(const Filename *fn)
39 return filename_from_str(fn->path);
42 const char *filename_to_str(const Filename *fn)
47 int filename_equal(const Filename *f1, const Filename *f2)
49 return !strcmp(f1->path, f2->path);
52 int filename_is_null(const Filename *fn)
57 void filename_free(Filename *fn)
63 int filename_serialise(const Filename *f, void *vdata)
65 char *data = (char *)vdata;
66 int len = strlen(f->path) + 1; /* include trailing NUL */
68 strcpy(data, f->path);
72 Filename *filename_deserialise(void *vdata, int maxsize, int *used)
74 char *data = (char *)vdata;
76 end = memchr(data, '\0', maxsize);
81 return filename_from_str(data);
85 static FILE *debug_fp = NULL;
90 debug_fp = fopen("debug.log", "w");
93 write(1, buf, strlen(buf));
100 char *get_username(void)
103 uid_t uid = getuid();
104 char *user, *ret = NULL;
107 * First, find who we think we are using getlogin. If this
108 * agrees with our uid, we'll go along with it. This should
109 * allow sharing of uids between several login names whilst
110 * coping correctly with people who have su'ed.
118 if (p && p->pw_uid == uid) {
120 * The result of getlogin() really does correspond to
126 * If that didn't work, for whatever reason, we'll do
127 * the simpler version: look up our uid in the password
128 * file and map it straight to a name.
141 * Display the fingerprints of the PGP Master Keys to the user.
142 * (This is here rather than in uxcons because it's appropriate even for
145 void pgp_fingerprints(void)
147 fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
148 "be used to establish a trust path from this executable to another\n"
149 "one. See the manual for more information.\n"
150 "(Note: these fingerprints have nothing to do with SSH!)\n"
152 "PuTTY Master Key (RSA), 1024-bit:\n"
153 " " PGP_RSA_MASTER_KEY_FP "\n"
154 "PuTTY Master Key (DSA), 1024-bit:\n"
155 " " PGP_DSA_MASTER_KEY_FP "\n", stdout);
159 * Set FD_CLOEXEC on a file descriptor
161 int cloexec(int fd) {
164 fdflags = fcntl(fd, F_GETFD);
165 if (fdflags == -1) return -1;
166 return fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
169 FILE *f_open(const Filename *filename, char const *mode, int is_private)
172 return fopen(filename->path, mode);
175 assert(mode[0] == 'w'); /* is_private is meaningless for read,
176 and tricky for append */
177 fd = open(filename->path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
180 return fdopen(fd, mode);
184 FontSpec *fontspec_new(const char *name)
186 FontSpec *f = snew(FontSpec);
187 f->name = dupstr(name);
190 FontSpec *fontspec_copy(const FontSpec *f)
192 return fontspec_new(f->name);
194 void fontspec_free(FontSpec *f)
199 int fontspec_serialise(FontSpec *f, void *data)
201 int len = strlen(f->name);
203 strcpy(data, f->name);
204 return len + 1; /* include trailing NUL */
206 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
208 char *data = (char *)vdata;
209 char *end = memchr(data, '\0', maxsize);
212 *used = end - data + 1;
213 return fontspec_new(data);