mv: implement -n option
[oweals/busybox.git] / libbb / xfuncs_printf.c
index 03aeaaa380aaf0cf2656c6cbc303833381ecb3e2..56ee459e45ffa4549692642646f7207ac19de5ce 100644 (file)
@@ -6,7 +6,7 @@
  * Copyright (C) 2006 Rob Landley
  * Copyright (C) 2006 Denys Vlasenko
  *
- * Licensed under GPL version 2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 
 /* We need to have separate xfuncs.c and xfuncs_printf.c because
@@ -134,7 +134,7 @@ int FAST_FUNC xopen3(const char *pathname, int flags, int mode)
        return ret;
 }
 
-// Die if we can't open an existing file and return a fd.
+// Die if we can't open a file and return a fd.
 int FAST_FUNC xopen(const char *pathname, int flags)
 {
        return xopen3(pathname, flags, 0666);
@@ -240,6 +240,14 @@ off_t FAST_FUNC xlseek(int fd, off_t offset, int whence)
        return off;
 }
 
+int FAST_FUNC xmkstemp(char *template)
+{
+       int fd = mkstemp(template);
+       if (fd < 0)
+               bb_perror_msg_and_die("can't create temp file '%s'", template);
+       return fd;
+}
+
 // Die with supplied filename if this FILE* has ferror set.
 void FAST_FUNC die_if_ferror(FILE *fp, const char *fn)
 {
@@ -323,6 +331,11 @@ void FAST_FUNC bb_unsetenv(const char *var)
        free(tp);
 }
 
+void FAST_FUNC bb_unsetenv_and_free(char *var)
+{
+       bb_unsetenv(var);
+       free(var);
+}
 
 // Die with an error message if we can't set gid.  (Because resource limits may
 // limit this user to a given number of processes, and if that fills up the
@@ -382,8 +395,12 @@ int FAST_FUNC xsocket(int domain, int type, int protocol)
                /* Hijack vaguely related config option */
 #if ENABLE_VERBOSE_RESOLUTION_ERRORS
                const char *s = "INET";
+# ifdef AF_PACKET
                if (domain == AF_PACKET) s = "PACKET";
+# endif
+# ifdef AF_NETLINK
                if (domain == AF_NETLINK) s = "NETLINK";
+# endif
 IF_FEATURE_IPV6(if (domain == AF_INET6) s = "INET6";)
                bb_perror_msg_and_die("socket(AF_%s,%d,%d)", s, type, protocol);
 #else
@@ -427,6 +444,16 @@ void FAST_FUNC xstat(const char *name, struct stat *stat_buf)
                bb_perror_msg_and_die("can't stat '%s'", name);
 }
 
+void FAST_FUNC xfstat(int fd, struct stat *stat_buf, const char *errmsg)
+{
+       /* errmsg is usually a file name, but not always:
+        * xfstat may be called in a spot where file name is no longer
+        * available, and caller may give e.g. "can't stat input file" string.
+        */
+       if (fstat(fd, stat_buf))
+               bb_simple_perror_msg_and_die(errmsg);
+}
+
 // selinux_or_die() - die if SELinux is disabled.
 void FAST_FUNC selinux_or_die(void)
 {
@@ -584,3 +611,14 @@ void FAST_FUNC generate_uuid(uint8_t *buf)
        /* variant = 10x */
        buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80;
 }
+
+#if BB_MMU
+pid_t FAST_FUNC xfork(void)
+{
+       pid_t pid;
+       pid = fork();
+       if (pid < 0) /* wtf? */
+               bb_perror_msg_and_die("vfork"+1);
+       return pid;
+}
+#endif