1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:17:34 +00:00

Userland: Use Core::ArgsParser for 'ping'

This commit is contained in:
Linus Groh 2020-08-05 20:55:01 +02:00 committed by Andreas Kling
parent 59942edcc0
commit ca799fe4ab

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <LibCore/ArgsParser.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -57,10 +58,11 @@ int main(int argc, char** argv)
return 1; return 1;
} }
if (argc != 2) { const char* host = nullptr;
printf("usage: ping <host>\n");
return 0; Core::ArgsParser args_parser;
} args_parser.add_positional_argument(host, "Host to ping", "host");
args_parser.parse(argc, argv);
int fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); int fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (fd < 0) { if (fd < 0) {
@ -87,9 +89,9 @@ int main(int argc, char** argv)
return 1; return 1;
} }
auto* hostent = gethostbyname(argv[1]); auto* hostent = gethostbyname(host);
if (!hostent) { if (!hostent) {
printf("Lookup failed for '%s'\n", argv[1]); printf("Lookup failed for '%s'\n", host);
return 1; return 1;
} }