summaryrefslogtreecommitdiff
path: root/src/mknod.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1994-11-11 16:14:19 +0000
committerJim Meyering <jim@meyering.net>1994-11-11 16:14:19 +0000
commit6b558ed117043015a6f87a1d77d5053bef655711 (patch)
tree6ffcb8eb471719e2c7e0fc6388f3638ec9c1c71c /src/mknod.c
parentb81d2117e2744cbe712972d808e907ba476253d1 (diff)
downloadcoreutils-6b558ed117043015a6f87a1d77d5053bef655711.tar.xz
(my_strtol): New function.
(main): Use it instead of atoi to convert argument strings to major and minor device numbers. Now, mknod diagnoses invalid device numbers and accepts octal and hexadecimal as well as decimal string arguments. Ralf Lammers <rlammers@physik.uni-osnabrueck.de> suggested that mknod accept hex device numbers for compatibility with HPUX's mknod program.
Diffstat (limited to 'src/mknod.c')
-rw-r--r--src/mknod.c61
1 files changed, 57 insertions, 4 deletions
diff --git a/src/mknod.c b/src/mknod.c
index 8d7ace86c..45db7d38d 100644
--- a/src/mknod.c
+++ b/src/mknod.c
@@ -56,6 +56,34 @@ static struct option const longopts[] =
{NULL, 0, NULL, 0}
};
+static int
+my_strtol (str, ptr, base, result)
+ const char *str;
+ const char **ptr;
+ int base;
+ long int *result;
+{
+ long int val;
+ char *terminator;
+ int return_code;
+
+ return_code = 0;
+
+ errno = 0;
+ val = strtol (str, &terminator, base);
+
+ if (terminator == str
+ || (ptr == NULL && *terminator != '\0')
+ || errno == ERANGE)
+ return_code = 1;
+
+ if (ptr != NULL)
+ *ptr = terminator;
+
+ *result = val;
+ return return_code;
+}
+
void
main (argc, argv)
int argc;
@@ -65,6 +93,9 @@ main (argc, argv)
struct mode_change *change;
char *symbolic_mode;
int optc;
+ int i_major, i_minor;
+ long int tmp_major, tmp_minor;
+ char *s;
program_name = argv[0];
symbolic_mode = NULL;
@@ -132,8 +163,19 @@ when creating block special files, major and minor device\n\
numbers must be specified");
usage (1);
}
- if (mknod (argv[optind], newmode | S_IFBLK,
- makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
+
+ s = argv[optind + 2];
+ if (my_strtol (s, NULL, 0, &tmp_major))
+ error (1, 0, "invalid major device number `%s'", s);
+
+ s = argv[optind + 3];
+ if (my_strtol (s, NULL, 0, &tmp_minor))
+ error (1, 0, "invalid minor device number `%s'", s);
+
+ i_major = (int) tmp_major;
+ i_minor = (int) tmp_minor;
+
+ if (mknod (argv[optind], newmode | S_IFBLK, makedev (i_major, i_minor)))
error (1, errno, "%s", argv[optind]);
#endif
break;
@@ -150,8 +192,19 @@ when creating character special files, major and minor device\n\
numbers must be specified");
usage (1);
}
- if (mknod (argv[optind], newmode | S_IFCHR,
- makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
+
+ s = argv[optind + 2];
+ if (my_strtol (s, NULL, 0, &tmp_major))
+ error (1, 0, "invalid major device number `%s'", s);
+
+ s = argv[optind + 3];
+ if (my_strtol (s, NULL, 0, &tmp_minor))
+ error (1, 0, "invalid minor device number `%s'", s);
+
+ i_major = (int) tmp_major;
+ i_minor = (int) tmp_minor;
+
+ if (mknod (argv[optind], newmode | S_IFCHR, makedev (i_major, i_minor)))
error (1, errno, "%s", argv[optind]);
#endif
break;