aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Dijkstra <tim@famdijkstra.org>2007-05-13 17:53:13 +0000
committerTim Dijkstra <tim@famdijkstra.org>2007-05-13 17:53:13 +0000
commit586e8a6437d31811c7eee4fc6d39919dc6c0d531 (patch)
treeb304ad5ebc28b5ebe81ec42213dee3d66bb2d112
parentb4c7bbade05ed2cbad39b147cee1a00b2a81e810 (diff)
downloadsuspend-utils-586e8a6437d31811c7eee4fc6d39919dc6c0d531.tar.gz
Fix handeling of long options. The recently introduced comments would make
all invocations with long options segfault.
-rw-r--r--config.c24
-rw-r--r--config.h7
-rw-r--r--resume.c30
-rw-r--r--s2ram.c27
-rw-r--r--s2ram.h43
-rw-r--r--suspend.c39
6 files changed, 102 insertions, 68 deletions
diff --git a/config.c b/config.c
index 0698de6..b038bff 100644
--- a/config.c
+++ b/config.c
@@ -104,23 +104,27 @@ int parse(char *my_name, char *file_name, int parc, struct config_par *parv)
return error;
}
-void usage(char *my_name, struct option_descr *options, const char *short_options)
+/* We're abusing struct option a bit. usage() expects an \0 in the
+ * name string, and after that a comment.
+ */
+void usage(char *my_name, struct option *options, const char *short_options)
{
- struct option_descr *opt;
+ struct option *opt;
printf("Usage: %s [options]", my_name);
- for (opt = options; opt->o.name; opt++)
+ for (opt = options; opt->name; opt++)
{
- if (strchr(short_options,opt->o.val))
- printf("\n -%c, --%s", opt->o.val, opt->o.name);
+ const char *descr = opt->name + strlen(opt->name) + 1;
+ if (strchr(short_options,opt->val))
+ printf("\n -%c, --%s", opt->val, opt->name);
else
- printf("\n --%s", opt->o.name);
+ printf("\n --%s", opt->name);
- if (opt->o.has_arg)
- printf(" <%s>", opt->o.name);
+ if (opt->has_arg)
+ printf(" <%s>", opt->name);
- if (strlen(opt->descr))
- printf("\t%s",opt->descr);
+ if (strlen(descr))
+ printf("\t%s",descr);
}
printf("\n");
diff --git a/config.h b/config.h
index 63d956f..6fb5141 100644
--- a/config.h
+++ b/config.h
@@ -21,13 +21,8 @@ struct config_par {
unsigned int len;
};
-struct option_descr {
- struct option o;
- const char *descr;
-};
-
int parse(char *my_name, char *file_name, int parc, struct config_par *parv);
-void usage(char *my_name, struct option_descr options[], const char *short_options);
+void usage(char *my_name, struct option options[], const char *short_options);
#define CONFIG_FILE "/etc/suspend.conf"
diff --git a/resume.c b/resume.c
index 45fd26d..bf68678 100644
--- a/resume.c
+++ b/resume.c
@@ -735,16 +735,24 @@ static int read_image(int dev, int fd, struct swsusp_header *swsusp_header)
/* Parse the command line and/or configuration file */
static inline int get_config(int argc, char *argv[])
{
- static struct option_descr options[] = {
- { { "help", no_argument, NULL, 'h'},
- "\t\t\tthis text." },
- { { "config", required_argument, NULL, 'f'},
- "\t\talternative configuration file." },
- { { "resume_device", required_argument, NULL, 'r'},
- "device that contains swap area"},
- { { "resume_offset", required_argument, NULL, 'o'},
- "offset of swap file in resume device."},
- { { NULL, 0, NULL, 0 }, ""}
+ static struct option options[] = {
+ {
+ "help\0\t\t\tthis text",
+ no_argument, NULL, 'h'
+ },
+ {
+ "config\0\t\talternative configuration file.",
+ required_argument, NULL, 'f'
+ },
+ {
+ "resume_device\0device that contains swap area",
+ required_argument, NULL, 'r'
+ },
+ {
+ "resume_offset\0offset of swap file in resume device.",
+ required_argument, NULL, 'o'
+ },
+ { NULL, 0, NULL, 0 }
};
int i, error;
char *conf_name = CONFIG_FILE;
@@ -754,7 +762,7 @@ static inline int get_config(int argc, char *argv[])
char *rdev = NULL;
const char *optstring = "hf:o:r:";
- while ((i = getopt_long(argc, argv, optstring, (struct option *)options, NULL)) != -1) {
+ while ((i = getopt_long(argc, argv, optstring, options, NULL)) != -1) {
switch (i) {
case 'h':
usage("resume", options, optstring);
diff --git a/s2ram.c b/s2ram.c
index 270cd86..10ff342 100644
--- a/s2ram.c
+++ b/s2ram.c
@@ -345,20 +345,25 @@ int main(int argc, char *argv[])
{
int i, id = -1, ret = 0, test_mode = 0;
int active_console = -1;
- struct option_descr options[] = {
- { { "help", no_argument, NULL, 'h'},
- "\tthis text." },
- { { "test", no_argument, NULL, 'n'},
- "\ttest if the machine is in the database." },
- { { "identify", no_argument, NULL, 'i'},
- "prints a string that identifies the machine." },
- HACKS_LONG_OPTS,
- { { NULL, 0, NULL, 0 },
- "" }
+ struct option options[] = {
+ {
+ "help\0\tthis text.",
+ no_argument, NULL, 'h'
+ },
+ {
+ "test\0\ttest if the machine is in the database.",
+ no_argument, NULL, 'n'
+ },
+ {
+ "identify\0prints a string that identifies the machine.",
+ no_argument, NULL, 'i'
+ },
+ HACKS_LONG_OPTS,
+ { NULL, 0, NULL, 0 }
};
const char *optstring = "hni" "fspmrva:";
- while ((i = getopt_long(argc, argv, optstring, (struct option *)options, NULL)) != -1) {
+ while ((i = getopt_long(argc, argv, optstring, options, NULL)) != -1) {
switch (i) {
case 'h':
usage("s2ram", options, optstring);
diff --git a/s2ram.h b/s2ram.h
index 20d8dc4..7618515 100644
--- a/s2ram.h
+++ b/s2ram.h
@@ -21,18 +21,31 @@ void s2ram_resume(void);
void s2ram_add_flag(int opt, const char *arg);
#define HACKS_LONG_OPTS \
- { { "force", no_argument, NULL, 1}, \
- "\tforce suspending, even on unknown machines.\n\nThe following options are only available with --force:" }, \
- { { "vbe_save", no_argument, NULL, 2}, \
- "\tsave VBE state before suspending and restore after resume."}, \
- { { "vbe_post", no_argument, NULL, 3}, \
- "\tVBE POST the graphics card after resume."}, \
- { { "vbe_mode", no_argument, NULL, 4}, \
- "\tget VBE mode before suspend and set it after resume."}, \
- { { "radeontool", no_argument, NULL, 5}, \
- "\tturn off the backlight on radeons before suspending."}, \
- { { "pci_save", no_argument, NULL, 6}, \
- "\tsave the PCI config space for the VGA card."}, \
- { { "acpi_sleep", required_argument, NULL, 7}, \
- "set the acpi_sleep parameter before suspend\n\t\t\t1=s3_bios, 2=s3_mode, 3=both" }
-
+ {\
+ "force\0\tforce suspending, even on unknown machines.\n\nThe following options are only available with --force:",\
+ no_argument, NULL, 1 \
+ },\
+ {\
+ "vbe_save\0\tsave VBE state before suspending and restore after resume.",\
+ no_argument, NULL, 2 \
+ },\
+ {\
+ "vbe_post\0\tVBE POST the graphics card after resume.",\
+ no_argument, NULL, 3 \
+ },\
+ {\
+ "vbe_mode\0\tget VBE mode before suspend and set it after resume.",\
+ no_argument, NULL, 4 \
+ },\
+ {\
+ "radeontool\0\tturn off the backlight on radeons before suspending.",\
+ no_argument, NULL, 5 \
+ },\
+ {\
+ "pci_save\0\tsave the PCI config space for the VGA card.",\
+ no_argument, NULL, 6 \
+ },\
+ {\
+ "acpi_sleep\0set the acpi_sleep parameter before suspend\n\t\t\t1=s3_bios, 2=s3_mode, 3=both",\
+ required_argument, NULL, 7 \
+ }
diff --git a/suspend.c b/suspend.c
index 221c80e..d355e33 100644
--- a/suspend.c
+++ b/suspend.c
@@ -1168,22 +1168,31 @@ static int lock_vt(void)
/* Parse the command line and/or configuration file */
static inline int get_config(int argc, char *argv[])
{
- static struct option_descr options[] = {
- { { "help", no_argument, NULL, 'h'},
- "\t\t\tthis message."},
- { { "config", required_argument, NULL, 'f'},
- "\t\talternative configuration file."},
- { { "image_size", required_argument, NULL, 's'},
- "\tdesired size of the image."},
- { { "resume_device", required_argument, NULL, 'r'},
- "device that contains swap area."},
- { { "resume_offset", required_argument, NULL, 'o'},
- "offset of swap file in resume device."},
+ static struct option options[] = {
+ {
+ "help\0\t\t\tthis text",
+ no_argument, NULL, 'h'
+ },
+ {
+ "config\0\t\talternative configuration file.",
+ required_argument, NULL, 'f'
+ },
+ {
+ "resume_device\0device that contains swap area",
+ required_argument, NULL, 'r'
+ },
+ {
+ "resume_offset\0offset of swap file in resume device.",
+ required_argument, NULL, 'o'
+ },
+ {
+ "image_size\0\tdesired size of the image.",
+ required_argument, NULL, 's'
+ },
#ifdef CONFIG_BOTH
- HACKS_LONG_OPTS,
+ HACKS_LONG_OPTS,
#endif
- { { NULL, 0, NULL, 0 },
- ""}
+ { NULL, 0, NULL, 0 }
};
int i, error;
char *conf_name = CONFIG_FILE;
@@ -1195,7 +1204,7 @@ static inline int get_config(int argc, char *argv[])
int set_rdev = 0;
const char *optstring = "hf:s:o:r:";
- while ((i = getopt_long(argc, argv, optstring, (struct option *) options, NULL)) != -1) {
+ while ((i = getopt_long(argc, argv, optstring, options, NULL)) != -1) {
switch (i) {
case 'h':
usage(my_name, options, optstring);