现在我的 set 函数有一个预定义的缓冲区大小,这会导致某些内容无法写入或额外的 NULL 被写入文件。
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define BUFFER_SIZE 1024
void handle_get_command(const char *fileName) {
int fileDescriptor = open(fileName, O_RDONLY);
if (fileDescriptor < 0) {
fprintf(stderr, "Invalid Command\n");
exit(1);
return;
}
char fileBuffer[BUFFER_SIZE];
ssize_t fileBytesRead;
while ((fileBytesRead = read(fileDescriptor, fileBuffer, BUFFER_SIZE)) > 0) {
write(STDOUT_FILENO, fileBuffer, fileBytesRead);
}
if (fileBytesRead < 0) {
fprintf(stderr, "Invalid Command\n");
exit(1);
}
close(fileDescriptor);
}
void handle_set_command(const char *fileName, size_t contentLength, const char *content) {
if (!fileName) {
fprintf(stderr, "Invalid Command\n");
exit(1);
}\
int fileDescriptor = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fileDescriptor < 0) {
fprintf(stderr, "Invalid Command\n");
exit(1);
}
if (contentLength > 0 && content) {
ssize_t bytesWritten = write(fileDescriptor, content, contentLength);
if (bytesWritten < 0 || (size_t)bytesWritten != contentLength) {
fprintf(stderr, "Invalid Command\n");
close(fileDescriptor);
exit(1);
}
}
close(fileDescriptor);
write(STDOUT_FILENO, "OK\n", 3);
}
int main(void) {
char inputBuffer[BUFFER_SIZE];
ssize_t bytesRead = read(STDIN_FILENO, inputBuffer, BUFFER_SIZE - 1);
if (bytesRead <= 0) {
fprintf(stderr, "Invalid Command\n");
exit(1);
}
inputBuffer[bytesRead] = '\0';
int x = 0;
if (inputBuffer[bytesRead - 1] != '\n') {
x = 9;
}
char *command = strtok(inputBuffer, "\n");
char *fileName = strtok(NULL, "\n");
if (strcmp(command, "get") == 0) {
if ((strtok(NULL, "\n") != NULL) || (x==9)) {
fprintf(stderr, "Invalid Command\n");
exit(1);
}
handle_get_command(fileName);
} else if (strcmp(command, "set") == 0) {
char *contentLengthStr = strtok(NULL, "\n");
char *content = strtok(NULL, "");
if (!fileName) {
fprintf(stderr, "Invalid Command\n");
exit(1);
}
size_t contentLength = strtoul(contentLengthStr, NULL, 10);
handle_set_command(fileName, contentLength, content);
} else {
fprintf(stderr, "Invalid Command\n");
exit(1);
}
return 0;
}
我需要将主函数中的 BUFFER_SIZE 设置为 STDIN 的大小,而我该如何做到这一点中的 BUFFER_SIZE 。我也尝试不使用 fgets 或 put 函数。
预定义的 BUFFER_SIZE 要么切断集合,要么用 NULL 值填充额外的缓冲区。
使用
read ( STDIN_FILENO, inputBuffer, BUFFER_SIZE - 1)
输入
set<enter>filename<enter>123<enter>stuff<enter>
set\n
中的
inputBuffer
输入
set filename 123 stuff<enter>
inputBuffer
sscanf
来解析字段。%n
说明符将给出扫描处理的字符数。它可用于提供指向内容字段开头的指针。
int main ( void) {
char inputBuffer[BUFFER_SIZE] = "";
ssize_t bytesRead = 0;
if ( 0 >= ( bytesRead = read ( STDIN_FILENO, inputBuffer, BUFFER_SIZE - 1))) {
fprintf ( stderr, "Invalid Command\n");
exit ( 1);
}
inputBuffer[bytesRead] = '\0';
if ( ( '\n' != inputBuffer[bytesRead - 1])) {
fprintf ( stderr, "Invalid Command\n");
exit ( 1);
}
char op = 0;
char fileName[512] = "";
size_t contentLength = 0;
int index = 0;
int fields = sscanf ( inputBuffer, " %cet%511s%zu %n", &op, fileName, &contentLength, &index);
if ( 'g' == op && 2 == fields) {
handle_get_command ( fileName);
}
else if ( 's' == op && index && 3 == fields) {
char *content = &inputBuffer[index];
handle_set_command ( fileName, contentLength, content);
} else {
fprintf ( stderr, "Invalid Command\n");
exit ( 1);
}
}
这只扫描
get
或set
的第一个字符。