ALT Linux Community general discussions
 help / color / mirror / Atom feed
* [Comm] segfault with getopt_long
@ 2005-03-14  8:18 Alexey Morsov
  2005-03-14  9:05 ` Alexey I. Froloff
  0 siblings, 1 reply; 5+ messages in thread
From: Alexey Morsov @ 2005-03-14  8:18 UTC (permalink / raw)
  To: ALT Linux Community

Привет,

пишу проггу на Си. Для обработки опций запуска использую <getopt.h>
и функцию getopt_long...
Все работает, НО! если дать неверную опцию в коротком виде (-e) то как 
и положенно выдается invalid option
А если дать неверную опцию в длинном виде (--eeee) то вылетает segfault
Из-за чего может быть такое?

======== Лист ==========
static struct option long_options[] = {
    {"start", 0, 0, 0},
    {"stop", 0, 0, 0},
    {"config", 1, 0, 0},
    {"log", 1, 0, 0},
    {"help", 0, 0, 0}
};

int main(int argc, char **argv)
{
    FILE *flog, *fpid;
    pid_t pid;
    struct passwd *pwd;
    char ver[6];
    int c, option_index=0;

    sscanf(VERSION, "%*[$]%*s %[0-9.] %*[$]", ver);
    printf("trafficd %s (c) Morsov, 2005. Distributed under GPL 
license\n\n", ver);

    c = getopt_long(argc, argv, "dsc:l:h", long_options, &option_index);

    if (c == -1) {
       print_help();
       exit(0);
    }

    switch (c) {
       case 0:
          printf ("option %s", long_options[option_index].name);
          if (optarg)
             printf (" with arg %s", optarg);
          printf ("\n");
          exit(0);
       case 'd':
          printf("option d\n");
          exit(0);
       case 's':
          printf("option s\n");
          exit(0);
       case 'c':
          printf("option c with arg = '%s'\n", optarg);
          exit(0);
       case 'l':
          printf("optarg l with arg = '%s'\n", optarg);
          exit(0);
       case 'h':
          print_help();
          exit(0);
    }

    return(0);
}

========================


-- 
Всего наилучшего,
Системный Администратор ЗАО "ИК "РИКОМ-ТРАСТ"
Алексей Морсов
ICQ: 196766290
Jabber: Samurai@jabber.pibhe.com
http://www.ricom.ru
http://www.fondmarket.ru


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Comm] segfault with getopt_long
  2005-03-14  8:18 [Comm] segfault with getopt_long Alexey Morsov
@ 2005-03-14  9:05 ` Alexey I. Froloff
  2005-03-14  9:30   ` Alexey Morsov
  2005-03-14 11:52   ` Alexey Morsov
  0 siblings, 2 replies; 5+ messages in thread
From: Alexey I. Froloff @ 2005-03-14  9:05 UTC (permalink / raw)
  To: ALT Linux Community

