OSDN Git Service

Initial commit for lib1stclass
authorwww <www@dev.minakoe.jp>
Mon, 21 Jun 2010 21:15:50 +0000 (06:15 +0900)
committerwww <www@dev.minakoe.jp>
Mon, 21 Jun 2010 21:15:50 +0000 (06:15 +0900)
host2ip.c [new file with mode: 0755]
is_num.c [new file with mode: 0755]
itoa.c [new file with mode: 0755]
lib1stclass.h [new file with mode: 0755]
makefile [new file with mode: 0755]
remove_path.c [new file with mode: 0755]
remove_special_char.c [new file with mode: 0755]
rmkdir.c [new file with mode: 0755]
safe_strcat.c [new file with mode: 0755]
safe_strncat.c [new file with mode: 0755]
shmf.c [new file with mode: 0755]

diff --git a/host2ip.c b/host2ip.c
new file mode 100755 (executable)
index 0000000..f4765ef
--- /dev/null
+++ b/host2ip.c
@@ -0,0 +1,45 @@
+#include <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include "lib1stclass.h"
+
+int host2ip(const char *server, char *ip){
+  struct addrinfo hint, *res, *p;
+  int error=0;
+  char ipstr[INET6_ADDRSTRLEN];
+  
+  memset(&hint, 0, sizeof(hint));
+  hint.ai_family = PF_INET;
+  hint.ai_socktype = SOCK_STREAM;
+
+  error = getaddrinfo(server, "http", &hint, &res);
+
+  if(error){
+    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error));
+    return 1;
+  }
+  
+  for(p = res;p != NULL; p = p->ai_next) {
+    void *addr;
+    char *ipver;
+    
+    if (p->ai_family == AF_INET) { // IPv4
+      struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
+      addr = &(ipv4->sin_addr);
+      ipver = "IPv4";
+    } else { // IPv6
+      struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
+      addr = &(ipv6->sin6_addr);
+      ipver = "IPv6";
+    }
+    
+    // convert the IP to a string and print it:
+    inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
+  }
+  freeaddrinfo(res); // free the linked list
+  safe_strcat(ip, ipstr);
+  return 0;
+}
diff --git a/is_num.c b/is_num.c
new file mode 100755 (executable)
index 0000000..31b370f
--- /dev/null
+++ b/is_num.c
@@ -0,0 +1,15 @@
+int is_num(const char *moji){
+  int i;
+  int is_num=0;
+  for(i=0; moji[i]; i++){
+    if( (moji[i] < '0') || (moji[i] > '9') ){
+      return 0;
+    }
+    else{
+      if(is_num==0){
+        is_num++;
+      }
+    }
+  }
+  return is_num;
+}
diff --git a/itoa.c b/itoa.c
new file mode 100755 (executable)
index 0000000..bb33c89
--- /dev/null
+++ b/itoa.c
@@ -0,0 +1,25 @@
+#include <string.h>
+
+void reverse(char s[]){
+  int c, i, j;
+  
+  for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
+    c = s[i];
+    s[i] = s[j];
+    s[j] = c;
+  }
+}
+
+void itoa(int n, char s[]){
+  int i, sign;
+  if ((sign = n) < 0)  /* record sign */
+    n = -n;          /* make n positive */
+  i = 0;
+  do {       /* generate digits in reverse order */
+        s[i++] = n % 10 + '0';   /* get next digit */
+  } while ((n /= 10) > 0);     /* delete it */
+  if (sign < 0)
+    s[i++] = '-';
+  s[i] = '\0';
+  reverse(s);
+}
diff --git a/lib1stclass.h b/lib1stclass.h
new file mode 100755 (executable)
index 0000000..b96bb2b
--- /dev/null
@@ -0,0 +1,17 @@
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int host2ip(const char *server, char *ip);
+int is_num(const char *moji);
+void itoa(int n, char s[]);
+int rmkdir(const char *check_dir);
+int remove_path(const char *path);
+int remove_special_char(const char *str, char *dst);
+int safe_strcat(char *dst, const char *append);
+int safe_strncat(char *dst, const char *append, const int len);
+int shmf(const char *file, char *shmfile);
+
+#ifdef __cplusplus
+};
+#endif
diff --git a/makefile b/makefile
new file mode 100755 (executable)
index 0000000..fb3875c
--- /dev/null
+++ b/makefile
@@ -0,0 +1,42 @@
+.PHONY: all
+all: lib1stclass.a
+
+lib1stclass.a: host2ip.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o itoa.o remove_path.o remove_special_char.o lib1stclass.h
+       ar rvs lib1stclass.a host2ip.o itoa.o rmkdir.o safe_strcat.o safe_strncat.o is_num.o remove_path.o remove_special_char.o;
+       ranlib lib1stclass.a;
+
+host2ip.o: host2ip.c
+       gcc -Wall -c host2ip.c;
+
+itoa.o: itoa.c
+       gcc -Wall -c itoa.c
+
+remove_special_char.o: remove_special_char.c;
+       gcc -Wall -c remove_special_char.c;
+
+remove_path.o: remove_path.c
+       gcc -Wall -c remove_path.c;
+
+rmkdir.o: rmkdir.c
+       gcc -Wall -c rmkdir.c;
+
+safe_strcat.o: safe_strcat.c
+       gcc -Wall -c safe_strcat.c;
+
+safe_strncat.o: safe_strncat.c
+       gcc -Wall -c safe_strncat.c;
+
+is_num.o: is_num.c
+       gcc -Wall -c is_num.c;
+
+.PHONY: install
+install:
+       mkdir -p /usr/local/lib;
+       cp lib1stclass.a /usr/local/lib;
+       cp lib1stclass.h /usr/local/include;
+
+.PHONY: clean
+clean:
+       rm *.o -f *.o;
+       rm lib1stclass.a -f lib1stclass.a;
+
diff --git a/remove_path.c b/remove_path.c
new file mode 100755 (executable)
index 0000000..9c9e8ce
--- /dev/null
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <string.h>
+#include "lib1stclass.h"
+int remove_path(const char *path){
+  DIR *dirp;
+  struct dirent *dp;
+  if((dirp = opendir(path))==NULL){
+    return 0;
+  }
+  else{
+    while( (dp = readdir(dirp)) != NULL){
+      if(!strcmp(dp->d_name, ".") || !strcmp(dp->d_name,"..")){
+      }
+      else{
+        char file[512]="";
+        safe_strcat(file, path);
+        safe_strcat(file, "/");
+        safe_strcat(file, dp->d_name);
+        remove(file);
+      }
+    }
+    closedir(dirp);
+    rmdir(path);
+  }
+  return 0;
+}
diff --git a/remove_special_char.c b/remove_special_char.c
new file mode 100755 (executable)
index 0000000..dca2335
--- /dev/null
@@ -0,0 +1,30 @@
+#include<string.h>
+#include<stdio.h>
+#include "lib1stclass.h"
+
+int remove_special_char(const char *str, char *dst){
+  int zero=0;
+  if(sizeof(dst)<sizeof(str)){
+    printf("Bad parameter for remove_special_character\n");
+    return 1;
+  }
+  if((strrchr(str, '\0'))==NULL){
+    printf("\\0 is missing for the input parameter of remove_special_character\n");
+    return 1;
+  }
+  for( ; *str!='\0'; str++, dst++){ 
+    if(*str == '/' || *str == ':' || *str == '?' || *str == '%'){
+      *dst='_';
+    }
+    else if(*str == '\0'){
+      *dst=*str;
+      zero++;
+      break;
+    }
+    else{
+      *dst=*str;
+    }
+  }
+  *dst='\0';
+  return 0;
+}
diff --git a/rmkdir.c b/rmkdir.c
new file mode 100755 (executable)
index 0000000..7e5cf44
--- /dev/null
+++ b/rmkdir.c
@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <string.h>
+
+int rmkdir(const char *check_dir){
+  struct stat st;
+  char *token_p=NULL;
+  char dir_path[512]="";
+  char moto[512]="";
+  if(stat(check_dir, &st)!=0){
+    if(mkdir(check_dir, 0755)==0){
+    }
+    else{
+      safe_strcat(moto, check_dir);
+      token_p=strtok(moto, "/");
+      safe_strcat(dir_path, "/");
+      safe_strcat(dir_path, token_p);
+      if(stat(dir_path, &st)!=0){
+        if(mkdir(dir_path, 0755)==0){
+        }
+        else{
+          return 1;
+        }
+      }
+      while((token_p=strtok(NULL,"/"))!=NULL){
+        safe_strcat(dir_path, "/");
+        safe_strcat(dir_path, token_p);
+        if(stat(dir_path, &st)!=0){
+          if(mkdir(dir_path, 0755)==0){
+          }
+          else{
+            return 1;
+          }
+        }
+      }
+    }
+  }
+  return 0;
+}
diff --git a/safe_strcat.c b/safe_strcat.c
new file mode 100755 (executable)
index 0000000..1a009b7
--- /dev/null
@@ -0,0 +1,14 @@
+#include <stdio.h>
+#include <string.h>
+#include "lib1stclass.h"
+
+int safe_strcat(char *dst, const char *append){
+  if( sizeof(append) >sizeof(dst) ){
+    printf("\"%s\" is too long for this system.\n", append);
+    return 1;
+  }
+  else{
+    strcat(dst, append);
+  }
+  return 0;
+}
diff --git a/safe_strncat.c b/safe_strncat.c
new file mode 100755 (executable)
index 0000000..497bb2f
--- /dev/null
@@ -0,0 +1,13 @@
+#include <stdio.h>
+#include <string.h>
+
+int safe_strncat(char *dst, const char *append, const int len){
+  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
new file mode 100755 (executable)
index 0000000..265e7d8
--- /dev/null
+++ b/shmf.c
@@ -0,0 +1,18 @@
+#include <string.h>
+
+char *shmf(const char *file, char *shmfile){
+  char tmp_file[1024]="";
+  int i;
+  safe_strcat(tmp_file, file);
+  for(i=0;i<1024;i++){
+    if(tmp_file[i] == '/'){
+      tmp_file[i]='_';
+    }
+    else if(tmp_file[i] == '\0'){
+      break;
+    }
+  }
+  safe_strcat(shmfile, "/dev/shm/");
+  safe_strcat(shmfile, tmp_file);
+  return tmp_file;
+}