blob: 16c8c5072916380b7e11d8d5e06678847418170c [file] [log] [blame]
Damien Miller61c51502000-08-18 14:01:04 +10001#include "includes.h"
Damien Millercaf6dd62000-08-29 11:33:50 +11002RCSID("$OpenBSD: util.c,v 1.4 2000/08/28 20:23:37 markus Exp $");
Damien Miller61c51502000-08-18 14:01:04 +10003
4#include "ssh.h"
5
6char *
7chop(char *s)
8{
9 char *t = s;
10 while (*t) {
11 if(*t == '\n' || *t == '\r') {
12 *t = '\0';
13 return s;
14 }
15 t++;
16 }
17 return s;
18
19}
20
21void
22set_nonblock(int fd)
23{
24 int val;
25 if (isatty(fd)) {
26 /* do not mess with tty's */
27 debug("no set_nonblock for tty fd %d", fd);
28 return;
29 }
30 val = fcntl(fd, F_GETFL, 0);
31 if (val < 0) {
32 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
33 return;
34 }
35 if (val & O_NONBLOCK)
36 return;
37 debug("fd %d setting O_NONBLOCK", fd);
38 val |= O_NONBLOCK;
39 if (fcntl(fd, F_SETFL, val) == -1)
Damien Millercaf6dd62000-08-29 11:33:50 +110040 if (errno != ENODEV)
41 error("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
42 fd, strerror(errno));
Damien Miller61c51502000-08-18 14:01:04 +100043}
44
45/* Characters considered whitespace in strsep calls. */
46#define WHITESPACE " \t\r\n"
47
48char *
49strdelim(char **s)
50{
51 char *old;
52 int wspace = 0;
53
54 if (*s == NULL)
55 return NULL;
56
57 old = *s;
58
59 *s = strpbrk(*s, WHITESPACE "=");
60 if (*s == NULL)
61 return (old);
62
63 /* Allow only one '=' to be skipped */
64 if (*s[0] == '=')
65 wspace = 1;
66 *s[0] = '\0';
67
68 *s += strspn(*s + 1, WHITESPACE) + 1;
69 if (*s[0] == '=' && !wspace)
70 *s += strspn(*s + 1, WHITESPACE) + 1;
71
72 return (old);
73}