|
|
1.1 root 1: #define _POSIX_SOURCE
2: #include <stddef.h>
3: #include <stdlib.h>
4: #include <stdio.h>
5: #include <string.h>
6: #include <errno.h>
7: #include "jukeface.h"
8: #include "jukebox.h"
9:
10: int j_wrshelf = 0;
11:
12: j_rdshelves(Jukebox *j, char *err)
13: {
14: FILE *fp;
15: static haveread = 0;
16: int shno;
17: char vname[256];
18:
19: if(haveread)
20: return(0);
21: for(shno = 0; shno < j->nshelves; shno++){
22: j->names[shno] = 0;
23: j->shelves[shno] = 0;
24: }
25: if((fp = fopen(JUKEDIR, "r")) == NULL){
26: sprintf(err, "%s: %s", JUKEDIR, strerror(errno));
27: return(-1);
28: }
29: while(fscanf(fp, "%d %s\n", &shno, vname) == 2){
30: if((shno < 0) || (shno >= j->nshelves)){
31: fprintf(stderr, "Warning: bad shelf number in %s: %d (vol_id=%s)\n",
32: JUKEDIR, shno, vname);
33: continue;
34:
35: }
36: j->names[shno] = strdup(vname);
37: j->shelves[shno] = 1;
38: }
39: fclose(fp);
40: haveread = 1;
41: return(0);
42: }
43:
44: j_wrshelves(Jukebox *j, char *err)
45: {
46: FILE *fp;
47: int shno;
48:
49: if((fp = fopen(JUKEDIR, "w")) == NULL){
50: sprintf(err, "%s: %s", JUKEDIR, strerror(errno));
51: return(-1);
52: }
53: for(shno = 0; shno < j->nshelves; shno++)
54: if(j->names[shno])
55: fprintf(fp, "%d %s\n", shno, j->names[shno]);
56: fclose(fp);
57: return(0);
58: }
59:
60: char *
61: j_name(Jukebox *j, int n)
62: {
63: static char err[1024];
64:
65: if(j_rdshelves(j, err) < 0)
66: return(err);
67: if((n >= j->nshelves) || (j->names[n] == 0))
68: return(NONAME);
69: return(j->names[n]);
70: }
71:
72: j_shelfof(Jukebox *j, char *vol_id)
73: {
74: int i;
75: char buf[512];
76:
77: for(;;){
78: for(i = 0; i < j->nshelves; i++)
79: if(j->shelves[i] && (strcmp(j->names[i], vol_id) == 0))
80: return(i);
81: if((i = j_warm(j, buf)) <= 0)
82: break;
83: }
84: if(i < 0)
85: fprintf(stderr, "jukebox: %s\n", buf);
86: return(-1);
87: }
88:
89: j_driveof(Jukebox *j, char *vol_id)
90: {
91: int i, sh;
92:
93: if((sh = j_shelfof(j, vol_id)) < 0)
94: return(-1);
95: for(i = 0; i < j->nluns; i++)
96: if(j->luns[i].shelf == sh)
97: return(i);
98: return(-1);
99: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.