- if (argc < 3) {
- errmsg:
- fprintf(stderr, "nbd-client version %s\n", PACKAGE_VERSION);
- fprintf(stderr, "Usage: nbd-client [bs=blocksize] [timeout=sec] host port nbd_device [-swap] [-persist] [-nofork]\n");
- fprintf(stderr, "Or : nbd-client -d nbd_device\n");
- fprintf(stderr, "Or : nbd-client -c nbd_device\n");
- fprintf(stderr, "Default value for blocksize is 1024 (recommended for ethernet)\n");
- fprintf(stderr, "Allowed values for blocksize are 512,1024,2048,4096\n"); /* will be checked in kernel :) */
- fprintf(stderr, "Note, that kernel 2.4.2 and older ones do not work correctly with\n");
- fprintf(stderr, "blocksizes other than 1024 without patches\n");
- return 1;
- }
-
- ++argv; --argc; /* skip programname */
-
- if (strcmp(argv[0], "-d")==0) {
- nbd = open(argv[1], O_RDWR);
- if (nbd < 0)
- err("Cannot open NBD: %m\nPlease ensure the 'nbd' module is loaded.");
- printf("Disconnecting: que, ");
- if (ioctl(nbd, NBD_CLEAR_QUE)< 0)
- err("Ioctl failed: %m\n");
- printf("disconnect, ");
-#ifdef NBD_DISCONNECT
- if (ioctl(nbd, NBD_DISCONNECT)<0)
- err("Ioctl failed: %m\n");
- printf("sock, ");
-#else
- fprintf(stderr, "Can't disconnect: I was not compiled with disconnect support!\n" );
- exit(1);
-#endif
- if (ioctl(nbd, NBD_CLEAR_SOCK)<0)
- err("Ioctl failed: %m\n");
- printf("done\n");
- return 0;
- }
- if(strcmp(argv[0], "-c")==0) {
- return check_conn(argv[1], 1);
- }
-
- if (strncmp(argv[0], "bs=", 3)==0) {
- blocksize=atoi(argv[0]+3);
- ++argv; --argc; /* skip blocksize */
+ while((c=getopt_long_only(argc, argv, "-b:c:d:hnN:pSst:", long_options, NULL))>=0) {
+ switch(c) {
+ case 1:
+ // non-option argument
+ if(strchr(optarg, '=')) {
+ // old-style 'bs=' or 'timeout='
+ // argument
+ fprintf(stderr, "WARNING: old-style command-line argument encountered. This is deprecated.\n");
+ if(!strncmp(optarg, "bs=", 3)) {
+ optarg+=3;
+ goto blocksize;
+ }
+ if(!strncmp(optarg, "timeout=", 8)) {
+ optarg+=8;
+ goto timeout;
+ }
+ usage("unknown option %s encountered", optarg);
+ exit(EXIT_FAILURE);
+ }
+ switch(nonspecial++) {
+ case 0:
+ // host
+ hostname=optarg;
+ break;
+ case 1:
+ // port
+ if(!strtol(optarg, NULL, 0)) {
+ // not parseable as a number, assume it's the device and we have a name
+ nbddev = optarg;
+ nonspecial++;
+ } else {
+ port = optarg;
+ if(name) {
+ usage("port and name specified at the same time. This is not supported.");
+ exit(EXIT_FAILURE);
+ }
+ }
+ break;
+ case 2:
+ // device
+ nbddev = optarg;
+ break;
+ default:
+ usage("too many non-option arguments specified");
+ exit(EXIT_FAILURE);
+ }
+ break;
+ case 'b':
+ blocksize:
+ blocksize=(int)strtol(optarg, NULL, 0);
+ break;
+ case 'c':
+ return check_conn(optarg, 1);
+ case 'd':
+ disconnect(optarg);
+ exit(EXIT_SUCCESS);
+ case 'h':
+ usage(NULL);
+ exit(EXIT_SUCCESS);
+ case 'n':
+ nofork=1;
+ break;
+ case 'N':
+ name=optarg;
+ if(port) {
+ usage("port and name specified at the same time. This is not supported.");
+ exit(EXIT_FAILURE);
+ }
+ port = NBD_DEFAULT_PORT;
+ break;
+ case 'p':
+ cont=1;
+ break;
+ case 's':
+ swap=1;
+ break;
+ case 'S':
+ sdp=1;
+ break;
+ case 't':
+ timeout:
+ timeout=strtol(optarg, NULL, 0);
+ break;
+ default:
+ fprintf(stderr, "E: option eaten by 42 mice\n");
+ exit(EXIT_FAILURE);
+ }