File: core_module.c

package info (click to toggle)
shntool 3.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,220 kB
  • ctags: 1,118
  • sloc: ansic: 8,515; sh: 3,761; makefile: 36
file content (147 lines) | stat: -rw-r--r-- 3,691 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*  core_module.c - public functions for both mode and format modules
 *  Copyright (C) 2000-2009  Jason Jordan <shnutils@freeshell.org>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <string.h>
#include "shntool.h"

CVSID("$Id: core_module.c,v 1.148 2009/03/11 17:18:01 jason Exp $")

/* public functions */

unsigned long check_for_id3v2_tag(FILE *f)
{
  id3v2_header id3v2hdr;
  unsigned long tag_size;

  /* read an ID3v2 header's size worth of data */
  if (sizeof(id3v2_header) != fread(&id3v2hdr,1,sizeof(id3v2_header),f)) {
    return 0;
  }

  /* verify this is an ID3v2 header */
  if (tagcmp((unsigned char *)id3v2hdr.magic,(unsigned char *)ID3V2_MAGIC) ||
      0xff == id3v2hdr.version[0] || 0xff == id3v2hdr.version[1] ||
      0x80 <= id3v2hdr.size[0] || 0x80 <= id3v2hdr.size[1] ||
      0x80 <= id3v2hdr.size[2] || 0x80 <= id3v2hdr.size[3])
  {
    return 0;
  }

  /* calculate and return ID3v2 tag size */
  tag_size = synchsafe_int_to_ulong(id3v2hdr.size);

  return tag_size;
}

FILE *open_input_internal(char *filename,bool *file_has_id3v2_tag,wlong *id3v2_tag_size)
/* opens a file, and if it contains an ID3v2 tag, skips past it */
{
  FILE *f;
  unsigned long tag_size;

  if (NULL == (f = fopen(filename,"rb"))) {
    return NULL;
  }

  if (file_has_id3v2_tag)
    *file_has_id3v2_tag = FALSE;

  if (id3v2_tag_size)
    *id3v2_tag_size = 0;

  /* check for ID3v2 tag on input */
  if (0 == (tag_size = check_for_id3v2_tag(f))) {
    fclose(f);
    return fopen(filename,"rb");
  }

  if (file_has_id3v2_tag)
    *file_has_id3v2_tag = TRUE;

  if (id3v2_tag_size)
    *id3v2_tag_size = (wlong)(tag_size + sizeof(id3v2_header));

  st_debug1("discarding %lu-byte ID3v2 tag at beginning of file: [%s]",tag_size+sizeof(id3v2_header),filename);

  if (fseek(f,(long)tag_size,SEEK_CUR)) {
    st_warning("error while discarding ID3v2 tag in file: [%s]",filename);
    fclose(f);
    return fopen(filename,"rb");
  }

  return f;
}

FILE *open_output(char *filename)
{
  return fopen(filename,"wb");
}

char *scan_env(char *envvar)
/* function to scan environment and return a pointer to the variable if it exists and is nonempty, otherwise returns NULL */
{
  char *envp;

  envp = getenv(envvar);

  if ((NULL == envp) || (!strcmp(envp,"")))
    return NULL;

  return envp;
}

void trim(char *str)
/* function to trim carriage returns and newlines from the end of strings */
{
  int i;

  i = strlen(str) - 1;

  while ((i >= 0) && (('\n' == str[i]) || ('\r' == str[i]))) {
    str[i] = 0;
    i--;
  }
}

char *basename(char *filename)
/* function to return the basename of a file (filename minus any directory names) */
{
  char *base;

  if ((base = strrchr(filename,PATHSEPCHAR)))
    base++;
  else
    base = filename;

  return base;
}

char *extname(char *filename)
/* function to return the extension of a file, if any */
{
  char *base,*ext;

  base = basename(filename);

  if ((ext = strrchr(base,'.')))
    ext++;
  else
    ext = NULL;

  return ext;
}