]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/_helpers.c
bup: add own gethostname() wrapper
[bup.git] / lib / bup / _helpers.c
index 4a6aaf592986cd63f0fed918e2b646af258dddf6..946ff4ad63763ba61847d44e5092502070a1eb05 100644 (file)
@@ -1862,6 +1862,20 @@ static PyObject *bup_getgrnam(PyObject *self, PyObject *args)
     return result;
 }
 
+static PyObject *bup_gethostname(PyObject *mod, PyObject *ignore)
+{
+#ifdef HOST_NAME_MAX
+    char buf[HOST_NAME_MAX + 1] = {};
+#else
+    /* 'SUSv2 guarantees that "Host names are limited to 255 bytes".' */
+    char buf[256] = {};
+#endif
+
+    if (gethostname(buf, sizeof(buf) - 1))
+        return PyErr_SetFromErrno(PyExc_IOError);
+    return PyBytes_FromString(buf);
+}
+
 static PyMethodDef helper_methods[] = {
     { "write_sparsely", bup_write_sparsely, METH_VARARGS,
       "Write buf excepting zeros at the end. Return trailing zero count." },
@@ -1949,6 +1963,8 @@ static PyMethodDef helper_methods[] = {
       "Return the group database entry for the given group name,"
       " as a tuple with all C strings as bytes(), or None if the group does"
       " not exist." },
+    { "gethostname", bup_gethostname, METH_NOARGS,
+      "Return the current hostname (as bytes)" },
     { NULL, NULL, 0, NULL },  // sentinel
 };