[go: up one dir, main page]

File: database.c

package info (click to toggle)
cdtool 2.1.4-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 156 kB
  • ctags: 105
  • sloc: ansic: 1,210; makefile: 135; sh: 99
file content (85 lines) | stat: -rw-r--r-- 2,695 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* "database.c" copyright 1994 thomas insel */

#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include <stdlib.h>

#include "config.h"
#include "database.h"

static char rcsid[] = "$Id: database.c,v 1.2 1997/12/23 21:38:52 wadeh Exp $";

cd_t *
read_db(char *tracks_buffer, char usedb) {

   /* given "tracks" line in tracks_buffer, search databases for a
    * corresponding entry, return cdname/artist/tracks information
    *
    * if the disc is listed more than once, this routine will return
    * the last entry for the disc from the first file that has any
    * entry for it.
    */

    char *path_env = getenv("CDTOOLDBPATH");
    char *path_bit;
    char dbpath_count=1, cur_file=0, found_it=0;
    struct passwd *prec = getpwuid(getuid());
    cd_t *temp_cd = (cd_t *)malloc(sizeof(cd_t));
    char dbpath[10][255];

    if (!usedb) {
        free ((void *)temp_cd);
        return temp_cd;
    }

    /* first, put default file in search path */
    strcpy(dbpath[0], prec->pw_dir);
    strcat(dbpath[0], "/.cdtooldb");

    /* read search path from environment */
    if (path_env != NULL)
        while (dbpath_count < 9 &&
                    (path_bit=strsep(&path_env, ":\0")) != NULL)
            strcpy(dbpath[dbpath_count++], path_bit);

    /* check files in search path until the disc's entry is found */
    while (!found_it && cur_file < dbpath_count) {
        char read_buffer[2048], cur_track=0, printing=0;
        FILE *fred = fopen(dbpath[cur_file], "r");
        cur_file++;

        while (fred != NULL && !feof(fred) ) {
            fgets(read_buffer, 2048, fred);
            if (strcmp(read_buffer, tracks_buffer) == 0) {
                printing = 1;
                found_it = 1;
            } else if (strncmp(read_buffer, "tracks ", 7) == 0) {
                printing = 0;
            } else if (printing) {
                if (strncmp(read_buffer,"track ",6) == 0)
                    strncpy(temp_cd->track_names[cur_track++], 
                                                  &read_buffer[6],99);
                else if (strncmp(read_buffer,"cdname ",7) == 0)
                    strncpy(temp_cd->cdname, &read_buffer[7],99);
                else if (strncmp(read_buffer,"artist ",7) == 0)
                    strncpy(temp_cd->artist, &read_buffer[7],99);
            }
        }

        if (fred == NULL)
        {
                int i;

                temp_cd->cdname[0] = '\0';
                temp_cd->artist[0] = '\0';
                for (i=0 ; i<100 ; i++ )
                        temp_cd->track_names[i][0] = '\0';
        }
        else
        {
                fclose(fred);
        }
    } /*while*/
    return temp_cd;
}