Merge pull request #25 from fischerling/improve-text-printing
Improve text printing
This commit is contained in:
+7
-3
@@ -18,9 +18,10 @@ static inline void print_content(const char *path, char *buffer, unsigned buflen
|
|||||||
// Open the file.
|
// Open the file.
|
||||||
int fd = open(path, O_RDONLY, 42);
|
int fd = open(path, O_RDONLY, 42);
|
||||||
if (fd >= 0) {
|
if (fd >= 0) {
|
||||||
|
ssize_t bytes_read = 0;
|
||||||
// Put on the standard output the characters.
|
// Put on the standard output the characters.
|
||||||
while (read(fd, buffer, buflen) > 0) {
|
while ((bytes_read = read(fd, buffer, buflen)) > 0) {
|
||||||
puts(buffer);
|
write(STDOUT_FILENO, buffer, bytes_read);
|
||||||
}
|
}
|
||||||
// Close the file descriptor.
|
// Close the file descriptor.
|
||||||
close(fd);
|
close(fd);
|
||||||
@@ -45,6 +46,7 @@ int main(int argc, char **argv)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
int status = 0;
|
||||||
// Prepare the buffer for reading.
|
// Prepare the buffer for reading.
|
||||||
char buffer[BUFSIZ];
|
char buffer[BUFSIZ];
|
||||||
// Iterate the arguments.
|
// Iterate the arguments.
|
||||||
@@ -60,16 +62,18 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
} else if (S_ISDIR(statbuf.st_mode)) {
|
} else if (S_ISDIR(statbuf.st_mode)) {
|
||||||
printf("cat: %s: Is a directory\n\n", argv[i]);
|
printf("cat: %s: Is a directory\n\n", argv[i]);
|
||||||
|
status = 1;
|
||||||
|
|
||||||
} else if (S_ISLNK(statbuf.st_mode)) {
|
} else if (S_ISLNK(statbuf.st_mode)) {
|
||||||
if (readlink(argv[i], buffer, BUFSIZ)) {
|
if (readlink(argv[i], buffer, BUFSIZ)) {
|
||||||
print_content(buffer, buffer, BUFSIZ);
|
print_content(buffer, buffer, BUFSIZ);
|
||||||
} else {
|
} else {
|
||||||
printf("cat: %s: %s\n\n", argv[i], strerror(errno));
|
printf("cat: %s: %s\n\n", argv[i], strerror(errno));
|
||||||
|
status = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
return 0;
|
return status;
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-16
@@ -40,6 +40,7 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
else if (argc == 2)
|
else if (argc == 2)
|
||||||
{
|
{
|
||||||
|
char *pager = "cat";
|
||||||
char filepath[PATH_MAX];
|
char filepath[PATH_MAX];
|
||||||
strcpy(filepath, "/usr/share/man/");
|
strcpy(filepath, "/usr/share/man/");
|
||||||
strcat(filepath, argv[1]);
|
strcat(filepath, argv[1]);
|
||||||
@@ -47,23 +48,11 @@ int main(int argc, char *argv[])
|
|||||||
int fd = open(filepath, O_RDONLY, 42);
|
int fd = open(filepath, O_RDONLY, 42);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
{
|
{
|
||||||
printf("%s: No manual entry for %s\n\n", argv[0], argv[1]);
|
printf("%s: No manual entry for %s\n", argv[0], argv[1]);
|
||||||
}
|
exit(1);
|
||||||
else
|
|
||||||
{
|
|
||||||
// Prepare the buffer for reading the man file.
|
|
||||||
char buffer[BUFSIZ];
|
|
||||||
// Put on the standard output the characters.
|
|
||||||
while (read(fd, buffer, BUFSIZ) > 0)
|
|
||||||
{
|
|
||||||
puts(buffer);
|
|
||||||
}
|
|
||||||
// Close the file descriptor.
|
|
||||||
close(fd);
|
|
||||||
// Terminate with a pair of newlines.
|
|
||||||
putchar('\n');
|
|
||||||
putchar('\n');
|
|
||||||
}
|
}
|
||||||
|
close(fd);
|
||||||
|
execlp(pager, pager, filepath);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user