[Ace-users] [ace-users] Proposal: ACE_OS::{get,set}progname()

J.T. Conklin jtc at acorntoolworks.com
Thu Oct 25 23:22:23 CDT 2007


Some years ago NetBSD added the {get,set}progname() functions, which
have since been adopted by the other *BSD's, Darwin, Cygwin, Newlib,
etc.

The getprogname() function is used to obtain the program name.  This
avoids the need to stash argv[0] away in a global variable, or passing
it as function arguments, etc.  On NetBSD, and most other systems that
support these two functions, the program name is set by the C/C++
startup code (this is how it's available to static constructors).

The setprogname() function was added for systems that don't have this
C startup magic.  On those systems, setprogname() must be called from
main with argv[0] to initialize the program name.  This isn't perfect,
it doesn't handle static constructors, but it works well enough in
practice.

I added ACE_OS::getprogname() and ACE_OS::setprogname() wrapper facade
functions to our local ACE repository while porting our system, which
used getprogname(), to Solaris, which doesn't support it.

These wrappers are small enough that I'm especially worried about
managing ongoing divergence between the DOC group and our repository
(at least not for this one change). On the other hand, the footprint
is miniscule and it may be useful to others.  While I'd like to see it
integrated, I guess don't feel that strongly.  Any opinions one way or
the other?

    --jtc

Index: OS_NS_stdlib.cpp
===================================================================
--- OS_NS_stdlib.cpp	(revision 79869)
+++ OS_NS_stdlib.cpp	(working copy)
@@ -774,4 +774,28 @@
 }
 #endif /* ACE_LACKS_MKSTEMP */
 
+#if defined (ACE_LACKS_GETPROGNAME) || defined (ACE_LACKS_SETPROGNAME)
+static const char *__progname = 0;
+#endif /* ACE_LACKS_GETPROGNAME || ACE_LACKS_SETPROGNAME */
+
+#if defined (ACE_LACKS_GETPROGNAME)
+const char*
+ACE_OS::getprogname_emulation ()
+{
+    return __progname;
+}
+#endif /* ACE_LACKS_GETPROGNAME */
+
+#if defined (ACE_LACKS_SETPROGNAME)
+void
+ACE_OS::setprogname_emulation (const char* progname) 
+{
+  const char *p = ACE_OS::strrchr (progname);
+  if (p != 0)
+    __progname = p + 1;
+  else
+    __progname = progname;
+}
+#endif /* ACE_LACKS_SETPROGNAME */
+
 ACE_END_VERSIONED_NAMESPACE_DECL
Index: OS_NS_stdlib.h
===================================================================
--- OS_NS_stdlib.h	(revision 79869)
+++ OS_NS_stdlib.h	(working copy)
@@ -275,6 +275,28 @@
   ACE_NAMESPACE_INLINE_FUNCTION
   int system (const ACE_TCHAR *s);
 
+  /// Get the name of the current program
+  ///
+  /// Originally from NetBSD, now found in *BSD, Cygwin, Darwin, etc.
+  ACE_NAMESPACE_INLINE_FUNCTION
+  const char *getprogname ();
+
+#if defined (ACE_LACKS_GETPROGNAME)
+  extern ACE_Export
+  const char *getprogname_emulation ();
+#endif
+
+  /// Set the name of the current program
+  ///
+  /// Originally from NetBSD, now found in *BSD, Cygwin, Darwin, etc.
+  ACE_NAMESPACE_INLINE_FUNCTION
+  void setprogname (const char* name);
+
+#if defined (ACE_LACKS_SETPROGNAME)
+  extern ACE_Export
+  void setprogname_emulation (const char* name);
+#endif
+
 } /* namespace ACE_OS */
 
 ACE_END_VERSIONED_NAMESPACE_DECL
Index: OS_NS_stdlib.inl
===================================================================
--- OS_NS_stdlib.inl	(revision 79869)
+++ OS_NS_stdlib.inl	(working copy)
@@ -476,4 +476,24 @@
 #endif /* ACE_LACKS_SYSTEM */
 }
 
+ACE_INLINE const char*
+ACE_OS::getprogname ()
+{
+#if defined (ACE_LACKS_GETPROGNAME)
+  return ACE_OS::getprogname_emulation ();
+#else
+  return ::getprogname ();
+#endif
+}
+
+ACE_INLINE void
+ACE_OS::setprogname (const char* name)
+{
+#if defined (ACE_LACKS_SETPROGNAME)
+  ACE_OS::setprogname_emulation (name);
+#else
+  ::setprogname (name);
+#endif
+}
+
 ACE_END_VERSIONED_NAMESPACE_DECL



-- 
J.T. Conklin



More information about the Ace-users mailing list