具体来说,我可以在 Unix (Sun-Solaris) 中编译这个程序,但我无法让它运行(我是 Unix 类(class),我们所有的程序/作业都将在学校服务器上完成)。他只是在类(class)的最后两周教我们 C。我对 C 相当陌生,不明白为什么它不起作用。任何帮助,将不胜感激。我对这个错误做了一些研究,但不幸的是,我在网上尝试/找到的方法要么不起作用,要么我没有正确执行它们。
代码如下。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
char** splitString(char* inputString, const char delim) {
char** output = 0;
size_t c = 0;
char* temp = inputString;
char* final_space = 0;
char delimiter[2];
delimiter[0] = delim;
delimiter[1] = 0;
while(*temp) {
if(delim == *temp) {
c++;
final_space = temp;
}
temp++;
}
c += final_space < (inputString + strlen(inputString) - 1);
c++;
output = malloc(sizeof(char*) * c);
if(output) {
size_t index = 0;
char* output = strtok(inputString, delimiter);
while(output) {
assert(index < c);
*(output + index++) = strdup(output);
output = strtok(0, delimiter);
}
assert(index == c - 1);
*(output + index) = 0;
}
return output;
}
void main() {
char input[50];
char** splitInput = NULL;
int rs;
printf("please enter a Unix FTP cmd. The program will check to see if it is a valid cmd: ");
gets(input);
splitInput = splitString(input, ' ');
printf("input[%s]", *(splitInput));
rs = strcmp(*(splitInput), "ascii");
if(rs == 0) {
printf("ascii is a valid ftp command\n");
return;
}
rs = strcmp(*(splitInput), "recv");
if(rs == 0) {
printf("recv is a valid ftp command\n");
free(splitInput);
printf("the entered cmd is %s", *(splitInput + 1));
return;
}
rs = strcmp(*(splitInput), "get");
if(rs == 0) {
printf("get is a valid ftp command\n");
free(splitInput);
printf("the entered cmd is %s", *(splitInput + 1));
return;
}
rs = strcmp(*(splitInput), "send");
if(rs == 0) {
printf("send is a valid ftp command\n");
free(splitInput);
printf("the entered cmd is %s", *(splitInput + 1));
return;
}
rs = strcmp(*(splitInput), "put");
if(rs == 0) {
printf("put is a real Unix FTP cmd\n");
free(splitInput);
printf("the entered cmd is %s", *(splitInput + 1));
return;
}
rs = strcmp(*(splitInput), "rmdir");
if(rs == 0) {
printf("rmdir is a real Unix FTP cmd\n");
printf("the entered cmd is %s", *(splitInput + 1));
return;
}
rs = strcmp(*(splitInput), "mkdir");
if(rs == 0) {
printf("mkdir is a real Unix FTP cmd\n");
free(splitInput);
printf("the entered cmd is %s", *(splitInput + 1));
return;
}
rs = strcmp(*(splitInput), "rmdir");
if(rs == 0) {
printf("rmdir is a real Unix FTP cmd\n");
free(splitInput);
printf("the entered cmd is %s", *(splitInput + 1));
return;
}
rs = strcmp(*(splitInput), "ls");
if(rs == 0) {
printf("ls a real Unix FTP cmd\n");
free(splitInput);
printf("the entered cmd is %s", *(splitInput + 1));
return;
}
rs = strcmp(*(splitInput), "cd");
if(rs == 0) {
printf("cd is a real Unix FTP cmd\n");
free(splitInput);
printf("the entered cmd is %s", *(splitInput + 1));
return;
}
rs = strcmp(*(splitInput), "status");
if(rs == 0) {
printf("status is a valid ftp command\n");
return;
}
rs = strcmp(*(splitInput), "quit");
if(rs == 0) {
printf("quit is a valid ftp command\n");
return;
} else {
printf(*(splitInput), " isn't a real Unix FTP cmd");
}
return;
}
这是我收到的错误的屏幕截图:Error inside PuTTy console
最佳答案
由于指针正在访问未分配的空间中的内存,因此出现段错误。正如 bruceg 指出的那样,这条线肯定导致了崩溃,也许之后还有更多……
如果您的代码只是为了验证 ftp 命令,您可以简单地使用 strtok 或 strtok_r 执行以下操作,运行“man strtok”查看详细信息和示例。
这是您可以编译和运行的代码,您可以从这里完成其余的工作。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main() {
char input[50];
int i;
char *token,*str,*cmd[2],*ptr;
printf("please enter a Unix FTP cmd. The program will check to see if it is a valid cmd: ");
fgets(input,50,stdin);
input[strlen(input)-1]='\0';
for (i=0,str=input;;i++,str=NULL) {
token=strtok_r(str," ",&ptr);
if (token==NULL) {
break;
}
cmd[i]=token;
}
printf("(%s)\n",cmd[0]);
if (!strcmp(cmd[0],"ascii")) {
printf("ascii is a valid ftp command\n");
}
else if (!strcmp(cmd[0],"get")) {
printf("get is a valid ftp command\n");
printf("the entered cmd is: %s %s\n",cmd[0],cmd[1]);
}
}
顺便说一句,您应该能够在学校实验室之外的 Mac 或 Linux 上使用 gcc 来完成此操作。如果您使用的是 Windows,在 cygwin 中安装 gcc 是快速方法之一。您的代码将在学校的 Solaris 服务器上编译。希望这会有所帮助。
关于c - 检查有效 Unix FTP 命令的程序(不工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49949721/