]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blobdiff - libxcmd/input.c
xfsprogs: convert to SPDX license tags
[thirdparty/xfsprogs-dev.git] / libxcmd / input.c
index 6ee3176ebf4a8660a788656e19e71cca452ab1a2..d232d4f3547dbcafa40e081503b159ecf662314a 100644 (file)
@@ -1,24 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright (c) 2003-2005 Silicon Graphics, Inc.
  * All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write the Free Software Foundation,
- * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include <xfs/xfs.h>
-#include <xfs/input.h>
+#include "platform_defs.h"
+#include "input.h"
 #include <ctype.h>
+#include <stdbool.h>
 
 #if defined(ENABLE_READLINE)
 # include <readline/history.h>
@@ -88,7 +77,7 @@ fetchline(void)
 
        if (!line)
                return NULL;
-       printf(get_prompt());
+       printf("%s", get_prompt());
        fflush(stdout);
        if (!fgets(line, MAXREADLINESZ, stdin)) {
                free(line);
@@ -135,143 +124,6 @@ doneline(
        free(vec);
 }
 
-#define EXABYTES(x)    ((long long)(x) << 60)
-#define PETABYTES(x)   ((long long)(x) << 50)
-#define TERABYTES(x)   ((long long)(x) << 40)
-#define GIGABYTES(x)   ((long long)(x) << 30)
-#define MEGABYTES(x)   ((long long)(x) << 20)
-#define KILOBYTES(x)   ((long long)(x) << 10)
-
-long long
-cvtnum(
-       size_t          blocksize,
-       size_t          sectorsize,
-       char            *s)
-{
-       long long       i;
-       char            *sp;
-       int             c;
-
-       i = strtoll(s, &sp, 0);
-       if (i == 0 && sp == s)
-               return -1LL;
-       if (*sp == '\0')
-               return i;
-
-       if (sp[1] == '\0')
-               return -1LL;
-
-       c = tolower(*sp);
-       switch (c) {
-       case 'b':
-               return i * blocksize;
-       case 's':
-               return i * sectorsize;
-       case 'k':
-               return KILOBYTES(i);
-       case 'm':
-               return MEGABYTES(i);
-       case 'g':
-               return GIGABYTES(i);
-       case 't':
-               return TERABYTES(i);
-       case 'p':
-               return PETABYTES(i);
-       case 'e':
-               return  EXABYTES(i);
-       }
-       return -1LL;
-}
-
-#define TO_EXABYTES(x) ((x) / EXABYTES(1))
-#define TO_PETABYTES(x)        ((x) / PETABYTES(1))
-#define TO_TERABYTES(x)        ((x) / TERABYTES(1))
-#define TO_GIGABYTES(x)        ((x) / GIGABYTES(1))
-#define TO_MEGABYTES(x)        ((x) / MEGABYTES(1))
-#define TO_KILOBYTES(x)        ((x) / KILOBYTES(1))
-
-void
-cvtstr(
-       double          value,
-       char            *str,
-       size_t          size)
-{
-       char            *fmt;
-       int             precise;
-
-       precise = ((double)value * 1000 == (double)(int)value * 1000);
-
-       if (value >= EXABYTES(1)) {
-               fmt = precise ? "%.f EiB" : "%.3f EiB";
-               snprintf(str, size, fmt, TO_EXABYTES(value));
-       } else if (value >= PETABYTES(1)) {
-               fmt = precise ? "%.f PiB" : "%.3f PiB";
-               snprintf(str, size, fmt, TO_PETABYTES(value));
-       } else if (value >= TERABYTES(1)) {
-               fmt = precise ? "%.f TiB" : "%.3f TiB";
-               snprintf(str, size, fmt, TO_TERABYTES(value));
-       } else if (value >= GIGABYTES(1)) {
-               fmt = precise ? "%.f GiB" : "%.3f GiB";
-               snprintf(str, size, fmt, TO_GIGABYTES(value));
-       } else if (value >= MEGABYTES(1)) {
-               fmt = precise ? "%.f MiB" : "%.3f MiB";
-               snprintf(str, size, fmt, TO_MEGABYTES(value));
-       } else if (value >= KILOBYTES(1)) {
-               fmt = precise ? "%.f KiB" : "%.3f KiB";
-               snprintf(str, size, fmt, TO_KILOBYTES(value));
-       } else {
-               snprintf(str, size, "%f bytes", value);
-       }
-}
-
-#define MINUTES_TO_SECONDS(m)  ((m) * 60)
-#define HOURS_TO_SECONDS(h)    ((h) * MINUTES_TO_SECONDS(60))
-#define DAYS_TO_SECONDS(d)     ((d) * HOURS_TO_SECONDS(24))
-#define WEEKS_TO_SECONDS(w)    ((w) * DAYS_TO_SECONDS(7))
-
-unsigned long
-cvttime(
-       char            *s)
-{
-       unsigned long   i;
-       char            *sp;
-
-       i = strtoul(s, &sp, 0);
-       if (i == 0 && sp == s)
-               return 0;
-       if (*sp == '\0')
-               return i;
-       if ((*sp == 'm' && sp[1] == '\0') ||
-           (strcmp(sp, "minutes") == 0) ||
-           (strcmp(sp, "minute") == 0))
-               return MINUTES_TO_SECONDS(i);
-       if ((*sp == 'h' && sp[1] == '\0') ||
-           (strcmp(sp, "hours") == 0) ||
-           (strcmp(sp, "hour") == 0))
-               return HOURS_TO_SECONDS(i);
-       if ((*sp == 'd' && sp[1] == '\0') ||
-           (strcmp(sp, "days") == 0) ||
-           (strcmp(sp, "day") == 0))
-               return DAYS_TO_SECONDS(i);
-       if ((*sp == 'w' && sp[1] == '\0') ||
-           (strcmp(sp, "weeks") == 0) ||
-           (strcmp(sp, "week") == 0))
-               return WEEKS_TO_SECONDS(i);
-       return 0;
-}
-
-struct timeval
-tadd(struct timeval t1, struct timeval t2)
-{
-       t1.tv_usec += t2.tv_usec;
-       if (t1.tv_usec > 1000000) {
-               t1.tv_usec -= 1000000;
-               t1.tv_sec++;
-       }
-       t1.tv_sec += t2.tv_sec;
-       return t1;
-}
-
 struct timeval
 tsub(struct timeval t1, struct timeval t2)
 {
@@ -290,9 +142,10 @@ tdiv(double value, struct timeval tv)
        return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
 }
 
-#define HOURS(sec)     ((sec) / (60 * 60))
-#define MINUTES(sec)   (((sec) % (60 * 60)) / 60)
-#define SECONDS(sec)   ((sec) % 60)
+#define HOURS(sec)             ((sec) / (60 * 60))
+#define MINUTES(sec)           (((sec) % (60 * 60)) / 60)
+#define SECONDS(sec)           ((sec) % 60)
+#define USEC_TO_100THS(usec)   ((usec) / 1000 / 10)
 
 void
 timestr(
@@ -301,14 +154,12 @@ timestr(
        size_t          size,
        int             format)
 {
-       double          usec = (double)tv->tv_usec / 1000000.0;
-
        if (format & TERSE_FIXED_TIME) {
                if (!HOURS(tv->tv_sec)) {
                        snprintf(ts, size, "%u:%02u.%02u",
                                (unsigned int) MINUTES(tv->tv_sec),
                                (unsigned int) SECONDS(tv->tv_sec),
-                               (unsigned int) usec * 100);
+                               (unsigned int) USEC_TO_100THS(tv->tv_usec));
                        return;
                }
                format |= VERBOSE_FIXED_TIME;   /* fallback if hours needed */
@@ -319,67 +170,45 @@ timestr(
                        (unsigned int) HOURS(tv->tv_sec),
                        (unsigned int) MINUTES(tv->tv_sec),
                        (unsigned int) SECONDS(tv->tv_sec),
-                       (unsigned int) usec * 100);
+                       (unsigned int) USEC_TO_100THS(tv->tv_usec));
        } else {
-               snprintf(ts, size, "0.%04u sec", (unsigned int) usec * 10000);
+               snprintf(ts, size, "0.%04u sec",
+                       (unsigned int) tv->tv_usec / 100);
        }
 }
 
 /*
- * Convert from arbitrary user strings into a numeric ID.
- * If its all numeric, we convert that inplace, else we do
- * the name lookup, and return the found identifier.
+ * Convert from a pair of arbitrary user strings into a timespec.
  */
 
