1STRCMP(3P) POSIX Programmer's Manual STRCMP(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 strcmp - compare two strings
13
15 #include <string.h>
16
17 int strcmp(const char *s1, const char *s2);
18
19
21 The strcmp() function shall compare the string pointed to by s1 to the
22 string pointed to by s2.
23
24 The sign of a non-zero return value shall be determined by the sign of
25 the difference between the values of the first pair of bytes (both
26 interpreted as type unsigned char) that differ in the strings being
27 compared.
28
30 Upon completion, strcmp() shall return an integer greater than, equal
31 to, or less than 0, if the string pointed to by s1 is greater than,
32 equal to, or less than the string pointed to by s2, respectively.
33
35 No errors are defined.
36
37 The following sections are informative.
38
40 Checking a Password Entry
41 The following example compares the information read from standard input
42 to the value of the name of the user entry. If the strcmp() function
43 returns 0 (indicating a match), a further check will be made to see if
44 the user entered the proper old password. The crypt() function shall
45 encrypt the old password entered by the user, using the value of the
46 encrypted password in the passwd structure as the salt. If this value
47 matches the value of the encrypted passwd in the structure, the entered
48 password oldpasswd is the correct user's password. Finally, the program
49 encrypts the new password so that it can store the information in the
50 passwd structure.
51
52
53 #include <string.h>
54 #include <unistd.h>
55 #include <stdio.h>
56 ...
57 int valid_change;
58 struct passwd *p;
59 char user[100];
60 char oldpasswd[100];
61 char newpasswd[100];
62 char savepasswd[100];
63 ...
64 if (strcmp(p->pw_name, user) == 0) {
65 if (strcmp(p->pw_passwd, crypt(oldpasswd, p->pw_passwd)) == 0) {
66 strcpy(savepasswd, crypt(newpasswd, user));
67 p->pw_passwd = savepasswd;
68 valid_change = 1;
69 }
70 else {
71 fprintf(stderr, "Old password is not valid\n");
72 }
73 }
74 ...
75
77 None.
78
80 None.
81
83 None.
84
86 strncmp(), the Base Definitions volume of IEEE Std 1003.1-2001,
87 <string.h>
88
90 Portions of this text are reprinted and reproduced in electronic form
91 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
92 -- Portable Operating System Interface (POSIX), The Open Group Base
93 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
94 Electrical and Electronics Engineers, Inc and The Open Group. In the
95 event of any discrepancy between this version and the original IEEE and
96 The Open Group Standard, the original IEEE and The Open Group Standard
97 is the referee document. The original Standard can be obtained online
98 at http://www.opengroup.org/unix/online.html .
99
100
101
102IEEE/The Open Group 2003 STRCMP(3P)