Sujith Ramakrishnan | 71bbbd1 | 2014-01-14 16:57:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google, Inc. nor the names of its contributors |
| 15 | * may be used to endorse or promote products derived from this |
| 16 | * software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 22 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 25 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 26 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 28 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 | * SUCH DAMAGE. |
| 30 | */ |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <unistd.h> |
| 34 | #include <sys/stat.h> |
| 35 | |
| 36 | static int print_usage() { |
| 37 | fprintf(stderr, "mknod <path> [b|c|u|p] <major> <minor>\n"); |
| 38 | return EXIT_FAILURE; |
| 39 | } |
| 40 | |
| 41 | int mknod_main(int argc, char **argv) { |
| 42 | char *path = NULL; |
| 43 | int major = 0; |
| 44 | int minor = 0; |
| 45 | int args = 0; |
| 46 | mode_t mode = 0660; |
| 47 | |
| 48 | /* Check correct argument count is 3 or 5 */ |
| 49 | if (argc != 3 && argc != 5) { |
| 50 | fprintf(stderr, "Incorrect argument count\n"); |
| 51 | return print_usage(); |
| 52 | } |
| 53 | |
| 54 | path = argv[1]; |
| 55 | |
| 56 | const char node_type = *argv[2]; |
| 57 | switch (node_type) { |
| 58 | case 'b': |
| 59 | mode |= S_IFBLK; |
| 60 | args = 5; |
| 61 | break; |
| 62 | case 'c': |
| 63 | case 'u': |
| 64 | mode |= S_IFCHR; |
| 65 | args = 5; |
| 66 | break; |
| 67 | case 'p': |
| 68 | mode |= S_IFIFO; |
| 69 | args = 3; |
| 70 | break; |
| 71 | default: |
| 72 | fprintf(stderr, "Invalid node type '%c'\n", node_type); |
| 73 | return print_usage(); |
| 74 | } |
| 75 | |
| 76 | if (argc != args) { |
| 77 | if (args == 5) { |
| 78 | fprintf(stderr, "Node type '%c' requires <major> and <minor>\n", node_type); |
| 79 | } else { |
| 80 | fprintf(stderr, "Node type '%c' does not require <major> and <minor>\n", node_type); |
| 81 | } |
| 82 | return print_usage(); |
| 83 | } |
| 84 | |
| 85 | if (args == 5) { |
| 86 | major = atoi(argv[3]); |
| 87 | minor = atoi(argv[4]); |
| 88 | } |
| 89 | |
| 90 | if (mknod(path, mode, makedev(major, minor))) { |
| 91 | perror("Unable to create node"); |
| 92 | return EXIT_FAILURE; |
| 93 | } |
| 94 | return 0; |
| 95 | } |