-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.c
188 lines (163 loc) · 3.97 KB
/
options.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* This file is part of uxlaunch
*
* (C) Copyright 2009 Intel Corporation
* Authors:
* Auke Kok <[email protected]>
* Arjan van de Ven <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <dirent.h>
#include <pwd.h>
#include "uxlaunch.h"
/* builtin defaults */
int tty = 2;
char session[256] = "/usr/bin/mutter --sm-disable";
char username[256] = "moblin";
int verbose = 0;
int x_session_only = 0;
static struct option opts[] = {
{ "user", 1, NULL, 'u' },
{ "tty", 1, NULL, 't' },
{ "session", 1, NULL, 's' },
{ "help", 0, NULL, 'h' },
{ "verbose", 0, NULL, 'v' },
{ 0, 0, NULL, 0}
};
void usage(const char *name)
{
printf("Usage: %s [OPTION...] [-- [session cmd] [session args]]\n", name);
printf(" -u, --user Start session as specific username\n");
printf(" -t, --tty Start session on alternative tty number\n");
printf(" -s, --session Start a non-default session\n");
printf(" -x, --xsession Start X apps inside an existing X session\n");
printf(" -v, --verbose Display lots of output to the console\n");
printf(" -h, --help Display this help message\n");
}
void get_options(int argc, char **argv)
{
int i = 0;
int c;
FILE *f;
DIR *dir;
struct dirent *entry;
/*
* default fallbacks are listed above in the declations
* each step below overrides them in order
*/
/* try and find a user in /home/ */
dir = opendir("/home");
while (dir) {
char buf[80];
char *u;
struct passwd *p;
entry = readdir(dir);
if (!entry)
break;
if (entry->d_name[0] == '.')
continue;
if (strcmp(entry->d_name, "lost+found")==0)
continue;
if (entry->d_type != DT_DIR)
continue;
u = strdup(entry->d_name);
/* check if this is actually a valid user */
p = getpwnam(u);
if (!p)
continue;
/* and make sure this is actually the guys homedir */
snprintf(buf, 80, "/home/%s", u);
if (strcmp(p->pw_dir, buf))
continue;
strncpy(username, u, 256);
free(u);
}
if (dir)
closedir(dir);
/* parse config file */
f = fopen("/etc/sysconfig/uxlaunch", "r");
if (f) {
char buf[256];
char *key;
char *val;
while (fgets(buf, 80, f) != NULL) {
char *c;
c = strchr(buf, '\n');
if (c) *c = 0; /* remove trailing \n */
if (buf[0] == '#')
continue; /* comment line */
key = strtok(buf, "=");
if (!key)
continue;
val = strtok(NULL, "=");
if (!val)
continue;
// todo: filter leading/trailing whitespace
if (!strcmp(key, "user"))
strncpy(username, val, 256);
if (!strcmp(key, "tty"))
tty = atoi(val);
if (!strcmp(key, "session"))
strncpy(session, val, 256);
}
fclose(f);
}
/* parse cmdline - overrides */
while (1) {
c = getopt_long(argc, argv, "u:t:s:hvx", opts, &i);
if (c == -1)
break;
switch (c) {
case 'u':
strncpy(username, optarg, 256);
break;
case 't':
tty = atoi(optarg);
break;
case 's':
strncpy(session, optarg, 256);
break;
case 'h':
usage(argv[0]);
exit (EXIT_SUCCESS);
break;
case 'v':
verbose = 1;
break;
case 'x':
x_session_only = 1;
break;
default:
break;
}
}
/* Get session command from startup line */
while (i < argc) {
if (!strcmp(argv[i++], "--")) {
session[0] = '\0';
while (i < argc) {
strncat(session, argv[i++], 256 - strlen(session));
if (i != argc)
strncat(session, " ", 256 - strlen(session));
}
}
}
open_log(!x_session_only ? LOGFILE : NULL);
lprintf("uxlaunch v%s started%s.", VERSION, x_session_only ? " for x session only" : "" );
lprintf("user \"%s\", tty #%d, session \"%s\"", username, tty, session);
pass = getpwnam(username);
if (!pass) {
lprintf("Error: can't find user \"%s\"", username);
exit(EXIT_FAILURE);
}
}