Permalink
Browse files

Have kernel like code style.

  • Loading branch information...
1 parent 76a8f44 commit ec353c19857e81a0c498657d7fc8f7bb62a175dd @yasar11732 committed Apr 15, 2012
Showing with 389 additions and 388 deletions.
  1. +26 −13 include/gstring.h
  2. +6 −11 include/utilities.h
  3. +82 −81 lib/gstring.c
  4. +56 −63 lib/utilities.c
  5. +37 −39 src/cat.c
  6. +24 −24 src/cp.c
  7. +78 −72 src/id.c
  8. +59 −59 src/mkdir.c
  9. +9 −11 src/whoami.c
  10. +12 −15 src/yes.c
View
@@ -1,16 +1,29 @@
#ifndef _GSTRING_H_
#define _GSTRING_H_
-typedef struct {
- char *content;
- size_t length; // excluding '\0'
-} Gstring;
-
-Gstring *gstrcat(const Gstring * const,const Gstring * const);
-Gstring *to_gstring(const char * const);
-char *from_gstring(Gstring *);
-void freegstring(Gstring *);
-Gstring *gstrdup(Gstring *);
-Gstring *gstrtoken(Gstring * const, const char * const , Gstring *);
-int appendstr2gstring(Gstring *,char *);
-int combinegstr(Gstring *, Gstring *);
+
+ /*
+ * String and it's length.
+ */
+struct Gstring {
+ char *content;
+ size_t length; // excluding '\0'
+};
+
+struct Gstring *gstrcat(const struct Gstring * const,
+ const struct Gstring * const);
+
+struct Gstring *togstring(const char * const);
+
+char *fromgstring(struct Gstring *);
+
+void freegstring(struct Gstring *);
+
+struct Gstring *gstrdup(struct Gstring *);
+
+struct Gstring *gstrtoken(struct Gstring * const,
+ const char * const ,struct Gstring *);
+
+int appendstr2gstring(struct Gstring *,char *);
+
+int combinegstr(struct Gstring *, struct Gstring *);
#endif
View
@@ -1,18 +1,13 @@
#ifndef _UTILITIES_H__
#define _UTILITIES_H__
-#include <stdio.h>
-char *
-strtoken(char * const, const char * const,char **);
+#include <stdio.h> // FILE pointer.
+char *strtoken(char * const, const char * const,char **);
-char *
-strfind(char * const string,const char * const chr);
+char *strfind(char * const,const char * const);
-int
-str2int(const char * const str);
+int str2int(const char * const);
-int
-cp(FILE *, FILE *);
+int cp(FILE *, FILE *);
-int
-file_exists(const char * const);
+int file_exists(const char * const);
#endif
View
@@ -4,119 +4,120 @@
#include "gstring.h"
#include "utilities.h"
-Gstring *gstrcat(const Gstring * const str1,const Gstring * const str2)
+struct Gstring *gstrcat(const struct Gstring * const str1,
+ const struct Gstring * const str2)
{
+ size_t s1len = str1->length;
+ size_t s2len = str2->length;
+ size_t total = s1len + s2len;
- size_t s1len = str1->length;
- size_t s2len = str2->length;
- size_t total = s1len + s2len;
+ char *buffer = malloc(sizeof(char) * (total + 1));
+ char *temp = buffer;
+ char *content = str1->content;
- char *buffer = malloc(sizeof(char) * (total + 1));
- char *temp = buffer;
- char *content = str1->content;
+ while(s1len--)
+ *temp++ = *content++;
- while(s1len--)
- *temp++ = *content++;
+ content = str2->content;
+ while(s2len--)
+ *temp++ = *content++;
- content = str2->content;
- while(s2len--)
- *temp++ = *content++;
-
- *temp = '\0';
- Gstring *newstring = malloc(sizeof(Gstring));
- newstring->content = buffer;
- newstring->length = total;
- return newstring;
+ *temp = '\0';
+ struct Gstring *newstring = malloc(sizeof(struct Gstring));
+ newstring->content = buffer;
+ newstring->length = total;
+ return newstring;
}
-Gstring *to_gstring(const char * const str)
+struct Gstring *togstring(const char * const str)
{
- Gstring *newstring = malloc(sizeof(Gstring));
- size_t size = strlen(str);
- newstring->length = size;
- char *buffer = malloc(sizeof(char) * (size + 1));
- newstring->content = strncpy(buffer,str,size + 1);
- return newstring;
+ struct Gstring *newstring = malloc(sizeof(struct Gstring));
+ size_t size = strlen(str);
+ newstring->length = size;
+ char *buffer = malloc(sizeof(char) * (size + 1));
+ newstring->content = strncpy(buffer,str,size + 1);
+ return newstring;
}
-char *from_gstring(Gstring *source)
+char *fromgstring(struct Gstring *source)
{
- char *newstr = malloc(sizeof(char) * (source->length + 1));
- return memcpy(newstr,
- source->content,
- sizeof(char) * (source->length + 1));
+ char *newstr = malloc(sizeof(char) * (source->length + 1));
+ return memcpy(newstr,
+ source->content,
+ sizeof(char) * (source->length + 1));
}
-void freegstring(Gstring *source)
+void freegstring(struct Gstring *source)
{
- free (source->content);
- free (source);
+ free (source->content);
+ free (source);
}
-int appendstr2gstring(Gstring *gstr,char *str)
+int appendstr2gstring(struct Gstring *gstr,char *str)
{
- size_t len = strlen(str);
- char *newstr = realloc(gstr->content,
- sizeof(char) * (gstr->length + len + 1));
+ size_t len = strlen(str);
+ char *newstr = realloc(gstr->content,
+ sizeof(char) * (gstr->length + len + 1));
+
+ if (newstr == NULL)
+ return -1;
- if (newstr == NULL)
- return -1;
-
- memcpy(newstr + gstr->length, str, sizeof(char) * (len + 1));
- gstr->content = newstr;
- gstr->length += len;
- return 0;
+ memcpy(newstr + gstr->length, str, sizeof(char) * (len + 1));
+ gstr->content = newstr;
+ gstr->length += len;
+ return 0;
}
/*
* Adds second one into first one, and free's second one
*/
-int combinegstr(Gstring *gstr1, Gstring *gstr2)
+int combinegstr(struct Gstring *gstr1, struct Gstring *gstr2)
{
- char *newstr = realloc(gstr1->content,
- gstr1->length + gstr2->length + 1);
- if (newstr == NULL)
- return -1;
- memcpy(newstr + gstr1->length, gstr2->content, gstr2->length + 1);
- gstr1->content = newstr;
- gstr1->length += gstr2->length;
- freegstring(gstr2);
- return 0;
+ char *newstr = realloc(gstr1->content,
+ gstr1->length + gstr2->length + 1);
+ if (newstr == NULL)
+ return -1;
+ memcpy(newstr + gstr1->length, gstr2->content, gstr2->length + 1);
+ gstr1->content = newstr;
+ gstr1->length += gstr2->length;
+ freegstring(gstr2);
+ return 0;
}
-Gstring *gstrdup(Gstring *input)
+struct Gstring *gstrdup(struct Gstring *input)
{
- char *buffer = malloc(sizeof(char) * (input->length + 1));
- Gstring *newstring = malloc(sizeof(Gstring));
- newstring->content = memcpy(buffer, input->content, input->length + 1);
- newstring->length = input->length;
- return newstring;
+ char *buffer = malloc(sizeof(char) * (input->length + 1));
+ struct Gstring *newstring = malloc(sizeof(struct Gstring));
+ newstring->content = memcpy(buffer, input->content, input->length + 1);
+ newstring->length = input->length;
+ return newstring;
}
+
/*
* Like strtoken, but for gstrings.
- * Important rest's content is part of
+ * Important! rest's content points to
* input's content. BE CAREFULL with that!
*/
-Gstring *gstrtoken(Gstring * const input, const char * const delim, Gstring *rest)
+struct Gstring *gstrtoken(struct Gstring * const input,
+ const char * const delim, struct Gstring *rest)
{
- char *delimpos = strfind(input->content, delim);
- if (delimpos == NULL) {
- // no delimeter in input
- Gstring *newstring = gstrdup(input);
- rest->length = 0;
- rest->content = &(input->content[input->length]);
- return newstring;
- }
-
- size_t new_length = delimpos - input->content;
- char *buffer = malloc(sizeof(char) * (new_length + 1));
- memcpy(buffer, input->content, new_length);
- buffer[new_length] = '\0';
- rest->content = delimpos + 1;
- rest->length = input->length - new_length - 1;
- Gstring *newstring = malloc(sizeof(Gstring));
- newstring->content = buffer;
- newstring->length = new_length;
- return newstring;
+ char *delimpos = strfind(input->content, delim);
+ if (delimpos == NULL) {
+ // no delimeter in input
+ struct Gstring *newstring = gstrdup(input);
+ rest->length = 0;
+ rest->content = &(input->content[input->length]);
+ return newstring;
+ }
+ size_t new_length = delimpos - input->content;
+ char *buffer = malloc(sizeof(char) * (new_length + 1));
+ memcpy(buffer, input->content, new_length);
+ buffer[new_length] = '\0';
+ rest->content = delimpos + 1;
+ rest->length = input->length - new_length - 1;
+ struct Gstring *newstring = malloc(sizeof(struct Gstring));
+ newstring->content = buffer;
+ newstring->length = new_length;
+ return newstring;
}
Oops, something went wrong.

0 comments on commit ec353c1

Please sign in to comment.