OSDN Git Service

VER0.1.4 VER0.1.4
authorHajime Kurita <www@dev.minakoe.jp>
Sun, 5 Sep 2010 01:14:48 +0000 (21:14 -0400)
committerHajime Kurita <www@dev.minakoe.jp>
Sun, 5 Sep 2010 01:14:48 +0000 (21:14 -0400)
13 files changed:
1stclass.h
1stclass.hpp
Makefile
host2ip.c
is_num.c
rel2abs.c
remove_not_file_char.c
remove_path.c
rmkdir.c
safe_strcat.c
safe_strncat.c [deleted file]
shmf.c
t/test.cpp

index b08cde7..9326a69 100755 (executable)
@@ -5,6 +5,7 @@
 *
 * Created by Hajime Kurita
 */
+#include<sys/types.h>
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -17,8 +18,7 @@ int rmkdir(const char *check_dir);
 int rel2abs(const char *rel_path, char *abs_path);
 int remove_path(const char *path);
 int remove_not_file_char(const char *str, char *dst);
-int safe_strcat(char *dst, const char *append);
-int safe_strncat(char *dst, const char *append, const unsigned int len);
+int safe_strcat(char *dst, const char *append, size_t size);
 int shmf(const char *file, char *shmfile);
 
 #ifdef __cplusplus
index 6645ff5..f653f6b 100755 (executable)
@@ -20,8 +20,7 @@ public:
   int remove_path(const string path);
   string remove_not_file_char(const string *str);
   int reverse_char(char s[]);
-  int safe_strcat(char *dst, const char *append);
-  int safe_strncat(char *dst, const char *append, const unsigned int len);
+  int safe_strcat(char *dst, const char *append, size_t size);
   string shmf(const string file);
 };
 #endif
index 8474fc3..5ae5b7b 100755 (executable)
--- a/Makefile
+++ b/Makefile
@@ -1,18 +1,18 @@
 all: lib1stclass.a libb1stclass.so lib1stclass_cxx.a lib1stclass_cxx.so t/test
 
-lib1stclass_cxx.a: host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o 1stclass.hpp safe_strcat_cxx.o safe_strncat_cxx.o shmf_cxx.o itoa_cxx.o reverse_char_cxx.o rel2abs_cxx.o
-       ar rvs lib1stclass_cxx.a host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o safe_strcat_cxx.o safe_strncat_cxx.o shmf_cxx.o itoa_cxx.o reverse_char_cxx.o rel2abs_cxx.o
+lib1stclass_cxx.a: host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o 1stclass.hpp safe_strcat_cxx.o shmf_cxx.o itoa_cxx.o reverse_char_cxx.o rel2abs_cxx.o
+       ar rvs lib1stclass_cxx.a host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o safe_strcat_cxx.o shmf_cxx.o itoa_cxx.o reverse_char_cxx.o rel2abs_cxx.o
        ranlib lib1stclass_cxx.a;
 
-lib1stclass_cxx.so: host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o 1stclass.hpp safe_strcat_cxx.o safe_strncat_cxx.o shmf_cxx.o reverse_char_cxx.o rel2abs_cxx.o
-       g++ -shared -fPIC -o lib1stclass_cxx.so host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o safe_strcat_cxx.o safe_strncat_cxx.o shmf_cxx.o reverse_char_cxx.o rel2abs_cxx.o
+lib1stclass_cxx.so: host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o 1stclass.hpp safe_strcat_cxx.o shmf_cxx.o reverse_char_cxx.o rel2abs_cxx.o
+       g++ -shared -fPIC -o lib1stclass_cxx.so host2ip_cxx.o is_num_cxx.o remove_path_cxx.o remove_not_file_char_cxx.o rmkdir_cxx.o safe_strcat_cxx.o shmf_cxx.o reverse_char_cxx.o rel2abs_cxx.o
 
-lib1stclass.a: host2ip.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o itoa.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
-       ar rvs lib1stclass.a host2ip.o itoa.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
+lib1stclass.a: host2ip.o rmkdir.o safe_strcat.o is_num.o itoa.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
+       ar rvs lib1stclass.a host2ip.o itoa.o rmkdir.o safe_strcat.o is_num.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
        ranlib lib1stclass.a;
 
