Changeset f3a37e28 in mainline
- Timestamp:
- 2012-08-20T18:04:01Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 7eaeec1
- Parents:
- 1f136a27
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cmp/cmp.c
r1f136a27 rf3a37e28 29 29 #include <errno.h> 30 30 #include <fcntl.h> 31 #include <getopt.h> 31 32 #include <mem.h> 32 33 #include <stdio.h> … … 46 47 #define CMP_BUFLEN 1024 47 48 49 static struct option const long_options[] = { 50 { "help", no_argument, 0, 'h' }, 51 { "version", no_argument, 0, 'v' }, 52 { 0, 0, 0, 0 } 53 }; 54 48 55 /* Dispays help for cat in various levels */ 49 56 void help_cmd_cmp(unsigned int level) … … 55 62 printf( 56 63 "Usage: %s [options] <file1> <file2>\n" 64 "Options:\n" 65 " -h, --help A short option summary\n" 66 " -v, --version Print version information and exit\n" 57 67 "No output is printed; the return code is 1 if the files differ.\n", 58 68 cmdname); … … 64 74 static int cmp_files(const char *fn0, const char *fn1) 65 75 { 66 int fd0, fd1; 67 char buffer0[CMP_BUFLEN], buffer1[CMP_BUFLEN]; 68 ssize_t size0, size1; 69 ssize_t offset0, offset1; 76 int rc = 0; 77 const char *fn[2] = {fn0, fn1}; 78 int fd[2] = {-1, -1}; 79 char buffer[2][CMP_BUFLEN]; 80 ssize_t offset[2]; 70 81 71 fd0 = open(fn0, O_RDONLY); 72 if (fd0 < 0) { 73 printf("Unable to open %s\n", fn0); 74 return errno; 75 } 76 fd1 = open(fn1, O_RDONLY); 77 if (fd1 < 0) { 78 printf("Unable to open %s\n", fn1); 79 return errno; 82 for (int i = 0; i < 2; i++) { 83 fd[i] = open(fn[i], O_RDONLY); 84 if (fd[i] < 0) { 85 rc = errno; 86 printf("Unable to open %s\n", fn[i]); 87 goto end; 88 } 80 89 } 81 90 82 91 do { 83 offset0 = offset1 = 0; 92 for (int i = 0; i < 2; i++) { 93 offset[i] = 0; 94 ssize_t size; 95 do { 96 size = read(fd[i], buffer[i] + offset[i], 97 CMP_BUFLEN - offset[i]); 98 if (size < 0) { 99 rc = errno; 100 printf("Error reading from %s\n", 101 fn[i]); 102 goto end; 103 } 104 offset[i] += size; 105 } while (size && offset[i] < CMP_BUFLEN); 106 } 84 107 85 do { 86 size0 = read(fd0, buffer0 + offset0, 87 CMP_BUFLEN - offset0); 88 if (size0 < 0) { 89 printf("Error reading from %s\n", fn0); 90 return errno; 91 } 92 offset0 += size0; 93 } while (size0 && offset0 < CMP_BUFLEN); 108 if (offset[0] != offset[1] || 109 bcmp(buffer[0], buffer[1], offset[0])) { 110 rc = 1; 111 goto end; 112 } 113 } while (offset[0] == CMP_BUFLEN); 94 114 95 do { 96 size1 = read(fd1, buffer1 + offset1, 97 CMP_BUFLEN - offset1); 98 if (size1 < 0) { 99 printf("Error reading from %s\n", fn1); 100 return errno; 101 } 102 offset1 += size1; 103 } while (size1 && offset1 < CMP_BUFLEN); 104 105 if (offset0 != offset1) 106 return 1; 107 if (bcmp(buffer0, buffer1, offset0)) 108 return 1; 109 } while (offset0 == CMP_BUFLEN); 110 111 return 0; 115 end: 116 if (fd[0] >= 0) 117 close(fd[0]); 118 if (fd[1] >= 0) 119 close(fd[1]); 120 return rc; 112 121 } 113 122 … … 115 124 int cmd_cmp(char **argv) 116 125 { 126 int rc; 117 127 unsigned int argc; 118 int rc;128 int c, opt_ind; 119 129 120 130 argc = cli_count_args(argv); 121 if (argc != 3) { 131 132 for (c = 0, optind = 0, opt_ind = 0; c != -1;) { 133 c = getopt_long(argc, argv, "hv", long_options, &opt_ind); 134 switch (c) { 135 case 'h': 136 help_cmd_cmp(HELP_LONG); 137 return CMD_SUCCESS; 138 case 'v': 139 printf("%s\n", CMP_VERSION); 140 return CMD_SUCCESS; 141 } 142 } 143 144 if (argc - optind != 2) { 122 145 printf("%s - incorrect number of arguments. Try `%s --help'\n", 123 146 cmdname, cmdname); … … 125 148 } 126 149 127 rc = cmp_files(argv[ 1], argv[2]);150 rc = cmp_files(argv[optind], argv[optind + 1]); 128 151 if (rc) 129 152 return CMD_FAILURE;
Note:
See TracChangeset
for help on using the changeset viewer.