示例#1
0
文件: my.c 项目: WLPhoenix/my-c
int
execute_template(const char* group, const char* name)
{
    char* myhome = getenv("MY_HOME");

    int homelen = strlen(myhome);
    int grouplen = strlen(group);
    int namelen = strlen(name);

    int grouppath_size = homelen + grouplen + 1;
    char grouppath[grouppath_size];
    memset( grouppath, 0, sizeof(char) * grouppath_size );
    strcpy( grouppath, myhome );
    strcat( grouppath, "/" );
    strcat( grouppath, group );

    int labelpath_size =  grouppath_size + namelen + 3;
    char labelpath[labelpath_size];
    memset( labelpath, 0, sizeof(char) * labelpath_size );
    strcpy(labelpath, grouppath);
    strcat(labelpath, "/T.");
    strcat(labelpath, name);

    int mappath_size =  grouppath_size + namelen + 3;
    char mappath[mappath_size];
    memset( mappath, 0, sizeof(char) * mappath_size );
    strcpy( mappath, grouppath);
    strcat( mappath, "/Z.");
    strcat( mappath, name);


    FILE * recordp;
    FILE * mapp;

    if( NULL != (recordp = fopen(labelpath, "r")) )
    {
        if( NULL != (mapp = fopen(mappath, "r" )) )
        {
            char line[1023];
            while( nextline(line, 1023, recordp) )
            {
                puts(line);
                if( 'd' == line[0] )
                {
                    char* path = strrchr( line, '\t');
                    if( NULL == path) {
                        continue;
                    }
                    path = &path[1];
                    mkdir( path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
                }
                else if( 'f' == line[0] )
                {
                    char* uuid_key = strrchr( line, '/');
                    if( NULL == uuid_key) {
                        continue;
                    }
                    uuid_key = &uuid_key[1];

                    int key_size = strlen(uuid_key);
                    char key[key_size];
                    strcpy( key, uuid_key );
                    uuid_key[0] = '\0';

                    char* trgtpath = strrchr( line, '\t' );
                    if( NULL == trgtpath) {
                        continue;
                    }
                    trgtpath = &trgtpath[1];

                    //At this point we have 'trgtpath' and 'key'
                    char* mapline = malloc( sizeof(char) * (255 + 37) );
                    memset( mapline, 0, sizeof(char) * (255 + 37) );

                    char* filename;
                    while( nextline( mapline, (255 + 37), mapp ) )
                    {
                        char* map_key = mapline;
                        char* separator = strrchr(mapline, '\t');
                        separator[0] = '\0';
                        puts("Seeking file.");
                        printf("Key 1: %s\n", key);
                        printf("Key 2: %s\n", map_key);
                        if (strcmp( key, map_key ) == 0)
                        {
                            filename = &separator[1];
                            printf("Located file: %s\n", filename);
                            break;
                        }
                    }
                    int trgtpath_size = strlen( trgtpath );
                    int filename_size = strlen( filename );

                    int frompath_size = labelpath_size + 37 ;
                    char frompath[frompath_size];
                    strcpy( frompath, labelpath );
                    strcat( frompath, "." );
                    strcat( frompath, key );

                    int topath_size = trgtpath_size + filename_size;
                    char topath[topath_size];
                    strcpy( topath, trgtpath );
                    strcat( topath, filename );

                    fCopy( frompath, topath );
                    free( mapline );
                }
            }

            fclose( mapp );
        }

        fclose( recordp );
    }
}
示例#2
0
int main()
{
	Fraction fiveThirds(5, 3); // Direct initialize a Fraction, calls Fraction(int, int) constructor
	Fraction fCopy(fiveThirds); // Direct initialize -- with what constructor?
	std::cout << fCopy ;
}
示例#3
0
文件: my.c 项目: WLPhoenix/my-c
int
__create_template__(char* labelpath, char* mappath, char* parent, char* filepath)
{
    puts( "__create_template__" );
    printf( "labelpath: %s\n", labelpath );
    printf( "mappath: %s\n", mappath );
    printf( "parent: %s\n", parent );
    printf( "filepath: %s\n", filepath );

    char* filename = strrchr( filepath, '/' );
    if(NULL == filename)
    {
        return -1;
    };
    filename = &filename[1];
    printf( "filename: %s\n", filename);

    struct stat sb;
    if( stat(filepath, &sb) == 0 )
    {
        if( S_ISDIR(sb.st_mode) )
        {
            //Append this file to the directory listing
            //d <permissions> <path>
            int parent_size = strlen(parent);
            int filename_size = strlen(filename);
            int record_size = parent_size + filename_size;
            char record[record_size];
            strcpy( record, parent );
            strcat( record, filename );

            FILE * fp;
            if( NULL != (fp = fopen(labelpath, "a")) )
            {
                fprintf( fp, "d\t%s\t%s\n", "|x|", record );
            }

            // Since it's a directory, recurse on all files below this
            DIR * dirp;
            int files_size = dirsize(filepath);
            char* files[files_size];

            if ( NULL != (dirp = opendir(filepath)) )
            {
                struct dirent * file;
                int i = 0;

                while( NULL != (file = readdir(dirp)) )
                {
                    files[i++] = file->d_name;
                }
                closedir(dirp);
            }
            for( int i = 0; i < files_size; i++ )
            {
                int newfile_size = strlen( files[i] );
                // These checks prevent you from infinitely recursing on . and ..
                if( newfile_size <= 0 )
                {
                    continue;
                }
                else if( newfile_size == 1 )
                {
                    if ( files[i][0] == '.' )
                    {
                        continue;
                    }
                }
                else if ( newfile_size == 2 )
                {
                    if ( files[i][0] == '.' && files[i][1] == '.' )
                    {
                        continue;
                    }
                }

                //Create the info for the recursive call
                char newparent[record_size + 1];
                memset( newparent, 0, sizeof(char) * (record_size + 1) );
                strcpy( newparent, record );
                strcat( newparent, "/" );

                char newfile[record_size + newfile_size + 1];
                memset( newfile, 0, sizeof(char) * (record_size + newfile_size + 1) );
                strcpy( newfile, newparent );
                strcat( newfile, files[i] );

                __create_template__(labelpath, mappath, newparent, newfile);
            }
        }
        else if ( S_ISREG(sb.st_mode ) )
        {
            uuid_t uuid_key;
            uuid_generate( (unsigned char *) uuid_key);
            int key_size = 36;
            char key[key_size];
            uuid_unparse( uuid_key, key );

            //Append this file to the directory listing
            //d <permissions> <path>
            int parent_size = strlen(parent);
            int record_size = parent_size + key_size;
            char record[record_size];
            strcpy( record, parent );
            strcat( record, key );

            FILE * fp;
            if( NULL != (fp = fopen(labelpath, "a")) )
            {
                fprintf( fp, "f\t%s\t%s\n", "|x|", record );
            }

            //Because this is a file, we want to copy it to our directory to be restored later
            int labelpath_size = strlen(labelpath);
            char cpytarget[labelpath_size + key_size + 1];
            strcpy( cpytarget, labelpath );
            strcat( cpytarget, "." );
            strcat( cpytarget, key );

            fCopy( filepath, cpytarget );

            //<uuid> <filename>
            FILE * mapp;
            if( NULL != (mapp = fopen(mappath, "a")) )
            {
                fprintf( mapp, "%s\t%s\n", key, filename );
            }
        }
    }
}