-libb1stclass.so: host2ip.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o itoa.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
-       g++ -shared -fPIC -o lib1stclass.so host2ip.o itoa.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
+libb1stclass.so: host2ip.o rmkdir.o safe_strcat.o is_num.o itoa.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
+       g++ -shared -fPIC -o lib1stclass.so host2ip.o itoa.o rmkdir.o safe_strcat.o is_num.o remove_path.o remove_not_file_char.o shmf.o reverse_char.o rel2abs.o
 
 host2ip.o: host2ip.c
        gcc -Wall -O3 -finline-functions -c host2ip.c;
@@ -68,12 +68,6 @@ safe_strcat.o: safe_strcat.c
 safe_strcat_cxx.o: safe_strcat.c
        g++ -Wall -O3 -finline-functions -o safe_strcat_cxx.o -c safe_strcat.c;
 
-safe_strncat.o: safe_strncat.c
-       gcc -Wall -O3 -finline-functions -c safe_strncat.c;
-
-safe_strncat_cxx.o: safe_strncat.c
-       g++ -Wall -O3 -finline-functions -o safe_strncat_cxx.o -c safe_strncat.c;
-
 shmf.o: shmf.c
        gcc -Wall -O3 -finline-functions -c shmf.c;
 
index ca3982b..9cbb6a2 100755 (executable)
--- a/host2ip.c
+++ b/host2ip.c
 
 #ifdef __cplusplus
 #include "1stclass.hpp"
