blob: 0923ef927a5def33cf1e5f1e85fbe0ff85c1cbb1 [file] [log] [blame]
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -07001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6#include <ctype.h>
7#include <string.h>
8
9#include "util.h"
10
11#include "libsyscalls.h"
12
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070013#if defined(__x86_64__)
14const char *log_syscalls[] = { "connect", "sendto" };
15#elif defined(__i386__)
16const char *log_syscalls[] = { "socketcall", "time" };
17#elif defined(__arm__)
18const char *log_syscalls[] = { "connect", "gettimeofday", "send" };
19#else
20#error "Unsupported platform"
21#endif
22
23const size_t log_syscalls_len = sizeof(log_syscalls)/sizeof(log_syscalls[0]);
24
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070025int lookup_syscall(const char *name)
26{
27 const struct syscall_entry *entry = syscall_table;
28 for (; entry->name && entry->nr >= 0; ++entry)
29 if (!strcmp(entry->name, name))
30 return entry->nr;
31 return -1;
32}
33
34const char *lookup_syscall_name(int nr)
35{
36 const struct syscall_entry *entry = syscall_table;
37 for (; entry->name && entry->nr >= 0; ++entry)
38 if (entry->nr == nr)
39 return entry->name;
40 return NULL;
41}
42
43char *strip(char *s)
44{
45 char *end;
46 while (*s && isblank(*s))
47 s++;
48 end = s + strlen(s) - 1;
49 while (end >= s && *end && (isblank(*end) || *end == '\n'))
50 end--;
51 *(end + 1) = '\0';
52 return s;
53}
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -080054
55char *tokenize(char **stringp, const char *delim) {
56 char *ret = NULL;
57
58 /* If the string is NULL or empty, there are no tokens to be found. */
59 if (stringp == NULL || *stringp == NULL || **stringp == '\0')
60 return NULL;
61
62 /*
63 * If the delimiter is NULL or empty,
64 * the full string makes up the only token.
65 */
66 if (delim == NULL || *delim == '\0') {
67 ret = *stringp;
68 *stringp = NULL;
69 return ret;
70 }
71
72 char *found;
73 while (**stringp != '\0') {
74 found = strstr(*stringp, delim);
75
76 if (!found) {
77 /*
78 * The delimiter was not found, so the full string
79 * makes up the only token, and we're done.
80 */
81 ret = *stringp;
82 *stringp = NULL;
83 break;
84 }
85
86 if (found != *stringp) {
87 /* There's a non-empty token before the delimiter. */
88 *found = '\0';
89 ret = *stringp;
90 *stringp = found + strlen(delim);
91 break;
92 }
93
94 /*
95 * The delimiter was found at the start of the string,
96 * skip it and keep looking for a non-empty token.
97 */
98 *stringp += strlen(delim);
99 }
100
101 return ret;
102}