int main( int argc, const char *argv[] ) { void *scanner; FILE *input; if( argc >= 2 ) input = fopen( argv[1], "r" ); else input = stdin; if( input == NULL ) { perror( "cannot open input file" ); exit( 1 ); } if( argc >= 3 ) { mkparent( argv[2] ); FILE *res = freopen( argv[2], "w", stdout ); if( res == NULL ) { perror( "cannot open output file" ); exit( 1 ); } } yylex_init( &scanner ); yyset_in( input, scanner ); yyparse( scanner ); yylex_destroy( scanner ); exit(0); }
void mkparent( const char *path ) { // Duplicate the path so that we can modify it char *dir = strdup(path); if( dir == NULL ) { perror( "Could not determine parent directory" ); exit( 1 ); } // Search for the end of the path char *cur = dir; while( *cur != 0 ) cur++; // Search backwards for the first path separator while( --cur > dir ) { if( *cur == '/' && *(cur-1) != '\\' ) { *cur = 0; break; } } // If there was no parent then finish if( cur == dir ) { free( dir ); return; } // If the directory already exists then finish struct stat st; if( stat(dir,&st) == 0 ) { free( dir ); return; } // Make the parent of the parent path mkparent( dir ); // Make the parent if( mkdir( dir, 0755 ) != 0 ) { free( dir ); perror( "Could not make parent directory" ); exit( 1 ); } // Free up the duplicated path free( dir ); }
void mkparent(const char *name) { char *buf, *tmp; struct stat st; buf = str_alloc(name); if ((tmp=strrchr(buf, '/'))!=0) { *tmp = 0; if (stat(buf, &st)) mkparent(buf); mkdir(buf, 0755); if (stat(buf, &st) || !S_ISDIR(st.st_mode)) ex("!dir"); } free(buf); }
void dofile(const char *name) { int fd; struct stat st; char *s; if ((fd=open(name, O_RDWR))<0) swrite("?"); else if (fstat(fd, &st) || !S_ISREG(st.st_mode)) ex("open"); else swrite_free(md5sum(fd, st.st_size)); s = sread(); switch (*s) { case 'o': close(fd); free(s); return; case 'a': if (fd<0) ex("!fd"); if (lseek(fd, st.st_size, SEEK_SET)!=st.st_size) ex("lseek"); break; case 'w': if (fd<0) ex("!fd"); close(fd); fd = -1; free(s); s = newname(name); if (rename(name, s)) ex("name"); case 'c': if (fd>=0) ex("fd!"); mkparent(name); if ((fd=open(name, O_WRONLY|O_CREAT|O_EXCL, 0644))<0) ex("create"); break; default: ex("prot"); } free(s); swrite("OK"); if (docopy(sock, fd, 1)) ex("broken stream"); if (close(fd)) ex("close"); swrite("NEXT"); }