[-- Attachment #1: Type: text/plain, Size: 785 bytes --]

* Alexey Morsov <samurai@> [050314 11:19]:
> А если дать неверную опцию в длинном виде (--eeee) то вылетает segfault
> Из-за чего может быть такое?

> ======== Лист ==========
> static struct option long_options[] = {
>    {"start", 0, 0, 0},
>    {"stop", 0, 0, 0},
>    {"config", 1, 0, 0},
>    {"log", 1, 0, 0},
>    {"help", 0, 0, 0}
> };
Почитайте info libc на тему Getopt Long Option Example.

P.S. hint: как getopt_long узнает что закончились описания опций?

-- 
Regards, Sir Raorn.
-------------------
Концепция целостного репозитария такова, что статическим библиотекам
в нем не место (имеются исключения).  Помимо разделяемости, динамические
библиотеки дают прозрачные зависимости, возможность асинхронной
поддержки пакетов и т.д.
		-- at in devel@

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Comm] segfault with getopt_long
  2005-03-14  9:05 ` Alexey I. Froloff
@ 2005-03-14  9:30   ` Alexey Morsov
  2005-03-14 11:52   ` Alexey Morsov
  1 sibling, 0 replies; 5+ messages in thread
From: Alexey Morsov @ 2005-03-14  9:30 UTC (permalink / raw)
  To: community


Alexey I. Froloff wrote:
> * Alexey Morsov <samurai@> [050314 11:19]:
> 
>>А если дать неверную опцию в длинном виде (--eeee) то вылетает segfault
>>Из-за чего может быть такое?
> 
> 
>>======== Лист ==========
>>static struct option long_options[] = {
>>   {"start", 0, 0, 0},
>>   {"stop", 0, 0, 0},
>>   {"config", 1, 0, 0},
>>   {"log", 1, 0, 0},
>>   {"help", 0, 0, 0}
>>};
> 
> Почитайте info libc на тему Getopt Long Option Example.
> 
> P.S. hint: как getopt_long узнает что закончились описания опций?
Точно.... поправил - все заработало.
Спасибо. :)
Только вот как я понимаю... разбирать опции (т.е. что программе делать в 
зависимости от тех или иных опций) лучше в ветке switch?


-- 
Всего наилучшего,
Системный Администратор ЗАО "ИК "РИКОМ-ТРАСТ"
Алексей Морсов
ICQ: 196766290
Jabber: Samurai@jabber.pibhe.com
http://www.ricom.ru
http://www.fondmarket.ru


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Comm] segfault with getopt_long
  2005-03-14  9:05 ` Alexey I. Froloff
  2005-03-14  9:30   ` Alexey Morsov
@ 2005-03-14 11:52   ` Alexey Morsov
  2005-03-14 11:59     ` Alexey I. Froloff
  1 sibling, 1 reply; 5+ messages in thread
From: Alexey Morsov @ 2005-03-14 11:52 UTC (permalink / raw)
  To: community


Alexey I. Froloff wrote:
> Почитайте info libc на тему Getopt Long Option Example.

Кстати почитал... не понял только как можно заставить программу выдавать 
  "try use --help" в случае если опция указана левая?

-- 
Всего наилучшего,
Системный Администратор ЗАО "ИК "РИКОМ-ТРАСТ"
Алексей Морсов
ICQ: 196766290
Jabber: Samurai@jabber.pibhe.com
http://www.ricom.ru
http://www.fondmarket.ru


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Comm] segfault with getopt_long
  2005-03-14 11:52   ` Alexey Morsov
@ 2005-03-14 11:59     ` Alexey I. Froloff
  0 siblings, 0 replies; 5+ messages in thread
From: Alexey I. Froloff @ 2005-03-14 11:59 UTC (permalink / raw)
  To: ALT Linux Community

[-- Attachment #1: Type: text/plain, Size: 502 bytes --]

* Alexey Morsov <samurai@> [050314 14:54]:
> > Почитайте info libc на тему Getopt Long Option Example.
> Кстати почитал... не понял только как можно заставить программу выдавать 
>  "try use --help" в случае если опция указана левая?
default: в switch() { }

-- 
Regards, Sir Raorn.
-------------------
> У меня немного голова перегрелась при разборке кода :)
Писать патч на перегретую голову опасно для вашего здоровья. :)
Для моего тоже не полезно такие патчи читать.
		-- ldv in devel@

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-03-14 11:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-14  8:18 [Comm] segfault with getopt_long Alexey Morsov
2005-03-14  9:05 ` Alexey I. Froloff
2005-03-14  9:30   ` Alexey Morsov
2005-03-14 11:52   ` Alexey Morsov
2005-03-14 11:59     ` Alexey I. Froloff

ALT Linux Community general discussions

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/community/0 community/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 community community/ http://lore.altlinux.org/community \
		mandrake-russian@linuxteam.iplabs.ru community@lists.altlinux.org community@lists.altlinux.ru community@lists.altlinux.com
	public-inbox-index community

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.community


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git