- int datadir_len = strlen (GDB_DATADIR);
- int sys_gdbinit_len = strlen (SYSTEM_GDBINIT);
- char *relocated_sysgdbinit;
-
- /* If SYSTEM_GDBINIT lives in data-directory, and data-directory
- has been provided, search for SYSTEM_GDBINIT there. */
- if (gdb_datadir_provided
- && datadir_len < sys_gdbinit_len
- && filename_ncmp (SYSTEM_GDBINIT, GDB_DATADIR, datadir_len) == 0
- && IS_DIR_SEPARATOR (SYSTEM_GDBINIT[datadir_len]))
- {
- /* Append the part of SYSTEM_GDBINIT that follows GDB_DATADIR
- to gdb_datadir. */
- char *tmp_sys_gdbinit = xstrdup (SYSTEM_GDBINIT + datadir_len);
- char *p;
-
- for (p = tmp_sys_gdbinit; IS_DIR_SEPARATOR (*p); ++p)
- continue;
- relocated_sysgdbinit = concat (gdb_datadir, SLASH_STRING, p,
- (char *) NULL);
- xfree (tmp_sys_gdbinit);
- }
- else
- {
- relocated_sysgdbinit = relocate_path (gdb_program_name,
- SYSTEM_GDBINIT,
- SYSTEM_GDBINIT_RELOCATABLE);
- }
- if (relocated_sysgdbinit && stat (relocated_sysgdbinit, &s) == 0)
- sysgdbinit = relocated_sysgdbinit;
- else
- xfree (relocated_sysgdbinit);
+ std::string relocated_sysgdbinit
+ = relocate_gdbinit_path_maybe_in_datadir
+ (SYSTEM_GDBINIT, SYSTEM_GDBINIT_RELOCATABLE);
+ if (!relocated_sysgdbinit.empty ()
+ && stat (relocated_sysgdbinit.c_str (), &s) == 0)
+ sysgdbinit.push_back (relocated_sysgdbinit);
+ }
+ if (SYSTEM_GDBINIT_DIR[0])
+ {
+ std::string relocated_gdbinit_dir
+ = relocate_gdbinit_path_maybe_in_datadir
+ (SYSTEM_GDBINIT_DIR, SYSTEM_GDBINIT_DIR_RELOCATABLE);
+ if (!relocated_gdbinit_dir.empty ()) {
+ gdb_dir_up dir (opendir (relocated_gdbinit_dir.c_str ()));
+ if (dir != nullptr)
+ {
+ std::vector<std::string> files;
+ for (;;)
+ {
+ struct dirent *ent = readdir (dir.get ());
+ if (ent == nullptr)
+ break;
+ std::string name (ent->d_name);
+ if (name == "." || name == "..")
+ continue;
+ /* ent->d_type is not available on all systems (e.g. mingw,
+ Solaris), so we have to call stat(). */
+ std::string filename
+ = relocated_gdbinit_dir + SLASH_STRING + name;
+ if (stat (filename.c_str (), &s) != 0
+ || !S_ISREG (s.st_mode))
+ continue;
+ const struct extension_language_defn *extlang
+ = get_ext_lang_of_file (filename.c_str ());
+ /* We effectively don't support "set script-extension
+ off/soft", because we are loading system init files here,
+ so it does not really make sense to depend on a
+ setting. */
+ if (extlang != nullptr && ext_lang_present_p (extlang))
+ files.push_back (std::move (filename));
+ }
+ std::sort (files.begin (), files.end ());
+ sysgdbinit.insert (sysgdbinit.end (),
+ files.begin (), files.end ());
+ }
+ }