Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function memchr & update cat program #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 184 additions & 61 deletions bin/cat/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009 Niek Linnenbank
* Copyright (C) 2009 Niek Linnenbank, 2015 Dan Rulos, 2016 Alvaro Stagg [[email protected]]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -16,81 +16,204 @@
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <Runtime.h>
#include <unistd.h>
#include <sys/stat.h>

int cat(char *prog, char *file)
{
char buf[1025];
int fd, e;
#define VERSION "v0.5.8"

/* Clear buffer. */
memset(buf, 0, sizeof(buf));
static int hflag, vflag, ret = 0;
static int flags[8];

/* Attempt to open the file first. */
if ((fd = open(file, O_RDONLY)) < 0)
{
printf("%s: failed to open '%s': %s\r\n",
prog, file, strerror(errno));
return EXIT_FAILURE;
}
/* Read contents. */
while (1)
{
e = read(fd, buf, sizeof(buf) - 1);
switch (e)
{
/* Error occurred. */
case -1:
printf("%s: failed to read '%s': %s\r\n",
prog, file, strerror(errno));
close(fd);
return EXIT_FAILURE;

/* End of file. */
case 0:
close(fd);
return EXIT_SUCCESS;

/* Print out results. */
default:
buf[e] = 0;
printf("%s", buf);
break;
}
}
return EXIT_FAILURE;
}
static void usage(void);
static void version(void);
static void cat(char *argv[], int argc, int flags[8]);
static void read(const char *fileName, int flags[8]);
static const char *fileName;

int get_file_size(const char *fileName);

int main(int argc, char **argv)
{
int ret = EXIT_SUCCESS, result;

/* Verify command-line arguments. */
if (argc < 2)
{
printf("usage: %s FILE1 FILE2 ...\r\n",
argv[0]);
return EXIT_FAILURE;
printf("Uso: %s [OPCIÓN]... [FICHERO]...\n", argv[0]);
return 1;
}
refreshMounts(0);

/* Cat all given files. */
for (int i = 0; i < argc - 1; i++)
for (int i = 0; i <= 8; i++)
flags[i] = 0;

for (int i = 1; i < argc; i++)
{
/* Perform cat. */
result = cat(argv[0], argv[i + 1]);

/* Update exit code if needed. */
if (result > ret)
if (strcmp(argv[i], "--help") == 0)
{
hflag = 1;
usage();
break;
}
else if ((strcmp(argv[i], "--version") == 0))
{
vflag = 1;
version();
break;
}
else if ((strcmp(argv[i], "-n") == 0) || (strcmp(argv[i], "--number") == 0))
flags[0] = 1;
else if ((strcmp(argv[i], "-E") == 0) || (strcmp(argv[i], "--show-ends") == 0) || (strcmp(argv[i], "-e") == 0))
flags[1] = 1;
else if ((strcmp(argv[i], "-t") == 0) || (strcmp(argv[i], "-T") == 0) || (strcmp(argv[i], "--show-tabs") == 0))
flags[2] = 1;
else if ((strcmp(argv[i], "-b") == 0) || (strcmp(argv[i], "--number-nonblank") == 0))
{
ret = result;
flags[0] = 0;
flags[3] = 1;
}
}
/* Done. */

if (hflag || vflag)
return ret;
else
cat(argv, argc, flags);

return ret;
}

static void usage(void)
{
printf("Modo de empleo: cat [OPCIÓN]... [FICHERO]...\n\n");

printf(" -b, --number-nonblank No enumera las lineas en blanco, remplaza -n.\n");
printf(" -e Lo mismo que -E.\n");
printf(" -E, --show-ends Muestra un $ al final de cada linea.\n");
printf(" -n, --number Enumera cada linea.\n");
printf(" -t Lo mismo que -T.\n");
printf(" -T, --show-tabs Muestra los caracteres de tabulación como ^I\n");
printf(" --help Muestra esta ayuda y finaliza.\n");
printf(" --version Informa de la versión y finaliza.\n\n");

printf("AmayaOS Coreutils %s (C) 2016 AmayaOS Team & Others\n", VERSION);
printf("Licencia GNU GPL v3 <http://www.gnu.org/licenses/>.\n");
printf("Reportar errores a traves de http://bugs.amayaos.com o [email protected]\n");
}

static void version(void)
{
printf("cat (AmayaOS CoreUtils) %s\n", VERSION);
printf("Copyright © 2016 AmayaOS, Inc.\n");
printf("Licencia GPLv3+: GPL de GNU versión 3 o posterior\n");
printf("<http://gnu.org/licenses/gpl.html>.\n");
printf("Esto es software libre: usted es libre de cambiarlo y redistribuirlo.\n");
printf("No hay NINGUNA GARANTÍA, hasta donde permite la ley.\n\n");

printf("Escrito por Dan Rulos y Alvaro Stagg.\n");
}

