Server for Information Technologies ������ ��������������
������� �������������� ����������
(095) 932-9212, 932-9213, 939-0783
E-mail: [email protected]
������ �������� ����(!) ������������� ���������� CIT Forum CD-ROM

GETOPT(3C)

��������
getopt - ������ ����� �������

���������

	int getopt (argc, argv, optstring)
	int argc;
	char **argv, *optstring;
	
	extern char *optarg;
	extern int optind, opterr;

��������
������� getopt ���������� ��������� ���� �� ������� argv, ��������������� �����-���� ����� � ������� optstring. ������� getopt ������������ ��� ������� ������������ ���������� ������ [��. intro(1), ������� 3 10]. ����� ����� ������� ��������� ���� ��������, ��� ������� ����������� ���������� � �������� ������������ ����� ������� ������������ ������� getopts(1) ��� ����������� ������� getopt.

������� optstring ������ ��������� �����, ������� ����� �������������� ����������, ������������ getopt. ���� �� ������ ������� ���������, �� ��������������, ��� ����� ����� �������� ��� ������ ����������, ���������� �� ����� ��������� ��� �����������.

� ���������� optarg ���������� ��������� �� ������ ��������� ������������ �����.

����� ������� ������ ������� getopt �������� �� ������� ���������� optind ������ ���� �������� ������� argv, ������� ������ �������������� ���������. �������� ���������� optind �� ������� ��������� � ������� getopt ����� 1.

����� ��� ����� ���������� (�� ���� ���������� ������ �� ����������, �� �������� � ������ �����), ��������� ������� getopt ����� -1. ����� ������������ ����������� ����� --, ����� �������� ����� �����. ����� ��� ���������� ��� �������, ����� ���������� �������� -1, � ���� ����� -- ����� ���������.

�����������
������� getopt ������ ��������� �� ������ � ����������� �������� � ���������� �������������� ���� ?, ����� ����������� ����, �� �������� � ������� optstring, ��� �� ����������� ��������� � �����, ��������� ��������. ������ ��������� �� ������ ����� ���� ���������, ��� ���� ���������� opterr ������� ��������� 0.

������ ��������� �������� ��������� ����������, ��� ����� ���������� ��������� �������, ����������� ����������������� ����� a � b, � ����� ����� o, ������� ������ ����� ��������:

     main (argc, argv)
     int argc;
     char **argv;
     {
       int c;
       extern char *optarg;
       extern int optind;
        ...
       while ((c=getopt (argc,argv,"abo:")) != -1)
         switch (c) {
         case 'a':
           if (bflg) 
             errflg++;
           else
             aflg++;
           break;
         case 'b':
           if (aflg) 
             errflg++;
           else
             bproc ();
           break;
         case 'o':
           ofile = optarg;
           break;
         case '?':
           errflg++;
         }
       if (errflg) {
         (void) fprintf (stderr, "usage: ... ");
         exit (2);
       }
       for ( ; optind < argc; optind++) {
         if (access (argv [optind], 4)) {
         ...
     }

���������������
���� ������� ���������� ������� getopt ��������� ��������� ����������� �� ������, �������� ��������� ������ [��. intro(1)], ���� �� ������� ������������, ��� ��� �������� ����������� ����� �� �������������� � ��������� ������� �������. �����, ��� � � �������, a � b ����� ��� ����������, � ����� o ��������� ��������. ���������� ��� �������� ����������� �� ������:

     cmd  -aboxxx file
����� �������� ������� 5: ����� � ���������� �� ������ ���� � ����� ������ � ���������� �������.
     cmd  -ab -oxxx file
�������� ������� 6: ����� �����, ������� ��������� ��������, ������ ���� ������ ��� ���������.

��������� �������� ���������� optind ��� ������ ������� getopt � ���������� ���������� argv ����� �������� � ��������������� �����������.

��. �����
getopts(1), intro(1) � ����������� ������������.
Comments: [email protected]
Designed by Andrey Novikov
Copyright © CIT