-prid_t
-prid_from_string(
-       char            *project)
-{
-       fs_project_t    *prj;
-       prid_t          prid;
-       char            *sp;
-
-       prid = (prid_t)strtoul(project, &sp, 10);
-       if (sp != project)
-               return prid;
-       prj = getprnam(project);
-       if (prj)
-               return prj->pr_prid;
-       return -1;
-}
-
-uid_t
-uid_from_string(
-       char            *user)
-{
-       struct passwd   *pwd;
-       uid_t           uid;
-       char            *sp;
-
-       uid = (uid_t)strtoul(user, &sp, 10);
-       if (sp != user)
-               return uid;
-       pwd = getpwnam(user);
-       if (pwd)
-               return pwd->pw_uid;
-       return -1;
+int
+timespec_from_string(
+       const char      * secs,
+       const char      * nsecs,
+       struct timespec * ts)
+{
+       char* p;
+       if (!secs || !nsecs || !ts)
+               return 1;
+       ts->tv_sec = strtoull(secs, &p, 0);
+       if (*p)
+               return 1;
+       ts->tv_nsec = strtoull(nsecs, &p, 0);
+       if (*p)
+               return 1;
+       return 0;
 }
 
-gid_t
-gid_from_string(
-       char            *group)
+bool isdigits_only(
+       const char *str)
 {
-       struct group    *grp;
-       gid_t           gid;
-       char            *sp;
+       int i;
 
-       gid = (gid_t)strtoul(group, &sp, 10);
-       if (sp != group)
-               return gid;
-       grp = getgrnam(group);
-       if (grp)
-               return grp->gr_gid;
-       return -1;
+       for (i = 0; i < strlen(str); i++) {
+               if (!isdigit(str[i]))
+                       return false;
+       }
+       return true;
 }
 
 #define HAVE_FTW_H 1   /* TODO: configure me */