freemyipod r72 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r71‎ | r72 | r73 >
Date:00:33, 10 August 2010
Author:theseven
Status:new
Tags:
Comment:
Finally make the FAT code work
Modified paths:
  • /embios/trunk/SOURCES (modified) (history)
  • /embios/trunk/fat.c (modified) (history)
  • /embios/trunk/strcasecmp.c (added) (history)
  • /embios/trunk/strcasestr.c (added) (history)
  • /embios/trunk/strlcat.c (added) (history)
  • /embios/trunk/strlcpy.c (added) (history)

Diff [purge]

Index: embios/trunk/strcasecmp.c
@@ -0,0 +1,28 @@
 2+#include "global.h"
 3+#include <string.h>
 4+#include <ctype.h>
 5+
 6+int strcasecmp(const char *s1, const char *s2)
 7+{
 8+ while (*s1 != '\0' && tolower(*s1) == tolower(*s2)) {
 9+ s1++;
 10+ s2++;
 11+ }
 12+
 13+ return tolower(*(unsigned char *) s1) - tolower(*(unsigned char *) s2);
 14+}
 15+
 16+int strncasecmp(const char *s1, const char *s2, size_t n)
 17+{
 18+ int d = 0;
 19+
 20+ for(; n != 0; n--)
 21+ {
 22+ int c1 = tolower(*s1++);
 23+ int c2 = tolower(*s2++);
 24+ if((d = c1 - c2) != 0 || c2 == '\0')
 25+ break;
 26+ }
 27+
 28+ return d;
 29+}
Index: embios/trunk/strlcpy.c
@@ -0,0 +1,52 @@
 2+/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
 3+
 4+/*
 5+ * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
 6+ *
 7+ * Permission to use, copy, modify, and distribute this software for any
 8+ * purpose with or without fee is hereby granted, provided that the above
 9+ * copyright notice and this permission notice appear in all copies.
 10+ *
 11+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 12+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 13+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 14+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 15+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 16+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 17+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 18+ */
 19+
 20+#include "global.h"
 21+#include <string.h>
 22+
 23+/*
 24+ * Copy src to string dst of size siz. At most siz-1 characters
 25+ * will be copied. Always NUL terminates (unless siz == 0).
 26+ * Returns strlen(src); if retval >= siz, truncation occurred.
 27+ */
 28+size_t
 29+strlcpy(char *dst, const char *src, size_t siz)
 30+{
 31+ char *d = dst;
 32+ const char *s = src;
 33+ size_t n = siz;
 34+
 35+ /* Copy as many bytes as will fit */
 36+ if (n != 0) {
 37+ while (--n != 0) {
 38+ if ((*d++ = *s++) == '\0')
 39+ break;
 40+ }
 41+ }
 42+
 43+ /* Not enough room in dst, add NUL and traverse rest of src */
 44+ if (n == 0) {
 45+ if (siz != 0)
 46+ *d = '\0'; /* NUL-terminate dst */
 47+ while (*s++)
 48+ ;
 49+ }
 50+
 51+ return(s - src - 1); /* count does not include NUL */
 52+}
 53+
Index: embios/trunk/SOURCES
@@ -42,6 +42,10 @@
4343 file.c
4444 dir.c
4545 storage.c
 46+strcasestr.c
 47+strcasecmp.c
 48+strlcpy.c
 49+strlcat.c
4650
4751 libc/strstr.c
4852 libc/strtok.c
Index: embios/trunk/fat.c
@@ -26,6 +26,7 @@
2727 #include "storage.h"
2828 #include "debug.h"
2929 #include "panic.h"
 30+#include "ctype.h"
3031
3132 #define BYTES2INT16(array,pos) \
3233 (array[pos] | (array[pos+1] << 8 ))
@@ -1307,7 +1308,7 @@
13081309
13091310 /* one dir entry needed for every 13 bytes of filename,
13101311 plus one entry for the short name */
1311 - entries_needed = (utf8length(name) + (NAME_BYTES_PER_ENTRY-1))
 1312+ entries_needed = (strlen(name) + (NAME_BYTES_PER_ENTRY-1))
13121313 / NAME_BYTES_PER_ENTRY + 1;
13131314 }
13141315
Index: embios/trunk/strlcat.c
@@ -0,0 +1,56 @@
 2+/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
 3+
 4+/*
 5+ * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
 6+ *
 7+ * Permission to use, copy, modify, and distribute this software for any
 8+ * purpose with or without fee is hereby granted, provided that the above
 9+ * copyright notice and this permission notice appear in all copies.
 10+ *
 11+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 12+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 13+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 14+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 15+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 16+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 17+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 18+ */
 19+
 20+#include "global.h"
 21+#include <string.h>
 22+
 23+/*
 24+ * Appends src to string dst of size siz (unlike strncat, siz is the
 25+ * full size of dst, not space left). At most siz-1 characters
 26+ * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
 27+ * Returns strlen(src) + MIN(siz, strlen(initial dst)).
 28+ * If retval >= siz, truncation occurred.
 29+ */
 30+size_t
 31+strlcat(char *dst, const char *src, size_t siz)
 32+{
 33+ char *d = dst;
 34+ const char *s = src;
 35+ size_t n = siz;
 36+ size_t dlen;
 37+
 38+ /* Find the end of dst and adjust bytes left but don't go past end */
 39+ while (n-- != 0 && *d != '\0')
 40+ d++;
 41+ dlen = d - dst;
 42+ n = siz - dlen;
 43+
 44+ if (n == 0)
 45+ return(dlen + strlen(s));
 46+ while (*s != '\0') {
 47+ if (n != 1) {
 48+ *d++ = *s;
 49+ n--;
 50+ }
 51+ s++;
 52+ }
 53+ *d = '\0';
 54+
 55+ return(dlen + (s - src)); /* count does not include NUL */
 56+}
 57+