static void cat(char *argv[], int argc, int flags[8])
{
int fd;

for (int i = 1; i < argc; i++)
{
if ((fd = open(argv[i], O_RDONLY)) < 0)
continue;
else
{
fileName = argv[i];
break;
}
}

if (fd < 0)
{
printf("%s: No existe el fichero o el directorio.\n", argv[0]);
ret = 1;
}
else
{
read(fileName, flags);
close(fd);
}
}

static void read(const char *fileName, int flags[8])
{
FILE *fp = fopen(fileName, "r");
int lines = 0;

if (fp == NULL)
{
printf("cat: %s: No existe el fichero o el directorio.\n", fileName);
ret = 1;
}
else
{
char cnt[get_file_size(fileName)];
int n_bytes = fread(cnt, 1, sizeof(cnt), fp);

if (flags[0] == 1 || (flags[3] == 1 && cnt[0] != '\n'))
{
lines++;
printf(" %d ", lines);
}

unsigned int i = 0;

for (i = 0; i < n_bytes - 1; i++)
{
printf("%c", cnt[i]);

if (flags[0] == 1)
{
if (cnt[i] == '\n')
{
lines++;
printf(" %d ", lines);
}
}

if (flags[1] == 1)
{
if (cnt[i + 1] == '\n')
printf("$");
}

if (flags[2] == 1)
{
if (cnt[i] == '\t')
{
printf("^I");
continue;
}
}

if (flags[3] == 1)
{
if (cnt[i] == '\n')
{
if (cnt[i + 1] == '\n')
continue;
else
{
lines++;
printf(" %d ", lines);
}
}
}
}

/* Imprime el último caracter perdido */
printf("%c\n", cnt[i]);
}
}

int get_file_size(const char *fileName)
{
struct stat st;

if (stat(fileName, &st) < 0)
return -1;
else
return st.st_size;
}
25 changes: 25 additions & 0 deletions bin/clear/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2015 Alvaro Stagg [[email protected]]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>

int main(int argc, char* argv[])
{
char str[] = {0x1b, 0x5b, 0x48, 0x1b, 0x5b, 0x4a, '\0'};
printf("%s", str);
return 0;
}
22 changes: 22 additions & 0 deletions bin/clear/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
# Copyright (C) 2010 Niek Linnenbank, 2016 Alvaro Stagg [[email protected]]
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

Import('build_env')

env = build_env.Clone()
env.UseLibraries([ 'libposix', 'liballoc', 'libstd', 'libexec', 'libarch', 'libipc' ])
env.TargetProgram('clear', 'Main.cpp', env['bin'])
24 changes: 17 additions & 7 deletions lib/libposix/string.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2009 Niek Linnenbank
*
* Copyright (C) 2009 Niek Linnenbank, 2016 Alvaro Stagg [[email protected]]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
Expand Down Expand Up @@ -131,23 +131,23 @@ extern C int strcpy(char *dest, const char *src);
extern C int strncpy(char *dest, const char *src, size_t sz);

/**
* Copy src to string dst of size siz. At most siz-1 characters
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* @note This function is copied from OpenBSD-4.3
* @param dst Destination string
* @param src Source string
* @param siz size_t of dst buffer
* @return strlen(src); if retval >= siz, truncation occurred.
* @return strlen(src); if retval >= siz, truncation occurred.
*/
extern C size_t strlcpy(char *dst, const char *src, size_t siz);

/**
* @brief Concatenate two strings.
*
*
* The strcat() function shall append a copy of the string pointed to
* by s2 (including the terminating NUL character) to the end of the
* string pointed to by s1.
*
*
* @param dest Destination string.
* @param src Source string.
* @return The strcat() function shall return s1; no return value is
Expand All @@ -164,7 +164,7 @@ extern C char * strcat(char *dest, const char *src);
* byte of s2 overwrites the NUL character at the end of s1. A terminating
* NUL character is always appended to the result. If copying takes place
* between objects that overlap, the behavior is undefined.
*
*
* @param dest Destination string.
* @param src Source string.
* @param n Number of character to concatenate.
Expand Down Expand Up @@ -211,6 +211,16 @@ extern C char * strchr(const char *s, int c);
*/
extern C char * strrchr(const char *s, int c);

/**
* @param ptr Pointer to the block of memory where the search is performed.
* @param value Value to be located. The value is passed as an int, but the
* function performs a byte per byte search using the unsigned char
* conversion of this value.
* @param num Number of bytes to be analyzed. size_t is an unsigned integral type.
* @return A pointer to the first occurrence of value in the block of memory pointed
* by ptr. If the value is not found, the function returns a null pointer.
*/
extern C void * memchr(const void *ptr, int value, size_t num);
/**
* @}
*/
Expand Down
Loading