+#include<iostream>a
 #include<string>
 string firstclass::host2ip(const string server_str){
   char ip[32]="";
   char *server=NULL;
   try{
-  server=new char[sizeof(char)*(server_str.length()+1)];
-  server[0]='\0';
-  safe_strcat(server, server_str.c_str());
+    server=new char[sizeof(char)*(server_str.length()+1)];
+    server[0]='\0';
+    safe_strcat(server, server_str.c_str(), sizeof(char)*(server_str.length()+1));
 #else
-#include "lib1stclass.h"
+#include "1stclass.h"
 int host2ip(const char *server, char *ip){
 #endif
-
   struct addrinfo hint, *res, *p;
   int error=0;
   char ipstr[INET6_ADDRSTRLEN];
@@ -69,7 +69,7 @@ int host2ip(const char *server, char *ip){
   
 #ifdef __cplusplus
     delete[] server;
-    firstclass::safe_strcat(ip, ipstr);
+    firstclass::safe_strcat(ip, ipstr, sizeof(ip));
     string last_ip(ip);
     return last_ip;
   }
@@ -79,7 +79,7 @@ int host2ip(const char *server, char *ip){
     return "";
   }
 #else
-  safe_strcat(ip, ipstr);
+  safe_strcat(ip, ipstr, sizeof(ip));
   return 0;
 #endif
 }
index 03d8032..1aeb324 100755 (executable)
--- a/is_num.c
+++ b/is_num.c
@@ -16,7 +16,7 @@ int firstclass::is_num(const string in_moji){
   try{
     moji=new char[sizeof(char)*(in_moji.length()+1)];
     moji[0]='\0';
-    safe_strcat(moji, in_moji.c_str());
+    safe_strcat(moji, in_moji.c_str(), sizeof(moji));
     
 #else
 
index 87bf022..f816a93 100755 (executable)
--- a/rel2abs.c
+++ b/rel2abs.c
 #include "1stclass.hpp"\r
 \r
 string firstclass::rel2abs(const string in_rel_path){\r
-  char rel_path[1024]="";;\r
-  safe_strcat(rel_path, in_rel_path.c_str());\r
-  char abs_path[1024]="";\r
+  char rel_path[2048]="";;\r
+  safe_strcat(rel_path, in_rel_path.c_str(), sizeof(rel_path));\r
+  char abs_path[2048]="";\r
 #else\r
 #include "1stclass.h"\r
 int rel2abs(const char *rel_path, char *abs_path){\r
 #endif\r
   if(rel_path[0]=='/'){\r
-    safe_strcat(abs_path, rel_path);\r
+    safe_strcat(abs_path, rel_path, 2048);\r
   }\r
   else if(rel_path[0]=='.'){\r
     char in_real[512]="";\r
-    char tail[1024]="";\r
+    char tail[2048]="";\r
     int found=0;\r
     int found_i=0;\r
     int i=0;\r
     int tail_i=0;\r
-    for(i=0;i<=1024;i++){\r
+    for(i=0;i<=2048;i++){\r
       if(rel_path[i]=='\0'){\r
         tail[tail_i]='\0';\r
         break;\r
@@ -55,18 +55,18 @@ int rel2abs(const char *rel_path, char *abs_path){
       in_real[found_i-1]='\0';\r
     }\r
     \r
-    char real_path[1024]="";\r
+    char real_path[2048]="";\r
     realpath(in_real, real_path);\r
-    safe_strcat(abs_path, real_path);\r
-    safe_strcat(abs_path, "/");\r
-    safe_strcat(abs_path, tail);\r
+    safe_strcat(abs_path, real_path, 2048);\r
+    safe_strcat(abs_path, "/", 2048);\r
+    safe_strcat(abs_path, tail, 2048);\r
   }\r
   else{\r
-    char real_path[1024]="";\r
+    char real_path[2048]="";\r
     realpath(".", real_path);\r
-    safe_strcat(abs_path, real_path);\r
-    safe_strcat(abs_path, "/");\r
-    safe_strcat(abs_path, rel_path);\r
+    safe_strcat(abs_path, real_path, 2048);\r
+    safe_strcat(abs_path, "/", 2048);\r
+    safe_strcat(abs_path, rel_path, 2048);\r
   }\r
   \r
 #ifdef __cplusplus\r
index a110998..b1a82ca 100755 (executable)
@@ -19,7 +19,7 @@ string firstclass::remove_not_file_char(const string *in_str){
     dst=new char[sizeof(char)*(in_str->length()+1)];
     str[0]='\0';
     dst[0]='\0';
-    safe_strcat(str, in_str->c_str());
+    safe_strcat(str, in_str->c_str(), in_str->length()+1);
 #else
 #include "lib1stclass.h"
 int remove_not_file_char(const char *str, char *dst){
index 7f55d11..0c920c9 100755 (executable)
@@ -18,7 +18,7 @@ int firstclass::remove_path(const string in_path){
   try {
     path=new char[sizeof(char)*(in_path.length()+1)];
     path[0]='\0';
-    safe_strcat(path, in_path.c_str());
+    safe_strcat(path, in_path.c_str(), in_path.length()+1);
   
 #else
 
@@ -43,9 +43,9 @@ int remove_path(const char *path){
       }
       else{
         char file[512]="";
-        safe_strcat(file, path);
-        safe_strcat(file, "/");
-        safe_strcat(file, dp->d_name);
+        safe_strcat(file, path, sizeof(file));
+        safe_strcat(file, "/", sizeof(file));
+        safe_strcat(file, dp->d_name, sizeof(file));
         remove(file);
       }
     }
index c8f0796..1d65121 100755 (executable)
--- a/rmkdir.c
+++ b/rmkdir.c
@@ -15,7 +15,7 @@
 int firstclass::rmkdir(const string in_dir){
   char abs_dir[2048]="";
   string abs_dir_str=rel2abs(in_dir);
-  safe_strcat(abs_dir, abs_dir_str.c_str());
+  safe_strcat(abs_dir, abs_dir_str.c_str(), sizeof(abs_dir));
 #else
 #include "1stclass.h"
 int rmkdir(const char *check_dir){
@@ -31,10 +31,10 @@ int rmkdir(const char *check_dir){
     if(mkdir(abs_dir, 0755)==0){
     }
     else{
-      safe_strcat(moto, abs_dir);
+      safe_strcat(moto, abs_dir, sizeof(moto));
       token_p=strtok(moto, "/");
-      safe_strcat(dir_path, "/");
-      safe_strcat(dir_path, token_p);
+      safe_strcat(dir_path, "/", sizeof(dir_path));
+      safe_strcat(dir_path, token_p, sizeof(dir_path));
       if(stat(dir_path, &st)!=0){
         if(mkdir(dir_path, 0755)==0){
         }
@@ -43,8 +43,8 @@ int rmkdir(const char *check_dir){
         }
       }
       while((token_p=strtok(NULL,"/"))!=NULL){
-        safe_strcat(dir_path, "/");
-        safe_strcat(dir_path, token_p);
+        safe_strcat(dir_path, "/", sizeof(dir_path));
+        safe_strcat(dir_path, token_p, sizeof(dir_path));
         if(stat(dir_path, &st)!=0){
           if(mkdir(dir_path, 0755)==0){
           }
index 7fd8013..dd6b15b 100755 (executable)
@@ -5,22 +5,38 @@
 *
 * Created by Hajime Kurita
 */
-#include <stdio.h>
+#include <sys/types.h>
 #include <string.h>
 
 #ifdef __cplusplus
 #include "1stclass.hpp"
-int firstclass::safe_strcat(char *dst, const char *append){
+int firstclass::safe_strcat(char *dst, const char *src, size_t size){
 #else
-#include "lib1stclass.h"
-int safe_strcat(char *dst, const char *append){
+#include "1stclass.h"
+int safe_strcat(char *dst, const char *src, size_t size){
 #endif
-  if( sizeof(append) >sizeof(dst) ){
-    printf("\"%s\" is too long for this system.\n", append);
+  char *d = dst;
+  const char *s = src;
+  size_t n = size;
+  size_t dlen;
+
+  while (n-- != 0 && *d != '\0'){
+    d++;
+  }
+  dlen = d - dst;
+  n = size - dlen;
+
+  if (n == 0){
     return 1;
   }
-  else{
-    strcat(dst, append);
+
+  while (*s != '\0') {
+    if (n != 1) {
+      *d++ = *s;
+      n--;
+    }
+    s++;
   }
+  *d = '\0';
   return 0;
 }
diff --git a/safe_strncat.c b/safe_strncat.c
deleted file mode 100755 (executable)
index 7b36e52..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
-* See the file LICENSE for redistribution information.
-*
-* Copyright: 2010- 1stclass.co.jp.  All rights reserved.
-*
-* Created by Hajime Kurita
-*/
-#include <stdio.h>
-#include <string.h>
-
-#ifdef __cplusplus
-#include "1stclass.hpp"
-int firstclass::safe_strncat(char *dst, const char *append, const unsigned int len){
-#else
-#include "1stclass.h"
-int safe_strncat(char *dst, const char *append, const unsigned int len){
-#endif
-  if( strlen(dst)>len ){
-    printf("\"%s\" is too long for this system.\n", append);
-    return 1;
-  }
-  else{
-    strncat(dst, append, len);
-  }
-  return 0;
-}
diff --git a/shmf.c b/shmf.c
index 5809552..d51ca57 100755 (executable)
--- a/shmf.c
+++ b/shmf.c
 #include "1stclass.hpp"
 string firstclass::shmf(const string in_file){
   const char *file=in_file.c_str();
-  char shm_file[1024]="";
+  char shm_file[2048]="";
 #else
-#include "lib1stclass.h"
+#include "1stclass.h"
 int shmf(const char *file, char *shm_file){
 #endif
 
-  char tmp_file[1024]="";
+  char tmp_file[2048]="";
   int i;
-  safe_strcat(tmp_file, file);
-  for(i=0;i<1024;i++){
+  safe_strcat(tmp_file, file, sizeof(tmp_file));
+  for(i=0;i<2048;i++){
     if(tmp_file[i] == '/'){
       tmp_file[i]='_';
     }
@@ -29,8 +29,8 @@ int shmf(const char *file, char *shm_file){
     }
   }
 
-  safe_strcat(shm_file, "/dev/shm/");
-  safe_strcat(shm_file, tmp_file);
+  safe_strcat(shm_file, "/dev/shm/", 2048);
+  safe_strcat(shm_file, tmp_file, 2048);
 
 #ifdef __cplusplus
   string result(shm_file);
index 742e015..6e926b3 100755 (executable)
@@ -26,8 +26,9 @@ private:
 void tester::test(){
   firstclass fst;
   struct stat st;
-// is_num: 数字かどうかの判別
+// is_num:
 // Judge whether this is number or not
+// 数字かどうかの判別
   if(fst.is_num("2") && fst.is_num("test")==0){
     cout << "is_num/C++: ok "<< endl;
     ok++;
@@ -46,8 +47,9 @@ void tester::test(){
     bad++;
   }
 
-// ホスト名からIPを得る
+// host2ip
 // Get IP from host name
+// ホスト名からIPを得る
   string server="www.yahoo.co.jp";
   string ip=fst.host2ip(server);
   if( (ip.at(0) - '1'>=0) && (ip.at(0) - '1'<=9) ){
@@ -71,8 +73,9 @@ void tester::test(){
     bad++;
   }
 
-// 相対パスから絶対パスを得る
+// rel2abs:
 // Get absolute path from relative path
+// 相対パスから絶対パスを得る
   string rel_path="../lib1stclass/1/2/3";
   string abs_path=fst.rel2abs(rel_path);
   if( abs_path.find("/")==0 &&  abs_path.find("lib1stclass/1/2/3")!=string::npos){
@@ -92,7 +95,7 @@ void tester::test(){
     ok++;
   }
   else{
-    cout << "rel2abs/C: bad" << endl;
+    cout << "rel2abs/C: bad" << "/" << abs_path_char << endl;
     bad++;
   }
 
@@ -141,16 +144,18 @@ void tester::test(){
     cout << "rel2abs 3/C: bad" << endl;
     bad++;
   }
-  
-// ディレクトリを再帰的に作る
+
+// rmkdir:
 // mkdir recursively
+// ディレクトリを再帰的に作る
   fst.rmkdir("test_dir/depth2");
   if(stat("test_dir/depth2", &st)==0){
     cout << "rmkdir/C++: ok" << endl;
     ok++;
 
-// パス全体を消去(ディレクトリを再帰的に消去)
+// remove_path:
 // Remove path completely
+// パス全体を消去(ディレクトリを再帰的に消去)
     fst.remove_path("test_dir");
     if(stat("test_dir", &st)!=0){
       cout << "remove_path/C++: ok" << endl;
@@ -185,8 +190,9 @@ void tester::test(){
     bad++;
   }
 
-// int型をchar型に変換
+// itoa
 // Convert int to char
+// int型をchar型に変換
   int integer=12345;
   char itoa_str[6]="";
 
@@ -211,8 +217,9 @@ void tester::test(){
     bad++;
   }
 
-// 文字を逆並びにする
+// reverse_char:
 // Reverse char
+// charを逆並びにする
   char example1[]="1234";
   fst.reverse_char(example1);
   if (strcmp(example1,"4321") == 0){
@@ -235,9 +242,9 @@ void tester::test(){
     bad++;
   }
 
-
-// ファイルシステムに使えないアスキー文字を「_」に変換
+// remove_not_file_char:
 // Convert ascii characters which cannot be used for file name into "_"
+// ファイルシステムに使えないアスキー文字を「_」に変換
   string not_file_str="bk\\sl/sm:as*qt?dq\"lt<rt>vt|";
   string file_str=fst.remove_not_file_char(&not_file_str);
   if (file_str=="bk_sl_sm_as_qt_dq_lt_rt_vt_"){
@@ -261,6 +268,8 @@ void tester::test(){
     bad++;
   }
 
+// shmf:
+// Create temporary file on /dev/shm/
 // /dev/shm/上への一時ファイル生成
   string file="/tmp/test.txt";
   if(fst.shmf(file)=="/dev/shm/_tmp_test.txt"){
@@ -283,6 +292,57 @@ void tester::test(){
     cout << "shmf/C: bad" << endl;
     bad++;
   }
+
+// safe_strcat:
+// Safe strcat
+// 安全なstrcat
+  char a1[20]="";
+  char b1[]="12345";
+  fst.safe_strcat(a1, b1, sizeof(a1));
+  if(strcmp(a1,b1)==0){
+    cout << "safe_strcat 1/C++: ok" << endl;
+    ok++;
+  }
+  else{
+    cout << "safe_strcat 1/C++: bad" << endl;
+    bad++;
+  }
+  
+  char a2[20]="";
+  char b2[]="12345";
+  safe_strcat(a2, b2, sizeof(a2));
+  if(strcmp(a2,b2)==0){
+    cout << "safe_strcat 2/C: ok" << endl;
+    ok++;
+  }
+  else{
+    cout << "safe_strcat 2/C: bad" << endl;
+    bad++;
+  }
+  
+  char a3[2]="";
+  char b3[]="12345";
+  fst.safe_strcat(a3, b3, sizeof(a3));
+  if(strcmp(a3,"1")==0){
+    cout << "safe_strcat 3/C++: ok" << endl;
+    ok++;
+  }
+  else{
+    cout << "safe_strcat 3/C++: bad" << endl;
+    bad++;
+  }
+  
+  char a4[2]="";
+  char b4[]="12345";
+  safe_strcat(a4, b4, sizeof(a4));
+  if(strcmp(a4,"1")==0){
+    cout << "safe_strcat 4/C: ok" << endl;
+    ok++;
+  }
+  else{
+    cout << "safe_strcat 4/C: bad" << endl;
+    bad++;
+  }
 }
 
 void tester::report(){