📄 parse.c
字号:
/* initiator/parse.c vi: set autoindent tabstop=8 shiftwidth=4 : This program is used to split the string returned by slptool and get the ipaddr, port number and iscsi-name(target name)*//* Copyright (C) 2001-2003 InterOperability Lab (IOL) University of New Hampshier (UNH) Durham, NH 03824 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. The name of IOL and/or UNH may not be used to endorse or promote products derived from this software without specific prior written permission.*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#define OPTIONS ":h:p:n:"#define PORTSIZE 5voidgetinfo(char *str, int opt){ char *url, *tmp, *name, *port; if ((tmp = strchr(str, '/')) == NULL) { fprintf(stderr, "invalid url-path format for the target\n"); exit(EXIT_FAILURE); } if (*(tmp + 1) != '/') { /* expected '//' before the url */ fprintf(stderr, "invalid url-path format for the target\n"); exit(EXIT_FAILURE); } tmp += 2; url = strchr(tmp, ':'); if (url == NULL) { /* no optional port number */ if (opt == 'p') { /* no port number in the string, use the default one */ fprintf(stdout, "%s", "3260"); exit(EXIT_SUCCESS); } if ((url = strchr(tmp, '/')) == NULL) { fprintf(stderr, "Format error: No '/' before the target name\n"); exit(EXIT_FAILURE); } if (opt == 'n') { url++; if ((name = strchr(url, '/')) != NULL || (name = strchr(url, ',')) != NULL) { /* string ended with identity */ *name = '\0'; } fprintf(stdout, "%s", url); exit(EXIT_SUCCESS); } } if (opt == 'h') { *url = '\0'; fprintf(stdout, "%s", tmp); exit(EXIT_SUCCESS); } else { url++; if ((port = strchr(url, '/')) == NULL) { fprintf(stderr, "Format error: No '/' before the target name\n"); exit(EXIT_FAILURE); } if (opt == 'p') { *port = '\0'; fprintf(stdout, "%s", url); exit(EXIT_SUCCESS); } else if (opt == 'n') { port++; if ((name = strchr(port, '/')) != NULL || (name = strchr(port, ',')) != NULL) { /* string ended with identity */ *name = '\0'; } fprintf(stdout, "%s", port); } }}intmain(int argc, char *argv[]){ int c; opterr = 0; if (argc != 3) { fprintf(stderr, "Useage: -h/-p/-n parse string_from_slp"); return EXIT_FAILURE; } while ((c = getopt(argc, argv, OPTIONS)) != EOF) switch (c) { case 'h': getinfo(argv[2], c); break; case 'p': getinfo(argv[2], c); break; case 'n': getinfo(argv[2], c); break; case ':': fprintf(stderr, "missing parameter to switch: %s\n", optarg); return EXIT_FAILURE; case '?': fprintf(stderr, "illegal switch %c\n", optopt); return EXIT_FAILURE; } return EXIT_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -