# Patch for command-line version of PHP

- Thread: https://externals.io/message/3738
- Original archive: https://news-web.php.net/php.internals/3738
- Started by Vesselin Atanasov on 2003-08-05T20:47:53+00:00
- Messages: 2
- 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

---

## #3738 — Vesselin Atanasov — 2003-08-05T20:47:53+00:00

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

Hello.
Here is a patch for the command-line version of PHP which makes it `chdir()`
to the
script directory just like the web version of PHP does. It also implements
the -C
option which has been a NOOP up to now.

diff -ruN php5-200308051930.orig/sapi/cli/php_cli.c
php5-200308051930/sapi/cli/php_cli.c
--- php5-200308051930.orig/sapi/cli/php_cli.c   2003-08-01
17:08:26.000000000 +0000
+++ php5-200308051930/sapi/cli/php_cli.c        2003-08-05
23:39:38.000000000 +0000
@@ -360,6 +360,7 @@
                    "       %s [options] -- [args...]\n"
                    "\n"
                                "  -a               Run interactively\n"
+                               "  -C               Don't chdir to the
script directory\n"
                                "  -c <path>|<file> Look for php.ini file in
this directory\n"
                                "  -n               No php.ini file will be
used\n"
                                "  -d foo[=bar]     Define INI entry foo
with value 'bar'\n"
@@ -527,6 +528,8 @@
        const char *param_error=NULL;
        int scan_input = 0;
        int hide_argv = 0;
+       int no_chdir=0;
+       char *new_cwd;
 /* end of temporary locals */
 #ifdef ZTS
        zend_compiler_globals *compiler_globals;
@@ -685,7 +688,7 @@
                                break;

                        case 'C': /* don't chdir to the script directory */
-                               /* This is default so NOP */
+                               no_chdir = 1;
                                break;
                        case 'd': /* define ini entries on command line */
                                define_command_line_ini_entry(optarg);
@@ -902,6 +905,12 @@
                zend_llist_destroy(&global_vars);

                PG(during_request_startup) = 0;
+               if (!no_chdir && strcmp(file_handle.filename, "-")) {
+                       new_cwd = strdup(file_handle.filename);
+                       php_dirname(new_cwd, strlen(new_cwd));
+                       chdir(new_cwd);
+                       free(new_cwd);
+               }
                switch (behavior) {
                case PHP_MODE_STANDARD:
                        if (strcmp(file_handle.filename, "-")) {

---

## #3740 — Edin Kadribasic — 2003-08-05T21:29:06+00:00

URL: https://externals.io/message/3738#3740

> Hello Vesselin,
>
> Tuesday, August 5, 2003, 10:47:53 PM, you wrote:
>
> VA> Hello.
> VA> Here is a patch for the command-line version of PHP which makes it
 `chdir()`
> VA> to the
> VA> script directory just like the web version of PHP does. It also
implements
> VA> the -C
> VA> option which has been a NOOP up to now.
>
>
> See README file of sapi/cli. This won't be changed. Maybe a patch that
does
> the other way round is ok -> a patch that adds a switch to chdir to the
> script.

No need for that. CLI was specifically designed not to chdir to scripts
directory. If you want that one liner would solve it for you:

chdir(dirname(__FILE__));

Edin
