From 6b558ed117043015a6f87a1d77d5053bef655711 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Fri, 11 Nov 1994 16:14:19 +0000 Subject: (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 suggested that mknod accept hex device numbers for compatibility with HPUX's mknod program. --- src/mknod.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file 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; -- cgit v1.2.3-70-g09d2