# [PATCH] Getopt with longopts and windows support - part 2

- Thread: https://externals.io/message/31469
- Original archive: https://news-web.php.net/php.internals/31469
- Started by David Soria Parra on 2007-08-06T13:57:26+00:00
- Messages: 9
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #31469 — David Soria Parra — 2007-08-06T13:57:26+00:00

URL: https://externals.io/message/31469#31469

Hi internals,

so based on a discussion of php.pecl we thought about using the php_getopt
for the PHP_FUNCTION(getopt) instead of the system provided getopt. Here
is the patch for that. Hopefully someone is reviewing and commiting the
patch as i don't have karma. 

It addes main/getopt.c main/php_getopt.h to the build system and uses the
php_getopt function in the ext/standard/basic_functions.c. Furthermore it
removes all HAVE_GETOPT and HARTMUT_0 switches. it also enable longopts
for both win and linux in the form
 
$opt = getopt("a", array("param:", "param2"));

Where ":" in the longopts marks that the parameter takes an argument.

The main/getopt.c and main/php_getopt.h is in fact taken from
sapi/cli/getopt.c and sapi/cli/php_getopt.h but is slightly modified.

http://experimentalworks.net/~dsp/getopt.patch.tar.gz

It also runs on windows.

To apply the patch:
 1) remove 
       sapi/cli/getopt.c sapi/cli/php_getopt.h 
       sapi/cgi/getopt.c sapi/cgi/php_getopt.h

 2) add main/getopt.c and main/php_getopt.h frm the archive
 3) apply the patch.

---

## #31475 — Marcus Boerger — 2007-08-07T12:22:52+00:00

URL: https://externals.io/message/31469#31475 · In reply to: #31469

Hello Jani,

  David has a point here. So first of all we should move getopt
implementation out of CLI/CGI and into main. Could you do that
first Jani?

marcus

Monday, August 6, 2007, 3:57:26 PM, you wrote:

> Hi internals,

> so based on a discussion of php.pecl we thought about using the php_getopt
> for the PHP_FUNCTION(getopt) instead of the system provided getopt. Here
> is the patch for that. Hopefully someone is reviewing and commiting the
> patch as i don't have karma. 

> It addes main/getopt.c main/php_getopt.h to the build system and uses the
> php_getopt function in the ext/standard/basic_functions.c. Furthermore it
> removes all HAVE_GETOPT and HARTMUT_0 switches. it also enable longopts
> for both win and linux in the form
>  
> $opt = getopt("a", array("param:", "param2"));

> Where ":" in the longopts marks that the parameter takes an argument.

> The main/getopt.c and main/php_getopt.h is in fact taken from
> sapi/cli/getopt.c and sapi/cli/php_getopt.h but is slightly modified.

> http://experimentalworks.net/~dsp/getopt.patch.tar.gz

> It also runs on windows.

> To apply the patch:
>  1) remove 
>        sapi/cli/getopt.c sapi/cli/php_getopt.h 
>        sapi/cgi/getopt.c sapi/cgi/php_getopt.h

>  2) add main/getopt.c and main/php_getopt.h frm the archive
>  3) apply the patch.




Best regards,
 Marcus

---

## #31476 — Jani Taskinen — 2007-08-07T14:59:56+00:00

URL: https://externals.io/message/31469#31476 · In reply to: #31475

Yes, I know about this. I was wondering about this thing long time
ago..just never got around doing anything about it.. :D

But let's wait for PHP 5.3 branch to get opened first. 

--Jani



> Hello Jani,
> 
>   David has a point here. So first of all we should move getopt
> implementation out of CLI/CGI and into main. Could you do that
> first Jani?
> 
> marcus
> 
> Monday, August 6, 2007, 3:57:26 PM, you wrote:
> 
> > […]
> 
> > […]
> 
> > […]
> 
> > […]
> 
> > […]
> 
> > […]
> 
> > […]
> 
> > […]
> 
> > […]
> 
> 
> 
> 
> Best regards,
>  Marcus

---

## #31477 — johannes@php.net — 2007-08-07T15:46:55+00:00

URL: https://externals.io/message/31469#31477 · In reply to: #31469

David,


