Add, forgotten in last commit.
[gnulib.git] / lib / getdomainname.c
index 354aa29..513b4e8 100644 (file)
@@ -13,7 +13,7 @@
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 /* Written by Simon Josefsson.  */
 
 #include "getdomainname.h"
 
 #include <string.h>
+#include <errno.h>
 
 /* Return the NIS domain name of the machine.
    WARNING! The NIS domain name is unrelated to the fully qualified host name
             of the machine.  It is also unrelated to email addresses.
-   WARNING! The NIS domain name is usually the empty string when not using NIS.
+   WARNING! The NIS domain name is usually the empty string or "(none)" when
+            not using NIS.
 
    Put up to LEN bytes of the NIS domain name into NAME.
    Null terminate it if the name is shorter than LEN.
+   If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
    Return 0 if successful, otherwise set errno and return -1.  */
 int
 getdomainname (char *name, size_t len)
 {
-  strncpy (name, "", len);     /* Hardcode your domain name if you want.  */
+  const char *result = "";     /* Hardcode your domain name if you want.  */
+  size_t result_len = strlen (result);
+
+  if (result_len > len)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+  memcpy (name, result, result_len);
+  if (result_len < len)
+    name[result_len] = '\0';
   return 0;
 }