# HG changeset patch # User Yoshiki Yazawa # Date 1255053876 -32400 # Node ID f1553492e8bbcd33bd294cc286a4103a8a580a0d # Parent 181737b0533c3d1353ae0b193a14f1167c3cbae1 ensure path of destination file exists. imported Jonathan Leffler's mkpath. diff -r 181737b0533c -r f1553492e8bb driver/modules.order --- a/driver/modules.order Sat Oct 03 13:25:13 2009 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -kernel//home/yaz/develop/pt1/driver/pt1_drv.ko diff -r 181737b0533c -r f1553492e8bb recpt1/Makefile --- a/recpt1/Makefile Sat Oct 03 13:25:13 2009 +0900 +++ b/recpt1/Makefile Fri Oct 09 11:04:36 2009 +0900 @@ -11,7 +11,7 @@ LIBS = $(PCSC_LIBS) $(B25_LIBS) -lm -lpthread LDFLAGS = -OBJS = recpt1.o decoder.o +OBJS = recpt1.o decoder.o mkpath.o DEPEND = .deps all: $(TARGET) diff -r 181737b0533c -r f1553492e8bb recpt1/mkpath.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/recpt1/mkpath.c Fri Oct 09 11:04:36 2009 +0900 @@ -0,0 +1,54 @@ +/* mkpath is originally written by Jonathan Leffler. + * see "http://stackoverflow.com/questions/675039/how-can-i-create-directory-tree-in-c-linux" for detail. + * copyright: (C) JLSS 1990-91,1997-98,2001,2005,2008 + */ + +#include +#include +#include +#include +#include + +static int +do_mkdir(const char *path, mode_t mode) +{ + struct stat st; + int status = 0; + + if (stat(path, &st) != 0) { + /* Directory does not exist */ + if (mkdir(path, mode) != 0) + status = -1; + } + else if (!S_ISDIR(st.st_mode)) { + errno = ENOTDIR; + status = -1; + } + + return(status); +} + +int +mkpath(const char *path, mode_t mode) +{ + char *pp; + char *sp; + int status; + char *copypath = strdup(path); + + status = 0; + pp = copypath; + while (status == 0 && (sp = strchr(pp, '/')) != 0) { + if (sp != pp) { + /* Neither root nor double slash in path */ + *sp = '\0'; + status = do_mkdir(copypath, mode); + *sp = '/'; + } + pp = sp + 1; + } + if (status == 0) + status = do_mkdir(path, mode); + free(copypath); + return (status); +} diff -r 181737b0533c -r f1553492e8bb recpt1/mkpath.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/recpt1/mkpath.h Fri Oct 09 11:04:36 2009 +0900 @@ -0,0 +1,6 @@ +#ifndef _MKPATH_H_ +#define _MKPATH_H_ + +int mkpath(const char *path, mode_t mode); + +#endif diff -r 181737b0533c -r f1553492e8bb recpt1/recpt1.c --- a/recpt1/recpt1.c Sat Oct 03 13:25:13 2009 +0900 +++ b/recpt1/recpt1.c Fri Oct 09 11:04:36 2009 +0900 @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -24,6 +25,7 @@ #include "recpt1.h" #include "decoder.h" #include "version.h" +#include "mkpath.h" /* maximum write length at once */ #define SIZE_CHANK 1316 @@ -783,6 +785,13 @@ } else { if(!fileless) { + int status; + char *dir = dirname(strdup(argv[optind + 2])); + status = mkpath(dir, 0777); + if(status == -1) + perror("mkpath"); + free(dir); + wfd = open(argv[optind + 2], (O_RDWR | O_CREAT | O_TRUNC), 0666); if(wfd < 0) { fprintf(stderr, "Cannot open output file: %s\n",