> Hi internals,
> 
> so based on a discussion of php.pecl we thought about using the php_getopt
> for the PHP_FUNCTION(getopt) instead of the system provided getopt. Here
> is the patch for that. Hopefully someone is reviewing and commiting the
> patch as i don't have karma. 

The idea is good imo, but there are a few things we have to do:

- We need more tests, as far as I saw using grep we just have a single
  testcase for `getopt()` which is certainly not enough when replacing the
  implementation.

- The current implementation is not thread-safe, I don't know about the
  glibc implementation but if we use our own implementation for
  PHP's getopt function that should be threadsafe.

- If you move php_getopt to main you have to mark it as PHPAPI so the
  SAPIs can use it.

Maybe there's more, didn't do a deeper review of the patch, yet, since
I'd like to wait a) until Jani moved the files and b) we have more
tests :-)

johannes

---

## #31479 — johannes@php.net — 2007-08-07T17:12:45+00:00

URL: https://externals.io/message/31469#31479

> Thread safe for CGI and CLI ? :)
> Or do you find the function useful outside those SAPIs?

Well David's plan is to use the implementation for PHP-level `getopt()`
and I can imagine doing crazy stuff, so if we can make it thread-safe in
a good way we should do that - I've seen to many people doing too crazy
stuff...

Of course that's not high priority...

johannes
> --Jani
> 
> 
> Johannes Schlüter kirjoitti:
> > […]

---

## #31480 — Stanislav Malyshev — 2007-08-07T20:02:10+00:00

URL: https://externals.io/message/31469#31480 · In reply to: #31475

>   David has a point here. So first of all we should move getopt
> implementation out of CLI/CGI and into main. Could you do that
> first Jani?

Why one would need getopt outside of cgi/cli? I.e. where would these 
options come from?
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #31481 — David Soria Parra — 2007-08-07T20:26:52+00:00

URL: https://externals.io/message/31469#31481 · In reply to: #31480

> Why one would need getopt outside of cgi/cli? I.e. where would these 
> options come from?

initially the idea was to use getopt on windows machines. After proposing
my patch which just includes the NetBSD `getopt()`, there was a
recommandation in php.pecl to use the php_getopt instead of the system
provided getopt to implement the PHP_FUNCTION(getopt). 

This imho makes sense. We got windows support as well as long parameter
support. As we use it in ext/standard/basic_functions.c we have to move
the getopt.c out of sapi/cli.

---

## #31482 — johannes@schlueters.de — 2007-08-07T21:01:13+00:00

URL: https://externals.io/message/31469#31482 · In reply to: #31480

> > […]
> 
> Why one would need getopt outside of cgi/cli? I.e. where would these 
> options come from?

Having the file in CLI as well as in CGI isn't good for maintaining it,
therefore we should move it imo.

johannes

---

## #32516 — Jani Taskinen — 2007-10-01T14:05:28+00:00

URL: https://externals.io/message/31469#32516 · In reply to: #31469

Patch applied to HEAD / PHP_5_3.

--Jani


> Hi internals,
> 
> so based on a discussion of php.pecl we thought about using the php_getopt
> for the PHP_FUNCTION(getopt) instead of the system provided getopt. Here
> is the patch for that. Hopefully someone is reviewing and commiting the
> patch as i don't have karma. 
> 
> It addes main/getopt.c main/php_getopt.h to the build system and uses the
> php_getopt function in the ext/standard/basic_functions.c. Furthermore it
> removes all HAVE_GETOPT and HARTMUT_0 switches. it also enable longopts
> for both win and linux in the form
>  
> $opt = getopt("a", array("param:", "param2"));
> 
> Where ":" in the longopts marks that the parameter takes an argument.
> 
> The main/getopt.c and main/php_getopt.h is in fact taken from
> sapi/cli/getopt.c and sapi/cli/php_getopt.h but is slightly modified.
> 
> http://experimentalworks.net/~dsp/getopt.patch.tar.gz
> 
> It also runs on windows.
> 
> To apply the patch:
>  1) remove 
>        sapi/cli/getopt.c sapi/cli/php_getopt.h 
>        sapi/cgi/getopt.c sapi/cgi/php_getopt.h
> 
>  2) add main/getopt.c and main/php_getopt.h frm the archive
>  3) apply the patch.
> 
-- 
Patches/Donations: http://pecl.php.net/~jani/
