Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/ipwn/cmd_fd.c
Views: 11766
/*1* Copyright (c) 2004-2005 vlad902 <vlad902 [at] gmail.com>2* Copyright (c) 2007 H D Moore <hdm [at] metasploit.com>3* This file is part of the Metasploit Framework.4* $Revision$5*/67#include <sys/types.h>8#include <sys/stat.h>9#include <stdlib.h>10#include <unistd.h>11#include <sys/socket.h>12#include <netinet/in.h>13#include <arpa/inet.h>14#include <string.h>15#include <errno.h>16#include <stdio.h>17#include <fcntl.h>1819#include "cmd.h"202122void cmd_open(int argc, char * argv[])23{24int fd;2526fd = open(argv[1], O_RDWR | O_CREAT | O_APPEND, S_IRWXU);27if(fd == -1)28fd = open(argv[1], O_RDONLY);2930if(fd == -1)31perror("open");32else33printf("open: %d\n", fd);34}3536void cmd_lseek(int argc, char * argv[])37{38int fd, offset, whence;39int ret;4041fd = atoi(argv[1]);42offset = atoi(argv[2]);43whence = -1;4445if(strcasecmp(argv[3], "SEEK_SET") == 0)46whence = SEEK_SET;47if(strcasecmp(argv[3], "SEEK_CUR") == 0)48whence = SEEK_CUR;49if(strcasecmp(argv[3], "SEEK_END") == 0)50whence = SEEK_END;5152if(whence == -1)53{54printf("whence was not SEEK_SET, SEEK_CUR, or SEEK_END\n");55return;56}5758if((ret = lseek(fd, offset, whence)) == -1)59perror("lseek");60else61printf("lseek: %i\n", ret);62}6364void cmd_read(int argc, char * argv[])65{66int fd, size;67int read_out, rsz;68char buf[512];6970fd = atoi(argv[1]);71{ /* Get max length to read... ugly. */72int cur, end;7374cur = lseek(fd, 0, SEEK_CUR);75end = lseek(fd, 0, SEEK_END);7677size = end - cur;78lseek(fd, cur, SEEK_SET);79}80if(argc > 2)81size = atoi(argv[2]);8283for(rsz = 0; rsz < size;)84{85read_out = read(fd, buf, __MIN_NUM(sizeof(buf), size - rsz));86if(read_out == -1)87return;88write(1, buf, read_out);89rsz += read_out;90}91}9293void cmd_write(int argc, char * argv[])94{95int fd, size = -1;96int read_in, rsz;97char buf[512];9899fd = atoi(argv[1]);100if(argc > 2)101size = atoi(argv[2]);102103for(rsz = 0; rsz < size || size == -1;)104{105if(size != -1)106read_in = read(1, buf, __MIN_NUM(sizeof(buf), size - rsz));107else108read_in = read(1, buf, sizeof(buf));109110if(read_in == -1)111return;112113if(size == -1 && read_in >= 3)114{115char local[sizeof(buf)];116117memcpy(local, buf, sizeof(buf));118if(local[read_in - 1] == '\n')119local[read_in - 1] = '\0';120if(local[read_in - 1] == '\r')121local[read_in - 1] = '\0';122123if(strcmp(local, "EOF") == 0)124return;125}126127write(fd, buf, read_in);128rsz += read_in;129}130}131132void cmd_close(int argc, char * argv[])133{134if(close(atoi(argv[1])) == -1)135perror("close");136}137138void cmd_dup(int argc, char * argv[])139{140int new_fd;141142if((new_fd = dup(atoi(argv[1]))) == -1)143perror("dup");144145printf("%i\n", new_fd);146}147148void cmd_dup2(int argc, char * argv[])149{150if(dup2(atoi(argv[1]), atoi(argv[2])) == -1)151perror("dup2");152}153154155