Index: embios/trunk/strcasestr.c
@@ -0,0 +1,123 @@
 2+/* Return the offset of one string within another.
 3+ Copyright (C) 1994,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
 4+ This file is part of the GNU C Library.
 5+
 6+ The GNU C Library is free software; you can redistribute it and/or
 7+ modify it under the terms of the GNU Lesser General Public
 8+ License as published by the Free Software Foundation; either
 9+ version 2.1 of the License, or (at your option) any later version.
 10+
 11+ The GNU C Library is distributed in the hope that it will be useful,
 12+ but WITHOUT ANY WARRANTY; without even the implied warranty of
 13+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 14+ Lesser General Public License for more details.
 15+
 16+ You should have received a copy of the GNU Lesser General Public
 17+ License along with the GNU C Library; if not, write to the Free
 18+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 19+ 02111-1307 USA. */
 20+
 21+/*
 22+ * My personal strstr() implementation that beats most other algorithms.
 23+ * Until someone tells me otherwise, I assume that this is the
 24+ * fastest implementation of strstr() in C.
 25+ * I deliberately chose not to comment it. You should have at least
 26+ * as much fun trying to understand it, as I had to write it :-).
 27+ *
 28+ * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de*/
 29+
 30+/* Faster looping by precalculating bl, bu, cl, cu before looping.
 31+ * 2004 Apr 08 Jose Da Silva, digital@joescat@com */
 32+
 33+#include "global.h"
 34+#include <string.h>
 35+#include <ctype.h>
 36+
 37+typedef unsigned chartype;
 38+
 39+char* strcasestr (const char* phaystack, const char* pneedle)
 40+{
 41+ const unsigned char *haystack, *needle;
 42+ chartype bl, bu, cl, cu;
 43+
 44+ haystack = (const unsigned char *) phaystack;
 45+ needle = (const unsigned char *) pneedle;
 46+
 47+ bl = tolower (*needle);
 48+ if (bl != '\0')
 49+ {
 50+ bu = toupper (bl);
 51+ haystack--;/* possible ANSI violation */
 52+ do
 53+ {
 54+ cl = *++haystack;
 55+ if (cl == '\0')
 56+ goto ret0;
 57+ }
 58+ while ((cl != bl) && (cl != bu));
 59+
 60+ cl = tolower (*++needle);
 61+ if (cl == '\0')
 62+ goto foundneedle;
 63+ cu = toupper (cl);
 64+ ++needle;
 65+ goto jin;
 66+
 67+ for (;;)
 68+ {
 69+ chartype a;
 70+ const unsigned char *rhaystack, *rneedle;
 71+
 72+ do
 73+ {
 74+ a = *++haystack;
 75+ if (a == '\0')
 76+ goto ret0;
 77+ if ((a == bl) || (a == bu))
 78+ break;
 79+ a = *++haystack;
 80+ if (a == '\0')
 81+ goto ret0;
 82+ shloop:
 83+ ;
 84+ }
 85+ while ((a != bl) && (a != bu));
 86+
 87+ jin: a = *++haystack;
 88+ if (a == '\0')
 89+ goto ret0;
 90+
 91+ if ((a != cl) && (a != cu))
 92+ goto shloop;
 93+
 94+ rhaystack = haystack-- + 1;
 95+ rneedle = needle;
 96+ a = tolower (*rneedle);
 97+
 98+ if (tolower (*rhaystack) == (int) a)
 99+ do
 100+ {
 101+ if (a == '\0')
 102+ goto foundneedle;
 103+ ++rhaystack;
 104+ a = tolower (*++needle);
 105+ if (tolower (*rhaystack) != (int) a)
 106+ break;
 107+ if (a == '\0')
 108+ goto foundneedle;
 109+ ++rhaystack;
 110+ a = tolower (*++needle);
 111+ }
 112+ while (tolower (*rhaystack) == (int) a);
 113+
 114+ needle = rneedle;/* took the register-poor approach */
 115+
 116+ if (a == '\0')
 117+ break;
 118+ }
 119+ }
 120+ foundneedle:
 121+ return (char*) haystack;
 122+ ret0:
 123+ return 0;
 124+}